Files
stradaautopartes/app/Printer/PrinterHandler.php
2025-05-25 18:41:39 -07:00

172 lines
6.4 KiB
PHP

<?php
namespace App\Printer;
use App\Models\Impresora;
use App\Models\Pedido;
use Illuminate\Support\Facades\Hash;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\Printer;
class PrinterHandler{
private Impresora $impresora;
private Printer $printer;
private $printerConfig;
const PRINTER_BY_IP = 1;
const PRINTER_BY_SHARED = 2;
public function __construct(Impresora $impresora,$type_printer) {
$this->impresora = $impresora;
if($type_printer == PrinterHandler::PRINTER_BY_IP)
{
$connector = new NetworkPrintConnector($impresora->estacion,$impresora->nombre_impresora);
}
else if($type_printer == PrinterHandler::PRINTER_BY_SHARED)
{
$connector = new WindowsPrintConnector("smb://".$impresora->estacion."/".$impresora->nombre_impresora."");
}
$this->printer = new Printer($connector);
}
public function text(string $message="") : void {
$this->printer->text($message."\n");
}
public function testPage()
{
$img = EscposImage::load('storage/img/proone_print.jpg',false);
$this->printer->bitImage($img);
$this->text("***** ESTRADA AUTOPARTES *****");
$this->text("Pagina de prueba de impresion");
$this->text("Estacion:".$this->impresora->estacion);
$this->text("Nombre Impresora:".$this->impresora->nombre_impresora);
$this->text("Desarrollado por Super Dev Bros");
$this->feed();
$this->cut();
$this->close();
}
public function printImage($path)
{
$img = EscposImage::load($path);
$this->printer->bitImage($img);
}
public function printPedido (Pedido $pedido, $hashed = true)
{
$IVA = 0;
$subTotal = 0;
$total = 0;
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->printImage('storage/img/proone_print.jpg');
$this->feed();
$this->printer -> setTextSize(2, 2);
if($hashed)
{
$this->text("CLIENTE");
}
$this->text("No. Pedido:");
$this->text($pedido->pedido);
$this->printer -> setTextSize(1, 1);
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->feed(1);
// $this->text("MIGUEL ANGEL ROMERO ESTRADA");
$this->text("DIEGO ESQUIVEL #130 INT. 3");
$this->text("PLAYAS DE ROSARITO, BC 22706 Mexico");
$this->text("R.F.C. : ROEM691011EZ4");
$this->text("TELEFONO: (661) 613-5549");
$this->text("CORREO: ventas@stradaautopartes.com");
$this->printer->setJustification(Printer::JUSTIFY_LEFT);
$this->feed(1);
$this->text("Atendido por: ".$pedido->user->name);
$this->feed(1);
$this->text("---------------- Datos Cliente ----------------");
$this->text("Cuenta: ".$pedido->cuenta);
$this->text("Telefono: ".$pedido->telefono);
$this->text("Fecha: ".now()->format('Y-m-d H:m:s'));
$this->text("Año:".$pedido->year);
$this->text("Marca:".$pedido->marca);
$this->text("Modelo:".$pedido->modelo);
$this->text("Motor:".$pedido->motor);
$this->text("---------------- Datos De Pago ----------------");
foreach ($pedido->primeraVenta()->getAbonos() as $value) {
$this->text("Metodo: ".$value[1]." | Anticipo: ".$value[0]);
}
$this->text("Adeudo: ".$pedido->primeraVenta()->faltante());
// $this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->text("-------------------- Pedido --------------------");
if($hashed)
{
$this->text('Codigo Linea Descripcion Cant Precio ');
}
else
{
// 48 digitos.
$this->text('Codigo Linea RT Descripcion Cant Precio ');
}
foreach ($pedido->productos as $producto) {
if($hashed)
{
$this->text(str_pad(substr(Hash::make($producto->codigo),0,10),11)."".str_pad($producto->linea,7)."".str_pad(substr($producto->descripcion,0,18),19)."".str_pad(intval($producto->pivot->unidades),5)."".round($producto->pivot->precio_unitario,8));
}
else
{
$this->text(str_pad(substr($producto->codigo ,0,10),11)."".str_pad($producto->linea,6)."".str_pad($producto->ruta->prefijo,5)."".str_pad(substr($producto->descripcion,0,11),12)."".str_pad(intval($producto->pivot->unidades),5)."".round($producto->pivot->precio_unitario,8));
}
// $this->text(str_pad(substr($producto->codigo,0,9),10)."".str_pad($producto->prefijo,6)."".str_pad(substr($producto->descripcion,0,14),15)."".str_pad($producto->pivot->unidades,10)."".round($producto->pivot->unidades*$producto->pivot->precio_unitario/1.16,2));
$subTotal+=$producto->pivot->unidades*$producto->pivot->precio_unitario/1.08;
}
$this->text("________________________________________________");
$IVA = $subTotal*0.08;
$total = round($subTotal+$IVA,2);
$this->feed(1);
$this->printer->setJustification(Printer::JUSTIFY_RIGHT);
$this->text("SubTotal: ".round($subTotal));
$this->text("IVA 8%: ".round($IVA));
$this->text("Total a Pagar: ".$total);
if($pedido->cambio>0)
{
$this->text("Cambio: ".$pedido->cambio);
}
$this->feed(1);
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->text("-------------------COMENTARIOS------------------");
$this->text($pedido->comentarios ?? "");
$this->text("________________________________________________");
$this->feed(1);
$this->text("Revise bien su mercancia, en partes electronicas");
$this->text("no hay garantia, no hay devolucion de efectivo.");
$this->text("Toda orden requiere un anticipo del 50%,");
$this->text("y toda cancelacion se cobrara el 50%");
$this->feed(1);
$this->feed(1);
$this->printer -> setTextSize(2, 2);
$this->text("¡Gracias por su compra!");
}
public function cut()
{
$this->printer->cut();
}
public function close()
{
$this->printer->close();
}
public function feed($lines=2)
{
$this->printer->feed($lines);
}
}