123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
-
- namespace App\Filament\Company\Pages\Reports;
-
- use App\Contracts\ExportableReport;
- use App\DTO\ReportDTO;
- use App\Filament\Company\Pages\Accounting\Transactions;
- use App\Models\Accounting\Account;
- use App\Models\Accounting\JournalEntry;
- use App\Services\ExportService;
- use App\Services\ReportService;
- use App\Support\Column;
- use App\Transformers\AccountTransactionReportTransformer;
- use Filament\Forms\Components\Actions;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Form;
- use Filament\Support\Enums\Alignment;
- use Filament\Support\Enums\MaxWidth;
- use Filament\Tables\Actions\Action;
- use Guava\FilamentClusters\Forms\Cluster;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Collection;
- use Symfony\Component\HttpFoundation\StreamedResponse;
-
- class AccountTransactions extends BaseReportPage
- {
- protected static string $view = 'filament.company.pages.reports.account-transactions';
-
- protected static ?string $slug = 'reports/account-transactions';
-
- protected static bool $shouldRegisterNavigation = false;
-
- protected ReportService $reportService;
-
- protected ExportService $exportService;
-
- public function boot(ReportService $reportService, ExportService $exportService): void
- {
- $this->reportService = $reportService;
- $this->exportService = $exportService;
- }
-
- public function getMaxContentWidth(): MaxWidth | string | null
- {
- return 'max-w-[90rem]';
- }
-
- protected function initializeDefaultFilters(): void
- {
- if (empty($this->getFilterState('selectedAccount'))) {
- $this->setFilterState('selectedAccount', 'all');
- }
- }
-
- /**
- * @return array<Column>
- */
- public function getTable(): array
- {
- return [
- Column::make('date')
- ->label('DATE')
- ->markAsDate()
- ->alignment(Alignment::Left),
- Column::make('description')
- ->label('DESCRIPTION')
- ->alignment(Alignment::Left),
- Column::make('debit')
- ->label('DEBIT')
- ->alignment(Alignment::Right),
- Column::make('credit')
- ->label('CREDIT')
- ->alignment(Alignment::Right),
- Column::make('balance')
- ->label('RUNNING BALANCE')
- ->alignment(Alignment::Right),
- ];
- }
-
- public function filtersForm(Form $form): Form
- {
- return $form
- ->columns(4)
- ->schema([
- Select::make('selectedAccount')
- ->label('Account')
- ->options($this->getAccountOptions())
- ->selectablePlaceholder(false)
- ->searchable(),
- $this->getDateRangeFormComponent(),
- Cluster::make([
- $this->getStartDateFormComponent(),
- $this->getEndDateFormComponent(),
- ])->extraFieldWrapperAttributes([
- 'class' => 'report-hidden-label',
- ]),
- Actions::make([
- Actions\Action::make('applyFilters')
- ->label('Update Report')
- ->action('applyFilters')
- ->keyBindings(['mod+s'])
- ->button(),
- ])->alignEnd()->verticallyAlignEnd(),
- ]);
- }
-
- protected function getAccountOptions(): array
- {
- $accounts = Account::query()
- ->get()
- ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
- ->map(fn (Collection $accounts) => $accounts->pluck('name', 'id'))
- ->toArray();
-
- $allAccountsOption = [
- 'All Accounts' => ['all' => 'All Accounts'],
- ];
-
- return $allAccountsOption + $accounts;
- }
-
- protected function buildReport(array $columns): ReportDTO
- {
- return $this->reportService->buildAccountTransactionsReport($this->getFormattedStartDate(), $this->getFormattedEndDate(), $columns, $this->getFilterState('selectedAccount'));
- }
-
- protected function getTransformer(ReportDTO $reportDTO): ExportableReport
- {
- return new AccountTransactionReportTransformer($reportDTO);
- }
-
- public function exportCSV(): StreamedResponse
- {
- return $this->exportService->exportToCsv($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
- }
-
- public function exportPDF(): StreamedResponse
- {
- return $this->exportService->exportToPdf($this->company, $this->report, $this->getFilterState('startDate'), $this->getFilterState('endDate'));
- }
-
- public function getEmptyStateHeading(): string | Htmlable
- {
- return 'No Transactions Found';
- }
-
- public function getEmptyStateDescription(): string | Htmlable | null
- {
- return 'Adjust the account or date range, or start by creating a transaction.';
- }
-
- public function getEmptyStateIcon(): string
- {
- return 'heroicon-o-x-mark';
- }
-
- public function getEmptyStateActions(): array
- {
- return [
- Action::make('createTransaction')
- ->label('Create Transaction')
- ->url(Transactions::getUrl()),
- ];
- }
-
- public function hasNoTransactionsForSelectedAccount(): bool
- {
- $query = JournalEntry::query();
- $selectedAccountId = $this->getFilterState('selectedAccount');
-
- if ($selectedAccountId !== 'all') {
- $query->where('account_id', $selectedAccountId);
- }
-
- if ($this->getFilterState('startDate') && $this->getFilterState('endDate')) {
- $query->whereHas('transaction', function (Builder $query) {
- $query->whereBetween('posted_at', [$this->getFormattedStartDate(), $this->getFormattedEndDate()]);
- });
- }
-
- return $query->doesntExist();
- }
-
- public function tableHasEmptyState(): bool
- {
- return $this->hasNoTransactionsForSelectedAccount();
- }
- }
|