TransactionController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Transaction\StoreTransactionRequest;
  5. use App\Http\Requests\Transaction\UpdateTransactionRequest;
  6. use App\Models\Customer;
  7. use App\Models\Laundry;
  8. use App\Models\Transaction;
  9. use App\Models\TransactionStatus;
  10. use Illuminate\Database\QueryException;
  11. use Illuminate\Support\Facades\DB;
  12. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  13. use Mike42\Escpos\Printer;
  14. class TransactionController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Inertia\Response
  20. */
  21. public function index()
  22. {
  23. return inertia('transaction/Index', [
  24. 'transactions' => Transaction::latest()
  25. ->filter(request()->only(['search']))
  26. ->paginate(10)
  27. ->withQueryString()
  28. ->through(fn($transaction) => [
  29. 'id' => $transaction->id,
  30. 'transactionNumber' => $transaction->transaction_number,
  31. 'dateLaundry' => $transaction->created_at,
  32. 'customer' => [
  33. 'number' => $transaction->customer->customer_number,
  34. 'name' => $transaction->customer->name,
  35. 'phone' => $transaction->customer->phone,
  36. ],
  37. 'price' => $transaction->totalPrice(),
  38. 'outlet' => $transaction->outlet->name,
  39. 'transactionStatusName' => $transaction->transactionStatus->name,
  40. 'transactionStatusId' => $transaction->transactionStatus->id,
  41. ]),
  42. 'transactionsStatus' => TransactionStatus::all()
  43. ->transform(fn($transactionStatus) => [
  44. 'label' => $transactionStatus->name,
  45. 'value' => $transactionStatus->id,
  46. ]),
  47. ]);
  48. }
  49. /**
  50. * Show the form for creating a new resource.
  51. *
  52. * @return \Inertia\Response
  53. */
  54. public function create()
  55. {
  56. return inertia('transaction/Create', [
  57. 'transactionNumber' => 'TS' . now()->format('YmdHis'),
  58. 'customers' => fn() => Customer::latest()
  59. ->filter(request('customer'))
  60. ->get()
  61. ->transform(fn($customer) => [
  62. 'id' => $customer->id,
  63. 'name' => $customer->name,
  64. 'customerNumber' => $customer->customer_number,
  65. 'phone' => $customer->phone,
  66. ]),
  67. 'laundries' => fn() => Laundry::latest()
  68. ->filter(request('laundry'))
  69. ->get()
  70. ->transform(fn($laundry) => [
  71. 'id' => $laundry->id,
  72. 'name' => $laundry->name,
  73. 'unit' => $laundry->unit,
  74. 'price' => $laundry->getRawOriginal('price'),
  75. ]),
  76. 'customerNumber' => fn() => 'CS' . now()->format('YmdHis'),
  77. 'genders' => [
  78. ['label' => 'Perempuan', 'value' => 1],
  79. ['label' => 'Laki-laki', 'value' => 2],
  80. ],
  81. ]);
  82. }
  83. /**
  84. * Store a newly created resource in storage.
  85. *
  86. * @param \Illuminate\Http\Request $request
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function store(StoreTransactionRequest $request)
  90. {
  91. DB::beginTransaction();
  92. try {
  93. $transaction = Transaction::create([
  94. 'transaction_number' => $request->transaction_number,
  95. 'discount' => $request->discount_all,
  96. 'transaction_status_id' => 1,
  97. 'user_id' => $request->user()->id,
  98. 'customer_id' => $request->customer_id,
  99. 'outlet_id' => $request->user()->outlet_id,
  100. ]);
  101. foreach ($request->laundries as $laundry) {
  102. $transaction->transactionDetails()->create([
  103. 'price' => $laundry['price'],
  104. 'discount' => $laundry['discount'],
  105. 'quantity' => $laundry['quantity'],
  106. 'laundry_id' => $laundry['laundryId'],
  107. ]);
  108. }
  109. DB::commit();
  110. // /* Information for the receipt */
  111. // $items = array(
  112. // new item("Example item #1", "4.00"),
  113. // new item("Another thing", "3.50"),
  114. // new item("Something else", "1.00"),
  115. // new item("A final item", "4.45"),
  116. // );
  117. // $subtotal = new item('Subtotal', '12.95');
  118. // $tax = new item('A local tax', '1.30');
  119. // $total = new item('Total', '14.25', true);
  120. // /* Date is kept the same for testing */
  121. // // $date = date('l jS \of F Y h:i:s A');
  122. // $date = "Monday 6th of April 2015 02:56:25 PM";
  123. // /* Start the printer */
  124. // // $logo = EscposImage::load(public_path('images/escpos-php.png'), false);
  125. // $connector = new FilePrintConnector("/dev/usb/lp1");
  126. // $printer = new Printer($connector);
  127. // /* Print top logo */
  128. // $printer->setJustification(Printer::JUSTIFY_CENTER);
  129. // // $printer->graphics($logo);
  130. // /* Name of shop */
  131. // $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  132. // $printer->text("ExampleMart Ltd.\n");
  133. // $printer->selectPrintMode();
  134. // $printer->text("Shop No. 42.\n");
  135. // $printer->feed();
  136. // /* Title of receipt */
  137. // $printer->setEmphasis(true);
  138. // $printer->text("SALES INVOICE\n");
  139. // $printer->setEmphasis(false);
  140. // /* Items */
  141. // $printer->setJustification(Printer::JUSTIFY_LEFT);
  142. // $printer->setEmphasis(true);
  143. // $printer->text(new item('', '$'));
  144. // $printer->setEmphasis(false);
  145. // foreach ($items as $item) {
  146. // $printer->text($item);
  147. // }
  148. // $printer->setEmphasis(true);
  149. // $printer->text($subtotal);
  150. // $printer->setEmphasis(false);
  151. // $printer->feed();
  152. // /* Tax and total */
  153. // $printer->text($tax);
  154. // $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  155. // $printer->text($total);
  156. // $printer->selectPrintMode();
  157. // /* Footer */
  158. // $printer->feed(2);
  159. // $printer->setJustification(Printer::JUSTIFY_CENTER);
  160. // $printer->text("Thank you for shopping at ExampleMart\n");
  161. // $printer->text("For trading hours, please visit example.com\n");
  162. // $printer->feed(2);
  163. // $printer->text($date . "\n");
  164. // /* Cut the receipt and open the cash drawer */
  165. // $printer->cut();
  166. // $printer->pulse();
  167. // $printer->close();
  168. $transaction = Transaction::latest()->first();
  169. $connector = new FilePrintConnector("/dev/usb/lp1");
  170. $printer = new Printer($connector);
  171. /* Brand */
  172. $printer->setJustification(Printer::JUSTIFY_CENTER);
  173. $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
  174. $printer->text("BAMB'S LAUNDRY \n");
  175. $printer->selectPrintMode();
  176. $printer->feed();
  177. /* Unique Id */
  178. $printer->barcode($transaction->transaction_number, Printer::BARCODE_CODE39);
  179. $printer->text("Id Transaksi: {$transaction->transaction_number} \n");
  180. $printer->feed(2);
  181. /* Title of receipt */
  182. $printer->setEmphasis(true);
  183. $printer->text("DETAIL TRANSAKSI\n");
  184. $printer->setEmphasis(false);
  185. /* Footer */
  186. $printer->setJustification(Printer::JUSTIFY_CENTER);
  187. $printer->text("** Terima Kasih ** \n");
  188. $printer->feed();
  189. $printer->text($transaction->created_at . "\n");
  190. /* Cut the receipt */
  191. $printer->cut();
  192. $printer->pulse();
  193. $printer->close();
  194. return to_route('transactions.index')->with('success', __('messages.success.store.transaction'));
  195. } catch (QueryException $e) {
  196. return back()->with('error', __('messages.error.store.transaction'));
  197. DB::rollBack();
  198. }
  199. }
  200. /**
  201. * Display the specified resource.
  202. *
  203. * @param Int $id
  204. * @return \Inertia\Response
  205. */
  206. public function show(Transaction $transaction)
  207. {
  208. return inertia('transaction/Show', [
  209. 'transaction' => [
  210. 'number' => $transaction->transaction_number,
  211. 'statusId' => $transaction->transactionStatus->id,
  212. 'status' => $transaction->transactionStatus->name,
  213. 'discount' => $transaction->discount,
  214. 'price' => $transaction->totalPrice(),
  215. 'dateLaundry' => $transaction->created_at,
  216. ],
  217. 'customer' => [
  218. 'number' => $transaction->customer->customer_number,
  219. 'name' => $transaction->customer->name,
  220. 'phone' => $transaction->customer->phone,
  221. 'address' => $transaction->customer->address,
  222. ],
  223. 'outlet' => [
  224. 'name' => $transaction->outlet->name,
  225. 'address' => $transaction->outlet->address,
  226. ],
  227. 'transactionDetails' => $transaction->transactionDetails
  228. ->transform(fn($transactionDetail) => [
  229. 'laundry' => "{$transactionDetail->laundry->name} {$transactionDetail->laundry->price}/{$transactionDetail->laundry->unit}",
  230. 'quantity' => $transactionDetail->quantity,
  231. 'discount' => $transactionDetail->discount,
  232. 'price' => $transactionDetail->price,
  233. 'totalPrice' => $transactionDetail->totalPrice(),
  234. ]),
  235. ]);
  236. }
  237. /**
  238. * Show the form for editing the specified resource.
  239. *
  240. * @param Int $id
  241. * @return \Inertia\Response
  242. */
  243. public function edit($id)
  244. {
  245. //
  246. }
  247. /**
  248. * Update the specified resource in storage.
  249. *
  250. * @param \Illuminate\Http\Request $request
  251. * @param Int $id
  252. * @return \Illuminate\Http\Response
  253. */
  254. public function update(UpdateTransactionRequest $request, Transaction $transaction)
  255. {
  256. $transaction->update($request->validated());
  257. return back()->with('success', __('messages.success.update.transaction_status'));
  258. }
  259. /**
  260. * Remove the specified resource from storage.
  261. *
  262. * @param Int $id
  263. * @return \Illuminate\Http\Response
  264. */
  265. public function destroy($id)
  266. {
  267. //
  268. }
  269. }
  270. class item
  271. {
  272. private $name;
  273. private $price;
  274. private $dollarSign;
  275. public function __construct($name = '', $price = '', $dollarSign = false)
  276. {
  277. $this->name = $name;
  278. $this->price = $price;
  279. $this->dollarSign = $dollarSign;
  280. }
  281. public function __toString()
  282. {
  283. $rightCols = 10;
  284. $leftCols = 38;
  285. if ($this->dollarSign) {
  286. $leftCols = $leftCols / 2 - $rightCols / 2;
  287. }
  288. $left = str_pad($this->name, $leftCols);
  289. $sign = ($this->dollarSign ? '$ ' : '');
  290. $right = str_pad($sign . $this->price, $rightCols, ' ', STR_PAD_LEFT);
  291. return "$left$right\n";
  292. }
  293. }