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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Component;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Components\Textarea;
  14. use Filament\Forms\Components\TextInput;
  15. use Filament\Forms\Form;
  16. use Filament\Forms\Get;
  17. use Filament\Forms\Set;
  18. use Filament\Pages\Page;
  19. use Filament\Support\Enums\MaxWidth;
  20. use Illuminate\Support\Collection;
  21. use Livewire\Attributes\Computed;
  22. use Livewire\Attributes\Url;
  23. class AccountChart extends Page
  24. {
  25. protected static ?string $title = 'Chart of Accounts';
  26. protected static ?string $slug = 'accounting/chart';
  27. protected static string $view = 'filament.company.pages.accounting.chart';
  28. #[Url]
  29. public ?string $activeTab = null;
  30. public function mount(): void
  31. {
  32. $this->activeTab = $this->activeTab ?? AccountCategory::Asset->value;
  33. }
  34. protected function configureAction(Action $action): void
  35. {
  36. $action
  37. ->modal()
  38. ->modalWidth(MaxWidth::TwoExtraLarge);
  39. }
  40. #[Computed]
  41. public function categories(): Collection
  42. {
  43. return AccountSubtype::withCount('accounts')
  44. ->get()
  45. ->groupBy('category');
  46. }
  47. public function editChartAction(): Action
  48. {
  49. return EditAction::make()
  50. ->iconButton()
  51. ->name('editChart')
  52. ->label('Edit account')
  53. ->modalHeading('Edit Account')
  54. ->icon('heroicon-m-pencil-square')
  55. ->record(fn (array $arguments) => Account::find($arguments['chart']))
  56. ->form(fn (Form $form) => $this->getChartForm($form)->operation('edit'));
  57. }
  58. public function createChartAction(): Action
  59. {
  60. return CreateAction::make()
  61. ->link()
  62. ->name('createChart')
  63. ->model(Account::class)
  64. ->label('Add a new account')
  65. ->icon('heroicon-o-plus-circle')
  66. ->form(fn (Form $form) => $this->getChartForm($form)->operation('create'))
  67. ->fillForm(fn (array $arguments): array => $this->getChartFormDefaults($arguments['subtype']));
  68. }
  69. private function getChartFormDefaults(int $subtypeId): array
  70. {
  71. $accountSubtype = AccountSubtype::find($subtypeId);
  72. $generatedCode = AccountCode::generate($accountSubtype);
  73. return [
  74. 'subtype_id' => $subtypeId,
  75. 'code' => $generatedCode,
  76. ];
  77. }
  78. private function getChartForm(Form $form, bool $useActiveTab = true): Form
  79. {
  80. return $form
  81. ->schema([
  82. $this->getTypeFormComponent($useActiveTab),
  83. $this->getCodeFormComponent(),
  84. $this->getNameFormComponent(),
  85. $this->getCurrencyFormComponent(),
  86. $this->getDescriptionFormComponent(),
  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. 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(Account::class)
  160. ->form(fn (Form $form) => $this->getChartForm($form, false)->operation('create')),
  161. ];
  162. }
  163. public function getCategoryLabel($categoryValue): string
  164. {
  165. return AccountCategory::from($categoryValue)->getPluralLabel();
  166. }
  167. }