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.

BaseAgingReportPage.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\Contracts\ExportableReport;
  4. use App\DTO\ReportDTO;
  5. use App\Enums\Accounting\DocumentEntityType;
  6. use App\Filament\Forms\Components\DateRangeSelect;
  7. use App\Services\ExportService;
  8. use App\Services\ReportService;
  9. use App\Support\Column;
  10. use App\Transformers\AgingReportTransformer;
  11. use Filament\Forms\Components\TextInput;
  12. use Filament\Forms\Form;
  13. use Filament\Support\Enums\Alignment;
  14. use Filament\Support\RawJs;
  15. use Symfony\Component\HttpFoundation\StreamedResponse;
  16. abstract class BaseAgingReportPage extends BaseReportPage
  17. {
  18. protected static string $view = 'filament.company.pages.reports.trial-balance';
  19. protected ReportService $reportService;
  20. protected ExportService $exportService;
  21. abstract protected function getEntityType(): DocumentEntityType;
  22. public function boot(ReportService $reportService, ExportService $exportService): void
  23. {
  24. $this->reportService = $reportService;
  25. $this->exportService = $exportService;
  26. }
  27. protected function initializeDefaultFilters(): void
  28. {
  29. if (empty($this->getFilterState('days_per_period'))) {
  30. $this->setFilterState('days_per_period', 30);
  31. }
  32. if (empty($this->getFilterState('number_of_periods'))) {
  33. $this->setFilterState('number_of_periods', 4);
  34. }
  35. }
  36. public function getTable(): array
  37. {
  38. $daysPerPeriod = $this->getFilterState('days_per_period');
  39. $numberOfPeriods = $this->getFilterState('number_of_periods');
  40. $columns = [
  41. Column::make('entity_name')
  42. ->label($this->getEntityType()->getLabel())
  43. ->alignment(Alignment::Left),
  44. Column::make('current')
  45. ->label('Current')
  46. ->alignment(Alignment::Right),
  47. ];
  48. for ($i = 1; $i < $numberOfPeriods; $i++) {
  49. $start = ($i - 1) * $daysPerPeriod + 1;
  50. $end = $i * $daysPerPeriod;
  51. $columns[] = Column::make("period_{$i}")
  52. ->label("{$start} to {$end}")
  53. ->alignment(Alignment::Right);
  54. }
  55. $columns[] = Column::make('over_periods')
  56. ->label('Over ' . (($numberOfPeriods - 1) * $daysPerPeriod))
  57. ->alignment(Alignment::Right);
  58. $columns[] = Column::make('total')
  59. ->label('Total')
  60. ->alignment(Alignment::Right);
  61. return $columns;
  62. }
  63. public function filtersForm(Form $form): Form
  64. {
  65. return $form
  66. ->columns(4)
  67. ->schema([
  68. DateRangeSelect::make('dateRange')
  69. ->label('As of')
  70. ->selectablePlaceholder(false)
  71. ->endDateField('asOfDate'),
  72. $this->getAsOfDateFormComponent(),
  73. TextInput::make('days_per_period')
  74. ->label('Days per period')
  75. ->integer()
  76. ->mask(RawJs::make(<<<'JS'
  77. $input > 365 ? '365' : '999'
  78. JS)),
  79. TextInput::make('number_of_periods')
  80. ->label('Number of periods')
  81. ->integer()
  82. ->mask(RawJs::make(<<<'JS'
  83. $input > 10 ? '10' : '99'
  84. JS)),
  85. ]);
  86. }
  87. protected function buildReport(array $columns): ReportDTO
  88. {
  89. return $this->reportService->buildAgingReport(
  90. $this->getFormattedAsOfDate(),
  91. $this->getEntityType(),
  92. $columns,
  93. $this->getFilterState('days_per_period'),
  94. $this->getFilterState('number_of_periods')
  95. );
  96. }
  97. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  98. {
  99. return new AgingReportTransformer($reportDTO, $this->getEntityType());
  100. }
  101. public function exportCSV(): StreamedResponse
  102. {
  103. return $this->exportService->exportToCsv(
  104. $this->company,
  105. $this->report,
  106. endDate: $this->getFilterState('asOfDate')
  107. );
  108. }
  109. public function exportPDF(): StreamedResponse
  110. {
  111. return $this->exportService->exportToPdf(
  112. $this->company,
  113. $this->report,
  114. endDate: $this->getFilterState('asOfDate')
  115. );
  116. }
  117. }