TestTransactionController.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\TypeVehicle;
  8. use App\Models\Vehicle;
  9. use App\Services\ParkingFeeService;
  10. use Carbon\Carbon;
  11. use Illuminate\Database\QueryException;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Str;
  15. use Inertia\Inertia;
  16. class TestTransactionController extends Controller
  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 \Inertia\Response
  31. */
  32. public function create()
  33. {
  34. $entryTransactions = EntryTransaction::whereNotIn('transaction_number', OutTransaction::pluck('entry_transaction_id'))
  35. ->latest()
  36. ->paginate(10)
  37. ->withQueryString()
  38. ->through(fn($transactionIn) => [
  39. 'id' => $transactionIn->id,
  40. 'entryCar' => $transactionIn->created_at,
  41. 'transactionNumber' => $transactionIn->transaction_number
  42. ]);
  43. $typeVehicles = function () {
  44. $vehicle = Vehicle::where('plat_number', request('plat_number'))->first();
  45. if ($vehicle) {
  46. return [[
  47. 'value' => $vehicle->typeVehicle->id,
  48. 'label' => $vehicle->typeVehicle->type
  49. ]];
  50. } else {
  51. return TypeVehicle::get()->transform(fn($typeVehicle) => [
  52. 'value' => $typeVehicle->id,
  53. 'label' => $typeVehicle->type
  54. ]);
  55. }
  56. };
  57. return inertia('testtransaction/Create.vue', compact([
  58. 'entryTransactions',
  59. 'typeVehicles'
  60. ]));
  61. }
  62. /**
  63. * Store a newly created resource in storage.
  64. *
  65. * @param \Illuminate\Http\Request $request
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function store(StoreTestTransactionRequest $request)
  69. {
  70. // if transaction out, else transaction in
  71. if ($request->id === 2) {
  72. $vehicle = Vehicle::where('plat_number', $request->plat_number)->first();
  73. // Check member or not
  74. if ($vehicle && $vehicle->member_id) {
  75. $expDate = $vehicle->member->getRawOriginal('exp_date');
  76. // Check expired member or not
  77. $exp = !Carbon::parse($expDate)->greaterThanOrEqualTo(now());
  78. if ($exp) {
  79. return back()->with('warning', __('words.member_expired'));
  80. }
  81. OutTransaction::create([
  82. 'plat_number' => $request->plat_number,
  83. 'entry_transaction_id' => $request->entry_transaction_id,
  84. 'type_vehicle_id' => $vehicle->type_vehicle_id,
  85. 'user_id' => auth()->user()->id
  86. ]);
  87. return back()->with('success', __('messages.success.store.transaction'));
  88. } else {
  89. /*
  90. * Pseudo code
  91. *
  92. * waktu_kendaraan_masuk
  93. * waktu_kendaraan_keluar
  94. * waktu_lama_parkir = perbedaan_waktu(waktu_kendaraan_keluar [dengan] waktu_kendaraan_masuk)
  95. *
  96. * tarif_waktu_parkir = [jam_pertama, jam_pertama + jam_kedua, dst]
  97. * tarif_harga_parkir = [harga_pertama, harga_pertama + harga_kedua, dst]
  98. *
  99. * foreach: waktu_parkir, index of tarif_waktu_parkir
  100. * if: waktu_lama_parkir > waktu_parkir
  101. * return index
  102. * else:
  103. * return index
  104. *
  105. * lama_parkir = tarif_waktu_parkir[index]
  106. * harga_parkir = tarif_harga_parkir[index]
  107. *
  108. */
  109. DB::beginTransaction();
  110. try {
  111. $outTransaction = OutTransaction::create([
  112. 'plat_number' => $request->plat_number,
  113. 'entry_transaction_id' => $request->entry_transaction_id,
  114. 'type_vehicle_id' => $request->type_vehicle_id,
  115. 'user_id' => auth()->user()->id
  116. ]);
  117. $outTransaction->mutation()->create([
  118. 'type' => 1,
  119. 'amount' => ParkingFeeService::totalPriceParking($request->entry_transaction_id)
  120. ]);
  121. DB::commit();
  122. return back()->with('success', __('messages.success.store.transaction'));
  123. } catch (QueryException $e) {
  124. DB::rollBack();
  125. return back()->with('error', __('messages.error.store.transaction'));
  126. }
  127. }
  128. } else {
  129. EntryTransaction::create([
  130. 'transaction_number' => Str::uuid()
  131. ]);
  132. return back()->with('success', __('messages.success.store.transaction'));
  133. }
  134. }
  135. /**
  136. * Display the specified resource.
  137. *
  138. * @param int $id
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function show($id)
  142. {
  143. //
  144. }
  145. /**
  146. * Show the form for editing the specified resource.
  147. *
  148. * @param int $id
  149. * @return \Illuminate\Http\Response
  150. */
  151. public function edit($id)
  152. {
  153. //
  154. }
  155. /**
  156. * Update the specified resource in storage.
  157. *
  158. * @param \Illuminate\Http\Request $request
  159. * @param int $id
  160. * @return \Illuminate\Http\Response
  161. */
  162. public function update(Request $request, $id)
  163. {
  164. //
  165. }
  166. /**
  167. * Remove the specified resource from storage.
  168. *
  169. * @param int $id
  170. * @return \Illuminate\Http\Response
  171. */
  172. public function destroy($id)
  173. {
  174. //
  175. }
  176. }