| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Http\Controllers\Controller;
- use App\Http\Requests\TestTransaction\StoreTestTransactionRequest;
- use App\Models\EntryTransaction;
- use App\Models\OutTransaction;
- use App\Models\ParkingFee;
- use App\Models\TypeVehicle;
- use App\Models\Vehicle;
- use App\Services\Helper;
- use Carbon\Carbon;
- use Illuminate\Database\QueryException;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Str;
- use Inertia\Inertia;
-
- class TestTransactionController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- }
-
- /**
- * Show the form for creating a new resource.
- *
- * @return \Inertia\Response
- */
- public function create()
- {
- $entryTransactions = EntryTransaction::whereNotIn('transaction_number', OutTransaction::pluck('entry_transaction_id'))
- ->latest()
- ->paginate(5)
- ->withQueryString()
- ->through(fn($transactionIn) => [
- 'id' => $transactionIn->id,
- 'createdAt' => $transactionIn->created_at,
- 'transactionNumber' => $transactionIn->transaction_number,
- ]);
-
- $entryTransactionCount = EntryTransaction::get()->count();
-
- $outTransactions = OutTransaction::latest()
- ->paginate(5)
- ->withQueryString()
- ->through(fn($transactionOut) => [
- 'id' => $transactionOut->id,
- 'createdAt' => $transactionOut->created_at,
- 'platNumber' => $transactionOut->plat_number,
- 'entryTransactionId' => $transactionOut->entry_transaction_id,
- ]);
-
- $outTransactionCount = OutTransaction::get()->count();
-
- $typeVehicles = function () {
- $vehicle = Vehicle::where('plat_number', request('plat_number'))->first();
-
- if ($vehicle) {
- return [[
- 'value' => $vehicle->typeVehicle->id,
- 'label' => $vehicle->typeVehicle->type,
- ]];
- } else {
- return TypeVehicle::get()->transform(fn($typeVehicle) => [
- 'value' => $typeVehicle->id,
- 'label' => $typeVehicle->type,
- ]);
- }
- };
-
- return inertia('test-transaction/Create.vue', compact([
- 'entryTransactions',
- 'entryTransactionCount',
- 'outTransactions',
- 'outTransactionCount',
- 'typeVehicles',
- ]));
- }
-
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(StoreTestTransactionRequest $request)
- {
- // if transaction out, else transaction in
- if ($request->id === 2) {
- $vehicle = Vehicle::where('plat_number', $request->plat_number)->first();
-
- // Check member or not
- if ($vehicle && $vehicle->member_id) {
- $expDate = $vehicle->member->getRawOriginal('exp_date');
-
- // Check expired member or not
- $exp = !Carbon::parse($expDate)->greaterThanOrEqualTo(now());
-
- if ($exp) {
- return back()->with('warning', __('words.member_expired'));
- }
-
- OutTransaction::create([
- 'plat_number' => $request->plat_number,
- 'entry_transaction_id' => $request->entry_transaction_id,
- 'type_vehicle_id' => $vehicle->type_vehicle_id,
- 'user_id' => auth()->user()->id,
- ]);
-
- return back()->with('success', __('messages.success.store.transaction'));
- } else {
- /**
- * Pseudo code
- *
- * waktu_kendaraan_masuk
- * waktu_kendaraan_keluar
- * waktu_lama_parkir = perbedaan_waktu(waktu_kendaraan_keluar [dengan] waktu_kendaraan_masuk)
- *
- * tarif_waktu_parkir = [jam_pertama, jam_pertama + jam_kedua, dst]
- * tarif_harga_parkir = [harga_pertama, harga_pertama + harga_kedua, dst]
- *
- * foreach: waktu_parkir, index of tarif_waktu_parkir
- * if: waktu_lama_parkir > waktu_parkir
- * return index
- * else:
- * return index
- *
- * lama_parkir = tarif_waktu_parkir[index]
- * harga_parkir = tarif_harga_parkir[index]
- *
- */
-
- $parkingFee = ParkingFee::get();
-
- $parkingTime = Helper::addPrevValue($parkingFee->pluck('time_period')->toArray());
-
- $parkingPrice = Helper::addPrevValue($parkingFee->pluck('price')->toArray());
-
- $entryTransaction = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
-
- $vehicleEntryTime = $entryTransaction->getRawOriginal('created_at');
-
- $longParkingPerHour = ceil(Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now()));
-
- $totalParkingPerDay = $longParkingPerHour > 24 ? ceil($longParkingPerHour / 24) : 0;
-
- $totalPriceParkingPerDay = $totalParkingPerDay * end($parkingPrice);
-
- $totalPriceParkingToday = 0;
-
- foreach ($parkingTime as $index => $time) {
- if ($time >= $longParkingPerHour) {
- $totalPriceParkingToday = $parkingPrice[$index];
-
- break;
- }
- }
-
- DB::beginTransaction();
-
- try {
- $outTransaction = OutTransaction::create([
- 'plat_number' => $request->plat_number,
- 'entry_transaction_id' => $request->entry_transaction_id,
- 'type_vehicle_id' => $request->type_vehicle_id,
- 'user_id' => auth()->user()->id,
- ]);
-
- $outTransaction->mutation()->create([
- 'type' => 1,
- 'amount' => $totalParkingPerDay ? $totalPriceParkingPerDay : $totalPriceParkingToday,
- ]);
-
- DB::commit();
-
- return back()->with('success', __('messages.success.store.transaction'));
- } catch (QueryException $e) {
- DB::rollBack();
-
- return back()->with('error', __('messages.error.store.transaction'));
- }
- }
- } else {
- EntryTransaction::create([
- 'transaction_number' => Str::uuid(),
- ]);
-
- return back()->with('success', __('messages.success.store.transaction'));
- }
- }
-
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
-
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- //
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- //
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- }
|