Skip to content

Instantly share code, notes, and snippets.

@scriptdev
Created August 7, 2023 19:24
Show Gist options
  • Save scriptdev/e27b93f568b048e947e8cacd77803ae9 to your computer and use it in GitHub Desktop.
Save scriptdev/e27b93f568b048e947e8cacd77803ae9 to your computer and use it in GitHub Desktop.
IMPRESSÃO TÉRMICA ( ESC/POS Print for PHP ) Mike42
<?php
# https://github.com/mike42/escpos-php
# composer: mike42/escpos-php
use Mike42EscposPrinter;
use Mike42EscposEscposImage;
use Mike42EscposImagickEscposImage;
use Mike42EscposPrintConnectorsFilePrintConnector;
use Mike42EscposPrintConnectorsWindowsPrintConnector;
public function onImprimir($param = Null)
{
$key = $param['key'];
$object = new Vendas($key, FALSE);
$atendente = SystemUser::findInTransaction('permission', $object->usuario)->login;
$config = Config::find('1');
$cobrartaxa = $config->taxa_servico;
$p_taxa = $config->p_taxa;
$endereco = $config->endereco;
$telefone = $config->telefone;
$impressora_venda = $config->impressora_venda;
$mesa = Mesas::find($object->mesa_id);
$connector = new WindowsPrintConnector('MOME DA IMPRESSORA NO WINDOWS');
$printer = new Printer($connector);
$tux = EscposImage::load("app/images/logo.png", false);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer -> bitImage($tux);
$printer -> setTextSize(1, 1);
$printer -> text(" \n");
$printer -> text(utf8_decode($endereco));
$printer -> text(" \n");
$printer -> text(utf8_decode($telefone));
$printer -> text(" \n");
$printer -> text(utf8_decode('CNPJ.: 42.263.161/0001-18'));
$printer -> text(" \n");
$printer -> text("------------------------------------------------");
$printer -> selectPrintMode(Printer::MODE_FONT_B);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer -> setTextSize(2, 2);
$printer -> text('MESA: '.$mesa->numero_mesa);
$printer->feed(2);
$printer -> selectPrintMode(Printer::MODE_FONT_B);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer -> setTextSize(2, 1);
$printer -> text($object->data_formatada);
$printer->feed(1);
$printer -> text('EXTRATO PARA SIMPLES CONFERÊNCIA');
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$printer->text('QTDE/PRODUTO PREÇO UN TOTAL');
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$items = VendasItens::where('venda_id', '=', $key)->load();
$total = 0;
foreach($items as $item)
{
$letras = strlen(trim(substr(Produtos::findInTransaction('sgbr', $item->produto_id)->descricao,0,25)));
$espacos = 28-$letras;
$produto = str_pad(substr(Produtos::findInTransaction('sgbr', $item->produto_id)->descricao,0,25),$espacos, " ");
$qtde = number_format($item->quantidade,0,',','.');
$preco = number_format($item->preco,2,',','.');
$total_un = number_format($item->preco*$item->quantidade,2,',','.');
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer ->selectPrintMode(Printer::MODE_FONT_A);
$printer->text("{$qtde} {$produto} ({$preco}) --> ({$total_un})");
$printer->feed(1);
$total += ($item->preco*$item->quantidade);
}
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$taxa = $total*$p_taxa*0.01;
$valor_taxa = number_format($total*$p_taxa*0.01,2,',','.');
$sub_total = number_format($total,2,',','.');
if ($cobrartaxa=='S')
{
$total_geral = number_format($total+$taxa,2,',','.');
$taxa_servico = number_format($taxa,2,',','.');
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer ->selectPrintMode(Printer::MODE_FONT_B);
$printer ->setTextSize(2, 1);
$printer ->text("SUB-TOTAL R$.: {$sub_total}");
$printer->feed(1);
$printer ->text("Taxa Serviço.: {$taxa_servico}");
$printer->feed(1);
$printer ->text("TOTAL A RECEBER R$.: {$total_geral}");
$printer->feed(1);
} else {
$total_geral = $sub_total;
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer ->selectPrintMode(Printer::MODE_FONT_B);
$printer ->setTextSize(2, 1);
$printer ->text("TOTAL R$.: {$total_geral}");
$printer->feed(1);
}
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_LEFT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer ->text("ATENDENTE: {$atendente}");
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer ->selectPrintMode(Printer::MODE_FONT_B);
$printer ->setTextSize(2, 1);
$printer ->text("Agradecemos a preferência!");
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer -> selectPrintMode(Printer::MODE_FONT_A);
$printer -> text("------------------------------------------------");
$printer->feed(1);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer ->selectPrintMode(Printer::MODE_FONT_B);
$printer ->setTextSize(2, 1);
$printer ->text("****DOCUMENTO NÃO FISCAL****");
$printer->feed(2);
$printer -> cut();
$printer -> close();
}
# AGRADECIMENTO: Robson Sorrilha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment