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

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