1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Transaction;
  4. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  5. use Mike42\Escpos\Printer;
  6. class ThermalPrinting
  7. {
  8. public function __construct(private Transaction $transaction)
  9. {}
  10. public function startPrinting(int $repeat = 1)
  11. {
  12. if ($repeat >= 1) {
  13. /* Start the printer */
  14. $connector = new FilePrintConnector("/dev/usb/lp1"); // Port usb connection example: /dev/usb/lp1
  15. $printer = new Printer($connector);
  16. /* Brand */
  17. $printer->setJustification(Printer::JUSTIFY_CENTER);
  18. $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  19. $printer->text("BAMB'S LAUNDRY\n");
  20. $printer->selectPrintMode();
  21. $printer->feed();
  22. /* Unique Id */
  23. $printer->text('ID TRANSAKSI' . "\n");
  24. $printer->setBarcodeTextPosition(Printer::BARCODE_TEXT_BELOW);
  25. $printer->setBarcodeHeight(48);
  26. $printer->setBarcodeWidth(2);
  27. $printer->barcode("{B" . $this->transaction->transaction_number, Printer::BARCODE_CODE128);
  28. $printer->feed();
  29. /* Title of receipt */
  30. $printer->setJustification(Printer::JUSTIFY_LEFT);
  31. $printer->setLineSpacing(6);
  32. $printer->setEmphasis(true);
  33. $printer->text("DETAIL TRANSAKSI\n");
  34. $printer->setEmphasis(false);
  35. $printer->setLineSpacing();
  36. $printer->feed();
  37. $printer->text("WAKTU : " . strtoupper($this->transaction->created_at) . "\n");
  38. $printer->text("OUTLET : {$this->transaction->outlet->name}\n");
  39. $printer->text("ID KUSTOMER: {$this->transaction->customer->customer_number}\n");
  40. $printer->feed();
  41. /* Detail Transaction */
  42. $titleType = str_pad("Jenis Laundry", 20);
  43. $titleQty = str_pad("Qty", 7);
  44. $titlePrice = str_pad("@", 11);
  45. $titleTotalPrice = str_pad("Harga", 10, ' ', STR_PAD_LEFT);
  46. $printer->text("$titleType$titleQty$titlePrice$titleTotalPrice\n");
  47. foreach ($this->transaction->transactionDetails as $transactionDetail) {
  48. $type = str_pad("{$transactionDetail->laundry->name}/{$transactionDetail->laundry->unit}", 20);
  49. $qty = str_pad("{$transactionDetail->quantity}", 7);
  50. $price = str_pad("{$transactionDetail->price}", 11);
  51. $totalPrice = str_pad("{$transactionDetail->totalPriceAsString()}", 10, ' ', STR_PAD_LEFT);
  52. $printer->text("$type$qty$price$totalPrice\n");
  53. $printer->text("DISKON: {$transactionDetail->discount}\n");
  54. }
  55. /* Discount and total */
  56. $printer->feed();
  57. $printer->text($this->textSpacing('SUBTOTAL', $this->transaction->subTotalAsString()));
  58. $printer->text($this->textSpacing('DISKON', $this->transaction->discount));
  59. $printer->setEmphasis(true);
  60. $printer->text($this->textSpacing('TOTAL', "Rp{$this->transaction->totalPriceAsString()}"));
  61. $printer->setEmphasis(false);
  62. $printer->feed();
  63. /* Footer */
  64. $printer->setJustification(Printer::JUSTIFY_CENTER);
  65. $printer->text("** TERIMA KASIH **\n");
  66. $printer->feed();
  67. /* Cut the receipt */
  68. $printer->cut();
  69. $printer->close();
  70. $this->startPrinting($repeat - 1);
  71. }
  72. }
  73. public function textSpacing(string $name, string $price)
  74. {
  75. $left = str_pad($name, 20);
  76. $right = str_pad($price, 28, ' ', STR_PAD_LEFT);
  77. return "$left$right\n";
  78. }
  79. }