From ee0f52a4c7271c2daeadb6ccfcc52904221b0cd4 Mon Sep 17 00:00:00 2001 From: Rustem Date: Mon, 31 Mar 2025 23:51:34 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B0=D0=BF=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/MobileApiController.php | 89 +++++++++++++------- modules/auto/app.json | 20 +++++ modules/auto/script.php | 6 ++ modules/auto_colors/app.json | 32 ++++++- modules/auto_colors/script.php | 6 ++ modules/auto_tariffs/app.json | 4 +- 6 files changed, 123 insertions(+), 34 deletions(-) diff --git a/app/Http/Controllers/MobileApiController.php b/app/Http/Controllers/MobileApiController.php index 4af7f5d..4a9b00d 100644 --- a/app/Http/Controllers/MobileApiController.php +++ b/app/Http/Controllers/MobileApiController.php @@ -64,7 +64,8 @@ class MobileApiController extends Controller $data['car_id'] = null; if ($request->header('Authorization')) { $user = auth()->guard('api')->user(); - $data['user_id'] = $user ? $user->id : null; + $data['user_id'] = $user?->id; + $authToken = $request->header('Authorization'); } else { $user = UniModel::model('core_users') ->where('email', $data['email']) @@ -85,18 +86,19 @@ class MobileApiController extends Controller 'message' => 'Заявка создана', ]; - if ($authToken) { - $car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id']); - if (isEmpty($car)) { + if ($data['user_id']) { + $car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id'], $data['color_code']); + if (!isset($car)) { return response()->json('Нет свободных машин'); } + $data['car_id'] = $car->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' => $car->id, + 'auto_id' => $data['car_id'], 'date' => $date, 'status' => AutoStatusEnums::Rent->name ]); @@ -200,14 +202,22 @@ class MobileApiController extends Controller $bodywork_id = UniModel::model('pipi_auto_bodywork')->where('name', $bodywork)->first()->id; $marks = $marks->where('bodywork_id', $bodywork_id); } + foreach ($marks as $mark) { if ($mark->name === 'Ввод остатков') { continue; } -// -// if (!$this->checkAvailableCar($started_at, $ended_at, $mark->id)) { -// continue; -// } + + if (!$this->checkAvailableCar($started_at, $ended_at, $mark->id)) { + continue; + } + + $cars = UniModel::model('pipi_auto')->where('model_id', $mark->id)->get(); + + + if ($cars->isEmpty()) { + continue; + } $brand = UniModel::model('pipi_auto_brands')->find($mark->brand_id); $tariffs = UniModel::model('pipi_auto_tariffs')->where('model_id', $mark->id)->get(); @@ -220,6 +230,7 @@ class MobileApiController extends Controller if ($photo) { $path = url('api/files/file/' . ltrim($photo->path, '/')); } + $tariffs_new = []; foreach ($tariffs as $tariff) { $tariffs_new[] = [ @@ -229,25 +240,39 @@ class MobileApiController extends Controller 'max' => $tariff?->day_range_end, ]; } + $carsByColor = $cars->groupBy('color_id'); - $availableMarks[$mark->name . '-' . $mark->year] = [ - 'id' => $mark->id, - 'brand' => $brand?->name, - 'mark' => $mark->name, - 'year' => $mark->year, - 'configuration' => $equipment?->name, - 'people' => $mark?->people ?? '5', - 'actuator' => $mark?->actuator ?? 'Передний', - 'fuel_type' => $mark?->fuel_type ?? 'АКПП', - 'hp' => $mark?->hp ?? '1.6', - 'engine_capacity' => $mark?->engine_capacity ?? '1591', - 'fuel_tank' => $mark?->fuel_tank ?? '50', - 'class' => $class->name ?? 'Эконом', - 'bodywork' => $bodywork->name ?? 'Седан', - 'conditioner' => $mark?->conditioner, - 'photo' => $path, - 'tariffs' => $tariffs_new - ]; + foreach ($carsByColor as $carColor => $carsOfColor) { + $carColor = UniModel::model('pipi_auto_colors')->find($carColor)->code; + + $colorPath = $path; + if ($carsOfColor->first()->photo_id) { + $photo = UniModel::model('core_files')->find($carsOfColor->first()->photo_id); + $colorPath = url('api/files/file/' . ltrim($photo->path, '/')); + } + + $key = $mark->name . '-' . $mark->year . '-' . $carColor; + + $availableMarks[$key] = [ + 'id' => $mark->id, + 'brand' => $brand?->name, + 'mark' => $mark->name, + 'year' => $mark->year, + 'color' => $carColor, + 'configuration' => $equipment?->name, + 'people' => $mark?->people ?? '5', + 'actuator' => $mark?->actuator ?? 'Передний', + 'fuel_type' => $mark?->fuel_type ?? 'АКПП', + 'hp' => $mark?->hp ?? '1.6', + 'engine_capacity' => $mark?->engine_capacity ?? '1591', + 'fuel_tank' => $mark?->fuel_tank ?? '50', + 'class' => $class->name ?? 'Эконом', + 'bodywork' => $bodywork->name ?? 'Седан', + 'conditioner' => $mark?->conditioner, + 'photo' => $colorPath, + 'tariffs' => $tariffs_new, + ]; + } } if (empty($availableMarks)) { @@ -256,11 +281,15 @@ class MobileApiController extends Controller return response()->json($availableMarks); } - public function getAvailableCar($started_at, $ended_at, $modelId) + public function getAvailableCar($started_at, $ended_at, $modelId, $color) { $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(); + $color = UniModel::model('pipi_auto_colors')->where('code', $color)->first()->id; + $cars = UniModel::model('pipi_auto') + ->where('color_id', $color) + ->where('model_id', $modelId) + ->get(); $busyCars = UniModel::model('pipi_auto_calendar') ->whereBetween('date', [$started_at, $ended_at]) ->pluck('auto_id') @@ -271,7 +300,7 @@ class MobileApiController extends Controller if ($availableCars->isNotEmpty()) { return $availableCars->first(); } else { - return (object) []; + return null; } } } diff --git a/modules/auto/app.json b/modules/auto/app.json index e853336..0ee34d8 100644 --- a/modules/auto/app.json +++ b/modules/auto/app.json @@ -149,6 +149,10 @@ "type": "timestamp", "nullable": true, "comment": "Дата удаления" + }, + "photo_id": { + "type": "int", + "validation": "nullable|int" } } }, @@ -300,6 +304,14 @@ "name": "owner_id", "label": "Владелец" } + }, + { + "size": 12, + "input": { + "name": "photo_id", + "label": "Фото", + "type": "file" + } } ] } @@ -384,6 +396,14 @@ "name": "owner_id", "label": "Владелец" } + }, + { + "size": 12, + "input": { + "name": "photo_id", + "label": "Фото", + "type": "file" + } } ] } diff --git a/modules/auto/script.php b/modules/auto/script.php index 75b64a8..45699b1 100644 --- a/modules/auto/script.php +++ b/modules/auto/script.php @@ -139,6 +139,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript { $table->string('guid')->unique()->nullable(); }); } + + if (!Schema::connection($segment->connector)->hasColumn('pipi_auto', 'photo_id')) { + Schema::connection($segment->connector)->table('pipi_auto', static function (Blueprint $table) { + $table->unsignedBigInteger('photo_id')->nullable(); + }); + } } } diff --git a/modules/auto_colors/app.json b/modules/auto_colors/app.json index 6471b35..2c3a0ed 100644 --- a/modules/auto_colors/app.json +++ b/modules/auto_colors/app.json @@ -16,6 +16,9 @@ }, "name": { "type": "string" + }, + "code": { + "type": "string" } } }, @@ -27,6 +30,10 @@ { "name": "name", "caption": "Название цвета" + }, + { + "name": "code", + "caption": "Код" } ], "action": { @@ -49,6 +56,13 @@ "name": "name", "label": "Название цвета" } + }, + { + "size": 6, + "input": { + "name": "code", + "label": "Код" + } } ] } @@ -66,11 +80,18 @@ { "cols": [ { - "size": 12, + "size": 6, "input": { "name": "name", "label": "Название" } + }, + { + "size": 6, + "input": { + "name": "code", + "label": "Код" + } } ] } @@ -86,11 +107,18 @@ { "cols": [ { - "size": 12, + "size": 6, "input": { "name": "name", "label": "Название" } + }, + { + "size": 6, + "input": { + "name": "code", + "label": "Код" + } } ] } diff --git a/modules/auto_colors/script.php b/modules/auto_colors/script.php index 57ce277..37ecf97 100644 --- a/modules/auto_colors/script.php +++ b/modules/auto_colors/script.php @@ -33,6 +33,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript { $table->softDeletes(); }); } + + if (!Schema::connection($segment->connector)->hasColumn('pipi_auto_colors', 'code')) { + Schema::connection($segment->connector)->table('pipi_auto_colors', static function (Blueprint $table) { + $table->string('code')->nullable(); + }); + } } } }; diff --git a/modules/auto_tariffs/app.json b/modules/auto_tariffs/app.json index 6471b35..e8ac8bd 100644 --- a/modules/auto_tariffs/app.json +++ b/modules/auto_tariffs/app.json @@ -1,11 +1,11 @@ { "module": "pipicar", - "name": "pipicar.auto_colors", + "name": "pipicar.auto_tariffs", "type": "crud", "title": "Цвета Автомобилей", "withHeader": false, "data": { - "table": "pipi_auto_colors", + "table": "pipi_auto_tariffs", "pk": "id", "limit": 25, "segment": true,