You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
topnotch_website/app/Filament/Resources/OrderResource.php

321 lines
12 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Enums\OrderAttributes;
use App\Enums\OrderStatus;
use App\Enums\OrderType;
use App\Filament\Resources\OrderResource\Pages;
use App\Models\Contact;
use App\Models\Customer;
use App\Models\Order;
use App\Models\ServiceType;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Split;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ToggleButtons;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Guava\FilamentClusters\Forms\Cluster;
use Icetalker\FilamentTableRepeater\Forms\Components\TableRepeater;
use Illuminate\Database\Eloquent\Builder;
class OrderResource extends Resource
{
protected static ?string $model = Order::class;
protected static ?string $navigationIcon = 'lucide-shopping-cart';
protected static ?string $navigationGroup = 'Production';
public static function form(Form $form): Form
{
return $form->schema([
Section::make([
Grid::make(1)
->schema([
Select::make('order_type')
->required()
->options(OrderType::class)
->searchable(),
Split::make([
Select::make('customer_id')
->required()
->label('Customer')
->options(Customer::all()->pluck('company_name', 'id'))
->reactive()
->searchable(),
Select::make('contact_id')
->label('Contact')
->options(fn ($get): array => Contact::where('customer_id', $get('customer_id') ?? null)
->get()
->pluck('full_name', 'id')
->toArray())
->searchable(),
]),
TextInput::make('customer_po')
->required()
->label('Customer PO'),
Split::make([
DatePicker::make('order_date')
->required()
->default(today()),
DatePicker::make('due_date')
->required()
->default(today()->add('10 days')),
]),
Textarea::make('notes')
->rows(3),
])->columnSpan(1),
Grid::make(1)
->schema([
ToggleButtons::make('status')
->required()
->options(OrderStatus::class)
->inline(),
ToggleButtons::make('order_attributes')
->options(OrderAttributes::class)
->multiple()
->inline(),
ToggleButtons::make('printed')
->boolean()
->inline(),
ToggleButtons::make('pre_production')
->label('Pre-production')
->boolean()
->inline()
->colors([
'true' => 'info',
'false' => 'info',
]),
])->columnSpan(1),
])->columns(2),
TableRepeater::make('order_products')
->label('Garments')
->schema([
TextInput::make('sku'),
TextInput::make('product_name')
->required(),
TextInput::make('color'),
Cluster::make([
TextInput::make('xs')
->placeholder('xs'),
TextInput::make('s')
->placeholder('s'),
TextInput::make('m')
->placeholder('m'),
TextInput::make('l')
->placeholder('l'),
TextInput::make('xl')
->placeholder('xl'),
TextInput::make('2xl')
->placeholder('2xl'),
TextInput::make('3xl')
->placeholder('3xl'),
TextInput::make('osfa')
->placeholder('osfa'),
])
->label('Sizes'),
])
->reorderable()
->cloneable(),
Repeater::make('services')
->label('Product Services')
->schema([
Grid::make(19)
->schema([
Select::make('serviceType')
->options(ServiceType::all()->pluck('name', 'id'))
->columnSpan(2)
->placeholder('Select...')
->searchable(),
TextInput::make('placement')
->columnSpan(3),
TextInput::make('serviceFileName')
->columnSpan(3)
->label('Logo Name'),
TextInput::make('serviceFileSetupNumber')
->label('Setup')
->columnSpan(1),
Cluster::make([
TextInput::make('serviceFileWidth')
->prefix('w'),
TextInput::make('serviceFileHeight')
->prefix('h'),
])
->label('Dimensions')
->columnSpan(4),
TextInput::make('amount')
->label('Quantity')
->live()
->reactive()
->afterStateUpdated(function ($state, Get $get, Set $set) {
$set('total_price', ($get('amount_price') * $state ?? 0));
})
->afterStateHydrated(function ($state, Get $get, Set $set) {
$set('total_price', ($get('amount_price') * $state ?? 0));
})
->prefix('#')
->columnSpan(2),
TextInput::make('amount_price')
->prefix('$')
->reactive()
->afterStateUpdated(function ($state, Get $get, Set $set) {
$set('total_price', ($get('amount') * $state ?? 0));
})
->afterStateHydrated(function ($state, Get $get, Set $set) {
$set('total_price', ($get('amount') * $state ?? 0));
})
->columnSpan(2),
TextInput::make('total_price')
->prefix('$')
->readOnly()
->columnSpan(2),
]),
Grid::make(9)
->schema([
TextInput::make('serviceFileCode')
->label('Code')
->columnSpan(1)
->placeholder('A1234'),
Textarea::make('notes')
->placeholder('Thread colors...')
->columnSpan(8),
]),
])
->reorderable()
->cloneable()
->columns(4)
->columnSpan(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\IconColumn::make('alert')
->getStateUsing(fn ($record) => $record->is_alert_danger || $record->is_alert_warning)
->label('')
->color(fn ($record) => $record->is_alert_danger ? 'danger' : 'warning')
->icon(function ($record) {
return $record->is_alert_danger
? 'lucide-calendar-clock' : ($record->rush
? OrderAttributes::rush->getIcon() : null);
})
->size(Tables\Columns\IconColumn\IconColumnSize::Small),
TextColumn::make('internal_po')
->label('Internal PO')
->fontFamily('mono')
->color('info')
->searchable()
->sortable(),
TextColumn::make('customer.company_name')
->searchable()
->sortable(),
TextColumn::make('customer_po')
->label('PO')
->wrap()
->weight('bold')
->color('code')
->searchable()
->sortable()
->extraHeaderAttributes([
'class' => 'w-full',
]),
TextColumn::make('order_date')
->searchable()
->sortable(),
TextColumn::make('due_date')
->searchable()
->sortable(),
TextColumn::make('status')
->badge()
->searchable()
->sortable(),
])
->defaultSort('order_date', 'desc')
->filters([
Tables\Filters\Filter::make('order_date')
->form([
DatePicker::make('created_from'),
DatePicker::make('created_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('order_date', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('order_date', '<=', $date),
);
}),
], )
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record}/edit'),
];
}
}