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.

AccountTransactions.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Accounting\Transactions;
  6. use App\Models\Accounting\Account;
  7. use App\Services\ExportService;
  8. use App\Services\ReportService;
  9. use App\Support\Column;
  10. use App\Transformers\AccountTransactionReportTransformer;
  11. use Filament\Forms\Components\Actions;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Form;
  14. use Filament\Support\Enums\Alignment;
  15. use Filament\Support\Enums\MaxWidth;
  16. use Filament\Tables\Actions\Action;
  17. use Guava\FilamentClusters\Forms\Cluster;
  18. use Illuminate\Contracts\Support\Htmlable;
  19. use Illuminate\Support\Collection;
  20. use Symfony\Component\HttpFoundation\StreamedResponse;
  21. class AccountTransactions extends BaseReportPage
  22. {
  23. protected static string $view = 'filament.company.pages.reports.account-transactions';
  24. protected static ?string $slug = 'reports/account-transactions';
  25. protected static bool $shouldRegisterNavigation = false;
  26. protected ReportService $reportService;
  27. protected ExportService $exportService;
  28. public function boot(ReportService $reportService, ExportService $exportService): void
  29. {
  30. $this->reportService = $reportService;
  31. $this->exportService = $exportService;
  32. }
  33. public function getMaxContentWidth(): MaxWidth | string | null
  34. {
  35. return 'max-w-[90rem]';
  36. }
  37. protected function initializeDefaultFilters(): void
  38. {
  39. if (empty($this->getFilterState('selectedAccount'))) {
  40. $this->setFilterState('selectedAccount', 'all');
  41. }
  42. }
  43. /**
  44. * @return array<Column>
  45. */
  46. public function getTable(): array
  47. {
  48. return [
  49. Column::make('date')
  50. ->label('Date')
  51. ->markAsDate()
  52. ->alignment(Alignment::Left),
  53. Column::make('description')
  54. ->label('Description')
  55. ->alignment(Alignment::Left),
  56. Column::make('debit')
  57. ->label('Debit')
  58. ->alignment(Alignment::Right),
  59. Column::make('credit')
  60. ->label('Credit')
  61. ->alignment(Alignment::Right),
  62. Column::make('balance')
  63. ->label('Balance')
  64. ->alignment(Alignment::Right),
  65. ];
  66. }
  67. public function filtersForm(Form $form): Form
  68. {
  69. return $form
  70. ->columns(4)
  71. ->schema([
  72. Select::make('selectedAccount')
  73. ->label('Account')
  74. ->options($this->getAccountOptions())
  75. ->selectablePlaceholder(false)
  76. ->searchable(),
  77. $this->getDateRangeFormComponent(),
  78. Cluster::make([
  79. $this->getStartDateFormComponent(),
  80. $this->getEndDateFormComponent(),
  81. ])->label("\u{200B}"), // its too bad hiddenLabel removes spacing of the label
  82. Actions::make([
  83. Actions\Action::make('applyFilters')
  84. ->label('Update Report')
  85. ->action('applyFilters')
  86. ->keyBindings(['mod+s'])
  87. ->button(),
  88. ])->alignEnd()->verticallyAlignEnd(),
  89. ]);
  90. }
  91. protected function getAccountOptions(): array
  92. {
  93. $accounts = Account::query()
  94. ->get()
  95. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  96. ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
  97. ->toArray();
  98. $allAccountsOption = [
  99. 'All Accounts' => ['all' => 'All Accounts'],
  100. ];
  101. return $allAccountsOption + $accounts;
  102. }
  103. protected function buildReport(array $columns): ReportDTO
  104. {
  105. return $this->reportService->buildAccountTransactionsReport($this->getFormattedStartDate(), $this->getFormattedEndDate(), $columns, $this->getFilterState('selectedAccount'));
  106. }
  107. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  108. {
  109. return new AccountTransactionReportTransformer($reportDTO);
  110. }
  111. public function exportCSV(): StreamedResponse
  112. {
  113. return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  114. }
  115. public function exportPDF(): StreamedResponse
  116. {
  117. return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  118. }
  119. public function getEmptyStateHeading(): string | Htmlable
  120. {
  121. return 'No Transactions Found';
  122. }
  123. public function getEmptyStateDescription(): string | Htmlable | null
  124. {
  125. return 'Adjust the account or date range, or start by creating a transaction.';
  126. }
  127. public function getEmptyStateIcon(): string
  128. {
  129. return 'heroicon-o-x-mark';
  130. }
  131. public function getEmptyStateActions(): array
  132. {
  133. return [
  134. Action::make('createTransaction')
  135. ->label('Create Transaction')
  136. ->url(Transactions::getUrl()),
  137. ];
  138. }
  139. public function tableHasEmptyState(): bool
  140. {
  141. if ($this->report) {
  142. return empty($this->report->getCategories());
  143. } else {
  144. return true;
  145. }
  146. }
  147. }