TransactionController.php 6.8KB

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