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.

EstimateOverview.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Filament\Company\Resources\Sales\EstimateResource\Widgets;
  3. use App\Enums\Accounting\EstimateStatus;
  4. use App\Filament\Company\Resources\Sales\EstimateResource\Pages\ListEstimates;
  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 EstimateOverview extends EnhancedStatsOverviewWidget
  11. {
  12. use InteractsWithPageTable;
  13. protected function getTablePage(): string
  14. {
  15. return ListEstimates::class;
  16. }
  17. protected function getStats(): array
  18. {
  19. $activeTab = $this->activeTab;
  20. if ($activeTab === 'draft') {
  21. $draftEstimates = $this->getPageTableQuery();
  22. $totalDraftCount = $draftEstimates->count();
  23. $totalDraftAmount = $draftEstimates->get()->sumMoneyInDefaultCurrency('total');
  24. $averageDraftTotal = $totalDraftCount > 0
  25. ? (int) round($totalDraftAmount / $totalDraftCount)
  26. : 0;
  27. return [
  28. EnhancedStatsOverviewWidget\EnhancedStat::make('Active Estimates', '-'),
  29. EnhancedStatsOverviewWidget\EnhancedStat::make('Accepted Estimates', '-'),
  30. EnhancedStatsOverviewWidget\EnhancedStat::make('Converted Estimates', '-'),
  31. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Estimate Total', CurrencyConverter::formatCentsToMoney($averageDraftTotal))
  32. ->suffix(CurrencyAccessor::getDefaultCurrency()),
  33. ];
  34. }
  35. $activeEstimates = $this->getPageTableQuery()->active();
  36. $totalActiveCount = $activeEstimates->count();
  37. $totalActiveAmount = $activeEstimates->get()->sumMoneyInDefaultCurrency('total');
  38. $acceptedEstimates = $this->getPageTableQuery()
  39. ->where('status', EstimateStatus::Accepted);
  40. $totalAcceptedCount = $acceptedEstimates->count();
  41. $totalAcceptedAmount = $acceptedEstimates->get()->sumMoneyInDefaultCurrency('total');
  42. $convertedEstimates = $this->getPageTableQuery()
  43. ->where('status', EstimateStatus::Converted);
  44. $totalConvertedCount = $convertedEstimates->count();
  45. $validEstimates = $this->getPageTableQuery()
  46. ->whereNotIn('status', [
  47. EstimateStatus::Draft,
  48. ]);
  49. $totalValidEstimatesCount = $validEstimates->count();
  50. $totalValidEstimateAmount = $validEstimates->get()->sumMoneyInDefaultCurrency('total');
  51. $averageEstimateTotal = $totalValidEstimatesCount > 0
  52. ? (int) round($totalValidEstimateAmount / $totalValidEstimatesCount)
  53. : 0;
  54. $percentConverted = '-';
  55. $percentConvertedSuffix = null;
  56. $percentConvertedDescription = null;
  57. if ($activeTab !== 'active') {
  58. $percentConverted = $totalValidEstimatesCount > 0
  59. ? Number::percentage(($totalConvertedCount / $totalValidEstimatesCount) * 100, maxPrecision: 1)
  60. : Number::percentage(0, maxPrecision: 1);
  61. $percentConvertedSuffix = 'converted';
  62. $percentConvertedDescription = $totalConvertedCount . ' converted';
  63. }
  64. return [
  65. EnhancedStatsOverviewWidget\EnhancedStat::make('Active Estimates', CurrencyConverter::formatCentsToMoney($totalActiveAmount))
  66. ->suffix(CurrencyAccessor::getDefaultCurrency())
  67. ->description($totalActiveCount . ' active estimates'),
  68. EnhancedStatsOverviewWidget\EnhancedStat::make('Accepted Estimates', CurrencyConverter::formatCentsToMoney($totalAcceptedAmount))
  69. ->suffix(CurrencyAccessor::getDefaultCurrency())
  70. ->description($totalAcceptedCount . ' accepted'),
  71. EnhancedStatsOverviewWidget\EnhancedStat::make('Converted Estimates', $percentConverted)
  72. ->suffix($percentConvertedSuffix)
  73. ->description($percentConvertedDescription),
  74. EnhancedStatsOverviewWidget\EnhancedStat::make('Average Estimate Total', CurrencyConverter::formatCentsToMoney($averageEstimateTotal))
  75. ->suffix(CurrencyAccessor::getDefaultCurrency())
  76. ->description($activeTab === 'all' ? 'Excludes draft estimates' : null),
  77. ];
  78. }
  79. }