Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AccountTransactions.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ])->extraFieldWrapperAttributes([
  82. 'class' => 'report-hidden-label',
  83. ]),
  84. Actions::make([
  85. Actions\Action::make('applyFilters')
  86. ->label('Update Report')
  87. ->action('applyFilters')
  88. ->keyBindings(['mod+s'])
  89. ->button(),
  90. ])->alignEnd()->verticallyAlignEnd(),
  91. ]);
  92. }
  93. protected function getAccountOptions(): array
  94. {
  95. $accounts = Account::query()
  96. ->get()
  97. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  98. ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
  99. ->toArray();
  100. $allAccountsOption = [
  101. 'All Accounts' => ['all' => 'All Accounts'],
  102. ];
  103. return $allAccountsOption + $accounts;
  104. }
  105. protected function buildReport(array $columns): ReportDTO
  106. {
  107. return $this->reportService->buildAccountTransactionsReport($this->getFormattedStartDate(), $this->getFormattedEndDate(), $columns, $this->getFilterState('selectedAccount'));
  108. }
  109. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  110. {
  111. return new AccountTransactionReportTransformer($reportDTO);
  112. }
  113. public function exportCSV(): StreamedResponse
  114. {
  115. return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  116. }
  117. public function exportPDF(): StreamedResponse
  118. {
  119. return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  120. }
  121. public function getEmptyStateHeading(): string | Htmlable
  122. {
  123. return 'No Transactions Found';
  124. }
  125. public function getEmptyStateDescription(): string | Htmlable | null
  126. {
  127. return 'Adjust the account or date range, or start by creating a transaction.';
  128. }
  129. public function getEmptyStateIcon(): string
  130. {
  131. return 'heroicon-o-x-mark';
  132. }
  133. public function getEmptyStateActions(): array
  134. {
  135. return [
  136. Action::make('createTransaction')
  137. ->label('Create Transaction')
  138. ->url(Transactions::getUrl()),
  139. ];
  140. }
  141. public function tableHasEmptyState(): bool
  142. {
  143. if ($this->report) {
  144. return empty($this->report->getCategories());
  145. } else {
  146. return true;
  147. }
  148. }
  149. }