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.

InvoiceOverview.php 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Filament\Company\Resources\Sales\InvoiceResource\Widgets;
  3. use App\Enums\Accounting\InvoiceStatus;
  4. use App\Filament\Company\Resources\Sales\InvoiceResource\Pages\ListInvoices;
  5. use App\Filament\Widgets\EnhancedStatsOverviewWidget;
  6. use App\Utilities\Currency\CurrencyAccessor;
  7. use App\Utilities\Currency\CurrencyConverter;
  8. use Filament\Widgets\Concerns\InteractsWithPageTable;
  9. use Illuminate\Support\Number;
  10. class InvoiceOverview extends EnhancedStatsOverviewWidget
  11. {
  12. use InteractsWithPageTable;
  13. protected function getTablePage(): string
  14. {
  15. return ListInvoices::class;
  16. }
  17. protected function getStats(): array
  18. {
  19. $activeTab = $this->activeTab;
  20. if ($activeTab === 'draft') {
  21. return [
  22. EnhancedStatsOverviewWidget\EnhancedStat::make('Total Unpaid', '-')
  23. ->suffix('')
  24. ->description(''),
  25. EnhancedStatsOverviewWidget\EnhancedStat::make('Due Within 30 Days', '-')
  26. ->suffix(''),
  27. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Payment Time', '-')
  28. ->suffix(''),
  29. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Invoice Total', '-')
  30. ->suffix(''),
  31. ];
  32. }
  33. $unpaidInvoices = $this->getPageTableQuery()->unpaid();
  34. $amountUnpaid = $unpaidInvoices->get()->sumMoneyInDefaultCurrency('amount_due');
  35. $amountOverdue = $unpaidInvoices
  36. ->clone()
  37. ->where('status', InvoiceStatus::Overdue)
  38. ->get()
  39. ->sumMoneyInDefaultCurrency('amount_due');
  40. $amountDueWithin30Days = $unpaidInvoices
  41. ->clone()
  42. ->whereBetween('due_date', [today(), today()->addMonth()])
  43. ->get()
  44. ->sumMoneyInDefaultCurrency('amount_due');
  45. $validInvoices = $this->getPageTableQuery()
  46. ->whereNotIn('status', [
  47. InvoiceStatus::Void,
  48. InvoiceStatus::Draft,
  49. ]);
  50. $totalValidInvoiceAmount = $validInvoices->get()->sumMoneyInDefaultCurrency('total');
  51. $totalValidInvoiceCount = $validInvoices->count();
  52. $averageInvoiceTotal = $totalValidInvoiceCount > 0
  53. ? (int) round($totalValidInvoiceAmount / $totalValidInvoiceCount)
  54. : 0;
  55. $averagePaymentTime = $this->getPageTableQuery()
  56. ->whereNotNull('paid_at')
  57. ->selectRaw('AVG(TIMESTAMPDIFF(DAY, approved_at, paid_at)) as avg_days')
  58. ->value('avg_days');
  59. $averagePaymentTimeFormatted = Number::format($averagePaymentTime ?? 0, maxPrecision: 1);
  60. return [
  61. EnhancedStatsOverviewWidget\EnhancedStat::make('Total Unpaid', CurrencyConverter::formatCentsToMoney($amountUnpaid))
  62. ->suffix(CurrencyAccessor::getDefaultCurrency())
  63. ->description('Includes ' . CurrencyConverter::formatCentsToMoney($amountOverdue) . ' overdue'),
  64. EnhancedStatsOverviewWidget\EnhancedStat::make('Due Within 30 Days', CurrencyConverter::formatCentsToMoney($amountDueWithin30Days))
  65. ->suffix(CurrencyAccessor::getDefaultCurrency()),
  66. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Payment Time', $averagePaymentTimeFormatted)
  67. ->suffix('days'),
  68. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Invoice Total', CurrencyConverter::formatCentsToMoney($averageInvoiceTotal))
  69. ->suffix(CurrencyAccessor::getDefaultCurrency()),
  70. ];
  71. }
  72. }