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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\Contracts\ExportableReport;
  4. use App\DTO\ReportDTO;
  5. use App\Filament\Company\Resources\Accounting\TransactionResource;
  6. use App\Models\Accounting\Account;
  7. use App\Models\Common\Client;
  8. use App\Models\Common\Vendor;
  9. use App\Services\ExportService;
  10. use App\Services\ReportService;
  11. use App\Support\Column;
  12. use App\Transformers\AccountTransactionReportTransformer;
  13. use Filament\Forms\Components\Actions;
  14. use Filament\Forms\Components\Select;
  15. use Filament\Forms\Form;
  16. use Filament\Support\Enums\Alignment;
  17. use Filament\Support\Enums\MaxWidth;
  18. use Filament\Tables\Actions\Action;
  19. use Guava\FilamentClusters\Forms\Cluster;
  20. use Illuminate\Contracts\Support\Htmlable;
  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 ReportService $reportService;
  27. protected ExportService $exportService;
  28. public function boot(ReportService $reportService, ExportService $exportService): void
  29. {
  30. $this->reportService = $reportService;
  31. $this->exportService = $exportService;
  32. }
  33. public function getMaxContentWidth(): MaxWidth | string | null
  34. {
  35. return 'max-w-8xl';
  36. }
  37. protected function initializeDefaultFilters(): void
  38. {
  39. if (empty($this->getFilterState('selectedAccount'))) {
  40. $this->setFilterState('selectedAccount', 'all');
  41. }
  42. if (empty($this->getFilterState('selectedEntity'))) {
  43. $this->setFilterState('selectedEntity', 'all');
  44. }
  45. }
  46. /**
  47. * @return array<Column>
  48. */
  49. public function getTable(): array
  50. {
  51. return [
  52. Column::make('date')
  53. ->label('DATE')
  54. ->markAsDate()
  55. ->alignment(Alignment::Left),
  56. Column::make('description')
  57. ->label('DESCRIPTION')
  58. ->alignment(Alignment::Left),
  59. Column::make('debit')
  60. ->label('DEBIT')
  61. ->alignment(Alignment::Right),
  62. Column::make('credit')
  63. ->label('CREDIT')
  64. ->alignment(Alignment::Right),
  65. Column::make('balance')
  66. ->label('RUNNING BALANCE')
  67. ->alignment(Alignment::Right),
  68. ];
  69. }
  70. public function filtersForm(Form $form): Form
  71. {
  72. return $form
  73. ->columns(5)
  74. ->schema([
  75. Select::make('selectedAccount')
  76. ->label('Account')
  77. ->options($this->getAccountOptions())
  78. ->selectablePlaceholder(false)
  79. ->searchable(),
  80. $this->getDateRangeFormComponent(),
  81. Cluster::make([
  82. $this->getStartDateFormComponent(),
  83. $this->getEndDateFormComponent(),
  84. ])->extraFieldWrapperAttributes([
  85. 'class' => 'report-hidden-label',
  86. ]),
  87. Select::make('selectedEntity')
  88. ->label('Entity')
  89. ->options($this->getEntityOptions())
  90. ->searchable()
  91. ->selectablePlaceholder(false),
  92. Actions::make([
  93. Actions\Action::make('applyFilters')
  94. ->label('Update report')
  95. ->action('applyFilters')
  96. ->keyBindings(['mod+s'])
  97. ->button(),
  98. ])->alignEnd()->verticallyAlignEnd(),
  99. ]);
  100. }
  101. protected function getAccountOptions(): array
  102. {
  103. $accounts = Account::query()
  104. ->get()
  105. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  106. ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
  107. ->toArray();
  108. $allAccountsOption = [
  109. 'All Accounts' => ['all' => 'All Accounts'],
  110. ];
  111. return $allAccountsOption + $accounts;
  112. }
  113. protected function getEntityOptions(): array
  114. {
  115. $clients = Client::query()
  116. ->orderBy('name')
  117. ->pluck('name', 'id')
  118. ->toArray();
  119. $vendors = Vendor::query()
  120. ->orderBy('name')
  121. ->pluck('name', 'id')
  122. ->mapWithKeys(fn ($name, $id) => [-$id => $name])
  123. ->toArray();
  124. $allEntitiesOption = [
  125. 'All Entities' => ['all' => 'All Entities'],
  126. ];
  127. return $allEntitiesOption + [
  128. 'Clients' => $clients,
  129. 'Vendors' => $vendors,
  130. ];
  131. }
  132. protected function buildReport(array $columns): ReportDTO
  133. {
  134. return $this->reportService->buildAccountTransactionsReport(
  135. startDate: $this->getFormattedStartDate(),
  136. endDate: $this->getFormattedEndDate(),
  137. columns: $columns,
  138. accountId: $this->getFilterState('selectedAccount'),
  139. entityId: $this->getFilterState('selectedEntity'),
  140. );
  141. }
  142. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  143. {
  144. return new AccountTransactionReportTransformer($reportDTO);
  145. }
  146. public function exportCSV(): StreamedResponse
  147. {
  148. return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  149. }
  150. public function exportPDF(): StreamedResponse
  151. {
  152. return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  153. }
  154. public function getEmptyStateHeading(): string | Htmlable
  155. {
  156. return 'No Transactions Found';
  157. }
  158. public function getEmptyStateDescription(): string | Htmlable | null
  159. {
  160. return 'Adjust the account or date range, or start by creating a transaction.';
  161. }
  162. public function getEmptyStateIcon(): string
  163. {
  164. return 'heroicon-o-x-mark';
  165. }
  166. public function getEmptyStateActions(): array
  167. {
  168. return [
  169. Action::make('createTransaction')
  170. ->label('Create transaction')
  171. ->url(TransactionResource::getUrl()),
  172. ];
  173. }
  174. public function tableHasEmptyState(): bool
  175. {
  176. return empty($this->report?->getCategories());
  177. }
  178. }