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 4.9KB

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