TransactionController.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Helpers\ThermalPrinting;
  5. use App\Http\Requests\Transaction\StoreTransactionRequest;
  6. use App\Http\Requests\Transaction\UpdateTransactionRequest;
  7. use App\Models\Customer;
  8. use App\Models\Laundry;
  9. use App\Models\Transaction;
  10. use App\Models\TransactionStatus;
  11. use Illuminate\Database\QueryException;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Http;
  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. $transaction = Transaction::latest()->first();
  117. $thermalPrinting = new ThermalPrinting($transaction);
  118. $thermalPrinting->startPrinting(2);
  119. $customer = Customer::find($request->customer_id);
  120. Http::post('https://gerbangchatapi.dijitalcode.com/chat/send?id=bambslaundry', [
  121. 'receiver' => $customer->phone,
  122. 'message' => 'Terima kasih sudah mempercayakan layanan laundry kepada Bamb\'s Laundry. Nomor transaksi Anda adalah *'.$request->transaction_number.'*',
  123. ]);
  124. return to_route('transactions.index')->with('success', __('Transaksi berhasil ditambahkan'));
  125. } catch (QueryException $e) {
  126. return back()->with('error', __('Penambahan transaksi gagal'));
  127. DB::rollBack();
  128. }
  129. }
  130. /**
  131. * Display the specified resource.
  132. *
  133. * @param Int $id
  134. * @return \Inertia\Response
  135. */
  136. public function show(Transaction $transaction)
  137. {
  138. return inertia('transaction/Show', [
  139. 'transaction' => [
  140. 'number' => $transaction->transaction_number,
  141. 'statusId' => $transaction->transactionStatus->id,
  142. 'status' => $transaction->transactionStatus->name,
  143. 'discount' => $transaction->discount,
  144. 'price' => $transaction->totalPrice(),
  145. 'dateLaundry' => $transaction->created_at,
  146. ],
  147. 'customer' => [
  148. 'number' => $transaction->customer->customer_number,
  149. 'name' => $transaction->customer->name,
  150. 'phone' => $transaction->customer->phone,
  151. 'address' => $transaction->customer->address,
  152. ],
  153. 'outlet' => [
  154. 'name' => $transaction->outlet->name,
  155. 'address' => $transaction->outlet->address,
  156. ],
  157. 'transactionDetails' => $transaction->transactionDetails
  158. ->transform(fn($transactionDetail) => [
  159. 'laundry' => "{$transactionDetail->laundry->name} {$transactionDetail->laundry->price}/{$transactionDetail->laundry->unit}",
  160. 'quantity' => $transactionDetail->quantity,
  161. 'discount' => $transactionDetail->discount,
  162. 'price' => $transactionDetail->price,
  163. 'totalPrice' => $transactionDetail->totalPrice(),
  164. ]),
  165. ]);
  166. }
  167. /**
  168. * Show the form for editing the specified resource.
  169. *
  170. * @param Int $id
  171. * @return \Inertia\Response
  172. */
  173. public function edit($id)
  174. {
  175. //
  176. }
  177. /**
  178. * Update the specified resource in storage.
  179. *
  180. * @param \Illuminate\Http\Request $request
  181. * @param Int $id
  182. * @return \Illuminate\Http\Response
  183. */
  184. public function update(UpdateTransactionRequest $request, Transaction $transaction)
  185. {
  186. $transaction->update($request->validated());
  187. if($transaction->transaction_status_id==2) {
  188. $status_message = " sudah diproses, harap menunggu sampai pemberitahuan selanjutnya.";
  189. } else if($transaction->transaction_status_id==3) {
  190. $status_message = " sudah selesai, silahkan diambil.";
  191. } else {
  192. $status_message = " sudah diambil. Silahkan datang lagi di kemudian hari jika ingin laundry kembali, terima kasih.";
  193. }
  194. Http::post('https://gerbangchatapi.dijitalcode.com/chat/send?id=bambslaundry', [
  195. 'receiver' => $transaction->customer->phone,
  196. 'message' => 'Layanan laundry Anda dengan nomor transaksi *'.$transaction->transaction_number.'*'.$status_message,
  197. ]);
  198. return back()->with('success', __('Transaksi berhasil diperbaharui'));
  199. }
  200. /**
  201. * Remove the specified resource from storage.
  202. *
  203. * @param Int $id
  204. * @return \Illuminate\Http\Response
  205. */
  206. public function destroy($id)
  207. {
  208. //
  209. }
  210. }