TestTransactionController.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. class TestTransactionController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. //
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return \Inertia\Response
  28. */
  29. public function create()
  30. {
  31. $entryTransactions = EntryTransaction::whereNotIn('transaction_number', OutTransaction::pluck('entry_transaction_id'))
  32. ->latest()
  33. ->paginate(5)
  34. ->withQueryString()
  35. ->through(fn($transactionIn) => [
  36. 'id' => $transactionIn->id,
  37. 'createdAt' => $transactionIn->created_at,
  38. 'transactionNumber' => $transactionIn->transaction_number,
  39. ]);
  40. $entryTransactionCount = EntryTransaction::get()->count();
  41. $outTransactions = OutTransaction::latest()
  42. ->paginate(5)
  43. ->withQueryString()
  44. ->through(fn($transactionOut) => [
  45. 'id' => $transactionOut->id,
  46. 'createdAt' => $transactionOut->created_at,
  47. 'platNumber' => $transactionOut->plat_number,
  48. 'entryTransactionId' => $transactionOut->entry_transaction_id,
  49. ]);
  50. $outTransactionCount = OutTransaction::get()->count();
  51. $typeVehicles = TypeVehicle::get()->transform(fn($typeVehicle) => [
  52. 'value' => $typeVehicle->id,
  53. 'label' => $typeVehicle->type,
  54. ]);
  55. return inertia('test-transaction/Create.vue', compact([
  56. 'entryTransactions',
  57. 'entryTransactionCount',
  58. 'outTransactions',
  59. 'outTransactionCount',
  60. 'typeVehicles',
  61. ]));
  62. }
  63. /**
  64. * Store a newly created resource in storage.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @return \Illuminate\Http\Response
  68. */
  69. public function store(StoreTestTransactionRequest $request)
  70. {
  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.out_transaction'));
  88. } else {
  89. $entryTransactions = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
  90. $vehicleEntryTime = $entryTransactions->getRawOriginal('created_at');
  91. $longParkingPerHours = Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now());
  92. $parkingFee = ParkingFee::get();
  93. return back()->with('success', __('messages.success.store.out_transaction'));
  94. }
  95. } else {
  96. EntryTransaction::create([
  97. 'transaction_number' => Str::uuid(),
  98. ]);
  99. return back()->with('success', __('messages.success.store.transaction'));
  100. }
  101. }
  102. /**
  103. * Display the specified resource.
  104. *
  105. * @param int $id
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function show($id)
  109. {
  110. //
  111. }
  112. /**
  113. * Show the form for editing the specified resource.
  114. *
  115. * @param int $id
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function edit($id)
  119. {
  120. //
  121. }
  122. /**
  123. * Update the specified resource in storage.
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @param int $id
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function update(Request $request, $id)
  130. {
  131. //
  132. }
  133. /**
  134. * Remove the specified resource from storage.
  135. *
  136. * @param int $id
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function destroy($id)
  140. {
  141. //
  142. }
  143. }