Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('catalogue_product_data', function (Blueprint $table) {
$table->foreignId('tax_class_id')
->nullable()
->after('product_status_id')
->constrained('pim_tax_classes')
->nullOnDelete()
->cascadeOnUpdate();
});
}

public function down(): void
{
Schema::table('catalogue_product_data', function (Blueprint $table) {
$table->dropForeign(['tax_class_id']);
$table->dropColumn('tax_class_id');
});
}
};
54 changes: 54 additions & 0 deletions src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ public static function form(Form $form): Form
->searchable()
->preload(),

Select::make("tenant_data.{$tenantId}.tax_class_id")
->label('Tax class')
->options(function () use ($tenantId) {
$query = \Eclipse\Catalogue\Models\TaxClass::query();
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key', 'site_id');
if ($tenantFK) {
$query->where($tenantFK, $tenantId);
}

return $query->orderBy('name')->pluck('name', 'id')->toArray();
})
->searchable()
->preload()
->placeholder('Select tax class'),

Select::make("tenant_data.{$tenantId}.groups")
->label('Groups')
->multiple()
Expand Down Expand Up @@ -677,6 +692,18 @@ public static function table(Table $table): Table
return is_array($category->name) ? ($category->name[app()->getLocale()] ?? reset($category->name)) : $category->name;
}),

TextColumn::make('tax_class')
->label('Tax class')
->toggleable(isToggledHiddenByDefault: true)
->getStateUsing(function (Product $record) {
$taxClass = $record->currentTenantData()?->taxClass;
if (! $taxClass) {
return null;
}

return $taxClass->name;
}),

TextColumn::make('type.name')
->label(__('eclipse-catalogue::product.table.columns.type')),

Expand Down Expand Up @@ -784,6 +811,33 @@ public static function table(Table $table): Table
$q->whereIn('product_status_id', (array) $selected);
});
}),
SelectFilter::make('tax_class_id')
->label('Tax class')
->multiple()
->options(function () {
$query = \Eclipse\Catalogue\Models\TaxClass::query();
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
$currentTenant = \Filament\Facades\Filament::getTenant();
if ($tenantFK && $currentTenant) {
$query->where($tenantFK, $currentTenant->id);
}

return $query->orderBy('name')->pluck('name', 'id')->toArray();
})
->query(function (Builder $query, array $data) {
$selected = $data['values'] ?? ($data['value'] ?? null);
if (empty($selected)) {
return;
}
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
$currentTenant = \Filament\Facades\Filament::getTenant();
$query->whereHas('productData', function ($q) use ($selected, $tenantFK, $currentTenant) {
if ($tenantFK && $currentTenant) {
$q->where($tenantFK, $currentTenant->id);
}
$q->whereIn('tax_class_id', (array) $selected);
});
}),
SelectFilter::make('category_id')
->label('Categories')
->multiple()
Expand Down
2 changes: 2 additions & 0 deletions src/Filament/Resources/ProductResource/Pages/EditProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function mutateFormDataBeforeFill(array $data): array
$data['sorting_label'] = $recordData->sorting_label;
$data['category_id'] = $recordData->category_id ?? null;
$data['product_status_id'] = $recordData->product_status_id ?? null;
$data['tax_class_id'] = $recordData->tax_class_id ?? null;
}

$data['groups'] = $this->record->groups()->pluck('pim_group.id')->toArray();
Expand All @@ -108,6 +109,7 @@ protected function mutateFormDataBeforeFill(array $data): array
'sorting_label' => $tenantRecord->sorting_label,
'category_id' => $tenantRecord->category_id ?? null,
'product_status_id' => $tenantRecord->product_status_id ?? null,
'tax_class_id' => $tenantRecord->tax_class_id ?? null,
'groups' => $this->record->groups()
->where('pim_group.'.config('eclipse-catalogue.tenancy.foreign_key', 'site_id'), $tenantId)
->pluck('pim_group.id')
Expand Down
93 changes: 48 additions & 45 deletions src/Filament/Resources/TaxClassResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class TaxClassResource extends Resource implements HasShieldPermissions

protected static ?string $recordTitleAttribute = 'name';

protected static bool $isScopedToTenant = true;

protected static ?string $tenantOwnershipRelationshipName = 'tenant';

public static function getModelLabel(): string
{
return __('eclipse-catalogue::tax-class.singular');
Expand All @@ -53,51 +49,58 @@ public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label(__('eclipse-catalogue::tax-class.fields.name'))
->required()
->maxLength(255)
->unique(
table: 'pim_tax_classes',
column: 'name',
ignoreRecord: true,
modifyRuleUsing: function ($rule) {
// Add tenant scope to unique validation
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
$tenantId = Filament::getTenant()?->id;
if ($tenantFK && $tenantId) {
$rule->where($tenantFK, $tenantId);
}

return $rule;
}
),
\Filament\Forms\Components\Grid::make(2)
->schema([
TextInput::make('name')
->label(__('eclipse-catalogue::tax-class.fields.name'))
->required()
->maxLength(255)
->unique(
table: 'pim_tax_classes',
column: 'name',
ignoreRecord: true,
modifyRuleUsing: function ($rule) {
// Add tenant scope to unique validation
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
$tenantId = Filament::getTenant()?->id;
if ($tenantFK && $tenantId) {
$rule->where($tenantFK, $tenantId);
}

return $rule;
}
),

TextInput::make('rate')
->label(__('eclipse-catalogue::tax-class.fields.rate'))
->required()
->numeric()
->minValue(0)
->maxValue(100)
->step(0.01)
->suffix('%'),
]),

Textarea::make('description')
->label(__('eclipse-catalogue::tax-class.fields.description'))
->rows(3)
->maxLength(65535),

TextInput::make('rate')
->label(__('eclipse-catalogue::tax-class.fields.rate'))
->required()
->numeric()
->minValue(0)
->maxValue(100)
->step(0.01)
->suffix('%'),

Toggle::make('is_default')
->label(__('eclipse-catalogue::tax-class.fields.is_default'))
->helperText(__('eclipse-catalogue::tax-class.messages.default_class_help')),

Placeholder::make('created_at')
->label('Created Date')
->content(fn (?TaxClass $record): string => $record?->created_at?->diffForHumans() ?? '-'),

Placeholder::make('updated_at')
->label('Last Modified Date')
->content(fn (?TaxClass $record): string => $record?->updated_at?->diffForHumans() ?? '-'),
->maxLength(65535)
->columnSpanFull(),

\Filament\Forms\Components\Grid::make(3)
->schema([
Toggle::make('is_default')
->label(__('eclipse-catalogue::tax-class.fields.is_default'))
->helperText(__('eclipse-catalogue::tax-class.messages.default_class_help')),

Placeholder::make('created_at')
->label('Created Date')
->content(fn (?TaxClass $record): string => $record?->created_at?->diffForHumans() ?? '-'),

Placeholder::make('updated_at')
->label('Last Modified Date')
->content(fn (?TaxClass $record): string => $record?->updated_at?->diffForHumans() ?? '-'),
]),
]);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Product extends Model implements HasMedia
'available_from_date',
'category_id',
'product_status_id',
'tax_class_id',
];

public function status(): ?ProductStatus
Expand All @@ -97,6 +98,11 @@ public function category(): ?Category
return $this->currentTenantData()?->category;
}

public function taxClass(): ?\Eclipse\Catalogue\Models\TaxClass
{
return $this->currentTenantData()?->taxClass;
}

public function type(): BelongsTo
{
return $this->belongsTo(ProductType::class, 'product_type_id');
Expand Down
7 changes: 7 additions & 0 deletions src/Models/ProductData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProductData extends Model
'product_id',
'category_id',
'product_status_id',
'tax_class_id',
'sorting_label',
'is_active',
'available_from_date',
Expand Down Expand Up @@ -54,6 +55,12 @@ public function status(): BelongsTo
return $this->belongsTo(ProductStatus::class, 'product_status_id');
}

/** @return BelongsTo<\Eclipse\Catalogue\Models\TaxClass, self> */
public function taxClass(): BelongsTo
{
return $this->belongsTo(\Eclipse\Catalogue\Models\TaxClass::class, 'tax_class_id');
}

/** @return BelongsTo<\Eclipse\Core\Models\Site, self> */
public function site(): BelongsTo
{
Expand Down
17 changes: 10 additions & 7 deletions src/Models/TaxClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\ValidationException;
use RuntimeException;

class TaxClass extends Model
{
Expand Down Expand Up @@ -43,19 +42,23 @@ protected static function boot()
{
parent::boot();

static::creating(function (self $category): void {
static::creating(function (self $model): void {
// Set tenant foreign key, if configured
$tenantModel = config('eclipse-catalogue.tenancy.model');
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');

if ($tenantModel && $tenantFK) {
$tenant = Filament::getTenant();

if (empty($tenant)) {
throw new RuntimeException('Tenancy is enabled, but no tenant is set');
if ($tenant) {
$model->{$tenantFK} = $tenant->id;
} else {
// Set default tenant ID when no Filament tenant is available
$defaultTenant = $tenantModel::first();
if ($defaultTenant) {
$model->{$tenantFK} = $defaultTenant->id;
}
}

$category->{$tenantFK} = $tenant->id;
}
});

Expand Down Expand Up @@ -95,7 +98,7 @@ public static function getDefault(?int $tenantId = null): ?self

// Add tenant scope if tenancy is configured
$tenantFK = config('eclipse-catalogue.tenancy.foreign_key');
$currentTenantId = $tenantId ?: \Filament\Facades\Filament::getTenant()?->id;
$currentTenantId = $tenantId ?: Filament::getTenant()?->id;
if ($tenantFK && $currentTenantId) {
$query->where($tenantFK, $currentTenantId);
}
Expand Down
Loading