TestTransactionController.php 7.7KB

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