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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\HasToggleTableColumnForm;
  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\Checkbox;
  14. use Filament\Forms\Components\Component;
  15. use Filament\Forms\Components\DatePicker;
  16. use Filament\Forms\Form;
  17. use Filament\Forms\Set;
  18. use Filament\Pages\Page;
  19. use Filament\Support\Enums\IconPosition;
  20. use Filament\Support\Enums\IconSize;
  21. use Illuminate\Support\Carbon;
  22. use Livewire\Attributes\Computed;
  23. use Livewire\Attributes\Session;
  24. use Symfony\Component\HttpFoundation\StreamedResponse;
  25. abstract class BaseReportPage extends Page
  26. {
  27. use HasDeferredFiltersForm;
  28. use HasToggleTableColumnForm;
  29. public string $fiscalYearStartDate;
  30. public string $fiscalYearEndDate;
  31. public Company $company;
  32. public bool $reportLoaded = false;
  33. #[Session]
  34. public array $toggledTableColumns = [];
  35. abstract protected function buildReport(array $columns): ReportDTO;
  36. abstract public function exportCSV(): StreamedResponse;
  37. abstract public function exportPDF(): StreamedResponse;
  38. abstract protected function getTransformer(ReportDTO $reportDTO): ExportableReport;
  39. /**
  40. * @return array<Column>
  41. */
  42. abstract public function getTable(): array;
  43. public function mount(): void
  44. {
  45. $this->initializeProperties();
  46. $this->loadDefaultDateRange();
  47. $this->loadDefaultTableColumnToggleState();
  48. }
  49. protected function initializeProperties(): void
  50. {
  51. $this->company = auth()->user()->currentCompany;
  52. $this->fiscalYearStartDate = $this->company->locale->fiscalYearStartDate();
  53. $this->fiscalYearEndDate = $this->company->locale->fiscalYearEndDate();
  54. }
  55. protected function loadDefaultDateRange(): void
  56. {
  57. $startDate = $this->getFilterState('startDate');
  58. $endDate = $this->getFilterState('endDate');
  59. if ($this->isValidDate($startDate) && $this->isValidDate($endDate)) {
  60. $matchingDateRange = app(DateRangeService::class)->getMatchingDateRangeOption(Carbon::parse($startDate), Carbon::parse($endDate));
  61. $this->setFilterState('dateRange', $matchingDateRange);
  62. } else {
  63. $this->setFilterState('dateRange', $this->getDefaultDateRange());
  64. $this->setDateRange(Carbon::parse($this->fiscalYearStartDate), Carbon::parse($this->fiscalYearEndDate));
  65. }
  66. }
  67. public function loadReportData(): void
  68. {
  69. unset($this->report);
  70. $this->reportLoaded = true;
  71. }
  72. protected function loadDefaultTableColumnToggleState(): void
  73. {
  74. $tableColumns = $this->getTable();
  75. foreach ($tableColumns as $column) {
  76. $columnName = $column->getName();
  77. if (empty($this->toggledTableColumns)) {
  78. if ($column->isToggleable()) {
  79. $this->toggledTableColumns[$columnName] = ! $column->isToggledHiddenByDefault();
  80. } else {
  81. $this->toggledTableColumns[$columnName] = true;
  82. }
  83. }
  84. // Handle cases where the toggle state needs to be reset
  85. if (! $column->isToggleable()) {
  86. $this->toggledTableColumns[$columnName] = true;
  87. } elseif ($column->isToggleable() && $column->isToggledHiddenByDefault() && isset($this->toggledTableColumns[$columnName]) && $this->toggledTableColumns[$columnName]) {
  88. $this->toggledTableColumns[$columnName] = false;
  89. }
  90. }
  91. }
  92. public function getDefaultDateRange(): string
  93. {
  94. return 'FY-' . now()->year;
  95. }
  96. protected function getToggledColumns(): array
  97. {
  98. return array_values(
  99. array_filter(
  100. $this->getTable(),
  101. fn (Column $column) => $this->toggledTableColumns[$column->getName()] ?? false,
  102. )
  103. );
  104. }
  105. #[Computed(persist: true)]
  106. public function report(): ?ExportableReport
  107. {
  108. if ($this->reportLoaded === false) {
  109. return null;
  110. }
  111. $columns = $this->getToggledColumns();
  112. $reportDTO = $this->buildReport($columns);
  113. return $this->getTransformer($reportDTO);
  114. }
  115. public function setDateRange(Carbon $start, Carbon $end): void
  116. {
  117. $this->setFilterState('startDate', $start->startOfDay()->toDateTimeString());
  118. $this->setFilterState('endDate', $end->isFuture() ? now()->endOfDay()->toDateTimeString() : $end->endOfDay()->toDateTimeString());
  119. }
  120. public function getFormattedStartDate(): string
  121. {
  122. return Carbon::parse($this->getFilterState('startDate'))->startOfDay()->toDateTimeString();
  123. }
  124. public function getFormattedEndDate(): string
  125. {
  126. return Carbon::parse($this->getFilterState('endDate'))->endOfDay()->toDateTimeString();
  127. }
  128. public function toggleTableColumnForm(Form $form): Form
  129. {
  130. return $form
  131. ->schema($this->getTableColumnToggleFormSchema());
  132. }
  133. protected function hasToggleableColumns(): bool
  134. {
  135. return ! empty($this->getTableColumnToggleFormSchema());
  136. }
  137. /**
  138. * @return array<Checkbox>
  139. */
  140. protected function getTableColumnToggleFormSchema(): array
  141. {
  142. $schema = [];
  143. foreach ($this->getTable() as $column) {
  144. if ($column->isToggleable()) {
  145. $schema[] = Checkbox::make($column->getName())
  146. ->label($column->getLabel());
  147. }
  148. }
  149. return $schema;
  150. }
  151. protected function getHeaderActions(): array
  152. {
  153. return [
  154. ActionGroup::make([
  155. Action::make('exportCSV')
  156. ->label('CSV')
  157. ->action(fn () => $this->exportCSV()),
  158. Action::make('exportPDF')
  159. ->label('PDF')
  160. ->action(fn () => $this->exportPDF()),
  161. ])
  162. ->label('Export')
  163. ->button()
  164. ->outlined()
  165. ->dropdownWidth('max-w-[7rem]')
  166. ->dropdownPlacement('bottom-end')
  167. ->icon('heroicon-c-chevron-down')
  168. ->iconSize(IconSize::Small)
  169. ->iconPosition(IconPosition::After),
  170. ];
  171. }
  172. protected function getDateRangeFormComponent(): Component
  173. {
  174. return DateRangeSelect::make('dateRange')
  175. ->label('Date Range')
  176. ->selectablePlaceholder(false)
  177. ->startDateField('startDate')
  178. ->endDateField('endDate');
  179. }
  180. protected function getStartDateFormComponent(): Component
  181. {
  182. return DatePicker::make('startDate')
  183. ->label('Start Date')
  184. ->live()
  185. ->afterStateUpdated(static function ($state, Set $set) {
  186. $set('dateRange', 'Custom');
  187. });
  188. }
  189. protected function getEndDateFormComponent(): Component
  190. {
  191. return DatePicker::make('endDate')
  192. ->label('End Date')
  193. ->live()
  194. ->afterStateUpdated(static function (Set $set) {
  195. $set('dateRange', 'Custom');
  196. });
  197. }
  198. }