Forráskód Böngészése

agregar archivos para comandas

David Gómez 2 hónapja
szülő
commit
c73d44da16
2 módosított fájl, 384 hozzáadás és 0 törlés
  1. 179 0
      comanda_movil.php
  2. 205 0
      precuenta_movil.php

+ 179 - 0
comanda_movil.php

@@ -0,0 +1,179 @@
+<?php
+/**
+ * Archivo para imprimir comandas desde app móvil
+ * Recibe datos vía POST y los distribuye a las impresoras según cocina
+ * Usa librería: mike42/escpos-php
+ */
+
+if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
+    header("Access-Control-Allow-Origin: *");
+    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
+    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
+    http_response_code(204);
+    exit;
+}
+
+header("Access-Control-Allow-Origin: *");
+header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
+header("Access-Control-Allow-Headers: Content-Type, Authorization");
+header('Content-Type: application/json; charset=utf-8');
+
+error_reporting(E_ALL);
+ini_set('display_errors', 0);
+ini_set('log_errors', 1);
+ini_set('error_log', 'logs/error.log');
+
+require __DIR__ . '/vendor/autoload.php';
+use Mike42\Escpos\Printer;
+use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
+
+// Recibir datos
+$dataPrint = null;
+
+if(isset($_POST["dataPrint"])){
+    if(is_string($_POST["dataPrint"])){
+        $dataPrint = json_decode($_POST["dataPrint"], true);
+    } else {
+        $dataPrint = $_POST["dataPrint"];
+    }
+}
+
+if(!$dataPrint){
+    echo json_encode(['success' => false, 'status' => 'error', 'message' => 'No se recibieron datos para imprimir', 'errores' => []]);
+    exit;
+}
+
+if (!isset($dataPrint['impresos']) || empty($dataPrint['impresos'])) {
+    echo json_encode(['success' => false, 'status' => 'error', 'message' => 'No hay comandas para imprimir', 'errores' => []]);
+    exit;
+}
+
+$impresos = $dataPrint['impresos'];
+$errores = array();
+$exitos = array();
+
+// Iterar sobre cada impresora/cocina
+foreach ($impresos as $nombreImpresora => $datosComanda) {
+    try {
+        // Crear connector según el nombre de la impresora
+        $connector = new WindowsPrintConnector($nombreImpresora);
+        $printer = new Printer($connector);
+        
+        // ENCABEZADO
+        $printer->setJustification(Printer::JUSTIFY_CENTER);
+        $printer->setTextSize(2, 2);
+        $printer->text($datosComanda['cocina'] . "\n");
+        $printer->setTextSize(1, 1);
+        $printer->text(str_repeat("=", 45) . "\n");
+        $printer->setEmphasis(true);
+        $printer->text("ORDEN #" . (isset($datosComanda['num_orden']) ? $datosComanda['num_orden'] : $datosComanda['id_orden']) . "\n");
+        $printer->setEmphasis(false);
+        $printer->text(str_repeat("=", 45) . "\n");
+        
+        // INFORMACIÓN DE LA ORDEN
+        $printer->setJustification(Printer::JUSTIFY_LEFT);
+        $printer->setEmphasis(true);
+        $printer->text("Fecha: ");
+        $printer->setEmphasis(false);
+        $printer->text(date('d/m/Y H:i', strtotime($datosComanda['fecha'])) . "\n");
+        
+        
+        $printer->setEmphasis(true);
+        $printer->text("Mesa: ");
+        $printer->setEmphasis(false);
+        $printer->text($datosComanda['mesa'] . "\n");
+        
+        /*
+        $printer->setEmphasis(true);
+        $printer->text("Mesero: ");
+        $printer->setEmphasis(false);
+        $printer->text($datosComanda['mesero'] . "\n");
+        
+        $printer->setEmphasis(true);
+        $printer->text("Cliente: ");
+        $printer->setEmphasis(false);
+        $printer->text($datosComanda['cliente'] . "\n");*/
+        
+        $printer->setEmphasis(true);
+        $printer->text("Servicio: ");
+        $printer->setEmphasis(false);
+        $printer->text($datosComanda['servicio'] . "\n");
+        
+        $printer->text(str_repeat("-", 45) . "\n");
+        
+        // PLATOS
+        $printer->setEmphasis(true);
+        $printer->text("PLATOS:\n");
+        $printer->setEmphasis(false);
+        $printer->text(str_repeat("-", 45) . "\n");
+        
+        foreach ($datosComanda['platos'] as $plato) {
+            // $plato es un array con [cant, nombre, acompanamientos, notas]
+            $cant = $plato[0];
+            $nombre = $plato[1];
+            $acompanamientos = isset($plato[2]) ? $plato[2] : '';
+            $notas = isset($plato[3]) ? $plato[3] : '';
+            
+            // Cantidad y nombre del plato
+            $printer->setEmphasis(true);
+            $printer->setTextSize(1, 1);
+            $printer->text(number_format($cant, 0) . "x " . $nombre . "\n");
+            $printer->setEmphasis(false);
+            
+            // Acompañamientos
+            if (!empty($acompanamientos)) {
+                $printer->text("   " . $acompanamientos . "\n");
+            }
+            
+            // Notas del plato
+            if (!empty($notas)) {
+                $printer->text("   Notas: " . $notas . "\n");
+            }
+            
+            $printer->text("\n");
+        }
+        
+        $printer->text(str_repeat("=", 45) . "\n");
+        
+        // NOTAS GENERALES DE LA ORDEN
+        if (!empty($datosComanda['notas'])) {
+            $printer->text("\n");
+            $printer->setEmphasis(true);
+            $printer->setTextSize(1, 1);
+            $printer->text("*** NOTAS ***\n");
+            $printer->setTextSize(1, 1);
+            $printer->setEmphasis(false);
+            $printer->text($datosComanda['notas'] . "\n");
+            $printer->text(str_repeat("=", 45) . "\n");
+        }
+        
+        $printer->feed(3);
+        $printer->cut();
+        $printer->close();
+        
+        $exitos[] = $nombreImpresora . " (" . $datosComanda['cocina'] . ")";
+        
+    } catch (Exception $e) {
+        $errores[] = array(
+            'impresora' => $nombreImpresora,
+            'cocina' => isset($datosComanda['cocina']) ? $datosComanda['cocina'] : 'N/A',
+            'error' => $e->getMessage()
+        );
+    }
+}
+
+// Respuesta
+$response = array(
+    'success' => count($errores) == 0,
+    'status' => count($errores) == 0 ? 'success' : 'partial',
+    'exitos' => $exitos,
+    'errores' => $errores,
+    'total_impresos' => count($impresos),
+    'total_exitos' => count($exitos),
+    'total_errores' => count($errores),
+    'message' => count($errores) == 0 
+        ? 'Todas las comandas se imprimieron correctamente' 
+        : 'Algunas comandas no se pudieron imprimir'
+);
+
+echo json_encode($response);

+ 205 - 0
precuenta_movil.php

@@ -0,0 +1,205 @@
+<?php
+/**
+ * Archivo para imprimir precuentas desde app móvil
+ * Imprime solo en la impresora de caja (normalmente POS-80C)
+  * Usa librería: mike42/escpos-php
+ */
+
+if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
+    header("Access-Control-Allow-Origin: *");
+    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
+    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
+    http_response_code(204);
+    exit;
+}
+
+header("Access-Control-Allow-Origin: *");
+header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
+header("Access-Control-Allow-Headers: Content-Type, Authorization");
+header('Content-Type: application/json; charset=utf-8');
+
+error_reporting(E_ALL);
+ini_set('display_errors', 0);
+ini_set('log_errors', 1);
+ini_set('error_log', 'logs/error.log');
+
+require __DIR__ . '/vendor/autoload.php';
+use Mike42\Escpos\Printer;
+use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
+
+// Recibir datos
+$dataPrint = null;
+
+if(isset($_POST["dataPrint"])){
+    if(is_string($_POST["dataPrint"])){
+        $dataPrint = json_decode($_POST["dataPrint"], true);
+    } else {
+        $dataPrint = $_POST["dataPrint"];
+    }
+}
+
+if(!$dataPrint){
+    echo json_encode([
+        'success' => false,
+        'status' => 'error', 
+        'message' => 'No se recibieron datos para imprimir'
+    ]);
+    exit;
+}
+
+try {
+    // Nombre de la impresora de caja (ajustar según tu configuración)
+    $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C";
+    
+    $connector = new WindowsPrintConnector($nombreImpresora);
+    $printer = new Printer($connector);
+    
+    $anchoPapel = isset($dataPrint['anchopapel']) ? intval($dataPrint['anchopapel']) : 80;
+    
+    // ENCABEZADO
+    $printer->setJustification(Printer::JUSTIFY_CENTER);
+    $printer->setTextSize(2, 2);
+    $printer->text($dataPrint['empresa']['nombre_empresa'] . "\n");
+    $printer->setTextSize(1, 1);
+    $printer->text($dataPrint['empresa']['direccion_empresa'] . "\n");
+    $printer->text("Tel: " . $dataPrint['empresa']['telefono_empresa'] . "\n");
+    $printer->text(str_repeat("=", 45) . "\n");
+    $printer->setEmphasis(true);
+    $printer->text("PRECUENTA\n");
+    $printer->setEmphasis(false);
+    $printer->text(str_repeat("=", 45) . "\n");
+    
+    // INFORMACIÓN DE LA ORDEN
+    $printer->setJustification(Printer::JUSTIFY_LEFT);
+    $printer->setEmphasis(true);
+    $printer->text("Orden #: ");
+    $printer->setEmphasis(false);
+    $printer->text($dataPrint['num_orden'] . "\n");
+    
+    $printer->setEmphasis(true);
+    $printer->text("Fecha: ");
+    $printer->setEmphasis(false);
+    $printer->text(date('d/m/Y H:i', strtotime($dataPrint['fecha'])) . "\n");
+    
+    if (!empty($dataPrint['salon'])) {
+        $printer->setEmphasis(true);
+        $printer->text("Salon: ");
+        $printer->setEmphasis(false);
+        $printer->text($dataPrint['salon'] . "\n");
+    }
+    
+    if (!empty($dataPrint['mesa'])) {
+        $printer->setEmphasis(true);
+        $printer->text("Mesa: ");
+        $printer->setEmphasis(false);
+        $printer->text($dataPrint['mesa'] . "\n");
+    }
+    
+    /*
+    if (!empty($dataPrint['mesero'])) {
+        $printer->setEmphasis(true);
+        $printer->text("Mesero: ");
+        $printer->setEmphasis(false);
+        $printer->text($dataPrint['mesero'] . "\n");
+    }*/
+    
+    if (!empty($dataPrint['cliente'])) {
+        $printer->setEmphasis(true);
+        $printer->text("Cliente: ");
+        $printer->setEmphasis(false);
+        $printer->text($dataPrint['cliente'] . "\n");
+    }
+    
+    if (!empty($dataPrint['servicio'])) {
+        $printer->setEmphasis(true);
+        $printer->text("Servicio: ");
+        $printer->setEmphasis(false);
+        $printer->text($dataPrint['servicio'] . "\n");
+    }
+    
+    $printer->text(str_repeat("-", 45) . "\n");
+    
+    // ENCABEZADO DE TABLA
+    $printer->setEmphasis(true);
+    $printer->text("CANT  DESCRIPCION               TOTAL\n");
+    $printer->setEmphasis(false);
+    $printer->text(str_repeat("-", 45) . "\n");
+    
+    // PLATOS
+    $subtotal = 0;
+    foreach ($dataPrint['platos'] as $plato) {
+        // Convertir objeto a array si es necesario
+        $platoArray = is_object($plato) ? (array)$plato : $plato;
+        
+        $cant = number_format($platoArray['cant'], 0);
+        $nombre = $platoArray['nombre'];
+        $precio = $platoArray['precio'];
+        $total = $platoArray['cant'] * $platoArray['precio'];
+        $subtotal += $total;
+        
+        // Línea del plato - cant y nombre
+        $cantPad = str_pad($cant, 4);
+        $nombreCorto = substr($nombre, 0, 25);
+        $totalStr = "$" . number_format($total, 2);
+        
+        $printer->text($cantPad . "x " . str_pad($nombreCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n");
+        
+        // Acompañamientos
+        if (isset($platoArray['acompanamientos']) && is_array($platoArray['acompanamientos']) && count($platoArray['acompanamientos']) > 0) {
+            foreach ($platoArray['acompanamientos'] as $acomp) {
+                $acompArray = is_object($acomp) ? (array)$acomp : $acomp;
+                $printer->text("     + " . $acompArray['acompanamiento'] . "\n");
+            }
+        }
+        
+        /*
+        if (!empty($platoArray['notas'])) {
+            $printer->text("     Nota: " . $platoArray['notas'] . "\n");
+        }*/
+    }
+    
+    $printer->text(str_repeat("=", 45) . "\n");
+    
+    // TOTALES
+    $printer->setJustification(Printer::JUSTIFY_RIGHT);
+    $printer->text("Subtotal:  $" . number_format($dataPrint['subtotal'], 2) . "\n");
+    
+    if (isset($dataPrint['descuento']) && $dataPrint['descuento'] > 0) {
+        $printer->text("Descuento: -$" . number_format($dataPrint['descuento'], 2) . "\n");
+    }
+    
+    if (isset($dataPrint['propina']) && $dataPrint['propina'] > 0) {
+        $printer->text("Propina:   $" . number_format($dataPrint['propina'], 2) . "\n");
+    }
+    
+    $total = $dataPrint['subtotal'] - (isset($dataPrint['descuento']) ? $dataPrint['descuento'] : 0) + (isset($dataPrint['propina']) ? $dataPrint['propina'] : 0);
+    
+    $printer->text(str_repeat("-", 45) . "\n");
+    $printer->setEmphasis(true);
+    $printer->setTextSize(2, 1);
+    $printer->text("TOTAL: $" . number_format($total, 2) . "\n");
+    $printer->setTextSize(1, 1);
+    $printer->setEmphasis(false);
+    $printer->text(str_repeat("=", 45) . "\n");
+    
+    $printer->setJustification(Printer::JUSTIFY_CENTER);
+    $printer->text("\n¡Gracias por su preferencia!\n");
+    $printer->text("Esta NO es una factura\n");
+    
+    $printer->feed(3);
+    $printer->cut();
+    $printer->close();
+    
+    echo json_encode([
+        'success' => true,
+        'status' => 'success',
+        'message' => 'Precuenta impresa correctamente en ' . $nombreImpresora
+    ]);
+    
+} catch (Exception $e) {
+    echo json_encode([
+        'success' => false,
+        'status' => 'error',
+        'message' => 'Error al imprimir: ' . $e->getMessage()
+    ]);
+}