|
13 | 13 | use Eclipse\Catalogue\Traits\HandlesTenantData; |
14 | 14 | use Eclipse\Catalogue\Traits\HasTenantFields; |
15 | 15 | use Eclipse\World\Models\Country; |
| 16 | +use Eclipse\World\Models\TariffCode; |
16 | 17 | use Filament\Forms\Components\CheckboxList; |
17 | 18 | use Filament\Forms\Components\Placeholder; |
18 | 19 | use Filament\Forms\Components\Radio; |
@@ -136,6 +137,24 @@ public static function form(Form $form): Form |
136 | 137 | ->searchable(['id', 'name']) |
137 | 138 | ->preload() |
138 | 139 | ->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')), |
139 | 158 | ]) |
140 | 159 | ->collapsible() |
141 | 160 | ->persistCollapsed(), |
@@ -182,7 +201,6 @@ public static function form(Form $form): Form |
182 | 201 | ->options(function () use ($tenantId) { |
183 | 202 | return Group::query() |
184 | 203 | ->where(config('eclipse-catalogue.tenancy.foreign_key', 'site_id'), $tenantId) |
185 | | - ->where('is_active', true) |
186 | 204 | ->orderBy('name') |
187 | 205 | ->pluck('name', 'id') |
188 | 206 | ->toArray(); |
@@ -498,6 +516,27 @@ public static function table(Table $table): Table |
498 | 516 | TextColumn::make('originCountry.name') |
499 | 517 | ->label(__('eclipse-catalogue::product.fields.origin_country_id')), |
500 | 518 |
|
| 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 | + |
501 | 540 | TextColumn::make('short_description') |
502 | 541 | ->words(5), |
503 | 542 |
|
@@ -565,18 +604,39 @@ public static function table(Table $table): Table |
565 | 604 | ->label(__('eclipse-catalogue::product.fields.origin_country_id')) |
566 | 605 | ->multiple() |
567 | 606 | ->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(), |
568 | 629 | SelectFilter::make('groups') |
569 | 630 | ->label('Groups') |
570 | 631 | ->multiple() |
571 | 632 | ->relationship('groups', 'name', function ($query) { |
572 | 633 | $currentTenant = \Filament\Facades\Filament::getTenant(); |
573 | 634 | $tenantFK = config('eclipse-catalogue.tenancy.foreign_key', 'site_id'); |
574 | 635 | if ($currentTenant) { |
575 | | - return $query->where($tenantFK, $currentTenant->id) |
576 | | - ->where('is_active', true); |
| 636 | + return $query->where($tenantFK, $currentTenant->id); |
577 | 637 | } |
578 | 638 |
|
579 | | - return $query->where('is_active', true); |
| 639 | + return $query; |
580 | 640 | }), |
581 | 641 | TernaryFilter::make('is_active') |
582 | 642 | ->label(__('eclipse-catalogue::product.table.columns.is_active')) |
@@ -707,14 +767,22 @@ public static function getGloballySearchableAttributes(): array |
707 | 767 | 'name', |
708 | 768 | 'short_description', |
709 | 769 | 'description', |
| 770 | + 'tariffCode.code', |
| 771 | + 'tariffCode.name', |
710 | 772 | ]; |
711 | 773 | } |
712 | 774 |
|
713 | 775 | public static function getGlobalSearchResultDetails(Model $record): array |
714 | 776 | { |
715 | | - return array_filter([ |
| 777 | + $details = [ |
716 | 778 | 'Code' => $record->code, |
717 | | - ]); |
| 779 | + ]; |
| 780 | + |
| 781 | + if ($record->tariffCode) { |
| 782 | + $details['Tariff Code'] = $record->tariffCode->code; |
| 783 | + } |
| 784 | + |
| 785 | + return array_filter($details); |
718 | 786 | } |
719 | 787 |
|
720 | 788 | protected static function getPlaceholderImageUrl(): string |
|
0 commit comments