TransactionController.php 6.0KB

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