diff --git a/app/Http/Controllers/MobileApiController.php b/app/Http/Controllers/MobileApiController.php index a6fbd92..0f1bae0 100644 --- a/app/Http/Controllers/MobileApiController.php +++ b/app/Http/Controllers/MobileApiController.php @@ -129,11 +129,11 @@ class MobileApiController extends Controller 'started_at' => $data['started_at'], 'ended_at' => $data['ended_at'], 'user_id' => $data['user_id'], - 'phone' => $data['phone'] ?? '' , + 'phone' => $user?->phone ?? $data['phone'] ?? '' , 'car_id' => $data['car_id'], - 'user_name' => $data['name'] ?? null, - 'user_surname' => $data['surname'] ?? null, - 'user_email' => $data['email'] ?? null, + '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, @@ -370,6 +370,7 @@ class MobileApiController extends Controller $cars = UniModel::model('pipi_auto') ->where('color_id', $color) ->where('model_id', $modelId) + ->whereNull('regular_user') ->get(); $busyCars = UniModel::model('pipi_auto_calendar') ->whereBetween('date', [$started_at, $ended_at]) diff --git a/modules/applications/Logic/Approve.php b/modules/applications/Logic/Approve.php index 95ee3da..8732677 100644 --- a/modules/applications/Logic/Approve.php +++ b/modules/applications/Logic/Approve.php @@ -7,6 +7,8 @@ use A7kz\Platform\Modules\Platform\Acl\Facades\Acl; use A7kz\Platform\Modules\Platform\Core\Services\Base\Logic; use App\Models\User; use App\Modules\applications\Enum\ApplicationStatus; +use App\Modules\auto\Enums\AutoStatusEnums; +use Carbon\CarbonPeriod; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Hash; @@ -16,70 +18,89 @@ class Approve extends Logic { $user = new User(); - $checkUser = UniModel::model('core_users')->where('email', $this->row->user_email)->first(); - if (isset($checkUser)) { - $this->row->user_id = $checkUser->id; - $this->row->status = ApplicationStatus::approved; - $this->row->save(); - return $this->response(); - } - $user->forceFill([ - 'name' => $this->row->user_name . ' ' . $this->row->surname_name, - 'username' => $this->row->user_email, - 'email' => $this->row->user_email, - 'phone' => $this->row->user_phone, - 'password' => Hash::make('password'), - ]); - - $user->save(); - - $this->row->user_id = $user->id; - $this->row->status = ApplicationStatus::approved; - $this->row->save(); - $userRole = UniModel::model('core_roles') - ->where('alias', 'user') - ->first(); - - if (!empty($userRole)) { - $userRoleModel = UniModel::model('core_user_roles'); - - $userRoleModel->forceFill([ - 'user_id' => $user->id, - 'role_id' => $userRole->id - ]); - - $userRoleModel->save(); - } - $company = UniModel::model(config('platform.company.tables.company')) - ->firstOrCreate([ - 'name' => $this->row->user_name . ' ' . $this->row->surname_name, - 'biniin' => $this->row->biniin ?? '', - 'fullname' => $this->row->user_name . ' ' . $this->row->surname_name, - 'segment' => 'sol' - ]); - - $ucr = UniModel::model(config('platform.company.tables.company_user_role')) - ->firstOrCreate([ - 'user_id' => $user->id, - 'role_id' => 7, - 'company_id' => $company->id - ]); - if (!$ucr) { - UniModel::model(config('platform.company.tables.company_user_role')) - ->create([ - 'user_id' => $user->id, - 'role_id' => 6, - 'company_id' => $company->id + if (!isset($this->row->user_id)) { + $checkUser = UniModel::model('core_users')->where('email', $this->row->user_email)->first(); + if (isset($checkUser)) { + $this->row->user_id = $checkUser->id; + $this->row->save(); + } else { + $user->forceFill([ + 'name' => $this->row->user_name . ' ' . $this->row->surname_name, + 'username' => $this->row->user_email, + 'email' => $this->row->user_email, + 'phone' => $this->row->user_phone, + 'password' => Hash::make('password'), ]); + + $user->save(); + + $this->row->user_id = $user->id; + $this->row->save(); + $userRole = UniModel::model('core_roles') + ->where('alias', 'user') + ->first(); + + if (!empty($userRole)) { + $userRoleModel = UniModel::model('core_user_roles'); + + $userRoleModel->forceFill([ + 'user_id' => $user->id, + 'role_id' => $userRole->id + ]); + + $userRoleModel->save(); + } + $company = UniModel::model(config('platform.company.tables.company')) + ->firstOrCreate([ + 'name' => $this->row->user_name . ' ' . $this->row->surname_name, + 'biniin' => $this->row->biniin ?? '', + 'fullname' => $this->row->user_name . ' ' . $this->row->surname_name, + 'segment' => 'sol' + ]); + + $ucr = UniModel::model(config('platform.company.tables.company_user_role')) + ->firstOrCreate([ + 'user_id' => $user->id, + 'role_id' => 7, + 'company_id' => $company->id + ]); + if (!$ucr) { + UniModel::model(config('platform.company.tables.company_user_role')) + ->create([ + 'user_id' => $user->id, + 'role_id' => 6, + 'company_id' => $company->id + ]); + } + + UniModel::model(config('platform.company.tables.company_user')) + ->firstOrCreate([ + 'company_id' => $company->id, + 'user_id' => $user->id, + 'default' => true, + ]); + } } - UniModel::model(config('platform.company.tables.company_user')) - ->firstOrCreate([ - 'company_id' => $company->id, - 'user_id' => $user->id, - 'default' => true, - ]); + $this->fillCalendar($this->row); + + $this->row->status = ApplicationStatus::approved->value; + $this->row->save(); return $this->response(); } + + private function fillCalendar($row): void + { + $period = CarbonPeriod::create($row->started_at, $row->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' => $row->car_id, + 'date' => $date, + 'status' => AutoStatusEnums::Rent->name + ]); + } + } } diff --git a/modules/applications/Logic/Cancel.php b/modules/applications/Logic/Cancel.php new file mode 100644 index 0000000..ea54e07 --- /dev/null +++ b/modules/applications/Logic/Cancel.php @@ -0,0 +1,37 @@ + $this->row->ended_at) { + $this->message = 'Вы не можете завершить заявку после даты окончания аренды'; + return $this->response(); + } + + $car = $this->row->car_id; + + if (isset($car)) { + UniModel::model('pipi_auto_calendar') + ->where('date', '>=', Carbon::now()->toDateString()) + ->where('date', '<=', $this->row->ended_at) + ->where('auto_id', $car)->delete(); + } + + $this->row->status = ApplicationStatus::rejected->value; + $this->row->ended_at = Carbon::now(); + $this->row->save(); + + return $this->response(); + } +} diff --git a/modules/applications/Logic/Complete.php b/modules/applications/Logic/Complete.php new file mode 100644 index 0000000..e072126 --- /dev/null +++ b/modules/applications/Logic/Complete.php @@ -0,0 +1,21 @@ +row->status = ApplicationStatus::completed->value; + $this->row->save(); + return $this->response(); + } +} diff --git a/modules/applications/app.json b/modules/applications/app.json index ea195b0..c07b93f 100644 --- a/modules/applications/app.json +++ b/modules/applications/app.json @@ -44,6 +44,12 @@ "user_email": { "type": "string" }, + "sum": { + "type": "int" + }, + "deposit": { + "type": "int" + }, "started_at": { "type": "date", "validation": "required|date" @@ -55,6 +61,25 @@ "photos": { "type": "json", "validation": "nullable" + }, + "address_start": { + "type": "foreign", + "table": "pipi_address", + "foreign": "id", + "display": [ + "name" + ], + "validation": "required|integer" + }, + "address_end": { + "type": "foreign", + "table": "pipi_address", + "alias": "address_end", + "foreign": "id", + "display": [ + "name" + ], + "validation": "required|integer" } } }, @@ -71,7 +96,7 @@ { "name": "user_surname", "caption": "Фамилия" }, { "name": "user_email", "caption": "Email" }, { "name": "started_at", "caption": "Начало" }, - { "name": "ended_at", "caption": "Окончание" } + { "name": "ended_at", "caption": "Дата окончания" } ], "action": { "head": [ @@ -131,7 +156,15 @@ }, { "cols": [ - { "size": 6, "input": { "name": "ended_at", "label": "Окончание" } }, + { "size": 3, "input": { "name": "address_start", "label": "Адрес получения" } }, + { "size": 3, "input": { "name": "address_end", "label": "Конечный адрес" } }, + { "size": 3, "input": { "name": "sum", "label": "Сумма" } }, + { "size": 3, "input": { "name": "deposit", "label": "Депозит" } } + ] + }, + { + "cols": [ + { "size": 6, "input": { "name": "ended_at", "label": "Дата окончания" } }, { "size": 6, "input": { "name": "photos", "label": "Фотографии", "type": "files"} } ] } @@ -161,7 +194,15 @@ "cols": [ { "size": 4, "input": { "name": "user_email", "label": "Email" } }, { "size": 4, "input": { "name": "started_at", "label": "Начало" } }, - { "size": 4, "input": { "name": "ended_at", "label": "Окончание" } } + { "size": 4, "input": { "name": "ended_at", "label": "Дата окончания" } } + ] + }, + { + "cols": [ + { "size": 3, "input": { "name": "address_start", "label": "Адрес получения" } }, + { "size": 3, "input": { "name": "address_end", "label": "Конечный адрес" } }, + { "size": 3, "input": { "name": "sum", "label": "Сумма" } }, + { "size": 3, "input": { "name": "deposit", "label": "Депозит" } } ] }, { @@ -199,7 +240,15 @@ "cols": [ { "size": 4, "input": { "name": "user_email", "label": "Email" } }, { "size": 4, "input": { "name": "started_at", "label": "Начало" } }, - { "size": 4, "input": { "name": "ended_at", "label": "Окончание" } } + { "size": 4, "input": { "name": "ended_at", "label": "Дата окончания" } } + ] + }, + { + "cols": [ + { "size": 3, "input": { "name": "address_start", "label": "Адрес получения" } }, + { "size": 3, "input": { "name": "address_end", "label": "Конечный адрес" } }, + { "size": 3, "input": { "name": "sum", "label": "Сумма" } }, + { "size": 3, "input": { "name": "deposit", "label": "Депозит" } } ] }, { @@ -220,13 +269,15 @@ "type": "logic", "name": "App.Modules.applications.Logic.Cancel", "label": "Отменить заявку", - "btn": "btn btn-danger" + "btn": "btn btn-danger", + "condition": "status,==,approved" }, { "type": "logic", - "name": "App.Modules.applications.Logic.Extend", - "label": "Продлить аренду", - "btn": "btn btn-success" + "name": "App.Modules.applications.Logic.Complete", + "label": "Завершить заявку", + "btn": "btn btn-success", + "condition": "status,==,review" }, "struct:crud.form.submit.save", "struct:crud.form.submit.close" diff --git a/modules/auto/app.json b/modules/auto/app.json index 0ee34d8..3d15289 100644 --- a/modules/auto/app.json +++ b/modules/auto/app.json @@ -153,6 +153,14 @@ "photo_id": { "type": "int", "validation": "nullable|int" + }, + "regular_user": { + "type": "foreign", + "table": "core_users", + "foreign": "id", + "display": ["username"], + "nullable": true, + "comment": "Постоянный клиент" } } }, @@ -301,8 +309,8 @@ { "size": 4, "input": { - "name": "owner_id", - "label": "Владелец" + "name": "regular_user", + "label": "Постоянный клиент" } }, { @@ -330,21 +338,24 @@ "size": 4, "input": { "name": "code", - "label": "Код" + "label": "Код", + "readonly": true } }, { "size": 4, "input": { "name": "name", - "label": "Наименование" + "label": "Наименование", + "readonly": true } }, { "size": 4, "input": { "name": "state_number", - "label": "Госномер" + "label": "Госномер", + "readonly": true } } ] @@ -355,21 +366,24 @@ "size": 4, "input": { "name": "brand_id", - "label": "Марка" + "label": "Марка", + "readonly": true } }, { "size": 4, "input": { "name": "model_id", - "label": "Модель" + "label": "Модель", + "readonly": true } }, { "size": 4, "input": { "name": "color_id", - "label": "Цвет" + "label": "Цвет", + "readonly": true } } ] @@ -380,21 +394,23 @@ "size": 4, "input": { "name": "manufacture_year", - "label": "Год производства" + "label": "Год производства", + "readonly": true } }, { "size": 4, "input": { "name": "estimated_cost", - "label": "Оценочная стоимость" + "label": "Оценочная стоимость", + "readonly": true } }, { "size": 4, "input": { - "name": "owner_id", - "label": "Владелец" + "name": "regular_user", + "label": "Постоянный клиент" } }, { diff --git a/modules/auto/script.php b/modules/auto/script.php index 45699b1..9a1c7e4 100644 --- a/modules/auto/script.php +++ b/modules/auto/script.php @@ -145,6 +145,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript { $table->unsignedBigInteger('photo_id')->nullable(); }); } + + if (!Schema::connection($segment->connector)->hasColumn('pipi_auto', 'regular_user')) { + Schema::connection($segment->connector)->table('pipi_auto', static function (Blueprint $table) { + $table->unsignedBigInteger('regular_user')->nullable(); + }); + } } }