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

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