您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AccountChart.php 11KB

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