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.
180 lines
6.9 KiB
PHP
180 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\InvoiceResource\Pages;
|
|
use App\Filament\Resources\InvoiceResource\RelationManagers\OrdersRelationManager;
|
|
use App\Filament\Resources\InvoiceResource\RelationManagers\ProductServicesRelationManager;
|
|
use App\Models\Customer;
|
|
use App\Models\Invoice;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Grid;
|
|
use Filament\Forms\Components\Section;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Split;
|
|
use Filament\Forms\Components\ToggleButtons;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class InvoiceResource extends Resource
|
|
{
|
|
protected static ?string $model = Invoice::class;
|
|
|
|
protected static ?string $navigationIcon = 'lucide-receipt-text';
|
|
|
|
protected static ?string $navigationGroup = 'Production';
|
|
|
|
protected static ?int $navigationSort = 2;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make([
|
|
Grid::make(2)
|
|
->schema([
|
|
Select::make('customer_id')
|
|
->required()
|
|
->label('Customer')
|
|
->options(Customer::all()->pluck('company_name', 'id'))
|
|
->reactive()
|
|
->searchable()
|
|
->columnSpan(2),
|
|
|
|
Split::make([
|
|
DatePicker::make('date')
|
|
->required()
|
|
->default(today()),
|
|
DatePicker::make('due_date'),
|
|
])
|
|
->columnSpan(2),
|
|
])->columnSpan(2),
|
|
|
|
Grid::make(1)
|
|
->schema([
|
|
ToggleButtons::make('gst')
|
|
->boolean()
|
|
->default(true)
|
|
->inline()
|
|
->colors([
|
|
'true' => 'info',
|
|
'false' => 'info',
|
|
]),
|
|
|
|
ToggleButtons::make('pst')
|
|
->boolean()
|
|
->default(false)
|
|
->inline()
|
|
->colors([
|
|
'true' => 'info',
|
|
'false' => 'info',
|
|
]),
|
|
ToggleButtons::make('paid')
|
|
->boolean()
|
|
->default(false)
|
|
->inline(),
|
|
])->columnSpan(1),
|
|
])
|
|
->columns(3)
|
|
->columnSpan(3),
|
|
])->columns(3);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('internal_id')
|
|
->label('ID')
|
|
->fontFamily('mono')
|
|
->color('primary')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('customer.company_name')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->date()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('gst_amount')
|
|
->label('GST')
|
|
->prefix('$'),
|
|
Tables\Columns\TextColumn::make('pst_amount')
|
|
->label('PST')
|
|
->prefix('$'),
|
|
Tables\Columns\TextColumn::make('subtotal')
|
|
->prefix('$'),
|
|
Tables\Columns\TextColumn::make('total')
|
|
->prefix('$')
|
|
->weight('bold'),
|
|
Tables\Columns\IconColumn::make('paid')
|
|
->boolean()
|
|
->sortable(),
|
|
])
|
|
->filters([
|
|
Tables\Filters\Filter::make('created_at')
|
|
->form([
|
|
Split::make([
|
|
DatePicker::make('created_from')
|
|
->label('From date'),
|
|
DatePicker::make('created_until')
|
|
->label('Until date'),
|
|
]),
|
|
])
|
|
->query(function (Builder $query, array $data): Builder {
|
|
return $query
|
|
->when(
|
|
$data['created_from'],
|
|
fn (Builder $query, $date): Builder => $query->whereDate('date', '>=', $date),
|
|
)
|
|
->when(
|
|
$data['created_until'],
|
|
fn (Builder $query, $date): Builder => $query->whereDate('date', '<=', $date),
|
|
);
|
|
}),
|
|
Tables\Filters\Filter::make('paid'),
|
|
], layout: Tables\Enums\FiltersLayout::AboveContentCollapsible)
|
|
->filtersFormColumns(3)
|
|
->defaultSort('created_at', 'desc')
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkAction::make('Mark as paid')
|
|
->action(fn (Collection $records) => $records->each->setPaid(true))
|
|
->icon('lucide-circle-check')
|
|
->deselectRecordsAfterCompletion(),
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\BulkAction::make('Mark as unpaid')
|
|
->action(fn (Collection $records) => $records->each->setPaid(false))
|
|
->icon('lucide-circle-x')
|
|
->deselectRecordsAfterCompletion(),
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
])
|
|
->label('Other actions'),
|
|
])
|
|
->selectCurrentPageOnly();
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
OrdersRelationManager::class,
|
|
ProductServicesRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListInvoices::route('/'),
|
|
'create' => Pages\CreateInvoice::route('/create'),
|
|
'edit' => Pages\EditInvoice::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|