| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?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()
- ]);
- }
- ?>
|