From fe68420bab16868ba27a8d45f8a027ed2ba84dd7 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Thu, 23 Nov 2023 15:34:04 +0500 Subject: [PATCH] Add branch & province model --- app/Models/Branch/Branch.php | 42 ++++++ app/Models/System/Location/Province.php | 30 ++++ app/Nova/Resource.php | 59 ++++++++ app/Nova/Resources/Branch/Branch.php | 139 ++++++++++++++++++ .../Branch/Concerns/BranchNovaRepo.php | 22 +++ app/Repos/Branch/BranchRepo.php | 7 + .../System/Settings/Location/RegionRepo.php | 73 +++++++++ ...23_11_23_143516_create_provinces_table.php | 30 ++++ ...023_11_23_143517_create_branches_table.php | 37 +++++ database/seeders/DatabaseSeeder.php | 51 ++++++- pint.json | 5 + 11 files changed, 490 insertions(+), 5 deletions(-) create mode 100644 app/Models/Branch/Branch.php create mode 100644 app/Models/System/Location/Province.php create mode 100644 app/Nova/Resource.php create mode 100644 app/Nova/Resources/Branch/Branch.php create mode 100644 app/Nova/Resources/Branch/Concerns/BranchNovaRepo.php create mode 100644 app/Repos/Branch/BranchRepo.php create mode 100644 app/Repos/System/Settings/Location/RegionRepo.php create mode 100644 database/migrations/2023_11_23_143516_create_provinces_table.php create mode 100644 database/migrations/2023_11_23_143517_create_branches_table.php create mode 100644 pint.json diff --git a/app/Models/Branch/Branch.php b/app/Models/Branch/Branch.php new file mode 100644 index 0000000..be51ad3 --- /dev/null +++ b/app/Models/Branch/Branch.php @@ -0,0 +1,42 @@ + + */ + protected $fillable = [ + 'name', + 'region', + 'province_id', + 'unique_code', + 'billing_username', + 'billing_password', + 'address', + ]; + + /** + * Translatable fields + * + * @var array + */ + public $translatable = [ + 'name', + 'address', + ]; +} diff --git a/app/Models/System/Location/Province.php b/app/Models/System/Location/Province.php new file mode 100644 index 0000000..89cb564 --- /dev/null +++ b/app/Models/System/Location/Province.php @@ -0,0 +1,30 @@ + + */ + protected $fillable = [ + 'region', + 'name', + ]; + + /** + * Translatable fields + * + * @var array + */ + public $translatable = ['name']; +} diff --git a/app/Nova/Resource.php b/app/Nova/Resource.php new file mode 100644 index 0000000..03277df --- /dev/null +++ b/app/Nova/Resource.php @@ -0,0 +1,59 @@ + + */ + public static $model = BranchModel::class; + + /** + * The single value that should be used to represent the resource when being displayed. + * + * @var string + */ + public static $title = 'name'; + + /** + * The columns that should be searched. + * + * @var array + */ + public static $search = [ + 'name', 'unique_code', + ]; + + /** + * Get the displayable label of the resource. + */ + public static function label(): string + { + return __('Branches'); + } + + /** + * Get the displayable singular label of the resource. + */ + public static function singularLabel(): string + { + return __('Branch'); + } + + /** + * Get the fields displayed by the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function fields(NovaRequest $request): array + { + return [ + ID::make()->sortable(), + + Text::make(__('Name'), 'name') + ->rules('required', 'string', 'max:255') + ->translatable(), + + Select::make(__('Region'), 'region') + ->displayUsingLabels() + ->searchable() + ->options(RegionRepo::values()) + ->default(RegionRepo::default()) + ->rules('required') + ->sortable(), + + Select::make(__('Province'), 'province_id') + ->displayUsingLabels() + ->searchable() + ->dependsOn('region', BranchNovaRepo::dependsOnRegion()), + + Text::make(__('Unique code'), 'unique_code') + ->rules('required', 'string', 'max:255'), + + Text::make(__('Billing username'), 'billing_username') + ->rules('nullable', 'string', 'max:255'), + + Text::make(__('Billing password'), 'billing_password') + ->rules('nullable', 'string', 'max:255'), + + Textare::make('address', 'address'), + ]; + } + + /** + * Get the cards available for the request. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function cards(NovaRequest $request) + { + return []; + } + + /** + * Get the filters available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function filters(NovaRequest $request) + { + return []; + } + + /** + * Get the lenses available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function lenses(NovaRequest $request) + { + return []; + } + + /** + * Get the actions available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function actions(NovaRequest $request) + { + return []; + } +} diff --git a/app/Nova/Resources/Branch/Concerns/BranchNovaRepo.php b/app/Nova/Resources/Branch/Concerns/BranchNovaRepo.php new file mode 100644 index 0000000..17425bd --- /dev/null +++ b/app/Nova/Resources/Branch/Concerns/BranchNovaRepo.php @@ -0,0 +1,22 @@ +options( + $formData->region + ? Branch::where('region', $formData->region)->pluck('name', 'id') + : [] + ); + }; + } +} diff --git a/app/Repos/Branch/BranchRepo.php b/app/Repos/Branch/BranchRepo.php new file mode 100644 index 0000000..5b2c829 --- /dev/null +++ b/app/Repos/Branch/BranchRepo.php @@ -0,0 +1,7 @@ + __('Ashgabat'), + self::AK => __('Arkadag'), + self::MR => __('Mary'), + self::AH => __('Ahal'), + self::LB => __('Lebap'), + self::BN => __('Balkan'), + self::DZ => __('Dashoguz'), + ]; + } + + /** + * Default region + */ + public static function default(): string + { + return self::AG; + } + + /** + * Label for given region + */ + public static function label(string $region = 'ag'): string + { + return static::values()[$region] ?? ''; + } +} diff --git a/database/migrations/2023_11_23_143516_create_provinces_table.php b/database/migrations/2023_11_23_143516_create_provinces_table.php new file mode 100644 index 0000000..ec21a8c --- /dev/null +++ b/database/migrations/2023_11_23_143516_create_provinces_table.php @@ -0,0 +1,30 @@ +id(); + $table->string('region', 2)->index(); + $table->jsonb('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('provinces'); + } +}; diff --git a/database/migrations/2023_11_23_143517_create_branches_table.php b/database/migrations/2023_11_23_143517_create_branches_table.php new file mode 100644 index 0000000..9ad5f03 --- /dev/null +++ b/database/migrations/2023_11_23_143517_create_branches_table.php @@ -0,0 +1,37 @@ +id(); + $table->jsonb('name'); + $table->jsonb('address')->nullable(); + $table->string('unique_code')->index()->nullable(); + + $table->string('region', 2)->index(); + $table->foreignId('province_id')->constrained()->nullOnDelete(); + + $table->string('billing_username')->nullable(); + $table->string('billing_password')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('branches'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a9f4519..2d2b5bd 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -3,7 +3,9 @@ namespace Database\Seeders; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; +use App\Models\User; use Illuminate\Database\Seeder; +use Spatie\Permission\Models\Role; class DatabaseSeeder extends Seeder { @@ -12,11 +14,50 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // \App\Models\User::factory(10)->create(); + $this->createRoles(); + $this->createAdmins(); + } - // \App\Models\User::factory()->create([ - // 'name' => 'Test User', - // 'email' => 'test@example.com', - // ]); + public function createRoles(): void + { + if (Role::count() > 0) { + return; + } + + $roles = [ + 'king', + 'superadmin', + 'admin', + 'operator', + 'user', + ]; + + foreach ($roles as $role) { + Role::create(['name' => $role]); + } + } + + public function createAdmins(): void + { + $admins = [ + [ + 'name' => 'Nurmuhammet Allanov', + 'email' => 'nurmuhammet@mail.com', + 'password' => '$2y$10$O7LFNdFIT3Rmfeo8tUfbqekB0x0incovkRP6eQuzvb7dVXysQyyBC', + ], [ + 'name' => 'Mahmyt Allaberdiyev', + 'email' => 'mahmyt1206@gmail.com', + 'password' => '$2y$10$O7LFNdFIT3Rmfeo8tUfbqekB0x0incovkRP6eQuzvb7dVXysQyyBC', + ], [ + 'name' => 'Döwran Myratlyýew', + 'email' => 'dovran.m@mail.ru', + 'password' => '$2y$10$EFQaBb.aM2KJRGGtuhjdM.3m4Mtm/vw68NjU2280d2RICDGI.o336', + ], + ]; + + foreach ($admins as $admin) { + $user = User::create($admin); + $user->assignRole('superadmin'); + } } } diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..78bf014 --- /dev/null +++ b/pint.json @@ -0,0 +1,5 @@ +{ + "exclude": [ + "nova" + ] +}