Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AccountChart.php 6.5KB

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