TransactionController.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\Transaction\StoreTransactionRequest;
  4. use App\Models\OutTransaction;
  5. use App\Models\TypeVehicle;
  6. use App\Models\Vehicle;
  7. use App\Services\ParkingFeeService;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\QueryException;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. class TransactionController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index()
  20. {
  21. //
  22. }
  23. /**
  24. * Show the form for creating a new resource.
  25. *
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function create()
  29. {
  30. $outTransactions = OutTransaction::latest()
  31. ->get()
  32. ->take(10)
  33. ->transform(fn($transactionOut) => [
  34. 'id' => $transactionOut->id,
  35. 'entryCar' => $transactionOut->entryTransaction->created_at,
  36. 'outCar' => $transactionOut->created_at,
  37. 'totalTimeParking' => ParkingFeeService::totalTimeParkingHuman(
  38. $transactionOut->entry_transaction_id
  39. ),
  40. 'totalPriceParking' => ParkingFeeService::totalPriceParkingString(
  41. $transactionOut->entry_transaction_id
  42. ),
  43. 'platNumber' => $transactionOut->plat_number,
  44. 'entryTransactionId' => $transactionOut->entry_transaction_id,
  45. ]);
  46. $typeVehicles = function () {
  47. $vehicle = Vehicle::where('plat_number', request('plat_number'))->first();
  48. if ($vehicle) {
  49. return [[
  50. 'value' => $vehicle->typeVehicle->id,
  51. 'label' => $vehicle->typeVehicle->type,
  52. ]];
  53. } else {
  54. return TypeVehicle::get()->transform(fn($typeVehicle) => [
  55. 'value' => $typeVehicle->id,
  56. 'label' => $typeVehicle->type,
  57. ]);
  58. }
  59. };
  60. $detailOutTransaction = function () {
  61. if (request('transaction_number')) {
  62. return [
  63. 'totalTimeParking' => ParkingFeeService::totalTimeParkingHuman(
  64. request('transaction_number')
  65. ),
  66. 'totalPriceParking' => ParkingFeeService::totalPriceParkingString(
  67. request('transaction_number')
  68. ),
  69. ];
  70. }
  71. };
  72. return inertia('transaction/Create.vue', compact([
  73. 'outTransactions',
  74. 'typeVehicles',
  75. 'detailOutTransaction',
  76. ]));
  77. }
  78. /**
  79. * Store a newly created resource in storage.
  80. *
  81. * @param \Illuminate\Http\Request $request
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function store(StoreTransactionRequest $request)
  85. {
  86. $vehicle = Vehicle::where('plat_number', $request->plat_number)->first();
  87. if ($vehicle && $vehicle->member_id) {
  88. $expDate = $vehicle->member->getRawOriginal('exp_date');
  89. $exp = !Carbon::parse($expDate)->greaterThanOrEqualTo(now());
  90. if ($exp) {
  91. return back()->with('warning', __('words.member_expired'));
  92. }
  93. OutTransaction::create([
  94. 'plat_number' => $request->plat_number,
  95. 'entry_transaction_id' => $request->entry_transaction_id,
  96. 'type_vehicle_id' => $vehicle->type_vehicle_id,
  97. 'user_id' => auth()->user()->id,
  98. ]);
  99. return back()->with('success', __('messages.success.store.transaction'));
  100. } else {
  101. DB::beginTransaction();
  102. try {
  103. $outTransaction = OutTransaction::create([
  104. 'plat_number' => $request->plat_number,
  105. 'entry_transaction_id' => $request->entry_transaction_id,
  106. 'type_vehicle_id' => $request->type_vehicle_id,
  107. 'user_id' => auth()->user()->id,
  108. ]);
  109. $outTransaction->mutation()->create([
  110. 'type' => 1,
  111. 'amount' => ParkingFeeService::totalPriceParking($request->entry_transaction_id),
  112. ]);
  113. DB::commit();
  114. return back()->with('success', __('messages.success.store.transaction'));
  115. } catch (QueryException $e) {
  116. DB::rollBack();
  117. return back()->with('error', __('messages.error.store.transaction'));
  118. }
  119. }
  120. }
  121. /**
  122. * Display the specified resource.
  123. *
  124. * @param int $id
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function show($id)
  128. {
  129. //
  130. }
  131. /**
  132. * Show the form for editing the specified resource.
  133. *
  134. * @param int $id
  135. * @return \Illuminate\Http\Response
  136. */
  137. public function edit($id)
  138. {
  139. //
  140. }
  141. /**
  142. * Update the specified resource in storage.
  143. *
  144. * @param \Illuminate\Http\Request $request
  145. * @param int $id
  146. * @return \Illuminate\Http\Response
  147. */
  148. public function update(Request $request, $id)
  149. {
  150. //
  151. }
  152. /**
  153. * Remove the specified resource from storage.
  154. *
  155. * @param int $id
  156. * @return \Illuminate\Http\Response
  157. */
  158. public function destroy($id)
  159. {
  160. //
  161. }
  162. }