TransactionController.php 12KB

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