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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\Contracts\ExportableReport;
  4. use App\DTO\ReportDTO;
  5. use App\Filament\Forms\Components\DateRangeSelect;
  6. use App\Models\Company;
  7. use App\Support\Column;
  8. use Filament\Actions\Action;
  9. use Filament\Actions\ActionGroup;
  10. use Filament\Forms\Components\Checkbox;
  11. use Filament\Forms\Components\Component;
  12. use Filament\Forms\Components\DatePicker;
  13. use Filament\Forms\Form;
  14. use Filament\Forms\Set;
  15. use Filament\Pages\Page;
  16. use Filament\Support\Enums\ActionSize;
  17. use Filament\Support\Enums\IconPosition;
  18. use Filament\Support\Enums\IconSize;
  19. use Filament\Support\Facades\FilamentIcon;
  20. use Illuminate\Support\Carbon;
  21. use Livewire\Attributes\Computed;
  22. use Livewire\Attributes\Session;
  23. use Symfony\Component\HttpFoundation\StreamedResponse;
  24. abstract class BaseReportPage extends Page
  25. {
  26. #[Session]
  27. public string $startDate = '';
  28. #[Session]
  29. public string $endDate = '';
  30. #[Session]
  31. public string $dateRange = '';
  32. public string $fiscalYearStartDate = '';
  33. public string $fiscalYearEndDate = '';
  34. public Company $company;
  35. public bool $reportLoaded = false;
  36. #[Session]
  37. public array $toggledTableColumns = [];
  38. abstract protected function buildReport(array $columns): ReportDTO;
  39. abstract public function exportCSV(): StreamedResponse;
  40. abstract public function exportPDF(): StreamedResponse;
  41. abstract protected function getTransformer(ReportDTO $reportDTO): ExportableReport;
  42. /**
  43. * @return array<Column>
  44. */
  45. abstract public function getTable(): array;
  46. public function mount(): void
  47. {
  48. $this->initializeProperties();
  49. $this->loadDefaultDateRange();
  50. $this->loadDefaultTableColumnToggleState();
  51. }
  52. protected function getForms(): array
  53. {
  54. return [
  55. 'toggleTableColumnForm',
  56. 'form',
  57. ];
  58. }
  59. protected function initializeProperties(): void
  60. {
  61. $this->company = auth()->user()->currentCompany;
  62. $this->fiscalYearStartDate = $this->company->locale->fiscalYearStartDate();
  63. $this->fiscalYearEndDate = $this->company->locale->fiscalYearEndDate();
  64. }
  65. protected function loadDefaultDateRange(): void
  66. {
  67. if (empty($this->dateRange)) {
  68. $this->dateRange = $this->getDefaultDateRange();
  69. $this->setDateRange(Carbon::parse($this->fiscalYearStartDate), Carbon::parse($this->fiscalYearEndDate));
  70. }
  71. }
  72. public function loadReportData(): void
  73. {
  74. unset($this->report);
  75. $this->reportLoaded = true;
  76. }
  77. protected function loadDefaultTableColumnToggleState(): void
  78. {
  79. $tableColumns = $this->getTable();
  80. if (empty($this->toggledTableColumns)) {
  81. foreach ($tableColumns as $column) {
  82. if ($column->isToggleable()) {
  83. if ($column->isToggledHiddenByDefault()) {
  84. $this->toggledTableColumns[$column->getName()] = false;
  85. } else {
  86. $this->toggledTableColumns[$column->getName()] = true;
  87. }
  88. } else {
  89. $this->toggledTableColumns[$column->getName()] = true;
  90. }
  91. }
  92. }
  93. foreach ($tableColumns as $column) {
  94. $columnName = $column->getName();
  95. if (! $column->isToggleable()) {
  96. $this->toggledTableColumns[$columnName] = true;
  97. }
  98. if ($column->isToggleable() && $column->isToggledHiddenByDefault() && isset($this->toggledTableColumns[$columnName]) && $this->toggledTableColumns[$columnName]) {
  99. $this->toggledTableColumns[$columnName] = false;
  100. }
  101. }
  102. }
  103. public function getDefaultDateRange(): string
  104. {
  105. return 'FY-' . now()->year;
  106. }
  107. protected function getToggledColumns(): array
  108. {
  109. return array_values(
  110. array_filter(
  111. $this->getTable(),
  112. fn (Column $column) => $this->toggledTableColumns[$column->getName()] ?? false,
  113. )
  114. );
  115. }
  116. #[Computed(persist: true)]
  117. public function report(): ?ExportableReport
  118. {
  119. if ($this->reportLoaded === false) {
  120. return null;
  121. }
  122. $columns = $this->getToggledColumns();
  123. $reportDTO = $this->buildReport($columns);
  124. return $this->getTransformer($reportDTO);
  125. }
  126. public function setDateRange(Carbon $start, Carbon $end): void
  127. {
  128. $this->startDate = $start->toDateString();
  129. $this->endDate = $end->isFuture() ? now()->toDateString() : $end->toDateString();
  130. }
  131. public function toggleColumnsAction(): Action
  132. {
  133. return Action::make('toggleColumns')
  134. ->label(__('filament-tables::table.actions.toggle_columns.label'))
  135. ->iconButton()
  136. ->size(ActionSize::Large)
  137. ->icon(FilamentIcon::resolve('tables::actions.toggle-columns') ?? 'heroicon-m-view-columns')
  138. ->color('gray');
  139. }
  140. public function toggleTableColumnForm(Form $form): Form
  141. {
  142. return $form
  143. ->schema($this->getTableColumnToggleFormSchema())
  144. ->statePath('toggledTableColumns');
  145. }
  146. protected function hasToggleableColumns(): bool
  147. {
  148. return ! empty($this->getTableColumnToggleFormSchema());
  149. }
  150. /**
  151. * @return array<Checkbox>
  152. */
  153. protected function getTableColumnToggleFormSchema(): array
  154. {
  155. $schema = [];
  156. foreach ($this->getTable() as $column) {
  157. if ($column->isToggleable()) {
  158. $schema[] = Checkbox::make($column->getName())
  159. ->label($column->getLabel());
  160. }
  161. }
  162. return $schema;
  163. }
  164. protected function getHeaderActions(): array
  165. {
  166. return [
  167. ActionGroup::make([
  168. Action::make('exportCSV')
  169. ->label('CSV')
  170. ->action(fn () => $this->exportCSV()),
  171. Action::make('exportPDF')
  172. ->label('PDF')
  173. ->action(fn () => $this->exportPDF()),
  174. ])
  175. ->label('Export')
  176. ->button()
  177. ->outlined()
  178. ->dropdownWidth('max-w-[7rem]')
  179. ->dropdownPlacement('bottom-end')
  180. ->icon('heroicon-c-chevron-down')
  181. ->iconSize(IconSize::Small)
  182. ->iconPosition(IconPosition::After),
  183. ];
  184. }
  185. protected function getDateRangeFormComponent(): Component
  186. {
  187. return DateRangeSelect::make('dateRange')
  188. ->label('Date Range')
  189. ->selectablePlaceholder(false)
  190. ->startDateField('startDate')
  191. ->endDateField('endDate');
  192. }
  193. protected function resetDateRange(): void
  194. {
  195. $this->dateRange = $this->getDefaultDateRange();
  196. $this->setDateRange(Carbon::parse($this->fiscalYearStartDate), Carbon::parse($this->fiscalYearEndDate));
  197. }
  198. protected function getStartDateFormComponent(): Component
  199. {
  200. return DatePicker::make('startDate')
  201. ->label('Start Date')
  202. ->displayFormat('Y-m-d')
  203. ->afterStateUpdated(static function (Set $set) {
  204. $set('dateRange', 'Custom');
  205. });
  206. }
  207. protected function getEndDateFormComponent(): Component
  208. {
  209. return DatePicker::make('endDate')
  210. ->label('End Date')
  211. ->displayFormat('Y-m-d')
  212. ->afterStateUpdated(static function (Set $set) {
  213. $set('dateRange', 'Custom');
  214. });
  215. }
  216. }