TestTransactionController.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 App\Services\Helper;
  11. use Carbon\Carbon;
  12. use Illuminate\Database\QueryException;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Str;
  16. use Inertia\Inertia;
  17. class TestTransactionController extends Controller
  18. {
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function index()
  25. {
  26. //
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return \Inertia\Response
  32. */
  33. public function create()
  34. {
  35. $entryTransactions = EntryTransaction::whereNotIn('transaction_number', OutTransaction::pluck('entry_transaction_id'))
  36. ->latest()
  37. ->paginate(5)
  38. ->withQueryString()
  39. ->through(fn($transactionIn) => [
  40. 'id' => $transactionIn->id,
  41. 'createdAt' => $transactionIn->created_at,
  42. 'transactionNumber' => $transactionIn->transaction_number,
  43. ]);
  44. $outTransactions = OutTransaction::latest()
  45. ->paginate(5)
  46. ->withQueryString()
  47. ->through(fn($transactionOut) => [
  48. 'id' => $transactionOut->id,
  49. 'createdAt' => $transactionOut->created_at,
  50. 'platNumber' => $transactionOut->plat_number,
  51. 'entryTransactionId' => $transactionOut->entry_transaction_id,
  52. ]);
  53. $typeVehicles = function () {
  54. $vehicle = Vehicle::where('plat_number', request('plat_number'))->first();
  55. if ($vehicle) {
  56. return [[
  57. 'value' => $vehicle->typeVehicle->id,
  58. 'label' => $vehicle->typeVehicle->type,
  59. ]];
  60. } else {
  61. return TypeVehicle::get()->transform(fn($typeVehicle) => [
  62. 'value' => $typeVehicle->id,
  63. 'label' => $typeVehicle->type,
  64. ]);
  65. }
  66. };
  67. return inertia('test-transaction/Create.vue', compact([
  68. 'entryTransactions',
  69. 'outTransactions',
  70. 'typeVehicles',
  71. ]));
  72. }
  73. /**
  74. * Store a newly created resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function store(StoreTestTransactionRequest $request)
  80. {
  81. // if transaction out, else transaction in
  82. if ($request->id === 2) {
  83. $vehicle = Vehicle::where('plat_number', $request->plat_number)->first();
  84. // Check member or not
  85. if ($vehicle && $vehicle->member_id) {
  86. $expDate = $vehicle->member->getRawOriginal('exp_date');
  87. // Check expired member or not
  88. $exp = !Carbon::parse($expDate)->greaterThanOrEqualTo(now());
  89. if ($exp) {
  90. return back()->with('warning', __('words.member_expired'));
  91. }
  92. OutTransaction::create([
  93. 'plat_number' => $request->plat_number,
  94. 'entry_transaction_id' => $request->entry_transaction_id,
  95. 'type_vehicle_id' => $vehicle->type_vehicle_id,
  96. 'user_id' => auth()->user()->id,
  97. ]);
  98. return back()->with('success', __('messages.success.store.transaction'));
  99. } else {
  100. /**
  101. * Pseudo code
  102. *
  103. * waktu_kendaraan_masuk
  104. * waktu_kendaraan_keluar
  105. * waktu_lama_parkir = perbedaan_waktu(waktu_kendaraan_keluar [dengan] waktu_kendaraan_masuk)
  106. *
  107. * tarif_waktu_parkir = [jam_pertama, jam_pertama + jam_kedua, dst]
  108. * tarif_harga_parkir = [harga_pertama, harga_pertama + harga_kedua, dst]
  109. *
  110. * foreach: waktu_parkir, index of tarif_waktu_parkir
  111. * if: waktu_lama_parkir > waktu_parkir
  112. * return index
  113. * else:
  114. * return index
  115. *
  116. * lama_parkir = tarif_waktu_parkir[index]
  117. * harga_parkir = tarif_harga_parkir[index]
  118. *
  119. */
  120. $parkingFee = ParkingFee::get();
  121. $parkingTime = Helper::addPrevValue($parkingFee->pluck('time_period')->toArray());
  122. $parkingPrice = Helper::addPrevValue($parkingFee->pluck('price')->toArray());
  123. $entryTransaction = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
  124. $vehicleEntryTime = $entryTransaction->getRawOriginal('created_at');
  125. $longParkingPerHour = ceil(Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now()));
  126. $totalParkingPerDay = $longParkingPerHour > 24 ? ceil($longParkingPerHour / 24) : 0;
  127. $totalPriceParkingPerDay = $totalParkingPerDay * end($parkingPrice);
  128. $totalPriceParkingToday = 0;
  129. foreach ($parkingTime as $index => $time) {
  130. if ($time >= $longParkingPerHour) {
  131. $totalPriceParkingToday = $parkingPrice[$index];
  132. break;
  133. }
  134. }
  135. DB::beginTransaction();
  136. try {
  137. $outTransaction = OutTransaction::create([
  138. 'plat_number' => $request->plat_number,
  139. 'entry_transaction_id' => $request->entry_transaction_id,
  140. 'type_vehicle_id' => $request->type_vehicle_id,
  141. 'user_id' => auth()->user()->id,
  142. ]);
  143. $outTransaction->mutation()->create([
  144. 'type' => 1,
  145. 'amount' => $totalParkingPerDay ? $totalPriceParkingPerDay : $totalPriceParkingToday,
  146. ]);
  147. DB::commit();
  148. return back()->with('success', __('messages.success.store.transaction'));
  149. } catch (QueryException $e) {
  150. DB::rollBack();
  151. return back()->with('error', __('messages.error.store.transaction'));
  152. }
  153. }
  154. } else {
  155. EntryTransaction::create([
  156. 'transaction_number' => Str::uuid(),
  157. ]);
  158. return back()->with('success', __('messages.success.store.transaction'));
  159. }
  160. }
  161. /**
  162. * Display the specified resource.
  163. *
  164. * @param int $id
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function show($id)
  168. {
  169. //
  170. }
  171. /**
  172. * Show the form for editing the specified resource.
  173. *
  174. * @param int $id
  175. * @return \Illuminate\Http\Response
  176. */
  177. public function edit($id)
  178. {
  179. //
  180. }
  181. /**
  182. * Update the specified resource in storage.
  183. *
  184. * @param \Illuminate\Http\Request $request
  185. * @param int $id
  186. * @return \Illuminate\Http\Response
  187. */
  188. public function update(Request $request, $id)
  189. {
  190. //
  191. }
  192. /**
  193. * Remove the specified resource from storage.
  194. *
  195. * @param int $id
  196. * @return \Illuminate\Http\Response
  197. */
  198. public function destroy($id)
  199. {
  200. //
  201. }
  202. }