Version 2.0

This commit is contained in:
2026-05-12 09:23:05 +00:00
parent f2bc048219
commit f1b68e5e78
34 changed files with 564 additions and 283 deletions

View File

@@ -2,7 +2,7 @@
namespace App\Filament\Resources\Feedback\Schemas;
use App\Models\CustomerProduct;
use App\Models\Contract;
use App\Models\Department;
use Filament\Forms\Components\ColorPicker;
use Filament\Forms\Components\FileUpload;
@@ -25,6 +25,7 @@ class FeedbackForm
->icon('heroicon-o-user-group')
->schema([
Select::make('customer_id')
->label(__('app.widget_customer'))
->relationship('customer', 'name')
->searchable()
->required()
@@ -37,48 +38,51 @@ class FeedbackForm
->live()
->columnSpan(1),
Select::make('customer_product_id')
Select::make('product_id_virtual')
->label(__('app.product'))
->searchable()
->options(fn ($get): array => self::getProductOptionsForCustomer($get('customer_id')))
->getSearchResultsUsing(function (string $search, $get): array {
$customerId = $get('customer_id');
if (! $customerId) {
return [];
}
$keyword = mb_strtolower($search);
return CustomerProduct::where('customer_id', $customerId)
->whereHas('product', fn ($q) => $q->whereRaw('LOWER(name) LIKE ?', ["%{$keyword}%"]))
->with('product')
$allIds = self::getProductIdsForCustomer($customerId);
if ($allIds->isEmpty()) {
return [];
}
return \App\Models\Product::whereIn('id', $allIds)
->whereRaw('LOWER(name) LIKE ?', ["%{$keyword}%"])
->limit(50)
->get()
->pluck('product.name', 'id')
->pluck('name', 'id')
->toArray();
})
->getOptionLabelUsing(fn ($value): ?string =>
CustomerProduct::find($value)?->product?->name
\App\Models\Product::find($value)?->name
)
->visible(fn ($get): bool => ! $get('is_general'))
->nullable()
->live(),
->live()
->columnSpan(1),
Select::make('contract_id')
->label(__('app.contract'))
->visible(fn ($get): bool => ! $get('is_general') && filled($get('customer_product_id')))
->visible(fn ($get): bool => ! $get('is_general') && filled($get('product_id_virtual')))
->options(function ($get): array {
$customerProductId = $get('customer_product_id');
if (! $customerProductId) {
$productId = $get('product_id_virtual');
if (! $productId) {
return [];
}
$cp = \App\Models\CustomerProduct::find($customerProductId);
if (! $cp) {
return [];
}
return \App\Models\Contract::where('product_id', $cp->product_id)
return Contract::where('product_id', $productId)
->where('status', 'active')
->with('customer')
->get()
->mapWithKeys(fn ($c) => [
$c->id => sprintf('%s (%s) - %s',
$c->id => sprintf('%s — %s (%s)',
\App\Enums\ContractType::from($c->type)->label(),
$c->customer->name,
$c->start_date?->format('d/m/Y') ?? 'N/A',
@@ -90,15 +94,11 @@ class FeedbackForm
if (filled($state)) {
return;
}
$customerProductId = $get('customer_product_id');
if (! $customerProductId) {
$productId = $get('product_id_virtual');
if (! $productId) {
return;
}
$cp = \App\Models\CustomerProduct::find($customerProductId);
if (! $cp) {
return;
}
$activeContract = \App\Models\Contract::where('product_id', $cp->product_id)
$activeContract = Contract::where('product_id', $productId)
->where('status', 'active')
->first();
if ($activeContract) {
@@ -115,17 +115,20 @@ class FeedbackForm
->icon('heroicon-o-chat-bubble-left-right')
->schema([
Select::make('feedback_channel_id')
->label(__('app.channel'))
->relationship('feedbackChannel', 'name')
->required()
->preload()
->columnSpan(1),
TextInput::make('title')
->label(__('app.blade_title'))
->required()
->maxLength(255)
->columnSpan(1),
Textarea::make('content')
->label(__('app.description'))
->required()
->rows(6)
->columnSpanFull(),
@@ -136,9 +139,9 @@ class FeedbackForm
->preload()
->columnSpanFull()
->createOptionForm([
TextInput::make('name')->required(),
TextInput::make('slug')->required(),
ColorPicker::make('color')->default('#3b82f6'),
TextInput::make('name')->label(__('app.name'))->required(),
TextInput::make('slug')->label(__('app.slug'))->required(),
ColorPicker::make('color')->label(__('app.color'))->default('#3b82f6'),
]),
])
->columns(2),
@@ -148,6 +151,7 @@ class FeedbackForm
->icon('heroicon-o-arrow-path-rounded-square')
->schema([
Select::make('status')
->label(__('app.status'))
->options([
'pending' => __('app.status_pending'),
'processing' => __('app.status_processing'),
@@ -159,6 +163,7 @@ class FeedbackForm
->columnSpan(1),
Select::make('assigned_to')
->label(__('app.assigned_to'))
->relationship('assignedTo', 'name')
->searchable()
->nullable()
@@ -198,10 +203,42 @@ class FeedbackForm
->disk('public')
->directory('feedback-attachments')
->acceptedFileTypes(['image/jpeg', 'image/png', 'application/pdf', 'video/mp4', 'video/quicktime'])
->maxSize(51200) // 50MB in KB
->maxSize(51200)
->columnSpan(1),
])
->columns(2),
]);
}
private static function getProductOptionsForCustomer(?int $customerId): array
{
$ids = self::getProductIdsForCustomer($customerId);
return \App\Models\Product::whereIn('id', $ids)
->limit(100)
->pluck('name', 'id')
->toArray();
}
private static function getProductIdsForCustomer(?int $customerId): \Illuminate\Support\Collection
{
if (! $customerId) {
return collect();
}
$ownedIds = Contract::where('customer_id', $customerId)
->where('category', 'ownership')
->where('status', 'active')
->pluck('product_id');
$customer = \App\Models\Customer::find($customerId);
$repIds = collect();
if ($customer?->email) {
$repIds = Contract::where('representative_email', $customer->email)
->where('status', 'active')
->pluck('product_id');
}
return $ownedIds->merge($repIds)->unique();
}
}