Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AccountTransactions.php 4.9KB

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