TestTransactionController.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\TestTransaction\StoreTestTransactionRequest;
  5. use App\Models\EntryTransaction;
  6. use App\Models\OutTransaction;
  7. use App\Models\ParkingFee;
  8. use App\Models\TypeVehicle;
  9. use App\Models\Vehicle;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Str;
  13. use Inertia\Inertia;
  14. class TestTransactionController 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 \Inertia\Response
  29. */
  30. public function create()
  31. {
  32. $entryTransactions = EntryTransaction::whereNotIn('transaction_number', OutTransaction::pluck('entry_transaction_id'))
  33. ->latest()
  34. ->paginate(5)
  35. ->withQueryString()
  36. ->through(fn($transactionIn) => [
  37. 'id' => $transactionIn->id,
  38. 'createdAt' => $transactionIn->created_at,
  39. 'transactionNumber' => $transactionIn->transaction_number,
  40. ]);
  41. $entryTransactionCount = EntryTransaction::get()->count();
  42. $outTransactions = OutTransaction::latest()
  43. ->paginate(5)
  44. ->withQueryString()
  45. ->through(fn($transactionOut) => [
  46. 'id' => $transactionOut->id,
  47. 'createdAt' => $transactionOut->created_at,
  48. 'platNumber' => $transactionOut->plat_number,
  49. 'entryTransactionId' => $transactionOut->entry_transaction_id,
  50. ]);
  51. $outTransactionCount = OutTransaction::get()->count();
  52. $typeVehicles = function () {
  53. $vehicle = Vehicle::where('plat_number', request('plat_number'))->first();
  54. if ($vehicle) {
  55. return [[
  56. 'value' => $vehicle->typeVehicle->id,
  57. 'label' => $vehicle->typeVehicle->type,
  58. ]];
  59. } else {
  60. return TypeVehicle::get()->transform(fn($typeVehicle) => [
  61. 'value' => $typeVehicle->id,
  62. 'label' => $typeVehicle->type,
  63. ]);
  64. }
  65. };
  66. return inertia('test-transaction/Create.vue', compact([
  67. 'entryTransactions',
  68. 'entryTransactionCount',
  69. 'outTransactions',
  70. 'outTransactionCount',
  71. 'typeVehicles',
  72. ]));
  73. }
  74. /**
  75. * Store a newly created resource in storage.
  76. *
  77. * @param \Illuminate\Http\Request $request
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function store(StoreTestTransactionRequest $request)
  81. {
  82. // if transaction out, else transaction in
  83. if ($request->id === 2) {
  84. $vehicle = Vehicle::where('plat_number', $request->plat_number)->first();
  85. // Check member or not
  86. if ($vehicle && $vehicle->member_id) {
  87. $expDate = $vehicle->member->getRawOriginal('exp_date');
  88. // Check expired member or not
  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.out_transaction'));
  100. } else {
  101. /**
  102. * Pseudo code
  103. *
  104. * waktu_kendaraan_masuk
  105. * waktu_kendaraan_keluar
  106. * waktu_lama_parkir = perbedaan_waktu(waktu_kendaraan_keluar [dengan] waktu_kendaraan_masuk)
  107. *
  108. * tarif_waktu_parkir = [jam_pertama, jam_pertama + jam_kedua, dst]
  109. * tarif_harga_parkir = [harga_pertama, harga_pertama + harga_kedua, dst]
  110. *
  111. * foreach: waktu_parkir, index of tarif_waktu_parkir
  112. * if: waktu_lama_parkir > waktu_parkir
  113. * return index
  114. * else:
  115. * return index
  116. *
  117. * lama_parkir = tarif_waktu_parkir[index]
  118. * harga_parkir = tarif_harga_parkir[index]
  119. *
  120. */
  121. $entryTransaction = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
  122. $vehicleEntryTime = $entryTransaction->getRawOriginal('created_at');
  123. $longParkingPerHours = ceil(Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now()));
  124. $parkingFee = ParkingFee::get()
  125. ->reduce(function ($prev, $parkingFee) use ($longParkingPerHours) {
  126. if ($prev + $parkingFee->time_period >= $longParkingPerHours) {
  127. return $parkingFee;
  128. }
  129. }, 0);
  130. return back()->with('success', __('messages.success.store.out_transaction'));
  131. }
  132. } else {
  133. EntryTransaction::create([
  134. 'transaction_number' => Str::uuid(),
  135. ]);
  136. return back()->with('success', __('messages.success.store.transaction'));
  137. }
  138. }
  139. /**
  140. * Display the specified resource.
  141. *
  142. * @param int $id
  143. * @return \Illuminate\Http\Response
  144. */
  145. public function show($id)
  146. {
  147. //
  148. }
  149. /**
  150. * Show the form for editing the specified resource.
  151. *
  152. * @param int $id
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function edit($id)
  156. {
  157. //
  158. }
  159. /**
  160. * Update the specified resource in storage.
  161. *
  162. * @param \Illuminate\Http\Request $request
  163. * @param int $id
  164. * @return \Illuminate\Http\Response
  165. */
  166. public function update(Request $request, $id)
  167. {
  168. //
  169. }
  170. /**
  171. * Remove the specified resource from storage.
  172. *
  173. * @param int $id
  174. * @return \Illuminate\Http\Response
  175. */
  176. public function destroy($id)
  177. {
  178. //
  179. }
  180. }