Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AccountTransactions.php 5.3KB

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