Skip to content

Commit 69b1f5c

Browse files
committed
Merge branch 'main' into fork/KilianTrunk/feat/product_prices
# Conflicts: # src/Models/Product.php
2 parents 6c991b1 + f298846 commit 69b1f5c

9 files changed

Lines changed: 151 additions & 28 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::table('catalogue_products', function (Blueprint $table) {
12+
$table->foreignId('tariff_code_id')->nullable()->after('origin_country_id')->constrained('world_tariff_codes')->cascadeOnUpdate()->cascadeOnDelete();
13+
});
14+
}
15+
16+
public function down(): void
17+
{
18+
Schema::table('catalogue_products', function (Blueprint $table) {
19+
$table->dropForeign(['tariff_code_id']);
20+
$table->dropColumn('tariff_code_id');
21+
});
22+
}
23+
};

resources/lang/en/product.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'fields' => [
88
'product_type' => 'Product Type',
99
'origin_country_id' => 'Country of Origin',
10+
'tariff_code_id' => 'Tariff code (CN)',
1011
'meta_title' => 'Meta Title',
1112
'meta_description' => 'Meta Description',
1213
'is_active' => 'Active',
@@ -19,6 +20,7 @@
1920
'placeholders' => [
2021
'product_type' => 'Select product type (optional)',
2122
'origin_country_id' => 'Select country of origin',
23+
'tariff_code_id' => 'Select tariff code (CN)',
2224
'meta_title' => 'SEO meta title',
2325
'meta_description' => 'SEO meta description',
2426
'category_id' => 'Select category (optional)',

resources/lang/sl/product.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'fields' => [
88
'product_type' => 'Tip proizvoda',
99
'origin_country_id' => 'Država izvora',
10+
'tariff_code_id' => 'Carinska oznaka (CN)',
1011
'meta_title' => 'Meta naslov',
1112
'meta_description' => 'Meta opis',
1213
'is_active' => 'Aktiven',
@@ -19,6 +20,7 @@
1920
'placeholders' => [
2021
'product_type' => 'Izberi tip proizvoda (neobvezno)',
2122
'origin_country_id' => 'Izberi državo izvora',
23+
'tariff_code_id' => 'Izberi carinsko oznako (CN)',
2224
'meta_title' => 'SEO meta naslov',
2325
'meta_description' => 'SEO meta opis',
2426
'category_id' => 'Izberi kategorijo (neobvezno)',

src/Factories/GroupFactory.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ class GroupFactory extends Factory
1111

1212
public function definition(): array
1313
{
14-
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key', 'site_id');
15-
16-
return [
17-
$tenantFK => null, // Will be set when creating
14+
$attributes = [
1815
'code' => $this->faker->unique()->slug(2),
1916
'name' => $this->faker->words(2, true),
2017
'is_active' => true,
2118
'is_browsable' => false,
2219
];
20+
21+
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
22+
23+
// Only include the tenant foreign key if it's a non-empty string
24+
if (! empty($tenantFK)) {
25+
$attributes[$tenantFK] = null; // Will be set when creating
26+
}
27+
28+
return $attributes;
2329
}
2430

2531
public function inactive(): static

src/Filament/Resources/GroupResource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
66
use Eclipse\Catalogue\Filament\Resources\GroupResource\Pages;
77
use Eclipse\Catalogue\Models\Group;
8+
use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
89
use Filament\Forms\Components\Section;
910
use Filament\Forms\Components\TextInput;
1011
use Filament\Forms\Components\Toggle;
@@ -154,7 +155,8 @@ public static function getRelations(): array
154155

155156
public static function getEloquentQuery(): Builder
156157
{
157-
$query = parent::getEloquentQuery();
158+
$query = parent::getEloquentQuery()
159+
->withoutGlobalScope(ActiveScope::class);
158160

159161
$currentTenant = \Filament\Facades\Filament::getTenant();
160162
if ($currentTenant) {

src/Filament/Resources/ProductResource.php

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Eclipse\Catalogue\Traits\HandlesTenantData;
1414
use Eclipse\Catalogue\Traits\HasTenantFields;
1515
use Eclipse\World\Models\Country;
16+
use Eclipse\World\Models\TariffCode;
1617
use Filament\Forms\Components\CheckboxList;
1718
use Filament\Forms\Components\Placeholder;
1819
use Filament\Forms\Components\Radio;
@@ -136,6 +137,24 @@ public static function form(Form $form): Form
136137
->searchable(['id', 'name'])
137138
->preload()
138139
->placeholder(__('eclipse-catalogue::product.placeholders.origin_country_id')),
140+
141+
Select::make('tariff_code_id')
142+
->label(__('eclipse-catalogue::product.fields.tariff_code_id'))
143+
->relationship('tariffCode', 'code', function ($query) {
144+
return $query->whereRaw('LENGTH(code) = 8');
145+
})
146+
->getOptionLabelFromRecordUsing(function (TariffCode $record) {
147+
$name = $record->name;
148+
if (is_array($name)) {
149+
$locale = app()->getLocale();
150+
$name = $name[$locale] ?? reset($name);
151+
}
152+
153+
return $record->code.''.$name;
154+
})
155+
->searchable(['code', 'name'])
156+
->preload()
157+
->placeholder(__('eclipse-catalogue::product.placeholders.tariff_code_id')),
139158
])
140159
->collapsible()
141160
->persistCollapsed(),
@@ -182,7 +201,6 @@ public static function form(Form $form): Form
182201
->options(function () use ($tenantId) {
183202
return Group::query()
184203
->where(config('eclipse-catalogue.tenancy.foreign_key', 'site_id'), $tenantId)
185-
->where('is_active', true)
186204
->orderBy('name')
187205
->pluck('name', 'id')
188206
->toArray();
@@ -498,6 +516,27 @@ public static function table(Table $table): Table
498516
TextColumn::make('originCountry.name')
499517
->label(__('eclipse-catalogue::product.fields.origin_country_id')),
500518

519+
TextColumn::make('tariffCode.code')
520+
->label(__('eclipse-catalogue::product.fields.tariff_code_id'))
521+
->getStateUsing(function (Product $record) {
522+
$tariffCode = $record->tariffCode;
523+
if (! $tariffCode) {
524+
return null;
525+
}
526+
527+
$name = $tariffCode->name;
528+
if (is_array($name)) {
529+
$locale = app()->getLocale();
530+
$name = $name[$locale] ?? reset($name);
531+
}
532+
533+
return $tariffCode->code.''.$name;
534+
})
535+
->toggleable()
536+
->toggledHiddenByDefault()
537+
->searchable()
538+
->copyable(),
539+
501540
TextColumn::make('short_description')
502541
->words(5),
503542

@@ -565,18 +604,39 @@ public static function table(Table $table): Table
565604
->label(__('eclipse-catalogue::product.fields.origin_country_id'))
566605
->multiple()
567606
->options(fn () => Country::query()->orderBy('name')->pluck('name', 'id')->toArray()),
607+
608+
SelectFilter::make('tariff_code_id')
609+
->label(__('eclipse-catalogue::product.fields.tariff_code_id'))
610+
->multiple()
611+
->options(function () {
612+
return TariffCode::query()
613+
->whereRaw('LENGTH(code) = 8')
614+
->orderBy('code')
615+
->get()
616+
->mapWithKeys(function ($tariffCode) {
617+
$name = $tariffCode->name;
618+
if (is_array($name)) {
619+
$locale = app()->getLocale();
620+
$name = $name[$locale] ?? reset($name);
621+
}
622+
623+
return [$tariffCode->id => $tariffCode->code.''.$name];
624+
})
625+
->toArray();
626+
})
627+
->searchable()
628+
->preload(),
568629
SelectFilter::make('groups')
569630
->label('Groups')
570631
->multiple()
571632
->relationship('groups', 'name', function ($query) {
572633
$currentTenant = \Filament\Facades\Filament::getTenant();
573634
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key', 'site_id');
574635
if ($currentTenant) {
575-
return $query->where($tenantFK, $currentTenant->id)
576-
->where('is_active', true);
636+
return $query->where($tenantFK, $currentTenant->id);
577637
}
578638

579-
return $query->where('is_active', true);
639+
return $query;
580640
}),
581641
TernaryFilter::make('is_active')
582642
->label(__('eclipse-catalogue::product.table.columns.is_active'))
@@ -707,14 +767,22 @@ public static function getGloballySearchableAttributes(): array
707767
'name',
708768
'short_description',
709769
'description',
770+
'tariffCode.code',
771+
'tariffCode.name',
710772
];
711773
}
712774

713775
public static function getGlobalSearchResultDetails(Model $record): array
714776
{
715-
return array_filter([
777+
$details = [
716778
'Code' => $record->code,
717-
]);
779+
];
780+
781+
if ($record->tariffCode) {
782+
$details['Tariff Code'] = $record->tariffCode->code;
783+
}
784+
785+
return array_filter($details);
718786
}
719787

720788
protected static function getPlaceholderImageUrl(): string

src/Models/Group.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Eclipse\Catalogue\Models;
44

55
use Eclipse\Catalogue\Factories\GroupFactory;
6+
use Eclipse\Common\Foundation\Models\Scopes\ActiveScope;
7+
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
68
use Illuminate\Database\Eloquent\Builder;
79
use Illuminate\Database\Eloquent\Factories\HasFactory;
810
use Illuminate\Database\Eloquent\Model;
911
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1012
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1113
use Illuminate\Support\Facades\DB;
1214

15+
#[ScopedBy(ActiveScope::class)]
1316
class Group extends Model
1417
{
1518
use HasFactory;
@@ -28,14 +31,6 @@ class Group extends Model
2831
'is_browsable' => 'boolean',
2932
];
3033

31-
/**
32-
* Scope: only active groups.
33-
*/
34-
public function scopeActive(Builder $query): Builder
35-
{
36-
return $query->where('is_active', true);
37-
}
38-
3934
/**
4035
* Scope: restrict by current tenant if tenancy is enabled and a tenant is selected.
4136
*/

src/Models/Product.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Product extends Model implements HasMedia
3636
'short_description',
3737
'description',
3838
'origin_country_id',
39+
'tariff_code_id',
3940
'meta_description',
4041
'meta_title',
4142
];
@@ -55,7 +56,6 @@ class Product extends Model implements HasMedia
5556
'meta_title' => 'array',
5657
'meta_description' => 'array',
5758
'deleted_at' => 'datetime',
58-
'category_id' => 'integer',
5959
'product_type_id' => 'integer',
6060
'available_from_date' => 'datetime',
6161
'is_active' => 'boolean',
@@ -102,6 +102,19 @@ public function originCountry(): BelongsTo
102102
return $this->belongsTo(Country::class, 'origin_country_id', 'id');
103103
}
104104

105+
public function tariffCode(): BelongsTo
106+
{
107+
return $this->belongsTo(\Eclipse\World\Models\TariffCode::class, 'tariff_code_id');
108+
}
109+
110+
/**
111+
* Get all per-tenant data rows for this product.
112+
*/
113+
public function productData(): HasMany
114+
{
115+
return $this->hasMany(ProductData::class, 'product_id');
116+
}
117+
105118
public function groups(): BelongsToMany
106119
{
107120
return $this->belongsToMany(Group::class, 'pim_group_has_product', 'product_id', 'group_id')
@@ -118,14 +131,6 @@ public function getHasFreeDeliveryAttribute(): bool
118131
return $this->getTenantFlagValue('has_free_delivery');
119132
}
120133

121-
/**
122-
* Get all per-tenant data rows for this product.
123-
*/
124-
public function productData(): HasMany
125-
{
126-
return $this->hasMany(ProductData::class, 'product_id');
127-
}
128-
129134
/**
130135
* Prices relationship.
131136
*/
@@ -177,6 +182,17 @@ public function getCoverImageAttribute()
177182
?? $this->getFirstMedia('images');
178183
}
179184

185+
public function toSearchableArray(): array
186+
{
187+
$data = $this->createSearchableArray();
188+
189+
if ($this->tariffCode) {
190+
$data['tariff_code'] = $this->tariffCode->code;
191+
}
192+
193+
return $data;
194+
}
195+
180196
public static function getTypesenseSettings(): array
181197
{
182198
return [
@@ -220,6 +236,11 @@ public static function getTypesenseSettings(): array
220236
'type' => 'int32',
221237
'optional' => true,
222238
],
239+
[
240+
'name' => 'tariff_code',
241+
'type' => 'string',
242+
'optional' => true,
243+
],
223244
],
224245
],
225246
'search-parameters' => [
@@ -229,6 +250,7 @@ public static function getTypesenseSettings(): array
229250
'name_*',
230251
'short_description_*',
231252
'description_*',
253+
'tariff_code',
232254
]),
233255
],
234256
];

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ protected function setUp(): void
2424
ini_set('display_errors', 1);
2525
error_reporting(E_ALL);
2626

27+
// Increase memory limit to 512M
28+
ini_set('memory_limit', '512M');
29+
2730
parent::setUp();
2831

2932
// Disable Scout during tests to prevent indexing operations

0 commit comments

Comments
 (0)