<?php
    session_start();
    require '../../vendor/autoload.php';
    require('functions_ticket.php');
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    use Mike42\Escpos\Printer;

    use Mike42\Escpos\EscposImage;
    #========================================================= #
    #=================== Recibiendo datos ==================== #
    
    $id_nota = $_POST['id'];
    $nota = get_nota($id_nota);
    $campo = get_productos_nota($id_nota);
    $no_nota = $nota[0]['CLAVE'];
    $mesero = $nota[1]['NOMBRE'];
    $tipo_venta = $nota[2]['ZONA'];
    $p=$nota[4]['PERSONAS'];  //Personas 
    $m=$nota[3]['MESA']; // Numero de mesa
    $subtotal=$nota[5]["SUBTOTAL"];
    $descuento=$nota[6]["DESCUENTO"];
    $total=$nota[7]["TOTAL"];
    $descuento_total = ($subtotal*$descuento)/100;

    date_default_timezone_set('America/Mexico_City');
    $hoy=new DateTime();

    #========================================================= #

    #=================== Datos de la impresora =============== #
    $name = $_POST['opcionImprComanda'];
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector($name);
    $printer = new Printer($connector, $profile);

    #========================================================= #

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);
    $printer->text("Mi Restaurant \n");
    $logo = EscposImage::load("../../assets/img/logos/logo.png", false);

    $printer -> graphics($logo);
    $printer -> feed();
    $printer->text("\n");
    
    $printer->setTextSize(1, 1);
    $printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
    $printer->text("NOTA DE VENTA \n");
    $printer -> selectPrintMode();
    $printer->setJustification(Printer::JUSTIFY_CENTER);
   
    $printer->text("Martha Angélica Samperio Fosado". "\n");
    $printer->text("RFC: SAFM780520L77". "\n");

    $printer->text("Carretera Federal Pahuca-Tulancingo". "\n");
    $printer->text("Kilometro 19.5". "\n");
    $printer->text("Teléfono: 7751365015". "\n");
    $printer->text("Sitio Web: www.micabanita.mx". "\n");
    $printer->text("------------------------------------------". "\n");

    #La fecha también
    $printer->text("Fecha:".date("Y-m-d H:i:s") . "\n");
    $printer->text("Tipo de venta:" . $tipo_venta . "\n");
    $printer->setTextSize(2, 1);
    $printer->text("M:" . $m . "\n");
    $printer->setTextSize(1, 1);
    $printer->text("Mesero: " . $mesero . "\n");
    $printer->text("No. Nota: " . $no_nota . "\n");
    $printer->text("------------------------------------------". "\n");
    $printer->text("Cant       Desc        Precio     Total \n");
    $printer->text("------------------------------------------". "\n");

    foreach($campo as $cam) {
        $pro_total = $cam['PRECIO'] *$cam['CANTIDAD'];
        $id_pro_nota = $cam['ID'];
        $printer->setJustification(Printer::JUSTIFY_CENTER);

        $printer->setEmphasis(false);
        $printer->setTextSize(1, 1);
        $printer->text($cam['PRO_NOMBRE']."\n"); 

        $printer->setJustification(Printer::JUSTIFY_LEFT);
        $printer->text("  ".$cam['CANTIDAD']  . "          " . " 0 " . "         " .  "$ ".$cam['PRECIO']  . "     " . "$ ".$pro_total. "\n");
        $query2 = query("UPDATE productos_nota SET impreso='IC', pdn_guardado=pdn_guardado+1 WHERE id='$id_pro_nota'");
       
    }
    $printer->text("\n");
    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setTextSize(1, 1);
    $printer->text("Subtotal:" . "$ " . $subtotal . "\n");
    $printer->text("Desc p/Partida:" . $descuento . " %" . "\n");
    $printer->text("Desc Gral:" . "$ " . $descuento_total . "\n");
    $printer->text("----------------------------------------". "\n");

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->setEmphasis(true);
    $printer->setTextSize(2, 2);

    $printer->text("TOTAL:" . " $ " . $total . "\n");
    $printer->setEmphasis(false);


    $printer->setTextSize(1, 1);
    $printer->text("\n");
    $printer->text("¡GRACIAS POR TU VISITA!\n");

     // Cortar el papel
     $printer->cut();

     // Enviar un pulso de apertura del cajón de efectivo
     $printer->pulse();
 
     // Esperar un segundo para que la impresora procese la orden
     sleep(1);
    
    // Cerrar la impresora
    $printer->close();
?>