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.

BaseReportPage.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\Contracts\ExportableReport;
  4. use App\DTO\ReportDTO;
  5. use App\Filament\Company\Pages\Concerns\HasDeferredFiltersForm;
  6. use App\Filament\Company\Pages\Concerns\HasTableColumnToggleForm;
  7. use App\Filament\Forms\Components\DateRangeSelect;
  8. use App\Models\Company;
  9. use App\Services\DateRangeService;
  10. use App\Support\Column;
  11. use Filament\Actions\Action;
  12. use Filament\Actions\ActionGroup;
  13. use Filament\Forms\Components\Component;
  14. use Filament\Forms\Components\DatePicker;
  15. use Filament\Forms\Set;
  16. use Filament\Pages\Page;
  17. use Filament\Support\Enums\IconPosition;
  18. use Filament\Support\Enums\IconSize;
  19. use Illuminate\Support\Carbon;
  20. use Livewire\Attributes\Computed;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. abstract class BaseReportPage extends Page
  23. {
  24. use HasDeferredFiltersForm;
  25. use HasTableColumnToggleForm;
  26. public string $fiscalYearStartDate;
  27. public string $fiscalYearEndDate;
  28. public Company $company;
  29. public bool $reportLoaded = false;
  30. abstract protected function buildReport(array $columns): ReportDTO;
  31. abstract public function exportCSV(): StreamedResponse;
  32. abstract public function exportPDF(): StreamedResponse;
  33. abstract protected function getTransformer(ReportDTO $reportDTO): ExportableReport;
  34. /**
  35. * @return array<Column>
  36. */
  37. abstract public function getTable(): array;
  38. public function mount(): void
  39. {
  40. $this->initializeProperties();
  41. $this->loadDefaultDateRange();
  42. }
  43. protected function initializeProperties(): void
  44. {
  45. $this->company = auth()->user()->currentCompany;
  46. $this->fiscalYearStartDate = $this->company->locale->fiscalYearStartDate();
  47. $this->fiscalYearEndDate = $this->company->locale->fiscalYearEndDate();
  48. }
  49. protected function loadDefaultDateRange(): void
  50. {
  51. $startDate = $this->getFilterState('startDate');
  52. $endDate = $this->getFilterState('endDate');
  53. if ($this->isValidDate($startDate) && $this->isValidDate($endDate)) {
  54. $matchingDateRange = app(DateRangeService::class)->getMatchingDateRangeOption(Carbon::parse($startDate), Carbon::parse($endDate));
  55. $this->setFilterState('dateRange', $matchingDateRange);
  56. } else {
  57. $this->setFilterState('dateRange', $this->getDefaultDateRange());
  58. $this->setDateRange(Carbon::parse($this->fiscalYearStartDate), Carbon::parse($this->fiscalYearEndDate));
  59. }
  60. }
  61. public function loadReportData(): void
  62. {
  63. unset($this->report);
  64. $this->reportLoaded = true;
  65. }
  66. public function getDefaultDateRange(): string
  67. {
  68. return 'FY-' . now()->year;
  69. }
  70. #[Computed(persist: true)]
  71. public function report(): ?ExportableReport
  72. {
  73. if ($this->reportLoaded === false) {
  74. return null;
  75. }
  76. $columns = $this->getToggledColumns();
  77. $reportDTO = $this->buildReport($columns);
  78. return $this->getTransformer($reportDTO);
  79. }
  80. public function setDateRange(Carbon $start, Carbon $end): void
  81. {
  82. $this->setFilterState('startDate', $start->startOfDay()->toDateTimeString());
  83. $this->setFilterState('endDate', $end->isFuture() ? now()->endOfDay()->toDateTimeString() : $end->endOfDay()->toDateTimeString());
  84. }
  85. public function getFormattedStartDate(): string
  86. {
  87. return Carbon::parse($this->getFilterState('startDate'))->startOfDay()->toDateTimeString();
  88. }
  89. public function getFormattedEndDate(): string
  90. {
  91. return Carbon::parse($this->getFilterState('endDate'))->endOfDay()->toDateTimeString();
  92. }
  93. protected function getHeaderActions(): array
  94. {
  95. return [
  96. ActionGroup::make([
  97. Action::make('exportCSV')
  98. ->label('CSV')
  99. ->action(fn () => $this->exportCSV()),
  100. Action::make('exportPDF')
  101. ->label('PDF')
  102. ->action(fn () => $this->exportPDF()),
  103. ])
  104. ->label('Export')
  105. ->button()
  106. ->outlined()
  107. ->dropdownWidth('max-w-[7rem]')
  108. ->dropdownPlacement('bottom-end')
  109. ->icon('heroicon-c-chevron-down')
  110. ->iconSize(IconSize::Small)
  111. ->iconPosition(IconPosition::After),
  112. ];
  113. }
  114. protected function getDateRangeFormComponent(): Component
  115. {
  116. return DateRangeSelect::make('dateRange')
  117. ->label('Date Range')
  118. ->selectablePlaceholder(false)
  119. ->startDateField('startDate')
  120. ->endDateField('endDate');
  121. }
  122. protected function getStartDateFormComponent(): Component
  123. {
  124. return DatePicker::make('startDate')
  125. ->label('Start Date')
  126. ->live()
  127. ->afterStateUpdated(static function ($state, Set $set) {
  128. $set('dateRange', 'Custom');
  129. });
  130. }
  131. protected function getEndDateFormComponent(): Component
  132. {
  133. return DatePicker::make('endDate')
  134. ->label('End Date')
  135. ->live()
  136. ->afterStateUpdated(static function (Set $set) {
  137. $set('dateRange', 'Custom');
  138. });
  139. }
  140. }