You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClientExporter.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Filament\Exports\Common;
  3. use App\Models\Common\Client;
  4. use Filament\Actions\Exports\ExportColumn;
  5. use Filament\Actions\Exports\Exporter;
  6. use Filament\Actions\Exports\Models\Export;
  7. class ClientExporter extends Exporter
  8. {
  9. protected static ?string $model = Client::class;
  10. public static function getColumns(): array
  11. {
  12. return [
  13. ExportColumn::make('name'),
  14. ExportColumn::make('account_number'),
  15. ExportColumn::make('primaryContact.full_name')
  16. ->label('Primary contact'),
  17. ExportColumn::make('primaryContact.email')
  18. ->label('Email'),
  19. ExportColumn::make('primaryContact.first_available_phone')
  20. ->label('Phone'),
  21. ExportColumn::make('currency_code'),
  22. ExportColumn::make('balance') // TODO: Potentially find an easier way to calculate this
  23. ->state(function (Client $record) {
  24. return $record->invoices()
  25. ->unpaid()
  26. ->get()
  27. ->sumMoneyInDefaultCurrency('amount_due');
  28. })
  29. ->money(),
  30. ExportColumn::make('overdue_amount')
  31. ->state(function (Client $record) {
  32. return $record->invoices()
  33. ->overdue()
  34. ->get()
  35. ->sumMoneyInDefaultCurrency('amount_due');
  36. })
  37. ->money(),
  38. ExportColumn::make('billingAddress.address_string')
  39. ->label('Billing address')
  40. ->enabledByDefault(false),
  41. ExportColumn::make('billingAddress.address_line_1')
  42. ->label('Billing address line 1'),
  43. ExportColumn::make('billingAddress.address_line_2')
  44. ->label('Billing address line 2'),
  45. ExportColumn::make('billingAddress.city')
  46. ->label('Billing city'),
  47. ExportColumn::make('billingAddress.state.name')
  48. ->label('Billing state'),
  49. ExportColumn::make('billingAddress.postal_code')
  50. ->label('Billing postal code'),
  51. ExportColumn::make('billingAddress.country.name')
  52. ->label('Billing country'),
  53. ExportColumn::make('shippingAddress.recipient')
  54. ->label('Shipping recipient')
  55. ->enabledByDefault(false),
  56. ExportColumn::make('shippingAddress.phone')
  57. ->label('Shipping phone')
  58. ->enabledByDefault(false),
  59. ExportColumn::make('shippingAddress.address_string')
  60. ->label('Shipping address')
  61. ->enabledByDefault(false),
  62. ExportColumn::make('shippingAddress.address_line_1')
  63. ->label('Shipping address line 1')
  64. ->enabledByDefault(false),
  65. ExportColumn::make('shippingAddress.address_line_2')
  66. ->label('Shipping address line 2')
  67. ->enabledByDefault(false),
  68. ExportColumn::make('shippingAddress.city')
  69. ->label('Shipping city')
  70. ->enabledByDefault(false),
  71. ExportColumn::make('shippingAddress.state.name')
  72. ->label('Shipping state')
  73. ->enabledByDefault(false),
  74. ExportColumn::make('shippingAddress.postal_code')
  75. ->label('Shipping postal code')
  76. ->enabledByDefault(false),
  77. ExportColumn::make('shippingAddress.country.name')
  78. ->label('Shipping country')
  79. ->enabledByDefault(false),
  80. ExportColumn::make('shippingAddress.notes')
  81. ->label('Delivery instructions')
  82. ->enabledByDefault(false),
  83. ExportColumn::make('website')
  84. ->enabledByDefault(false),
  85. ExportColumn::make('notes')
  86. ->enabledByDefault(false),
  87. ];
  88. }
  89. public static function getCompletedNotificationBody(Export $export): string
  90. {
  91. $body = 'Your client export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
  92. if ($failedRowsCount = $export->getFailedRowsCount()) {
  93. $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
  94. }
  95. return $body;
  96. }
  97. }