TransactionController.php 5.5KB

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