|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Exports; |
| 4 | + |
| 5 | +use App\Models\Sale; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Filament\Actions\Exports\ExportColumn; |
| 8 | +use Filament\Actions\Exports\Exporter; |
| 9 | +use Filament\Actions\Exports\Models\Export; |
| 10 | + |
| 11 | +class SaleExporter extends Exporter |
| 12 | +{ |
| 13 | + use ReportExporterTrait; |
| 14 | + |
| 15 | + protected string $filename = 'sales-report'; |
| 16 | + |
| 17 | + protected static ?string $model = Sale::class; |
| 18 | + |
| 19 | + public static function getColumns(): array |
| 20 | + { |
| 21 | + return [ |
| 22 | + ExportColumn::make('invoice_number'), |
| 23 | + ExportColumn::make('sale_date') |
| 24 | + ->formatStateUsing(fn ($state) => Carbon::parse($state)->format('M d, Y')), |
| 25 | + ExportColumn::make('pay_until') |
| 26 | + ->label('Due Date') |
| 27 | + ->formatStateUsing(fn ($state) => now()->addDays($state)->format('M d, Y')), |
| 28 | + ExportColumn::make('vat'), |
| 29 | + ExportColumn::make('discount'), |
| 30 | + ExportColumn::make('discount_type') |
| 31 | + ->formatStateUsing(fn ($state) => $state->getLabel()), |
| 32 | + ExportColumn::make('total_amount'), |
| 33 | + ExportColumn::make('paid_amount'), |
| 34 | + ExportColumn::make('customer.name'), |
| 35 | + ExportColumn::make('paymentType.name'), |
| 36 | + ExportColumn::make('user.name'), |
| 37 | + ExportColumn::make('created_at'), |
| 38 | + ExportColumn::make('updated_at'), |
| 39 | + ExportColumn::make('deleted_at'), |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + public static function getCompletedNotificationBody(Export $export): string |
| 44 | + { |
| 45 | + $body = 'Your sale export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.'; |
| 46 | + |
| 47 | + if ($failedRowsCount = $export->getFailedRowsCount()) { |
| 48 | + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.'; |
| 49 | + } |
| 50 | + |
| 51 | + return $body; |
| 52 | + } |
| 53 | +} |
0 commit comments