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.

AccountBalances.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\DTO\AccountBalanceReportDTO;
  4. use App\Forms\Components\DateRangeSelect;
  5. use App\Models\Company;
  6. use App\Services\AccountBalancesExportService;
  7. use App\Services\AccountService;
  8. use Barryvdh\DomPDF\Facade\Pdf;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\ActionGroup;
  11. use Filament\Forms\Components\DatePicker;
  12. use Filament\Forms\Components\Split;
  13. use Filament\Forms\Form;
  14. use Filament\Forms\Set;
  15. use Filament\Pages\Page;
  16. use Filament\Support\Enums\IconPosition;
  17. use Filament\Support\Enums\IconSize;
  18. use Illuminate\Support\Carbon;
  19. use Symfony\Component\HttpFoundation\StreamedResponse;
  20. class AccountBalances extends Page
  21. {
  22. protected static string $view = 'filament.company.pages.reports.account-balances';
  23. protected static ?string $slug = 'reports/account-balances';
  24. public string $startDate = '';
  25. public string $endDate = '';
  26. public string $dateRange = '';
  27. public string $fiscalYearStartDate = '';
  28. public string $fiscalYearEndDate = '';
  29. public Company $company;
  30. public AccountBalanceReportDTO $accountBalanceReport;
  31. protected AccountService $accountService;
  32. protected AccountBalancesExportService $accountBalancesExportService;
  33. public function boot(AccountService $accountService, AccountBalancesExportService $accountBalancesExportService): void
  34. {
  35. $this->accountService = $accountService;
  36. $this->accountBalancesExportService = $accountBalancesExportService;
  37. }
  38. public function mount(): void
  39. {
  40. $this->company = auth()->user()->currentCompany;
  41. $this->fiscalYearStartDate = $this->company->locale->fiscalYearStartDate();
  42. $this->fiscalYearEndDate = $this->company->locale->fiscalYearEndDate();
  43. $this->dateRange = $this->getDefaultDateRange();
  44. $this->setDateRange(Carbon::parse($this->fiscalYearStartDate), Carbon::parse($this->fiscalYearEndDate));
  45. $this->loadAccountBalances();
  46. }
  47. public function getDefaultDateRange(): string
  48. {
  49. return 'FY-' . now()->year;
  50. }
  51. public function loadAccountBalances(): void
  52. {
  53. $this->accountBalanceReport = $this->accountService->buildAccountBalanceReport($this->startDate, $this->endDate);
  54. }
  55. protected function getHeaderActions(): array
  56. {
  57. return [
  58. ActionGroup::make([
  59. Action::make('exportCSV')
  60. ->label('CSV')
  61. ->action(fn () => $this->exportCSV()),
  62. Action::make('exportPDF')
  63. ->label('PDF')
  64. ->action(fn () => $this->exportPDF()),
  65. ])
  66. ->label('Export')
  67. ->button()
  68. ->outlined()
  69. ->dropdownWidth('max-w-[7rem]')
  70. ->dropdownPlacement('bottom-end')
  71. ->icon('heroicon-c-chevron-down')
  72. ->iconSize(IconSize::Small)
  73. ->iconPosition(IconPosition::After),
  74. ];
  75. }
  76. public function exportCSV(): StreamedResponse
  77. {
  78. return $this->accountBalancesExportService->exportToCsv($this->company, $this->accountBalanceReport, $this->startDate, $this->endDate);
  79. }
  80. public function exportPDF(): StreamedResponse
  81. {
  82. $pdf = Pdf::loadView('components.company.reports.account-balances', [
  83. 'accountBalanceReport' => $this->accountBalanceReport,
  84. 'startDate' => Carbon::parse($this->startDate)->format('M d, Y'),
  85. 'endDate' => Carbon::parse($this->endDate)->format('M d, Y'),
  86. ])->setPaper('a4');
  87. return response()->streamDownload(function () use ($pdf) {
  88. echo $pdf->stream();
  89. }, 'account-balances.pdf');
  90. }
  91. public function form(Form $form): Form
  92. {
  93. return $form
  94. ->schema([
  95. Split::make([
  96. DateRangeSelect::make('dateRange')
  97. ->label('Date Range')
  98. ->selectablePlaceholder(false)
  99. ->startDateField('startDate')
  100. ->endDateField('endDate'),
  101. DatePicker::make('startDate')
  102. ->label('Start Date')
  103. ->displayFormat('Y-m-d')
  104. ->afterStateUpdated(static function (Set $set) {
  105. $set('dateRange', 'Custom');
  106. }),
  107. DatePicker::make('endDate')
  108. ->label('End Date')
  109. ->displayFormat('Y-m-d')
  110. ->afterStateUpdated(static function (Set $set) {
  111. $set('dateRange', 'Custom');
  112. }),
  113. ])->live(),
  114. ]);
  115. }
  116. public function setDateRange(Carbon $start, Carbon $end): void
  117. {
  118. $this->startDate = $start->format('Y-m-d');
  119. $this->endDate = $end->isFuture() ? now()->format('Y-m-d') : $end->format('Y-m-d');
  120. }
  121. public static function shouldRegisterNavigation(): bool
  122. {
  123. return false;
  124. }
  125. }