TransactionController.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. 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. if (Auth::user()->hasRole('Admin')) {
  24. $transactions = Transaction::latest();
  25. } else {
  26. $transactions = Transaction::where('outlet_id', Auth::user()->outlet_id)->latest();
  27. }
  28. return inertia('transaction/Index', [
  29. 'transactions' => $transactions
  30. ->filter(request()->only(['search']))
  31. ->paginate(10)
  32. ->withQueryString()
  33. ->through(fn($transaction) => [
  34. 'id' => $transaction->id,
  35. 'transactionNumber' => $transaction->transaction_number,
  36. 'dateLaundry' => $transaction->created_at,
  37. 'customer' => [
  38. 'number' => $transaction->customer->customer_number,
  39. 'name' => $transaction->customer->name,
  40. 'phone' => $transaction->customer->phone,
  41. ],
  42. 'price' => $transaction->totalPrice(),
  43. 'outlet' => $transaction->outlet->name,
  44. 'transactionStatusName' => $transaction->transactionStatus->name,
  45. 'transactionStatusId' => $transaction->transactionStatus->id,
  46. ]),
  47. 'transactionsStatus' => TransactionStatus::all()
  48. ->transform(fn($transactionStatus) => [
  49. 'label' => $transactionStatus->name,
  50. 'value' => $transactionStatus->id,
  51. ]),
  52. ]);
  53. }
  54. /**
  55. * Show the form for creating a new resource.
  56. *
  57. * @return \Inertia\Response
  58. */
  59. public function create()
  60. {
  61. return inertia('transaction/Create', [
  62. 'transactionNumber' => 'TS' . now()->format('YmdHis'),
  63. 'customers' => fn() => Customer::latest()
  64. ->filter(request('customer'))
  65. ->get()
  66. ->transform(fn($customer) => [
  67. 'id' => $customer->id,
  68. 'name' => $customer->name,
  69. 'customerNumber' => $customer->customer_number,
  70. 'phone' => $customer->phone,
  71. ]),
  72. 'laundries' => fn() => Laundry::latest()
  73. ->filter(request('laundry'))
  74. ->get()
  75. ->transform(fn($laundry) => [
  76. 'id' => $laundry->id,
  77. 'name' => $laundry->name,
  78. 'unit' => $laundry->unit,
  79. 'price' => $laundry->getRawOriginal('price'),
  80. ]),
  81. 'customerNumber' => fn() => 'CS' . now()->format('YmdHis'),
  82. 'genders' => [
  83. ['label' => 'Perempuan', 'value' => 1],
  84. ['label' => 'Laki-laki', 'value' => 2],
  85. ],
  86. ]);
  87. }
  88. /**
  89. * Store a newly created resource in storage.
  90. *
  91. * @param \Illuminate\Http\Request $request
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function store(StoreTransactionRequest $request)
  95. {
  96. DB::beginTransaction();
  97. try {
  98. $transaction = Transaction::create([
  99. 'transaction_number' => $request->transaction_number,
  100. 'discount' => $request->discount_all,
  101. 'transaction_status_id' => 1,
  102. 'user_id' => $request->user()->id,
  103. 'customer_id' => $request->customer_id,
  104. 'outlet_id' => $request->user()->outlet_id,
  105. ]);
  106. foreach ($request->laundries as $laundry) {
  107. $transaction->transactionDetails()->create([
  108. 'price' => $laundry['price'],
  109. 'discount' => $laundry['discount'],
  110. 'quantity' => $laundry['quantity'],
  111. 'laundry_id' => $laundry['laundryId'],
  112. ]);
  113. }
  114. DB::commit();
  115. $transaction = Transaction::latest()->first();
  116. $thermalPrinting = new ThermalPrinting($transaction);
  117. $thermalPrinting->startPrinting(2);
  118. return to_route('transactions.index')->with('success', __('Transaksi berhasil ditambahkan'));
  119. } catch (QueryException $e) {
  120. return back()->with('error', __('Penambahan transaksi gagal'));
  121. DB::rollBack();
  122. }
  123. }
  124. /**
  125. * Display the specified resource.
  126. *
  127. * @param Int $id
  128. * @return \Inertia\Response
  129. */
  130. public function show(Transaction $transaction)
  131. {
  132. return inertia('transaction/Show', [
  133. 'transaction' => [
  134. 'number' => $transaction->transaction_number,
  135. 'statusId' => $transaction->transactionStatus->id,
  136. 'status' => $transaction->transactionStatus->name,
  137. 'discount' => $transaction->discount,
  138. 'price' => $transaction->totalPrice(),
  139. 'dateLaundry' => $transaction->created_at,
  140. ],
  141. 'customer' => [
  142. 'number' => $transaction->customer->customer_number,
  143. 'name' => $transaction->customer->name,
  144. 'phone' => $transaction->customer->phone,
  145. 'address' => $transaction->customer->address,
  146. ],
  147. 'outlet' => [
  148. 'name' => $transaction->outlet->name,
  149. 'address' => $transaction->outlet->address,
  150. ],
  151. 'transactionDetails' => $transaction->transactionDetails
  152. ->transform(fn($transactionDetail) => [
  153. 'laundry' => "{$transactionDetail->laundry->name} {$transactionDetail->laundry->price}/{$transactionDetail->laundry->unit}",
  154. 'quantity' => $transactionDetail->quantity,
  155. 'discount' => $transactionDetail->discount,
  156. 'price' => $transactionDetail->price,
  157. 'totalPrice' => $transactionDetail->totalPrice(),
  158. ]),
  159. ]);
  160. }
  161. /**
  162. * Show the form for editing the specified resource.
  163. *
  164. * @param Int $id
  165. * @return \Inertia\Response
  166. */
  167. public function edit($id)
  168. {
  169. //
  170. }
  171. /**
  172. * Update the specified resource in storage.
  173. *
  174. * @param \Illuminate\Http\Request $request
  175. * @param Int $id
  176. * @return \Illuminate\Http\Response
  177. */
  178. public function update(UpdateTransactionRequest $request, Transaction $transaction)
  179. {
  180. $transaction->update($request->validated());
  181. return back()->with('success', __('Transaksi berhasil diperbaharui'));
  182. }
  183. /**
  184. * Remove the specified resource from storage.
  185. *
  186. * @param Int $id
  187. * @return \Illuminate\Http\Response
  188. */
  189. public function destroy($id)
  190. {
  191. //
  192. }
  193. }