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

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