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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Filament\Company\Pages\Reports;
  3. use App\DTO\AccountBalanceReportDTO;
  4. use App\Models\Company;
  5. use App\Services\AccountBalancesExportService;
  6. use App\Services\AccountService;
  7. use Barryvdh\DomPDF\Facade\Pdf;
  8. use Carbon\CarbonPeriod;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\ActionGroup;
  11. use Filament\Forms\Components\DatePicker;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Components\Split;
  14. use Filament\Forms\Form;
  15. use Filament\Forms\Set;
  16. use Filament\Pages\Page;
  17. use Filament\Support\Enums\IconPosition;
  18. use Filament\Support\Enums\IconSize;
  19. use Illuminate\Support\Carbon;
  20. use Symfony\Component\HttpFoundation\StreamedResponse;
  21. class AccountBalances extends Page
  22. {
  23. protected static string $view = 'filament.company.pages.reports.account-balances';
  24. protected static ?string $slug = 'reports/account-balances';
  25. public string $startDate = '';
  26. public string $endDate = '';
  27. public string $dateRange = '';
  28. public string $fiscalYearStartDate = '';
  29. public string $fiscalYearEndDate = '';
  30. public Company $company;
  31. public AccountBalanceReportDTO $accountBalanceReport;
  32. protected AccountService $accountService;
  33. protected AccountBalancesExportService $accountBalancesExportService;
  34. public function boot(AccountService $accountService, AccountBalancesExportService $accountBalancesExportService): void
  35. {
  36. $this->accountService = $accountService;
  37. $this->accountBalancesExportService = $accountBalancesExportService;
  38. }
  39. public function mount(): void
  40. {
  41. $this->company = auth()->user()->currentCompany;
  42. $this->fiscalYearStartDate = $this->company->locale->fiscalYearStartDate();
  43. $this->fiscalYearEndDate = $this->company->locale->fiscalYearEndDate();
  44. $this->dateRange = $this->getDefaultDateRange();
  45. $this->updateDateRange($this->dateRange);
  46. $this->loadAccountBalances();
  47. }
  48. public function getDefaultDateRange(): string
  49. {
  50. return 'FY-' . now()->year;
  51. }
  52. public function loadAccountBalances(): void
  53. {
  54. $this->accountBalanceReport = $this->accountService->buildAccountBalanceReport($this->startDate, $this->endDate);
  55. }
  56. protected function getHeaderActions(): array
  57. {
  58. return [
  59. ActionGroup::make([
  60. Action::make('exportCSV')
  61. ->label('CSV')
  62. ->action(fn () => $this->exportCSV()),
  63. Action::make('exportPDF')
  64. ->label('PDF')
  65. ->action(fn () => $this->exportPDF()),
  66. ])
  67. ->label('Export')
  68. ->button()
  69. ->outlined()
  70. ->dropdownWidth('max-w-[7rem]')
  71. ->dropdownPlacement('bottom-end')
  72. ->icon('heroicon-c-chevron-down')
  73. ->iconSize(IconSize::Small)
  74. ->iconPosition(IconPosition::After),
  75. ];
  76. }
  77. public function exportCSV(): StreamedResponse
  78. {
  79. return $this->accountBalancesExportService->exportToCsv($this->company, $this->accountBalanceReport, $this->startDate, $this->endDate);
  80. }
  81. public function exportPDF(): StreamedResponse
  82. {
  83. $pdf = Pdf::loadView('components.company.reports.account-balances', [
  84. 'accountBalanceReport' => $this->accountBalanceReport,
  85. 'startDate' => Carbon::parse($this->startDate)->format('M d, Y'),
  86. 'endDate' => Carbon::parse($this->endDate)->format('M d, Y'),
  87. ])->setPaper('a4');
  88. return response()->streamDownload(function () use ($pdf) {
  89. echo $pdf->stream();
  90. }, 'account-balances.pdf');
  91. }
  92. public function form(Form $form): Form
  93. {
  94. return $form
  95. ->schema([
  96. Split::make([
  97. Select::make('dateRange')
  98. ->label('Date Range')
  99. ->options($this->getDateRangeOptions())
  100. ->selectablePlaceholder(false)
  101. ->afterStateUpdated(function ($state) {
  102. $this->updateDateRange($state);
  103. }),
  104. DatePicker::make('startDate')
  105. ->label('Start Date')
  106. ->displayFormat('Y-m-d')
  107. ->afterStateUpdated(static function (Set $set) {
  108. $set('dateRange', 'Custom');
  109. }),
  110. DatePicker::make('endDate')
  111. ->label('End Date')
  112. ->displayFormat('Y-m-d')
  113. ->afterStateUpdated(static function (Set $set) {
  114. $set('dateRange', 'Custom');
  115. }),
  116. ])->live(),
  117. ]);
  118. }
  119. public function getDateRangeOptions(): array
  120. {
  121. $earliestDate = Carbon::parse($this->accountService->getEarliestTransactionDate());
  122. $currentDate = now();
  123. $fiscalYearStartCurrent = Carbon::parse($this->fiscalYearStartDate);
  124. $options = [
  125. 'Fiscal Year' => [],
  126. 'Fiscal Quarter' => [],
  127. 'Calendar Year' => [],
  128. 'Calendar Quarter' => [],
  129. 'Month' => [],
  130. 'Custom' => [],
  131. ];
  132. $period = CarbonPeriod::create($earliestDate, '1 month', $currentDate);
  133. foreach ($period as $date) {
  134. $options['Fiscal Year']['FY-' . $date->year] = $date->year;
  135. $fiscalYearStart = $fiscalYearStartCurrent->copy()->subYears($currentDate->year - $date->year);
  136. for ($i = 0; $i < 4; $i++) {
  137. $quarterNumber = $i + 1;
  138. $quarterStart = $fiscalYearStart->copy()->addMonths(($quarterNumber - 1) * 3);
  139. $quarterEnd = $quarterStart->copy()->addMonths(3)->subDay();
  140. if ($quarterStart->lessThanOrEqualTo($currentDate) && $quarterEnd->greaterThanOrEqualTo($earliestDate)) {
  141. $options['Fiscal Quarter']['FQ-' . $quarterNumber . '-' . $date->year] = 'Q' . $quarterNumber . ' ' . $date->year;
  142. }
  143. }
  144. $options['Calendar Year']['Y-' . $date->year] = $date->year;
  145. $quarterKey = 'Q-' . $date->quarter . '-' . $date->year;
  146. $options['Calendar Quarter'][$quarterKey] = 'Q' . $date->quarter . ' ' . $date->year;
  147. $options['Month']['M-' . $date->format('Y-m')] = $date->format('F Y');
  148. $options['Custom']['Custom'] = 'Custom';
  149. }
  150. $options['Fiscal Year'] = array_reverse($options['Fiscal Year'], true);
  151. $options['Fiscal Quarter'] = array_reverse($options['Fiscal Quarter'], true);
  152. $options['Calendar Year'] = array_reverse($options['Calendar Year'], true);
  153. $options['Calendar Quarter'] = array_reverse($options['Calendar Quarter'], true);
  154. $options['Month'] = array_reverse($options['Month'], true);
  155. return $options;
  156. }
  157. public function updateDateRange($state): void
  158. {
  159. [$type, $param1, $param2] = explode('-', $state) + [null, null, null];
  160. $this->processDateRange($type, $param1, $param2);
  161. }
  162. public function processDateRange($type, $param1, $param2): void
  163. {
  164. match ($type) {
  165. 'FY' => $this->processFiscalYear($param1),
  166. 'FQ' => $this->processFiscalQuarter($param1, $param2),
  167. 'Y' => $this->processCalendarYear($param1),
  168. 'Q' => $this->processCalendarQuarter($param1, $param2),
  169. 'M' => $this->processMonth("{$param1}-{$param2}"),
  170. 'Custom' => null,
  171. };
  172. }
  173. public function processFiscalYear($year): void
  174. {
  175. $currentYear = now()->year;
  176. $diff = $currentYear - $year;
  177. $fiscalYearStart = Carbon::parse($this->fiscalYearStartDate)->subYears($diff);
  178. $fiscalYearEnd = Carbon::parse($this->fiscalYearEndDate)->subYears($diff);
  179. $this->setDateRange($fiscalYearStart, $fiscalYearEnd);
  180. }
  181. public function processFiscalQuarter($quarter, $year): void
  182. {
  183. $currentYear = now()->year;
  184. $diff = $currentYear - $year;
  185. $fiscalYearStart = Carbon::parse($this->company->locale->fiscal_year_start_date)->subYears($diff);
  186. $quarterStart = $fiscalYearStart->copy()->addMonths(($quarter - 1) * 3);
  187. $quarterEnd = $quarterStart->copy()->addMonths(3)->subDay();
  188. $this->setDateRange($quarterStart, $quarterEnd);
  189. }
  190. public function processCalendarYear($year): void
  191. {
  192. $start = Carbon::createFromDate($year)->startOfYear();
  193. $end = Carbon::createFromDate($year)->endOfYear();
  194. $this->setDateRange($start, $end);
  195. }
  196. public function processCalendarQuarter($quarter, $year): void
  197. {
  198. $month = ($quarter - 1) * 3 + 1;
  199. $start = Carbon::createFromDate($year, $month, 1);
  200. $end = Carbon::createFromDate($year, $month, 1)->endOfQuarter();
  201. $this->setDateRange($start, $end);
  202. }
  203. public function processMonth($yearMonth): void
  204. {
  205. $start = Carbon::parse($yearMonth)->startOfMonth();
  206. $end = Carbon::parse($yearMonth)->endOfMonth();
  207. $this->setDateRange($start, $end);
  208. }
  209. public function setDateRange(Carbon $start, Carbon $end): void
  210. {
  211. $this->startDate = $start->format('Y-m-d');
  212. $this->endDate = $end->isFuture() ? now()->format('Y-m-d') : $end->format('Y-m-d');
  213. }
  214. public static function shouldRegisterNavigation(): bool
  215. {
  216. return false;
  217. }
  218. }