ThermalPrinting.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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"); // 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->text("{$this->transaction->outlet->address}\n");
  22. $printer->text("Telepon: {$this->transaction->outlet->phone}\n");
  23. $printer->feed();
  24. /* Unique Id */
  25. $printer->text('ID TRANSAKSI' . "\n");
  26. $printer->setBarcodeTextPosition(Printer::BARCODE_TEXT_BELOW);
  27. $printer->setBarcodeHeight(48);
  28. $printer->setBarcodeWidth(2);
  29. $printer->barcode("{B" . $this->transaction->transaction_number, Printer::BARCODE_CODE128);
  30. $printer->feed();
  31. /* Title of receipt */
  32. $printer->setJustification(Printer::JUSTIFY_LEFT);
  33. $printer->setLineSpacing(6);
  34. $printer->setEmphasis(true);
  35. $printer->text("DETAIL TRANSAKSI\n");
  36. $printer->setEmphasis(false);
  37. $printer->setLineSpacing();
  38. $printer->feed();
  39. $printer->text("WAKTU : " . strtoupper($this->transaction->created_at) . "\n");
  40. $printer->text("OUTLET : {$this->transaction->outlet->name}\n");
  41. $printer->text("ID KUSTOMER: {$this->transaction->customer->customer_number}\n");
  42. $printer->feed();
  43. /* Detail Transaction */
  44. $titleType = str_pad("Laundry/Produk", 20);
  45. $titleQty = str_pad("Qty", 7);
  46. $titlePrice = str_pad("@", 11);
  47. $titleTotalPrice = str_pad("Harga", 10, ' ', STR_PAD_LEFT);
  48. $printer->text("$titleType$titleQty$titlePrice$titleTotalPrice\n");
  49. foreach ($this->transaction->transaction_details as $transactionDetail) {
  50. if($transactionDetail->laundry) {
  51. $type = str_pad("{$transactionDetail->laundry->name}/{$transactionDetail->laundry->unit}", 20);
  52. } else {
  53. $type = str_pad("{$transactionDetail->product->name}/{$transactionDetail->product->unit}", 20);
  54. }
  55. $qty = str_pad("{$transactionDetail->quantity}", 7);
  56. $price = str_pad("{$transactionDetail->price}", 11);
  57. $totalPrice = str_pad("{$transactionDetail->totalPriceAsString}", 10, ' ', STR_PAD_LEFT);
  58. $printer->text("$type$qty$price$totalPrice\n");
  59. $printer->text("DISKON: {$transactionDetail->discount}\n");
  60. }
  61. /* Discount and total */
  62. $printer->feed();
  63. $printer->text($this->textSpacing('SUBTOTAL', $this->transaction->subTotalAsString));
  64. $printer->text($this->textSpacing('DISKON', $this->transaction->discount));
  65. $printer->setEmphasis(true);
  66. $printer->text($this->textSpacing('TOTAL', "Rp{$this->transaction->totalPriceAsString}"));
  67. $printer->setEmphasis(false);
  68. $printer->feed();
  69. /* Footer */
  70. $printer->setJustification(Printer::JUSTIFY_CENTER);
  71. $printer->text("** TERIMA KASIH **\n");
  72. $printer->feed();
  73. /* Cut the receipt */
  74. $printer->cut();
  75. $printer->close();
  76. $this->startPrinting($repeat - 1);
  77. }
  78. }
  79. public function textSpacing(string $name, string $price)
  80. {
  81. $left = str_pad($name, 20);
  82. $right = str_pad($price, 28, ' ', STR_PAD_LEFT);
  83. return "$left$right\n";
  84. }
  85. }