From 0abbc7d768e41ee68e5e9b6f9ac456fd07a205e6 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 17:59:22 +0200 Subject: [PATCH 1/9] Implement migration for channel_shipping_method table with foreign key constraints and default values --- ...0_create_channel_shipping_method_table.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php diff --git a/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php b/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php new file mode 100644 index 0000000..972729d --- /dev/null +++ b/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php @@ -0,0 +1,35 @@ +prefix . 'channel_shipping_method', function (Blueprint $table) { + $table->id(); + $table->foreignIdFor(Channel::class)->constrained( + $this->prefix . 'channels' + ); + $table->foreignIdFor(ShippingMethod::class)->constrained( + $this->prefix . 'shipping_methods' + ); + $table->boolean('visible')->default(true)->index(); + $table->boolean('enabled')->default(true)->index(); + $table->timestamp('starts_at')->nullable()->index(); + $table->timestamp('ends_at')->nullable()->index(); + $table->timestamps(); + + $table->unique(['channel_id', 'shipping_method_id']); + }); + } + + public function down(): void + { + Schema::dropIfExists($this->prefix . 'channel_shipping_method'); + } +}; From 2186cdd7143a8925d7c63941e8ee59dfbd2c30c0 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:08:57 +0200 Subject: [PATCH 2/9] Add channels relationship to shipping methods and update related files --- resources/lang/en/relationmanagers.php | 3 +++ .../Pages/ListShippingMethod.php | 9 ++++++- .../ManageShippingMethodAvailability.php | 6 +++-- src/Models/ShippingMethod.php | 19 +++++++++++++++ src/ShippingServiceProvider.php | 24 +++++++++++++++---- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/resources/lang/en/relationmanagers.php b/resources/lang/en/relationmanagers.php index 0db0284..e3f2098 100644 --- a/resources/lang/en/relationmanagers.php +++ b/resources/lang/en/relationmanagers.php @@ -5,6 +5,9 @@ 'customer_groups' => [ 'description' => "Associate customer groups to this shipping method to determine it's availability.", ], + 'channels' => [ + 'description' => "Associate channels to this shipping method to determine it's availability.", + ], ], 'shipping_rates' => [ 'title_plural' => 'Shipping Rates', diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php index 047617b..b850186 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php @@ -6,6 +6,7 @@ use Filament\Forms\Components\Group; use Lunar\Admin\Support\Pages\BaseListRecords; use Lunar\Models\CustomerGroup; +use Lunar\Models\Channel; use Lunar\Shipping\Filament\Resources\ShippingMethodResource; use Lunar\Shipping\Models\ShippingMethod; @@ -25,9 +26,15 @@ protected function getDefaultHeaderActions(): array ShippingMethodResource::getDescriptionFormComponent(), ])->after(function (ShippingMethod $shippingMethod) { $customerGroups = CustomerGroup::pluck('id')->mapWithKeys( - fn ($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] + fn($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] ); $shippingMethod->customerGroups()->sync($customerGroups); + + // Also sync channels + $channels = Channel::pluck('id')->mapWithKeys( + fn($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] + ); + $shippingMethod->channels()->sync($channels); }), ]; } diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index db2bb40..1132f7c 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -6,17 +6,18 @@ use Filament\Support\Facades\FilamentIcon; use Lunar\Admin\Filament\Resources\ProductResource\RelationManagers\CustomerGroupRelationManager; use Lunar\Admin\Support\Pages\BaseManageRelatedRecords; +use Lunar\Admin\Support\RelationManagers\ChannelRelationManager; use Lunar\Shipping\Filament\Resources\ShippingMethodResource; class ManageShippingMethodAvailability extends BaseManageRelatedRecords { protected static string $resource = ShippingMethodResource::class; - protected static string $relationship = 'customerGroups'; + // We'll use a different relationship approach since we need multiple relationships + protected static ?string $relationship = null; public function getTitle(): string { - return __('lunarpanel.shipping::shippingmethod.pages.availability.label'); } @@ -34,6 +35,7 @@ public function getRelationManagers(): array { return [ RelationGroup::make('Availability', [ + ChannelRelationManager::class, CustomerGroupRelationManager::make([ 'description' => __('lunarpanel.shipping::relationmanagers.shipping_methods.customer_groups.description'), ]), diff --git a/src/Models/ShippingMethod.php b/src/Models/ShippingMethod.php index 5ef58b5..c1b6bea 100644 --- a/src/Models/ShippingMethod.php +++ b/src/Models/ShippingMethod.php @@ -36,6 +36,7 @@ protected static function booted() static::deleting(function (self $shippingMethod) { DB::beginTransaction(); $shippingMethod->customerGroups()->detach(); + $shippingMethod->channels()->detach(); $shippingMethod->shippingRates()->delete(); DB::commit(); }); @@ -76,4 +77,22 @@ public function customerGroups(): BelongsToMany 'ends_at', ])->withTimestamps(); } + + /** + * Return the channels relationship. + */ + public function channels(): BelongsToMany + { + $prefix = config('lunar.database.table_prefix'); + + return $this->belongsToMany( + \Lunar\Models\Channel::class, + "{$prefix}channel_shipping_method" + )->withPivot([ + 'visible', + 'enabled', + 'starts_at', + 'ends_at', + ])->withTimestamps(); + } } diff --git a/src/ShippingServiceProvider.php b/src/ShippingServiceProvider.php index a054879..686dcd7 100644 --- a/src/ShippingServiceProvider.php +++ b/src/ShippingServiceProvider.php @@ -23,7 +23,7 @@ class ShippingServiceProvider extends ServiceProvider { public function register() { - $this->mergeConfigFrom(__DIR__.'/../config/shipping-tables.php', 'lunar.shipping-tables'); + $this->mergeConfigFrom(__DIR__ . '/../config/shipping-tables.php', 'lunar.shipping-tables'); } public function boot(ShippingModifiers $shippingModifiers) @@ -32,13 +32,13 @@ public function boot(ShippingModifiers $shippingModifiers) return; } - $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'lunarpanel.shipping'); + $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'lunarpanel.shipping'); if (! config('lunar.database.disable_migrations', false)) { - $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); } - $this->loadViewsFrom(__DIR__.'/../resources/views', 'shipping'); + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'shipping'); $shippingModifiers->add( ShippingModifier::class, @@ -64,6 +64,20 @@ public function boot(ShippingModifiers $shippingModifiers) )->withTimestamps(); }); + \Lunar\Models\Channel::resolveRelationUsing('shippingMethods', function ($channel) { + $prefix = config('lunar.database.table_prefix'); + + return $channel->belongsToMany( + ShippingMethod::class, + "{$prefix}channel_shipping_method" + )->withPivot([ + 'visible', + 'enabled', + 'starts_at', + 'ends_at', + ])->withTimestamps(); + }); + Product::resolveRelationUsing('shippingExclusions', function ($product) { return $product->morphMany(ShippingExclusion::class, 'purchasable'); }); @@ -73,7 +87,7 @@ public function boot(ShippingModifiers $shippingModifiers) }); ModelManifest::addDirectory( - __DIR__.'/Models' + __DIR__ . '/Models' ); Relation::morphMap([ From 99303714ae349caf32d0d1b11004354a67848660 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:16:25 +0200 Subject: [PATCH 3/9] WIP --- .../Pages/ManageShippingMethodAvailability.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index 1132f7c..48ad857 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -14,7 +14,7 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords protected static string $resource = ShippingMethodResource::class; // We'll use a different relationship approach since we need multiple relationships - protected static ?string $relationship = null; + protected static ?string $relationship = ''; public function getTitle(): string { From e3755a0e0d7f57fb04ac5828db39a102d90d6458 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:18:34 +0200 Subject: [PATCH 4/9] WIP --- .../Pages/ManageShippingMethodAvailability.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index 48ad857..4cafea1 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -13,8 +13,7 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords { protected static string $resource = ShippingMethodResource::class; - // We'll use a different relationship approach since we need multiple relationships - protected static ?string $relationship = ''; + protected static ?string $relationship = 'channels'; public function getTitle(): string { From f6b2d23142d426e06e99b839ba7054adc662e79a Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:19:52 +0200 Subject: [PATCH 5/9] WIP --- .../Pages/ManageShippingMethodAvailability.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index 4cafea1..dfdbb10 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -13,7 +13,7 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords { protected static string $resource = ShippingMethodResource::class; - protected static ?string $relationship = 'channels'; + protected static string $relationship = 'channels'; public function getTitle(): string { From a3a69dc22a30dfacd90228ad99a9b43d6b013ad8 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:32:33 +0200 Subject: [PATCH 6/9] Remove 'visible' field from channel_shipping_method migration and related models, updating sync logic in ListShippingMethod. --- .../2024_07_29_100000_create_channel_shipping_method_table.php | 1 - .../ShippingMethodResource/Pages/ListShippingMethod.php | 3 +-- src/Models/ShippingMethod.php | 1 - src/ShippingServiceProvider.php | 1 - 4 files changed, 1 insertion(+), 5 deletions(-) diff --git a/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php b/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php index 972729d..b116c22 100644 --- a/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php +++ b/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php @@ -18,7 +18,6 @@ public function up(): void $table->foreignIdFor(ShippingMethod::class)->constrained( $this->prefix . 'shipping_methods' ); - $table->boolean('visible')->default(true)->index(); $table->boolean('enabled')->default(true)->index(); $table->timestamp('starts_at')->nullable()->index(); $table->timestamp('ends_at')->nullable()->index(); diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php index b850186..e64f1e0 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php @@ -30,9 +30,8 @@ protected function getDefaultHeaderActions(): array ); $shippingMethod->customerGroups()->sync($customerGroups); - // Also sync channels $channels = Channel::pluck('id')->mapWithKeys( - fn($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] + fn($id) => [$id => ['enabled' => true, 'starts_at' => now()]] ); $shippingMethod->channels()->sync($channels); }), diff --git a/src/Models/ShippingMethod.php b/src/Models/ShippingMethod.php index c1b6bea..dc809ca 100644 --- a/src/Models/ShippingMethod.php +++ b/src/Models/ShippingMethod.php @@ -89,7 +89,6 @@ public function channels(): BelongsToMany \Lunar\Models\Channel::class, "{$prefix}channel_shipping_method" )->withPivot([ - 'visible', 'enabled', 'starts_at', 'ends_at', diff --git a/src/ShippingServiceProvider.php b/src/ShippingServiceProvider.php index 686dcd7..ec55e96 100644 --- a/src/ShippingServiceProvider.php +++ b/src/ShippingServiceProvider.php @@ -71,7 +71,6 @@ public function boot(ShippingModifiers $shippingModifiers) ShippingMethod::class, "{$prefix}channel_shipping_method" )->withPivot([ - 'visible', 'enabled', 'starts_at', 'ends_at', From 2baff5b233f331ff4e78bae3e00486f270d5c589 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 19:39:16 +0200 Subject: [PATCH 7/9] update date in migration --- ...=> 2025_04_11_100000_create_channel_shipping_method_table.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename database/migrations/{2024_07_29_100000_create_channel_shipping_method_table.php => 2025_04_11_100000_create_channel_shipping_method_table.php} (100%) diff --git a/database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php b/database/migrations/2025_04_11_100000_create_channel_shipping_method_table.php similarity index 100% rename from database/migrations/2024_07_29_100000_create_channel_shipping_method_table.php rename to database/migrations/2025_04_11_100000_create_channel_shipping_method_table.php From 41f0965d3b7336e728bad8693ff85f4bbacbfb4a Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 21:11:19 +0200 Subject: [PATCH 8/9] Update shipping method availability to use customer groups instead of channels and enhance relation manager descriptions --- .../Pages/ManageShippingMethodAvailability.php | 6 ++++-- src/Models/ShippingMethod.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index dfdbb10..7ec075c 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -13,7 +13,7 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords { protected static string $resource = ShippingMethodResource::class; - protected static string $relationship = 'channels'; + protected static string $relationship = 'customerGroups'; public function getTitle(): string { @@ -34,7 +34,9 @@ public function getRelationManagers(): array { return [ RelationGroup::make('Availability', [ - ChannelRelationManager::class, + ChannelRelationManager::make([ + 'description' => __('lunarpanel.shipping::relationmanagers.shipping_methods.channels.description'), + ]), CustomerGroupRelationManager::make([ 'description' => __('lunarpanel.shipping::relationmanagers.shipping_methods.customer_groups.description'), ]), diff --git a/src/Models/ShippingMethod.php b/src/Models/ShippingMethod.php index dc809ca..51182f4 100644 --- a/src/Models/ShippingMethod.php +++ b/src/Models/ShippingMethod.php @@ -86,7 +86,7 @@ public function channels(): BelongsToMany $prefix = config('lunar.database.table_prefix'); return $this->belongsToMany( - \Lunar\Models\Channel::class, + Channel::class, "{$prefix}channel_shipping_method" )->withPivot([ 'enabled', From 55d1448b10b25dbc024cd940ec51e23670f44919 Mon Sep 17 00:00:00 2001 From: Michal Urva Date: Fri, 11 Apr 2025 21:16:58 +0200 Subject: [PATCH 9/9] Add Channel model import to ShippingMethod --- src/Models/ShippingMethod.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Models/ShippingMethod.php b/src/Models/ShippingMethod.php index 51182f4..221036b 100644 --- a/src/Models/ShippingMethod.php +++ b/src/Models/ShippingMethod.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\DB; use Lunar\Base\BaseModel; use Lunar\Base\Traits\HasCustomerGroups; +use Lunar\Models\Channel; use Lunar\Models\CustomerGroup; use Lunar\Shipping\Database\Factories\ShippingMethodFactory; use Lunar\Shipping\Facades\Shipping;