選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AccountChart.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. ->searchable()
  104. ->options($this->getChartSubtypeOptions($useActiveTab))
  105. ->afterStateUpdated(static function (?string $state, Set $set): void {
  106. if ($state) {
  107. $accountSubtype = AccountSubtype::find($state);
  108. $generatedCode = AccountCode::generate($accountSubtype);
  109. $set('code', $generatedCode);
  110. $set('is_bank_account', false);
  111. $set('bankAccount.type', null);
  112. $set('bankAccount.number', null);
  113. }
  114. });
  115. }
  116. protected function getCodeFormComponent(): Component
  117. {
  118. return TextInput::make('code')
  119. ->label('Code')
  120. ->required()
  121. ->hiddenOn('edit')
  122. ->validationAttribute('account code')
  123. ->unique(table: Account::class, column: 'code', ignoreRecord: true)
  124. ->validateAccountCode(static fn (Get $get) => $get('subtype_id'));
  125. }
  126. protected function getBankAccountFormComponents(): array
  127. {
  128. return [
  129. Checkbox::make('is_bank_account')
  130. ->live()
  131. ->visible(function (Get $get, string $operation) {
  132. if ($operation === 'edit') {
  133. return false;
  134. }
  135. $subtype = $get('subtype_id');
  136. if (empty($subtype)) {
  137. return false;
  138. }
  139. $accountSubtype = AccountSubtype::find($subtype);
  140. if (! $accountSubtype) {
  141. return false;
  142. }
  143. return in_array($accountSubtype->category, [
  144. AccountCategory::Asset,
  145. AccountCategory::Liability,
  146. ]) && $accountSubtype->multi_currency;
  147. })
  148. ->afterStateUpdated(static function ($state, Get $get, Set $set) {
  149. if ($state) {
  150. $subtypeId = $get('subtype_id');
  151. if (empty($subtypeId)) {
  152. return;
  153. }
  154. $subtype = AccountSubtype::find($subtypeId);
  155. if (! $subtype) {
  156. return;
  157. }
  158. // Set default bank account type based on account category
  159. if ($subtype->category === AccountCategory::Asset) {
  160. $set('bankAccount.type', BankAccountType::Depository->value);
  161. } elseif ($subtype->category === AccountCategory::Liability) {
  162. $set('bankAccount.type', BankAccountType::Credit->value);
  163. }
  164. } else {
  165. // Clear bank account fields
  166. $set('bankAccount.type', null);
  167. $set('bankAccount.number', null);
  168. }
  169. }),
  170. Group::make()
  171. ->relationship('bankAccount')
  172. ->schema([
  173. Select::make('type')
  174. ->label('Bank account type')
  175. ->options(function (Get $get) {
  176. $subtype = $get('../subtype_id');
  177. if (empty($subtype)) {
  178. return [];
  179. }
  180. $accountSubtype = AccountSubtype::find($subtype);
  181. if (! $accountSubtype) {
  182. return [];
  183. }
  184. if ($accountSubtype->category === AccountCategory::Asset) {
  185. return [
  186. BankAccountType::Depository->value => BankAccountType::Depository->getLabel(),
  187. BankAccountType::Investment->value => BankAccountType::Investment->getLabel(),
  188. ];
  189. } elseif ($accountSubtype->category === AccountCategory::Liability) {
  190. return [
  191. BankAccountType::Credit->value => BankAccountType::Credit->getLabel(),
  192. BankAccountType::Loan->value => BankAccountType::Loan->getLabel(),
  193. ];
  194. }
  195. return [];
  196. })
  197. ->searchable()
  198. ->columnSpan(1)
  199. ->disabledOn('edit')
  200. ->required(),
  201. TextInput::make('number')
  202. ->label('Bank account number')
  203. ->unique(ignoreRecord: true, modifyRuleUsing: static function (Unique $rule, $state) {
  204. $companyId = Auth::user()->currentCompany->id;
  205. return $rule->where('company_id', $companyId)->where('number', $state);
  206. })
  207. ->maxLength(20)
  208. ->validationAttribute('account number'),
  209. ])
  210. ->visible(static function (Get $get, ?Account $record, string $operation) {
  211. if ($operation === 'create') {
  212. return (bool) $get('is_bank_account');
  213. }
  214. if ($operation === 'edit' && $record) {
  215. return (bool) $record->bankAccount;
  216. }
  217. return false;
  218. }),
  219. ];
  220. }
  221. protected function getNameFormComponent(): Component
  222. {
  223. return TextInput::make('name')
  224. ->label('Name')
  225. ->required();
  226. }
  227. protected function getCurrencyFormComponent(): Component
  228. {
  229. return CreateCurrencySelect::make('currency_code')
  230. ->disabledOn('edit')
  231. ->required(false)
  232. ->requiredIfAccepted('is_bank_account')
  233. ->validationMessages([
  234. 'required_if_accepted' => 'The currency is required for bank accounts.',
  235. ])
  236. ->visible(function (Get $get): bool {
  237. return filled($get('subtype_id')) && AccountSubtype::find($get('subtype_id'))->multi_currency;
  238. });
  239. }
  240. protected function getDescriptionFormComponent(): Component
  241. {
  242. return Textarea::make('description')
  243. ->label('Description');
  244. }
  245. protected function getArchiveFormComponent(): Component
  246. {
  247. return Checkbox::make('archived')
  248. ->label('Archive account')
  249. ->helperText('Archived accounts will not be available for selection in transactions, offerings, or other new records.')
  250. ->hiddenOn('create');
  251. }
  252. private function getChartSubtypeOptions($useActiveTab = true): array
  253. {
  254. $subtypes = $useActiveTab ?
  255. AccountSubtype::where('category', $this->activeTab)->get() :
  256. AccountSubtype::all();
  257. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  258. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  259. ->toArray();
  260. }
  261. protected function getHeaderActions(): array
  262. {
  263. return [
  264. CreateAction::make()
  265. ->button()
  266. ->label('Add new account')
  267. ->model(Account::class)
  268. ->form(fn (Form $form) => $this->getChartForm($form, false)->operation('create')),
  269. ];
  270. }
  271. public function getCategoryLabel($categoryValue): string
  272. {
  273. return AccountCategory::from($categoryValue)->getPluralLabel();
  274. }
  275. }