master
parent
e0f426ce54
commit
772af756b5
|
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||||
use A7kz\Platform\Models\UniModel;
|
use A7kz\Platform\Models\UniModel;
|
||||||
use A7kz\Platform\Modules\Platform\Acl\Facades\Acl;
|
use A7kz\Platform\Modules\Platform\Acl\Facades\Acl;
|
||||||
use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
|
use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
|
||||||
|
use App\Models\User;
|
||||||
use App\Modules\applications\Enum\ApplicationStatus;
|
use App\Modules\applications\Enum\ApplicationStatus;
|
||||||
use App\Modules\auto\Enums\AutoStatusEnums;
|
use App\Modules\auto\Enums\AutoStatusEnums;
|
||||||
use App\Service\DepositService;
|
use App\Service\DepositService;
|
||||||
|
|
@ -63,85 +64,86 @@ class MobileApiController extends Controller
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendApplication(Request $request): JsonResponse {
|
public function sendApplication(Request $request): JsonResponse {
|
||||||
$data = $request->all();
|
try {
|
||||||
$data['started_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['started_at']);
|
$data = $request->all();
|
||||||
$data['ended_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['ended_at']);
|
$data['started_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['started_at']);
|
||||||
$authToken = null;
|
$data['ended_at'] = Carbon::createFromFormat('d-m-Y H:i', $data['ended_at']);
|
||||||
$data['car_id'] = null;
|
$authToken = null;
|
||||||
if ($request->header('Authorization')) {
|
$data['car_id'] = null;
|
||||||
$user = auth()->guard('api')->user();
|
if ($request->header('Authorization')) {
|
||||||
$data['user_id'] = $user?->id;
|
$user = auth()->guard('api')->user();
|
||||||
$authToken = $request->header('Authorization');
|
$data['user_id'] = $user?->id;
|
||||||
} else {
|
$authToken = $request->header('Authorization');
|
||||||
$user = UniModel::model('core_users')
|
|
||||||
->where('email', $data['email'])
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if ($user) {
|
|
||||||
$data['user_id'] = $user->id;
|
|
||||||
$tokenResult = $user->createToken('auth_token');
|
|
||||||
$authToken = $tokenResult->accessToken;
|
|
||||||
} else {
|
} else {
|
||||||
$data['user_id'] = null;
|
$user = User::where('email', $data['email'])->first();
|
||||||
|
|
||||||
|
if ($user) {
|
||||||
|
$data['user_id'] = $user->id;
|
||||||
|
$tokenResult = $user->createToken('auth_token');
|
||||||
|
$authToken = $tokenResult->plainTextToken;
|
||||||
|
} else {
|
||||||
|
$data['user_id'] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
$response = [
|
||||||
|
'status' => 'OK',
|
||||||
|
'message' => 'Заявка создана',
|
||||||
|
];
|
||||||
|
|
||||||
$response = [
|
$car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id'], $data['color_code']);
|
||||||
'status' => 'OK',
|
$status = ApplicationStatus::pending->value;
|
||||||
'message' => 'Заявка создана',
|
if (!isset($car)) {
|
||||||
];
|
return response()->json('Нет свободных машин');
|
||||||
|
|
||||||
$car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id'], $data['color_code']);
|
|
||||||
$status = ApplicationStatus::pending->value;
|
|
||||||
if (!isset($car)) {
|
|
||||||
return response()->json('Нет свободных машин');
|
|
||||||
}
|
|
||||||
$data['car_id'] = $car->id;
|
|
||||||
if ($data['user_id']) {
|
|
||||||
$period = CarbonPeriod::create($data['started_at'], $data['ended_at']);
|
|
||||||
|
|
||||||
$dates = array_map(fn($date) => $date->toDateString(), iterator_to_array($period));
|
|
||||||
|
|
||||||
foreach ($dates as $date) {
|
|
||||||
UniModel::model('pipi_auto_calendar')->create([
|
|
||||||
'auto_id' => $data['car_id'],
|
|
||||||
'date' => $date,
|
|
||||||
'status' => AutoStatusEnums::Rent->name
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
$status = ApplicationStatus::approved->value;
|
$data['car_id'] = $car->id;
|
||||||
$response['auth_token'] = $authToken;
|
if ($data['user_id']) {
|
||||||
$response['message'] = 'Заявка создана, и вы были автоматически авторизованы';
|
$period = CarbonPeriod::create($data['started_at'], $data['ended_at']);
|
||||||
} elseif (!isset($data['user_id'])) {
|
|
||||||
$response['message'] = 'Заявка создана, с вами свяжется наш оператор';
|
|
||||||
}
|
|
||||||
$address_end = UniModel::model('pipi_address')->where('name', $data['address_end'])->first();
|
|
||||||
$address_start = UniModel::model('pipi_address')->where('name', $data['address_start'])->first();
|
|
||||||
$service = new DepositService;
|
|
||||||
$sums = $service->calculateSummary($data['mark_id'], $data['started_at'], $data['ended_at']);
|
|
||||||
$sum = $sums['discounted_sum'];
|
|
||||||
if ($data['deposit'] != $sums['deposit_base']) {
|
|
||||||
$sum += $data['deposit'];
|
|
||||||
}
|
|
||||||
UniModel::model('pipi_applications')->create([
|
|
||||||
'rent_day' => $data['rent_day'],
|
|
||||||
'started_at' => $data['started_at'],
|
|
||||||
'ended_at' => $data['ended_at'],
|
|
||||||
'user_id' => $data['user_id'],
|
|
||||||
'phone' => $user?->phone ?? $data['phone'] ?? '' ,
|
|
||||||
'car_id' => $data['car_id'],
|
|
||||||
'user_name' => $user?->name ?? $data['name'] ?? null,
|
|
||||||
'user_surname' => $user?->name ?? $data['surname'] ?? null,
|
|
||||||
'user_email' => $user?->email ?? $data['email'] ?? null,
|
|
||||||
'address_end' => $address_end?->id,
|
|
||||||
'address_start' => $address_start?->id,
|
|
||||||
'deposit' => $data['deposit'] ?? null,
|
|
||||||
'status' => $status,
|
|
||||||
'sum' => $sum
|
|
||||||
]);
|
|
||||||
|
|
||||||
return response()->json($response);
|
$dates = array_map(fn($date) => $date->toDateString(), iterator_to_array($period));
|
||||||
|
|
||||||
|
foreach ($dates as $date) {
|
||||||
|
UniModel::model('pipi_auto_calendar')->create([
|
||||||
|
'auto_id' => $data['car_id'],
|
||||||
|
'date' => $date,
|
||||||
|
'status' => AutoStatusEnums::Rent->name
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$status = ApplicationStatus::approved->value;
|
||||||
|
$response['auth_token'] = $authToken;
|
||||||
|
$response['message'] = 'Заявка создана, и вы были автоматически авторизованы';
|
||||||
|
} elseif (!isset($data['user_id'])) {
|
||||||
|
$response['message'] = 'Заявка создана, с вами свяжется наш оператор';
|
||||||
|
}
|
||||||
|
$address_end = UniModel::model('pipi_address')->where('name', $data['address_end'])->first();
|
||||||
|
$address_start = UniModel::model('pipi_address')->where('name', $data['address_start'])->first();
|
||||||
|
$service = new DepositService;
|
||||||
|
$sums = $service->calculateSummary($data['mark_id'], $data['started_at'], $data['ended_at']);
|
||||||
|
$sum = $sums['discounted_sum'];
|
||||||
|
if ($data['deposit'] != $sums['deposit_base']) {
|
||||||
|
$sum += $data['deposit'];
|
||||||
|
}
|
||||||
|
UniModel::model('pipi_applications')->create([
|
||||||
|
'rent_day' => $data['rent_day'],
|
||||||
|
'started_at' => $data['started_at'],
|
||||||
|
'ended_at' => $data['ended_at'],
|
||||||
|
'user_id' => $data['user_id'],
|
||||||
|
'phone' => $user?->phone ?? $data['phone'] ?? '' ,
|
||||||
|
'car_id' => $data['car_id'],
|
||||||
|
'user_name' => $user?->name ?? $data['name'] ?? null,
|
||||||
|
'user_surname' => $user?->name ?? $data['surname'] ?? null,
|
||||||
|
'user_email' => $user?->email ?? $data['email'] ?? null,
|
||||||
|
'address_end' => $address_end?->id,
|
||||||
|
'address_start' => $address_start?->id,
|
||||||
|
'deposit' => $data['deposit'] ?? null,
|
||||||
|
'status' => $status,
|
||||||
|
'sum' => $sum
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json($response);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return response()->json($e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getApplications(): JsonResponse
|
public function getApplications(): JsonResponse
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue