Доработка апи
parent
9fc5991cc4
commit
bd6e96d132
|
|
@ -5,6 +5,8 @@ namespace App\Http\Controllers;
|
|||
use A7kz\Platform\Models\UniModel;
|
||||
use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
|
||||
use App\Modules\applications\Enum\ApplicationStatus;
|
||||
use App\Modules\auto\Enums\AutoStatusEnums;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -14,6 +16,7 @@ use Illuminate\Support\Facades\Log;
|
|||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Mpdf\Tag\Mark;
|
||||
use function PHPUnit\Framework\isEmpty;
|
||||
|
||||
class MobileApiController extends Controller
|
||||
{
|
||||
|
|
@ -26,6 +29,7 @@ class MobileApiController extends Controller
|
|||
foreach ($marks as $mark) {
|
||||
$brand = UniModel::model('pipi_auto_brands')->find($mark->brand_id);
|
||||
$tariffs = UniModel::model('pipi_auto_tariffs')->where('model_id', $mark->id)->get();
|
||||
$equipment = UniModel::model('pipi_auto_equipment')->where('id', $mark->equipment_id)->first();
|
||||
if ($mark->name != 'Ввод остатков') {
|
||||
foreach ($tariffs as $tariff) {
|
||||
$tariffs_new[$tariff?->day_range_start . '-' . $tariff->day_range_end] = [
|
||||
|
|
@ -40,6 +44,7 @@ class MobileApiController extends Controller
|
|||
'brand' => $brand->name,
|
||||
'mark' => $mark->name,
|
||||
'year' => $mark->year,
|
||||
'configuration' => $equipment?->name,
|
||||
'tariffs' => $tariffs_new
|
||||
];
|
||||
}
|
||||
|
|
@ -70,6 +75,34 @@ class MobileApiController extends Controller
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
$response = [
|
||||
'status' => 'OK',
|
||||
'message' => 'Заявка создана',
|
||||
];
|
||||
|
||||
if ($authToken) {
|
||||
$car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id']);
|
||||
if (isEmpty($car)) {
|
||||
return response()->json('Нет свободных машин');
|
||||
}
|
||||
$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' => $car->id,
|
||||
'date' => $date,
|
||||
'status' => AutoStatusEnums::Rent->name
|
||||
]);
|
||||
}
|
||||
$response['auth_token'] = $authToken;
|
||||
$response['message'] = 'Заявка создана, и вы были автоматически авторизованы';
|
||||
} elseif (!isset($data['user_id'])) {
|
||||
$response['message'] = 'Заявка создана, с вами свяжется наш оператор';
|
||||
}
|
||||
|
||||
UniModel::model('pipi_applications')->create([
|
||||
'rent_day' => $data['rent_day'],
|
||||
'started_at' => $data['started_at'],
|
||||
|
|
@ -83,18 +116,6 @@ class MobileApiController extends Controller
|
|||
'status' => ApplicationStatus::pending->value
|
||||
]);
|
||||
|
||||
$response = [
|
||||
'status' => 'OK',
|
||||
'message' => 'Заявка создана',
|
||||
];
|
||||
|
||||
if ($authToken) {
|
||||
$response['auth_token'] = $authToken;
|
||||
$response['message'] = 'Заявка создана, и вы были автоматически авторизованы';
|
||||
} elseif (!isset($data['user_id'])) {
|
||||
$response['message'] = 'Заявка создана, с вами свяжется наш оператор';
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
|
|
@ -126,4 +147,94 @@ class MobileApiController extends Controller
|
|||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkAvailableCar($started_at = null, $ended_at = null, $modelId = null): bool {
|
||||
if (!$started_at || !$ended_at || !$modelId) {
|
||||
$request = request();
|
||||
$started_at = $started_at ?? $request->input('started_at');
|
||||
$ended_at = $ended_at ?? $request->input('ended_at');
|
||||
$modelId = $modelId ?? $request->input('model_id');
|
||||
}
|
||||
|
||||
if (!$started_at || !$ended_at || !$modelId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$started_at = Carbon::parse($started_at)->format('Y-m-d');
|
||||
$ended_at = Carbon::parse($ended_at)->format('Y-m-d');
|
||||
$cars = UniModel::model('pipi_auto')->where('model_id', $modelId)->get();
|
||||
$busyCars = UniModel::model('pipi_auto_calendar')
|
||||
->whereBetween('date', [$started_at, $ended_at])
|
||||
->pluck('auto_id')
|
||||
->toArray();
|
||||
|
||||
$availableCars = $cars->reject(fn($car) => in_array($car->id, $busyCars));
|
||||
|
||||
return $availableCars->isNotEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getAvailableMarksList(Request $request): JsonResponse
|
||||
{
|
||||
$started_at = $request->query('started_at');
|
||||
$ended_at = $request->query('ended_at');
|
||||
|
||||
$marks = UniModel::model('pipi_brand_models')->get();
|
||||
$availableMarks = [];
|
||||
|
||||
foreach ($marks as $mark) {
|
||||
if ($mark->name === 'Ввод остатков') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->checkAvailableCar($started_at, $ended_at, $mark->id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$brand = UniModel::model('pipi_auto_brands')->find($mark->brand_id);
|
||||
$tariffs = UniModel::model('pipi_auto_tariffs')->where('model_id', $mark->id)->get();
|
||||
$equipment = UniModel::model('pipi_auto_equipment')->where('id', $mark->equipment_id)->first();
|
||||
|
||||
$tariffs_new = [];
|
||||
foreach ($tariffs as $tariff) {
|
||||
$tariffs_new[$tariff?->day_range_start . '-' . $tariff->day_range_end] = [
|
||||
'name' => $tariff?->name,
|
||||
'price' => $tariff?->base_rate,
|
||||
'min' => $tariff?->day_range_start,
|
||||
'max' => $tariff?->day_range_end,
|
||||
];
|
||||
}
|
||||
|
||||
$availableMarks[$mark->name . '-' . $mark->year] = [
|
||||
'id' => $mark->id,
|
||||
'brand' => $brand->name ?? null,
|
||||
'mark' => $mark->name,
|
||||
'year' => $mark->year,
|
||||
'configuration' => $equipment?->name,
|
||||
'tariffs' => $tariffs_new,
|
||||
];
|
||||
}
|
||||
|
||||
return response()->json($availableMarks);
|
||||
}
|
||||
|
||||
public function getAvailableCar($started_at, $ended_at, $modelId)
|
||||
{
|
||||
$started_at = Carbon::parse($started_at)->format('Y-m-d');
|
||||
$ended_at = Carbon::parse($ended_at)->format('Y-m-d');
|
||||
$cars = UniModel::model('pipi_auto')->where('model_id', $modelId)->get();
|
||||
$busyCars = UniModel::model('pipi_auto_calendar')
|
||||
->whereBetween('date', [$started_at, $ended_at])
|
||||
->pluck('auto_id')
|
||||
->toArray();
|
||||
|
||||
$availableCars = $cars->reject(fn($car) => in_array($car->id, $busyCars));
|
||||
|
||||
if ($availableCars->isNotEmpty()) {
|
||||
return $availableCars->first();
|
||||
} else {
|
||||
return (object) [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ Route::prefix('1c')->group(function () {
|
|||
|
||||
Route::prefix('mobile')->group(function () {
|
||||
Route::get('getMarks', [MobileApiController::class, 'getMarks']);
|
||||
Route::get('getAvailableMarksList', [MobileApiController::class, 'getAvailableMarksList']);
|
||||
Route::post('checkAvailableCar', [MobileApiController::class, 'checkAvailableCar']);
|
||||
Route::post('sendApplication', [MobileApiController::class, 'sendApplication']);
|
||||
Route::post('getApplications', [MobileApiController::class, 'getApplications']);
|
||||
Route::post('login', [MobileApiController::class, 'login']);
|
||||
|
|
|
|||
Loading…
Reference in New Issue