ThermalPrintingController.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. // use App\Models\Transaction;
  5. use App\Models\Transaction;
  6. use Exception;
  7. use Hoa\Socket\Client as SocketClient;
  8. use Hoa\Websocket\Client as WebsocketClient;
  9. class ThermalPrintingController extends Controller
  10. {
  11. public function __invoke(Transaction $transaction)
  12. {
  13. // $thermalPrinting = new ThermalPrinting($transaction);
  14. // $thermalPrinting->startPrinting(1);
  15. $transaction->load(['outlet', 'customer', 'transactionDetails.laundry']);
  16. $discount = $transaction->discount;
  17. $subTotalAsString = $transaction->subTotalAsString();
  18. $totalPriceAsString = $transaction->totalPriceAsString();
  19. foreach ($transaction->transactionDetails as $transactionDetail) {
  20. $totalPriceAsStringDetail = $transactionDetail->totalPriceAsString();
  21. $transactionDetail->totalPriceAsString = $totalPriceAsStringDetail;
  22. }
  23. $transaction->discount = $discount;
  24. $transaction->subTotalAsString = $subTotalAsString;
  25. $transaction->totalPriceAsString = $totalPriceAsString;
  26. try {
  27. $socket = new WebsocketClient(
  28. new SocketClient('ws://127.0.0.1:5544')
  29. );
  30. $socket->setHost('escpos-server');
  31. $socket->connect();
  32. $socket->send(json_encode($transaction));
  33. $socket->close();
  34. } catch (Exception $e) {
  35. return back()->with('error', __('messages.error.store.transaction'));
  36. }
  37. return back();
  38. }
  39. }