Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AccountChart.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Filament\Company\Pages\Accounting;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Models\Accounting\Account;
  5. use App\Models\Accounting\Account as ChartModel;
  6. use App\Models\Accounting\AccountSubtype;
  7. use App\Services\AccountService;
  8. use App\Utilities\Accounting\AccountCode;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use Filament\Actions\Action;
  11. use Filament\Actions\CreateAction;
  12. use Filament\Actions\EditAction;
  13. use Filament\Forms\Components\Select;
  14. use Filament\Forms\Components\Textarea;
  15. use Filament\Forms\Components\TextInput;
  16. use Filament\Forms\Form;
  17. use Filament\Forms\Get;
  18. use Filament\Forms\Set;
  19. use Filament\Pages\Page;
  20. use Filament\Support\Enums\MaxWidth;
  21. use Illuminate\Support\Collection;
  22. use Livewire\Attributes\Computed;
  23. use Livewire\Attributes\Url;
  24. class AccountChart extends Page
  25. {
  26. protected static ?string $title = 'Chart of Accounts';
  27. protected static ?string $slug = 'accounting/chart';
  28. protected static string $view = 'filament.company.pages.accounting.chart';
  29. public ?ChartModel $chart = null;
  30. #[Url]
  31. public ?string $activeTab = null;
  32. protected AccountService $accountService;
  33. public function boot(AccountService $accountService): void
  34. {
  35. $this->accountService = $accountService;
  36. }
  37. public function mount(): void
  38. {
  39. $this->activeTab = $this->activeTab ?? AccountCategory::Asset->value;
  40. }
  41. public function getAccountBalance(Account $account): ?string
  42. {
  43. $company = $account->company;
  44. $startDate = $company->locale->fiscalYearStartDate();
  45. $endDate = $company->locale->fiscalYearEndDate();
  46. return $this->accountService->getEndingBalance($account, $startDate, $endDate)?->formatted();
  47. }
  48. protected function configureAction(Action $action): void
  49. {
  50. $action
  51. ->modalWidth(MaxWidth::TwoExtraLarge)
  52. ->stickyModalHeader()
  53. ->stickyModalFooter();
  54. }
  55. #[Computed]
  56. public function categories(): Collection
  57. {
  58. return AccountSubtype::withCount('accounts')
  59. ->get()
  60. ->groupBy('category');
  61. }
  62. public function editChartAction(): Action
  63. {
  64. return EditAction::make()
  65. ->iconButton()
  66. ->record($this->chart)
  67. ->name('editChart')
  68. ->label('Edit account')
  69. ->modalHeading('Edit Account')
  70. ->icon('heroicon-m-pencil-square')
  71. ->mountUsing(function (array $arguments, Form $form) {
  72. $chartId = $arguments['chart'];
  73. $this->chart = ChartModel::find($chartId);
  74. $form
  75. ->fill($this->chart->toArray())
  76. ->operation('edit')
  77. ->model($this->chart); // This is needed for form relationships to work (maybe a bug in Filament regarding passed arguments related to timing)
  78. })
  79. ->form($this->getChartForm());
  80. }
  81. public function createChartAction(): Action
  82. {
  83. return CreateAction::make()
  84. ->link()
  85. ->name('createChart')
  86. ->form($this->getChartForm())
  87. ->model(ChartModel::class)
  88. ->label('Add a new account')
  89. ->icon('heroicon-o-plus-circle')
  90. ->mountUsing(function (array $arguments, Form $form) {
  91. $subtypeId = $arguments['subtype'];
  92. $this->chart = new ChartModel([
  93. 'subtype_id' => $subtypeId,
  94. ]);
  95. if ($subtypeId) {
  96. $companyId = auth()->user()->currentCompany->id;
  97. $generatedCode = AccountCode::generate($companyId, $subtypeId);
  98. $this->chart->code = $generatedCode;
  99. }
  100. $form->fill($this->chart->toArray())
  101. ->operation('create');
  102. });
  103. }
  104. private function getChartForm(bool $useActiveTab = true): array
  105. {
  106. return [
  107. Select::make('subtype_id')
  108. ->label('Type')
  109. ->required()
  110. ->live()
  111. ->disabled(static fn (string $operation, ?ChartModel $record) => $operation === 'edit' && $record?->default === true)
  112. ->options($this->getChartSubtypeOptions($useActiveTab))
  113. ->afterStateUpdated(static function (?string $state, Set $set): void {
  114. if ($state) {
  115. $companyId = auth()->user()->currentCompany->id;
  116. $generatedCode = AccountCode::generate($companyId, $state);
  117. $set('code', $generatedCode);
  118. }
  119. }),
  120. TextInput::make('code')
  121. ->label('Code')
  122. ->required()
  123. ->validationAttribute('account code')
  124. ->unique(table: ChartModel::class, column: 'code', ignoreRecord: true)
  125. ->validateAccountCode(static fn (Get $get) => $get('subtype_id')),
  126. TextInput::make('name')
  127. ->label('Name')
  128. ->required(),
  129. Select::make('currency_code')
  130. ->localizeLabel('Currency')
  131. ->relationship('currency', 'name')
  132. ->default(CurrencyAccessor::getDefaultCurrency())
  133. ->preload()
  134. ->searchable()
  135. ->visible(function (Get $get): bool {
  136. return filled($get('subtype_id')) && AccountSubtype::find($get('subtype_id'))->multi_currency;
  137. })
  138. ->live(),
  139. Textarea::make('description')
  140. ->label('Description')
  141. ->autosize(),
  142. ];
  143. }
  144. private function getChartSubtypeOptions($useActiveTab = true): array
  145. {
  146. $subtypes = $useActiveTab ?
  147. AccountSubtype::where('category', $this->activeTab)->get() :
  148. AccountSubtype::all();
  149. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  150. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  151. ->toArray();
  152. }
  153. protected function getHeaderActions(): array
  154. {
  155. return [
  156. CreateAction::make()
  157. ->button()
  158. ->label('Add New Account')
  159. ->model(ChartModel::class)
  160. ->form($this->getChartForm(false)),
  161. ];
  162. }
  163. public function getCategoryLabel($categoryValue): string
  164. {
  165. return AccountCategory::from($categoryValue)->getLabel();
  166. }
  167. }