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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-8xl';
  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(5)
  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. ->selectablePlaceholder(false),
  94. Actions::make([
  95. Actions\Action::make('applyFilters')
  96. ->label('Update Report')
  97. ->action('applyFilters')
  98. ->keyBindings(['mod+s'])
  99. ->button(),
  100. ])->alignEnd()->verticallyAlignEnd(),
  101. ]);
  102. }
  103. protected function getAccountOptions(): array
  104. {
  105. $accounts = Account::query()
  106. ->get()
  107. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  108. ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
  109. ->toArray();
  110. $allAccountsOption = [
  111. 'All Accounts' => ['all' => 'All Accounts'],
  112. ];
  113. return $allAccountsOption + $accounts;
  114. }
  115. protected function getEntityOptions(): array
  116. {
  117. $clients = Client::query()
  118. ->orderBy('name')
  119. ->pluck('name', 'id')
  120. ->toArray();
  121. $vendors = Vendor::query()
  122. ->orderBy('name')
  123. ->pluck('name', 'id')
  124. ->mapWithKeys(fn ($name, $id) => [-$id => $name])
  125. ->toArray();
  126. $allEntitiesOption = [
  127. 'All Entities' => ['all' => 'All Entities'],
  128. ];
  129. return $allEntitiesOption + [
  130. 'Clients' => $clients,
  131. 'Vendors' => $vendors,
  132. ];
  133. }
  134. protected function buildReport(array $columns): ReportDTO
  135. {
  136. return $this->reportService->buildAccountTransactionsReport(
  137. startDate: $this->getFormattedStartDate(),
  138. endDate: $this->getFormattedEndDate(),
  139. columns: $columns,
  140. accountId: $this->getFilterState('selectedAccount'),
  141. entityId: $this->getFilterState('selectedEntity'),
  142. );
  143. }
  144. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  145. {
  146. return new AccountTransactionReportTransformer($reportDTO);
  147. }
  148. public function exportCSV(): StreamedResponse
  149. {
  150. return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  151. }
  152. public function exportPDF(): StreamedResponse
  153. {
  154. return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  155. }
  156. public function getEmptyStateHeading(): string | Htmlable
  157. {
  158. return 'No Transactions Found';
  159. }
  160. public function getEmptyStateDescription(): string | Htmlable | null
  161. {
  162. return 'Adjust the account or date range, or start by creating a transaction.';
  163. }
  164. public function getEmptyStateIcon(): string
  165. {
  166. return 'heroicon-o-x-mark';
  167. }
  168. public function getEmptyStateActions(): array
  169. {
  170. return [
  171. Action::make('createTransaction')
  172. ->label('Create Transaction')
  173. ->url(Transactions::getUrl()),
  174. ];
  175. }
  176. public function hasNoTransactionsForSelectedAccount(): bool
  177. {
  178. $query = JournalEntry::query();
  179. $selectedAccountId = $this->getFilterState('selectedAccount');
  180. if ($selectedAccountId !== 'all') {
  181. $query->where('account_id', $selectedAccountId);
  182. }
  183. if ($this->getFilterState('startDate') && $this->getFilterState('endDate')) {
  184. $query->whereHas('transaction', function (Builder $query) {
  185. $query->whereBetween('posted_at', [$this->getFormattedStartDate(), $this->getFormattedEndDate()]);
  186. });
  187. }
  188. return $query->doesntExist();
  189. }
  190. public function tableHasEmptyState(): bool
  191. {
  192. return $this->hasNoTransactionsForSelectedAccount();
  193. }
  194. }