ThermalPrinting.php 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  3. use Mike42\Escpos\Printer;
  4. class ThermalPrinting
  5. {
  6. public function __construct($transaction)
  7. {
  8. $this->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");
  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->transaction_details 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. $transactionDetail->discount != '0%' ? $printer->text("DISKON: {$transactionDetail->discount}\n") : null;
  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 == '0%' ? "{$this->transaction->discount}" : "{$this->transaction->discount} (-{$this->transaction->discountAsString})"));
  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. }