|
|
@@ -9,8 +9,11 @@ use App\Models\OutTransaction;
|
|
9
|
9
|
use App\Models\ParkingFee;
|
|
10
|
10
|
use App\Models\TypeVehicle;
|
|
11
|
11
|
use App\Models\Vehicle;
|
|
|
12
|
+use App\Services\Helper;
|
|
12
|
13
|
use Carbon\Carbon;
|
|
|
14
|
+use Illuminate\Database\QueryException;
|
|
13
|
15
|
use Illuminate\Http\Request;
|
|
|
16
|
+use Illuminate\Support\Facades\DB;
|
|
14
|
17
|
use Illuminate\Support\Str;
|
|
15
|
18
|
use Inertia\Inertia;
|
|
16
|
19
|
|
|
|
@@ -112,7 +115,7 @@ class TestTransactionController extends Controller
|
|
112
|
115
|
'user_id' => auth()->user()->id,
|
|
113
|
116
|
]);
|
|
114
|
117
|
|
|
115
|
|
- return back()->with('success', __('messages.success.store.out_transaction'));
|
|
|
118
|
+ return back()->with('success', __('messages.success.store.transaction'));
|
|
116
|
119
|
} else {
|
|
117
|
120
|
/**
|
|
118
|
121
|
* Pseudo code
|
|
|
@@ -135,20 +138,55 @@ class TestTransactionController extends Controller
|
|
135
|
138
|
*
|
|
136
|
139
|
*/
|
|
137
|
140
|
|
|
|
141
|
+ $parkingFee = ParkingFee::get();
|
|
|
142
|
+
|
|
|
143
|
+ $parkingTime = Helper::addPrevValue($parkingFee->pluck('time_period')->toArray());
|
|
|
144
|
+
|
|
|
145
|
+ $parkingPrice = Helper::addPrevValue($parkingFee->pluck('price')->toArray());
|
|
|
146
|
+
|
|
138
|
147
|
$entryTransaction = EntryTransaction::where('transaction_number', $request->entry_transaction_id)->first();
|
|
139
|
148
|
|
|
140
|
149
|
$vehicleEntryTime = $entryTransaction->getRawOriginal('created_at');
|
|
141
|
150
|
|
|
142
|
|
- $longParkingPerHours = ceil(Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now()));
|
|
|
151
|
+ $longParkingPerHour = ceil(Carbon::parse($vehicleEntryTime)->floatDiffInRealHours(now()));
|
|
|
152
|
+
|
|
|
153
|
+ $totalParkingPerDay = $longParkingPerHour > 24 ? ceil($longParkingPerHour / 24) : 0;
|
|
|
154
|
+
|
|
|
155
|
+ $totalPriceParkingPerDay = $totalParkingPerDay * end($parkingPrice);
|
|
|
156
|
+
|
|
|
157
|
+ $totalPriceParkingToday = 0;
|
|
|
158
|
+
|
|
|
159
|
+ foreach ($parkingTime as $index => $time) {
|
|
|
160
|
+ if ($time >= $longParkingPerHour) {
|
|
|
161
|
+ $totalPriceParkingToday = $parkingPrice[$index];
|
|
143
|
162
|
|
|
144
|
|
- $parkingFee = ParkingFee::get()
|
|
145
|
|
- ->reduce(function ($prev, $parkingFee) use ($longParkingPerHours) {
|
|
146
|
|
- if ($prev + $parkingFee->time_period >= $longParkingPerHours) {
|
|
147
|
|
- return $parkingFee;
|
|
148
|
|
- }
|
|
149
|
|
- }, 0);
|
|
|
163
|
+ break;
|
|
|
164
|
+ }
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ DB::beginTransaction();
|
|
|
168
|
+
|
|
|
169
|
+ try {
|
|
|
170
|
+ $outTransaction = OutTransaction::create([
|
|
|
171
|
+ 'plat_number' => $request->plat_number,
|
|
|
172
|
+ 'entry_transaction_id' => $request->entry_transaction_id,
|
|
|
173
|
+ 'type_vehicle_id' => $request->type_vehicle_id,
|
|
|
174
|
+ 'user_id' => auth()->user()->id,
|
|
|
175
|
+ ]);
|
|
|
176
|
+
|
|
|
177
|
+ $outTransaction->mutation()->create([
|
|
|
178
|
+ 'type' => 1,
|
|
|
179
|
+ 'amount' => $totalParkingPerDay ? $totalPriceParkingPerDay : $totalPriceParkingToday,
|
|
|
180
|
+ ]);
|
|
150
|
181
|
|
|
151
|
|
- return back()->with('success', __('messages.success.store.out_transaction'));
|
|
|
182
|
+ DB::commit();
|
|
|
183
|
+
|
|
|
184
|
+ return back()->with('success', __('messages.success.store.transaction'));
|
|
|
185
|
+ } catch (QueryException $e) {
|
|
|
186
|
+ DB::rollBack();
|
|
|
187
|
+
|
|
|
188
|
+ return back()->with('error', __('messages.error.store.transaction'));
|
|
|
189
|
+ }
|
|
152
|
190
|
}
|
|
153
|
191
|
} else {
|
|
154
|
192
|
EntryTransaction::create([
|