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.

AccountChart.php 6.5KB

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