Bladeren bron

Agregar ticket de precuenta, sonido para que pite la comanda y redondeo de decimales en factura

David Gómez 1 maand geleden
bovenliggende
commit
e154dfd6b9
4 gewijzigde bestanden met toevoegingen van 213 en 1 verwijderingen
  1. 1 1
      factura_electronica.php
  2. 210 0
      print_caja/precuenta_diaria_caja.php
  3. 1 0
      print_express/comanda_express.php
  4. 1 0
      print_movil/comanda_movil.php

+ 1 - 1
factura_electronica.php

@@ -66,7 +66,7 @@ if($dataPrint){
     $totalGravado = $dataPrint->totalGrabadas;
     $totalExento = $dataPrint->totalExento;
     $totalNoSujeto = $dataPrint->totalNS;
-    $totalFinal = $dataPrint->totales->totalTotal;
+    $totalFinal = floatval(str_replace(',', '', $dataPrint->totales->totalTotal));
     $totalCambio = floatval($dataPrint->efectivo) - $totalFinal;
 
     $dteNControl = $dataPrint->dte_numero_control;

+ 210 - 0
print_caja/precuenta_diaria_caja.php

@@ -0,0 +1,210 @@
+<?php
+/**
+ * Archivo para imprimir precuentas por rango de fecha (Precuenta Diaria)
+ * Este archivo debe copiarse al servidor de impresión (printserver)
+ * Imprime 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', __DIR__ . '/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"])){
+    $dataPrint = is_array($_POST["dataPrint"]) ? $_POST["dataPrint"] : json_decode(json_encode($_POST["dataPrint"]), true);
+}
+
+if(!$dataPrint){
+    echo json_encode([
+        'success' => false,
+        'status'  => 'error',
+        'message' => 'No se recibieron datos para imprimir'
+    ]);
+    exit;
+}
+
+try {
+    $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C";
+
+    $connector = new WindowsPrintConnector($nombreImpresora);
+    $printer   = new Printer($connector);
+
+    // ── ENCABEZADO ────────────────────────────────────────────────
+    $printer->setJustification(Printer::JUSTIFY_CENTER);
+    $printer->setTextSize(2, 2);
+    $printer->text($dataPrint['nombre_empresa'] . "\n");
+    $printer->setTextSize(1, 1);
+    if (!empty($dataPrint['direccion_empresa'])) {
+        $printer->text($dataPrint['direccion_empresa'] . "\n");
+    }
+    $printer->text(str_repeat("=", 45) . "\n");
+    $printer->setEmphasis(true);
+    $printer->text("PRECUENTA DIARIA\n");
+    $printer->setEmphasis(false);
+
+    // Rango de fechas
+    if (!empty($dataPrint['subtotal_fecha'])) {
+        $printer->text("Periodo: " . $dataPrint['subtotal_fecha'] . "\n");
+    }
+
+    $printer->text(str_repeat("=", 45) . "\n");
+
+    // ── INFORMACIÓN DE LA ORDEN ───────────────────────────────────
+    $printer->setJustification(Printer::JUSTIFY_LEFT);
+
+    if (isset($dataPrint['orden'])) {
+        $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
+
+        if (!empty($orden['num_orden'])) {
+            $printer->setEmphasis(true);
+            $printer->text("Orden #: ");
+            $printer->setEmphasis(false);
+            $printer->text($orden['num_orden'] . "\n");
+        }
+
+        if (!empty($orden['salon'])) {
+            $printer->setEmphasis(true);
+            $printer->text("Salon: ");
+            $printer->setEmphasis(false);
+            $printer->text($orden['salon'] . "\n");
+        }
+
+        if (!empty($orden['mesa'])) {
+            $printer->setEmphasis(true);
+            $printer->text("Mesa: ");
+            $printer->setEmphasis(false);
+            $printer->text($orden['mesa'] . "\n");
+        }
+
+        if (!empty($orden['cliente'])) {
+            $printer->setEmphasis(true);
+            $printer->text("Cliente: ");
+            $printer->setEmphasis(false);
+            $printer->text($orden['cliente'] . "\n");
+        }
+
+        if (!empty($orden['servicio'])) {
+            $printer->setEmphasis(true);
+            $printer->text("Servicio: ");
+            $printer->setEmphasis(false);
+            $printer->text($orden['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;
+    if (isset($dataPrint['platos']) && is_array($dataPrint['platos'])) {
+        foreach ($dataPrint['platos'] as $plato) {
+            $platoArray = is_object($plato) ? (array)$plato : $plato;
+
+            $cant    = isset($platoArray['cant'])   ? number_format($platoArray['cant'], 0) : '0';
+            $nombre  = isset($platoArray['nombre']) ? $platoArray['nombre'] : '';
+            $precio  = isset($platoArray['precio']) ? floatval($platoArray['precio']) : 0;
+            $cantNum = isset($platoArray['cant'])   ? floatval($platoArray['cant'])   : 0;
+            $total   = $cantNum * $precio;
+            $subtotal += $total;
+
+            $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;
+                    if (isset($acompArray['acompanamiento'])) {
+                        $printer->text("     + " . $acompArray['acompanamiento'] . "\n");
+                    }
+                }
+            }
+        }
+    }
+
+    $printer->text(str_repeat("=", 45) . "\n");
+
+    // ── TOTALES ───────────────────────────────────────────────────
+    $printer->setJustification(Printer::JUSTIFY_RIGHT);
+
+    $ordenSubtotal  = $subtotal; // Para precuenta diaria usar suma real de los platos filtrados
+    $ordenDescuento = 0;
+    $ordenPropina   = 0;
+
+    if (isset($dataPrint['orden'])) {
+        $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
+        $ordenDescuento = isset($orden['descuento']) ? floatval($orden['descuento']) : 0;
+        $ordenPropina   = isset($orden['propina'])   ? floatval($orden['propina'])   : 0;
+    }
+
+    $printer->text("Subtotal:  $" . number_format($ordenSubtotal, 2) . "\n");
+
+    if ($ordenDescuento > 0) {
+        $printer->text("Descuento: -$" . number_format($ordenDescuento, 2) . "\n");
+    }
+
+    if ($ordenPropina > 0) {
+        $printer->text("Propina:   $" . number_format($ordenPropina, 2) . "\n");
+    }
+
+    $totalFinal = $ordenSubtotal - $ordenDescuento + $ordenPropina;
+
+    $printer->text(str_repeat("-", 45) . "\n");
+    $printer->setEmphasis(true);
+    $printer->setTextSize(2, 1);
+    $printer->text("TOTAL: $" . number_format($totalFinal, 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 Diaria impresa correctamente en ' . $nombreImpresora
+    ]);
+
+} catch (Exception $e) {
+    echo json_encode([
+        'success' => false,
+        'status'  => 'error',
+        'message' => 'Error al imprimir: ' . $e->getMessage()
+    ]);
+}
+?>

+ 1 - 0
print_express/comanda_express.php

@@ -207,6 +207,7 @@ if($dataComanda){
             // CORTAR PAPEL Y CERRAR IMPRESORA
             $printer->feed(3);
             $printer->cut();
+	        $printer->getPrintConnector()->write("\x1B\x42" . chr(4) . chr(1));
             $printer->close();
             
             $resultados[] = [

+ 1 - 0
print_movil/comanda_movil.php

@@ -149,6 +149,7 @@ foreach ($impresos as $nombreImpresora => $datosComanda) {
         
         $printer->feed(3);
         $printer->cut();
+        $printer->getPrintConnector()->write("\x1B\x42" . chr(4) . chr(1));
         $printer->close();
         
         $exitos[] = $nombreImpresora . " (" . $datosComanda['cocina'] . ")";