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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Filament\Company\Pages\Accounting;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Banking\BankAccountType;
  5. use App\Filament\Forms\Components\CreateCurrencySelect;
  6. use App\Models\Accounting\Account;
  7. use App\Models\Accounting\AccountSubtype;
  8. use App\Utilities\Accounting\AccountCode;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\CreateAction;
  11. use Filament\Actions\EditAction;
  12. use Filament\Forms\Components\Checkbox;
  13. use Filament\Forms\Components\Component;
  14. use Filament\Forms\Components\Group;
  15. use Filament\Forms\Components\Select;
  16. use Filament\Forms\Components\Textarea;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Forms\Form;
  19. use Filament\Forms\Get;
  20. use Filament\Forms\Set;
  21. use Filament\Pages\Page;
  22. use Filament\Support\Enums\MaxWidth;
  23. use Illuminate\Support\Collection;
  24. use Illuminate\Support\Facades\Auth;
  25. use Illuminate\Validation\Rules\Unique;
  26. use Livewire\Attributes\Computed;
  27. use Livewire\Attributes\Url;
  28. class AccountChart extends Page
  29. {
  30. protected static ?string $title = 'Chart of Accounts';
  31. protected static ?string $slug = 'accounting/chart';
  32. protected static string $view = 'filament.company.pages.accounting.chart';
  33. #[Url]
  34. public ?string $activeTab = AccountCategory::Asset->value;
  35. protected function configureAction(Action $action): void
  36. {
  37. $action
  38. ->modal()
  39. ->slideOver()
  40. ->modalWidth(MaxWidth::TwoExtraLarge);
  41. }
  42. #[Computed]
  43. public function categories(): Collection
  44. {
  45. return AccountSubtype::withCount('accounts')
  46. ->with('accounts')
  47. ->with('accounts.adjustment')
  48. ->get()
  49. ->groupBy('category');
  50. }
  51. public function editChartAction(): Action
  52. {
  53. return EditAction::make()
  54. ->iconButton()
  55. ->name('editChart')
  56. ->label('Edit account')
  57. ->modalHeading('Edit Account')
  58. ->icon('heroicon-m-pencil-square')
  59. ->record(fn (array $arguments) => Account::find($arguments['chart']))
  60. ->form(fn (Form $form) => $this->getChartForm($form)->operation('edit'));
  61. }
  62. public function createChartAction(): Action
  63. {
  64. return CreateAction::make()
  65. ->link()
  66. ->name('createChart')
  67. ->model(Account::class)
  68. ->label('Add a new account')
  69. ->icon('heroicon-o-plus-circle')
  70. ->form(fn (Form $form) => $this->getChartForm($form)->operation('create'))
  71. ->fillForm(fn (array $arguments): array => $this->getChartFormDefaults($arguments['subtype']));
  72. }
  73. private function getChartFormDefaults(int $subtypeId): array
  74. {
  75. $accountSubtype = AccountSubtype::find($subtypeId);
  76. $generatedCode = AccountCode::generate($accountSubtype);
  77. return [
  78. 'subtype_id' => $subtypeId,
  79. 'code' => $generatedCode,
  80. ];
  81. }
  82. private function getChartForm(Form $form, bool $useActiveTab = true): Form
  83. {
  84. return $form
  85. ->schema([
  86. $this->getTypeFormComponent($useActiveTab),
  87. $this->getCodeFormComponent(),
  88. $this->getNameFormComponent(),
  89. ...$this->getBankAccountFormComponents(),
  90. $this->getCurrencyFormComponent(),
  91. $this->getDescriptionFormComponent(),
  92. $this->getArchiveFormComponent(),
  93. ]);
  94. }
  95. protected function getTypeFormComponent(bool $useActiveTab = true): Component
  96. {
  97. return Select::make('subtype_id')
  98. ->label('Type')
  99. ->required()
  100. ->live()
  101. ->disabledOn('edit')
  102. ->options($this->getChartSubtypeOptions($useActiveTab))
  103. ->afterStateUpdated(static function (?string $state, Set $set): void {
  104. if ($state) {
  105. $accountSubtype = AccountSubtype::find($state);
  106. $generatedCode = AccountCode::generate($accountSubtype);
  107. $set('code', $generatedCode);
  108. $set('is_bank_account', false);
  109. $set('bankAccount.type', null);
  110. $set('bankAccount.number', null);
  111. }
  112. });
  113. }
  114. protected function getCodeFormComponent(): Component
  115. {
  116. return TextInput::make('code')
  117. ->label('Code')
  118. ->required()
  119. ->hiddenOn('edit')
  120. ->validationAttribute('account code')
  121. ->unique(table: Account::class, column: 'code', ignoreRecord: true)
  122. ->validateAccountCode(static fn (Get $get) => $get('subtype_id'));
  123. }
  124. protected function getBankAccountFormComponents(): array
  125. {
  126. return [
  127. Checkbox::make('is_bank_account')
  128. ->live()
  129. ->visible(function (Get $get, string $operation) {
  130. if ($operation === 'edit') {
  131. return false;
  132. }
  133. $subtype = $get('subtype_id');
  134. if (empty($subtype)) {
  135. return false;
  136. }
  137. $accountSubtype = AccountSubtype::find($subtype);
  138. if (! $accountSubtype) {
  139. return false;
  140. }
  141. return in_array($accountSubtype->category, [
  142. AccountCategory::Asset,
  143. AccountCategory::Liability,
  144. ]) && $accountSubtype->multi_currency;
  145. })
  146. ->afterStateUpdated(static function ($state, Get $get, Set $set) {
  147. if ($state) {
  148. $subtypeId = $get('subtype_id');
  149. if (empty($subtypeId)) {
  150. return;
  151. }
  152. $subtype = AccountSubtype::find($subtypeId);
  153. if (! $subtype) {
  154. return;
  155. }
  156. // Set default bank account type based on account category
  157. if ($subtype->category === AccountCategory::Asset) {
  158. $set('bankAccount.type', BankAccountType::Depository->value);
  159. } elseif ($subtype->category === AccountCategory::Liability) {
  160. $set('bankAccount.type', BankAccountType::Credit->value);
  161. }
  162. } else {
  163. // Clear bank account fields
  164. $set('bankAccount.type', null);
  165. $set('bankAccount.number', null);
  166. }
  167. }),
  168. Group::make()
  169. ->relationship('bankAccount')
  170. ->schema([
  171. Select::make('type')
  172. ->label('Bank account type')
  173. ->options(function (Get $get) {
  174. $subtype = $get('../subtype_id');
  175. if (empty($subtype)) {
  176. return [];
  177. }
  178. $accountSubtype = AccountSubtype::find($subtype);
  179. if (! $accountSubtype) {
  180. return [];
  181. }
  182. if ($accountSubtype->category === AccountCategory::Asset) {
  183. return [
  184. BankAccountType::Depository->value => BankAccountType::Depository->getLabel(),
  185. BankAccountType::Investment->value => BankAccountType::Investment->getLabel(),
  186. ];
  187. } elseif ($accountSubtype->category === AccountCategory::Liability) {
  188. return [
  189. BankAccountType::Credit->value => BankAccountType::Credit->getLabel(),
  190. BankAccountType::Loan->value => BankAccountType::Loan->getLabel(),
  191. ];
  192. }
  193. return [];
  194. })
  195. ->searchable()
  196. ->columnSpan(1)
  197. ->disabledOn('edit')
  198. ->required(),
  199. TextInput::make('number')
  200. ->label('Bank account number')
  201. ->unique(ignoreRecord: true, modifyRuleUsing: static function (Unique $rule, $state) {
  202. $companyId = Auth::user()->currentCompany->id;
  203. return $rule->where('company_id', $companyId)->where('number', $state);
  204. })
  205. ->maxLength(20)
  206. ->validationAttribute('account number'),
  207. ])
  208. ->visible(static function (Get $get, ?Account $record, string $operation) {
  209. if ($operation === 'create') {
  210. return (bool) $get('is_bank_account');
  211. }
  212. if ($operation === 'edit' && $record) {
  213. return (bool) $record->bankAccount;
  214. }
  215. return false;
  216. }),
  217. ];
  218. }
  219. protected function getNameFormComponent(): Component
  220. {
  221. return TextInput::make('name')
  222. ->label('Name')
  223. ->required();
  224. }
  225. protected function getCurrencyFormComponent(): Component
  226. {
  227. return CreateCurrencySelect::make('currency_code')
  228. ->disabledOn('edit')
  229. ->required(false)
  230. ->requiredIfAccepted('is_bank_account')
  231. ->validationMessages([
  232. 'required_if_accepted' => 'The currency is required for bank accounts.',
  233. ])
  234. ->visible(function (Get $get): bool {
  235. return filled($get('subtype_id')) && AccountSubtype::find($get('subtype_id'))->multi_currency;
  236. });
  237. }
  238. protected function getDescriptionFormComponent(): Component
  239. {
  240. return Textarea::make('description')
  241. ->label('Description');
  242. }
  243. protected function getArchiveFormComponent(): Component
  244. {
  245. return Checkbox::make('archived')
  246. ->label('Archive account')
  247. ->helperText('Archived accounts will not be available for selection in transactions.')
  248. ->hiddenOn('create');
  249. }
  250. private function getChartSubtypeOptions($useActiveTab = true): array
  251. {
  252. $subtypes = $useActiveTab ?
  253. AccountSubtype::where('category', $this->activeTab)->get() :
  254. AccountSubtype::all();
  255. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  256. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  257. ->toArray();
  258. }
  259. protected function getHeaderActions(): array
  260. {
  261. return [
  262. CreateAction::make()
  263. ->button()
  264. ->label('Add new account')
  265. ->model(Account::class)
  266. ->form(fn (Form $form) => $this->getChartForm($form, false)->operation('create')),
  267. ];
  268. }
  269. public function getCategoryLabel($categoryValue): string
  270. {
  271. return AccountCategory::from($categoryValue)->getPluralLabel();
  272. }
  273. }