TestTransactionController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 ($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.out_transaction'));
  99. } else {
  100. $entryTransactions = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
  101. $vehicleEntryTime = $entryTransactions->getRawOriginal('created_at');
  102. $longParkingPerHours = Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now());
  103. $parkingFee = ParkingFee::get();
  104. return back()->with('success', __('messages.success.store.out_transaction'));
  105. }
  106. } else {
  107. EntryTransaction::create([
  108. 'transaction_number' => Str::uuid(),
  109. ]);
  110. return back()->with('success', __('messages.success.store.transaction'));
  111. }
  112. }
  113. /**
  114. * Display the specified resource.
  115. *
  116. * @param int $id
  117. * @return \Illuminate\Http\Response
  118. */
  119. public function show($id)
  120. {
  121. //
  122. }
  123. /**
  124. * Show the form for editing the specified resource.
  125. *
  126. * @param int $id
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function edit($id)
  130. {
  131. //
  132. }
  133. /**
  134. * Update the specified resource in storage.
  135. *
  136. * @param \Illuminate\Http\Request $request
  137. * @param int $id
  138. * @return \Illuminate\Http\Response
  139. */
  140. public function update(Request $request, $id)
  141. {
  142. //
  143. }
  144. /**
  145. * Remove the specified resource from storage.
  146. *
  147. * @param int $id
  148. * @return \Illuminate\Http\Response
  149. */
  150. public function destroy($id)
  151. {
  152. //
  153. }
  154. }