Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AccountTransactions.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\Tables\Actions\Action;
  19. use Guava\FilamentClusters\Forms\Cluster;
  20. use Illuminate\Contracts\Support\Htmlable;
  21. use Illuminate\Database\Eloquent\Builder;
  22. use Illuminate\Support\Collection;
  23. use Symfony\Component\HttpFoundation\StreamedResponse;
  24. class AccountTransactions extends BaseReportPage
  25. {
  26. protected static string $view = 'filament.company.pages.reports.account-transactions';
  27. protected ReportService $reportService;
  28. protected ExportService $exportService;
  29. public function boot(ReportService $reportService, ExportService $exportService): void
  30. {
  31. $this->reportService = $reportService;
  32. $this->exportService = $exportService;
  33. }
  34. protected function initializeDefaultFilters(): void
  35. {
  36. if (empty($this->getFilterState('selectedAccount'))) {
  37. $this->setFilterState('selectedAccount', 'all');
  38. }
  39. if (empty($this->getFilterState('basis'))) {
  40. $this->setFilterState('basis', 'accrual');
  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(3)
  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('basis')
  88. ->label('Accounting Basis')
  89. ->options([
  90. 'accrual' => 'Accrual (Paid & Unpaid)',
  91. 'cash' => 'Cash Basis (Paid)',
  92. ])
  93. ->selectablePlaceholder(false),
  94. Select::make('selectedEntity')
  95. ->label('Entity')
  96. ->options($this->getEntityOptions())
  97. ->searchable()
  98. ->selectablePlaceholder(false),
  99. Actions::make([
  100. Actions\Action::make('applyFilters')
  101. ->label('Update Report')
  102. ->action('applyFilters')
  103. ->keyBindings(['mod+s'])
  104. ->button(),
  105. ])->alignEnd()->verticallyAlignEnd(),
  106. ]);
  107. }
  108. protected function getAccountOptions(): array
  109. {
  110. $accounts = Account::query()
  111. ->get()
  112. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  113. ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
  114. ->toArray();
  115. $allAccountsOption = [
  116. 'All Accounts' => ['all' => 'All Accounts'],
  117. ];
  118. return $allAccountsOption + $accounts;
  119. }
  120. protected function getEntityOptions(): array
  121. {
  122. $clients = Client::query()
  123. ->orderBy('name')
  124. ->pluck('name', 'id')
  125. ->toArray();
  126. $vendors = Vendor::query()
  127. ->orderBy('name')
  128. ->pluck('name', 'id')
  129. ->mapWithKeys(fn ($name, $id) => [-$id => $name])
  130. ->toArray();
  131. $allEntitiesOption = [
  132. 'All Entities' => ['all' => 'All Entities'],
  133. ];
  134. return $allEntitiesOption + [
  135. 'Clients' => $clients,
  136. 'Vendors' => $vendors,
  137. ];
  138. }
  139. protected function buildReport(array $columns): ReportDTO
  140. {
  141. return $this->reportService->buildAccountTransactionsReport(
  142. startDate: $this->getFormattedStartDate(),
  143. endDate: $this->getFormattedEndDate(),
  144. columns: $columns,
  145. accountId: $this->getFilterState('selectedAccount'),
  146. basis: $this->getFilterState('basis'),
  147. entityId: $this->getFilterState('selectedEntity'),
  148. );
  149. }
  150. protected function getTransformer(ReportDTO $reportDTO): ExportableReport
  151. {
  152. return new AccountTransactionReportTransformer($reportDTO);
  153. }
  154. public function exportCSV(): StreamedResponse
  155. {
  156. return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  157. }
  158. public function exportPDF(): StreamedResponse
  159. {
  160. return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
  161. }
  162. public function getEmptyStateHeading(): string | Htmlable
  163. {
  164. return 'No Transactions Found';
  165. }
  166. public function getEmptyStateDescription(): string | Htmlable | null
  167. {
  168. return 'Adjust the account or date range, or start by creating a transaction.';
  169. }
  170. public function getEmptyStateIcon(): string
  171. {
  172. return 'heroicon-o-x-mark';
  173. }
  174. public function getEmptyStateActions(): array
  175. {
  176. return [
  177. Action::make('createTransaction')
  178. ->label('Create Transaction')
  179. ->url(Transactions::getUrl()),
  180. ];
  181. }
  182. public function hasNoTransactionsForSelectedAccount(): bool
  183. {
  184. $query = JournalEntry::query();
  185. $selectedAccountId = $this->getFilterState('selectedAccount');
  186. if ($selectedAccountId !== 'all') {
  187. $query->where('account_id', $selectedAccountId);
  188. }
  189. if ($this->getFilterState('startDate') && $this->getFilterState('endDate')) {
  190. $query->whereHas('transaction', function (Builder $query) {
  191. $query->whereBetween('posted_at', [$this->getFormattedStartDate(), $this->getFormattedEndDate()]);
  192. });
  193. }
  194. return $query->doesntExist();
  195. }
  196. public function tableHasEmptyState(): bool
  197. {
  198. return $this->hasNoTransactionsForSelectedAccount();
  199. }
  200. }