TransactionController.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. 'filters' => request()->all('search', 'dates'),
  30. 'transactions' => $transactions
  31. ->filter(request()->only('search', 'dates'))
  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. return to_route('transactions.index')->with('success', __('Transaksi berhasil ditambahkan'));
  120. } catch (QueryException $e) {
  121. return back()->with('error', __('Penambahan transaksi gagal'));
  122. DB::rollBack();
  123. }
  124. }
  125. /**
  126. * Display the specified resource.
  127. *
  128. * @param Int $id
  129. * @return \Inertia\Response
  130. */
  131. public function show(Transaction $transaction)
  132. {
  133. return inertia('transaction/Show', [
  134. 'transaction' => [
  135. 'number' => $transaction->transaction_number,
  136. 'statusId' => $transaction->transactionStatus->id,
  137. 'status' => $transaction->transactionStatus->name,
  138. 'discount' => $transaction->discount,
  139. 'price' => $transaction->totalPrice(),
  140. 'dateLaundry' => $transaction->created_at,
  141. ],
  142. 'customer' => [
  143. 'number' => $transaction->customer->customer_number,
  144. 'name' => $transaction->customer->name,
  145. 'phone' => $transaction->customer->phone,
  146. 'address' => $transaction->customer->address,
  147. ],
  148. 'outlet' => [
  149. 'name' => $transaction->outlet->name,
  150. 'address' => $transaction->outlet->address,
  151. ],
  152. 'transactionDetails' => $transaction->transactionDetails
  153. ->transform(fn($transactionDetail) => [
  154. 'laundry' => "{$transactionDetail->laundry->name} {$transactionDetail->laundry->price}/{$transactionDetail->laundry->unit}",
  155. 'quantity' => $transactionDetail->quantity,
  156. 'discount' => $transactionDetail->discount,
  157. 'price' => $transactionDetail->price,
  158. 'totalPrice' => $transactionDetail->totalPrice(),
  159. ]),
  160. ]);
  161. }
  162. /**
  163. * Show the form for editing the specified resource.
  164. *
  165. * @param Int $id
  166. * @return \Inertia\Response
  167. */
  168. public function edit($id)
  169. {
  170. //
  171. }
  172. /**
  173. * Update the specified resource in storage.
  174. *
  175. * @param \Illuminate\Http\Request $request
  176. * @param Int $id
  177. * @return \Illuminate\Http\Response
  178. */
  179. public function update(UpdateTransactionRequest $request, Transaction $transaction)
  180. {
  181. $transaction->update($request->validated());
  182. return back()->with('success', __('Transaksi berhasil diperbaharui'));
  183. }
  184. /**
  185. * Remove the specified resource from storage.
  186. *
  187. * @param Int $id
  188. * @return \Illuminate\Http\Response
  189. */
  190. public function destroy($id)
  191. {
  192. //
  193. }
  194. }