147 lines
5.7 KiB
PHP
147 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Admin\Resources;
|
|
|
|
use App\Enums\IconEnum;
|
|
use App\Models\Customer;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Support\Enums\FontWeight;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Columns\Summarizers\Sum;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Query\Builder;
|
|
|
|
class CustomerReportResource extends Resource
|
|
{
|
|
protected static ?string $model = Customer::class;
|
|
|
|
protected static ?string $navigationIcon = IconEnum::CUSTOMER_SALES->value;
|
|
|
|
protected static ?string $navigationGroup = 'Reports';
|
|
|
|
protected static ?string $navigationLabel = 'Customer Reports';
|
|
|
|
protected static ?int $navigationSort = 4;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
//
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('company_name')
|
|
->label('Customer')
|
|
->sortable()
|
|
// ->searchable()
|
|
->extraHeaderAttributes(['class' => 'w-full']),
|
|
|
|
Tables\Columns\TextColumn::make('invoices.subtotal')
|
|
->label('Subtotal')
|
|
->money()
|
|
->alignRight()
|
|
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('subtotal'))
|
|
->summarize(Sum::make('subtotal')
|
|
->label('')
|
|
->money()
|
|
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('subtotal'))
|
|
),
|
|
|
|
Tables\Columns\TextColumn::make('invoices.hst_amount')
|
|
->label('HST')
|
|
->money()
|
|
->alignRight()
|
|
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('hst_amount'))
|
|
->summarize(Sum::make('hst_amount')
|
|
->label('')
|
|
->money()
|
|
->using(fn (Table $table, Builder $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('hst_amount'))
|
|
),
|
|
|
|
Tables\Columns\TextColumn::make('invoices.gst_amount')
|
|
->label('GST')
|
|
->money()
|
|
->alignRight()
|
|
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('gst_amount'))
|
|
->summarize(Sum::make('gst_amount')
|
|
->label('')
|
|
->money()
|
|
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('gst_amount'))
|
|
),
|
|
|
|
Tables\Columns\TextColumn::make('invoices.pst_amount')
|
|
->label('PST')
|
|
->money()
|
|
->alignRight()
|
|
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('pst_amount'))
|
|
->summarize(Sum::make('pst_amount')
|
|
->label('')
|
|
->money()
|
|
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('pst_amount'))
|
|
),
|
|
|
|
Tables\Columns\TextColumn::make('invoices.total')
|
|
->label('Total')
|
|
->money()
|
|
->weight(FontWeight::Bold)
|
|
->alignRight()
|
|
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('total'))
|
|
->summarize(Sum::make('total')
|
|
->label('')
|
|
->money()
|
|
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('total'))
|
|
),
|
|
])
|
|
|
|
->filters([
|
|
Tables\Filters\Filter::make('created_at')
|
|
->form([
|
|
DatePicker::make('created_at')
|
|
->label('From date'),
|
|
]),
|
|
|
|
Tables\Filters\Filter::make('created_until')
|
|
->form([
|
|
DatePicker::make('created_until')
|
|
->label('Until date'),
|
|
]),
|
|
], layout: Tables\Enums\FiltersLayout::AboveContent);
|
|
}
|
|
|
|
protected static function applyDateFilters($query, Table $table): void
|
|
{
|
|
$createdAt = $table->getFilter('created_at')?->getState()['created_at'] ?? null;
|
|
$createdUntil = $table->getFilter('created_until')?->getState()['created_until'] ?? null;
|
|
|
|
$query->when($createdAt, fn ($q, $date) => $q->whereDate('created_at', '>=', $date));
|
|
$query->when($createdUntil, fn ($q, $date) => $q->whereDate('created_at', '<=', $date));
|
|
}
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return auth()->user()->is_admin ?? false;
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => \App\Filament\Admin\Resources\CustomerReportResource\Pages\ListCustomerReports::route('/'),
|
|
];
|
|
}
|
|
}
|