Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\AccountSubtype;
  6. use App\Utilities\Accounting\AccountCode;
  7. use App\Utilities\Currency\CurrencyAccessor;
  8. use Filament\Actions\Action;
  9. use Filament\Actions\CreateAction;
  10. use Filament\Actions\EditAction;
  11. use Filament\Forms\Components\Checkbox;
  12. use Filament\Forms\Components\Component;
  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. #[Url]
  30. public ?string $activeTab = AccountCategory::Asset->value;
  31. protected function configureAction(Action $action): void
  32. {
  33. $action
  34. ->modal()
  35. ->modalWidth(MaxWidth::TwoExtraLarge);
  36. }
  37. #[Computed]
  38. public function categories(): Collection
  39. {
  40. return AccountSubtype::withCount('accounts')
  41. ->get()
  42. ->groupBy('category');
  43. }
  44. public function editChartAction(): Action
  45. {
  46. return EditAction::make()
  47. ->iconButton()
  48. ->name('editChart')
  49. ->label('Edit account')
  50. ->modalHeading('Edit Account')
  51. ->icon('heroicon-m-pencil-square')
  52. ->record(fn (array $arguments) => Account::find($arguments['chart']))
  53. ->form(fn (Form $form) => $this->getChartForm($form)->operation('edit'));
  54. }
  55. public function createChartAction(): Action
  56. {
  57. return CreateAction::make()
  58. ->link()
  59. ->name('createChart')
  60. ->model(Account::class)
  61. ->label('Add a new account')
  62. ->icon('heroicon-o-plus-circle')
  63. ->form(fn (Form $form) => $this->getChartForm($form)->operation('create'))
  64. ->fillForm(fn (array $arguments): array => $this->getChartFormDefaults($arguments['subtype']));
  65. }
  66. private function getChartFormDefaults(int $subtypeId): array
  67. {
  68. $accountSubtype = AccountSubtype::find($subtypeId);
  69. $generatedCode = AccountCode::generate($accountSubtype);
  70. return [
  71. 'subtype_id' => $subtypeId,
  72. 'code' => $generatedCode,
  73. ];
  74. }
  75. private function getChartForm(Form $form, bool $useActiveTab = true): Form
  76. {
  77. return $form
  78. ->schema([
  79. $this->getTypeFormComponent($useActiveTab),
  80. $this->getCodeFormComponent(),
  81. $this->getNameFormComponent(),
  82. $this->getCurrencyFormComponent(),
  83. $this->getDescriptionFormComponent(),
  84. $this->getArchiveFormComponent(),
  85. ]);
  86. }
  87. protected function getTypeFormComponent(bool $useActiveTab = true): Component
  88. {
  89. return Select::make('subtype_id')
  90. ->label('Type')
  91. ->required()
  92. ->live()
  93. ->disabled(static function (string $operation): bool {
  94. return $operation === 'edit';
  95. })
  96. ->options($this->getChartSubtypeOptions($useActiveTab))
  97. ->afterStateUpdated(static function (?string $state, Set $set): void {
  98. if ($state) {
  99. $accountSubtype = AccountSubtype::find($state);
  100. $generatedCode = AccountCode::generate($accountSubtype);
  101. $set('code', $generatedCode);
  102. }
  103. });
  104. }
  105. protected function getCodeFormComponent(): Component
  106. {
  107. return TextInput::make('code')
  108. ->label('Code')
  109. ->required()
  110. ->validationAttribute('account code')
  111. ->unique(table: Account::class, column: 'code', ignoreRecord: true)
  112. ->validateAccountCode(static fn (Get $get) => $get('subtype_id'));
  113. }
  114. protected function getNameFormComponent(): Component
  115. {
  116. return TextInput::make('name')
  117. ->label('Name')
  118. ->required();
  119. }
  120. protected function getCurrencyFormComponent()
  121. {
  122. return Select::make('currency_code')
  123. ->localizeLabel('Currency')
  124. ->relationship('currency', 'name')
  125. ->default(CurrencyAccessor::getDefaultCurrency())
  126. ->preload()
  127. ->searchable()
  128. ->disabled(static function (string $operation): bool {
  129. return $operation === 'edit';
  130. })
  131. ->visible(function (Get $get): bool {
  132. return filled($get('subtype_id')) && AccountSubtype::find($get('subtype_id'))->multi_currency;
  133. })
  134. ->live();
  135. }
  136. protected function getDescriptionFormComponent(): Component
  137. {
  138. return Textarea::make('description')
  139. ->label('Description')
  140. ->autosize();
  141. }
  142. protected function getArchiveFormComponent(): Component
  143. {
  144. return Checkbox::make('archived')
  145. ->label('Archive Account')
  146. ->helperText('Archived accounts will not be available for selection in transactions.')
  147. ->hidden(static function (string $operation): bool {
  148. return $operation === 'create';
  149. });
  150. }
  151. private function getChartSubtypeOptions($useActiveTab = true): array
  152. {
  153. $subtypes = $useActiveTab ?
  154. AccountSubtype::where('category', $this->activeTab)->get() :
  155. AccountSubtype::all();
  156. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  157. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  158. ->toArray();
  159. }
  160. protected function getHeaderActions(): array
  161. {
  162. return [
  163. CreateAction::make()
  164. ->button()
  165. ->label('Add New Account')
  166. ->model(Account::class)
  167. ->form(fn (Form $form) => $this->getChartForm($form, false)->operation('create')),
  168. ];
  169. }
  170. public function getCategoryLabel($categoryValue): string
  171. {
  172. return AccountCategory::from($categoryValue)->getPluralLabel();
  173. }
  174. }