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

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