|
@@ -48,7 +48,7 @@ class AccountChart extends Page
|
48
|
48
|
}
|
49
|
49
|
|
50
|
50
|
#[Computed]
|
51
|
|
- public function categories(): Collection
|
|
51
|
+ public function accountCategories(): Collection
|
52
|
52
|
{
|
53
|
53
|
return AccountSubtype::withCount('accounts')
|
54
|
54
|
->with(['accounts' => function ($query) {
|
|
@@ -58,42 +58,39 @@ class AccountChart extends Page
|
58
|
58
|
->groupBy('category');
|
59
|
59
|
}
|
60
|
60
|
|
61
|
|
- public function editChartAction(): Action
|
|
61
|
+ public function editAccountAction(): Action
|
62
|
62
|
{
|
63
|
|
- return EditAction::make()
|
64
|
|
- ->iconButton()
|
65
|
|
- ->name('editChart')
|
|
63
|
+ return EditAction::make('editAccount')
|
66
|
64
|
->label('Edit account')
|
67
|
|
- ->modalHeading('Edit Account')
|
|
65
|
+ ->iconButton()
|
68
|
66
|
->icon('heroicon-m-pencil-square')
|
69
|
|
- ->record(fn (array $arguments) => Account::find($arguments['chart']))
|
70
|
|
- ->form(fn (Form $form) => $this->getChartForm($form)->operation('edit'));
|
|
67
|
+ ->record(fn (array $arguments) => Account::find($arguments['account']))
|
|
68
|
+ ->form(fn (Form $form) => $this->getAccountForm($form)->operation('edit'));
|
71
|
69
|
}
|
72
|
70
|
|
73
|
|
- public function createChartAction(): Action
|
|
71
|
+ public function createAccountAction(): Action
|
74
|
72
|
{
|
75
|
|
- return CreateAction::make()
|
|
73
|
+ return CreateAction::make('createAccount')
|
76
|
74
|
->link()
|
77
|
|
- ->name('createChart')
|
78
|
75
|
->model(Account::class)
|
79
|
76
|
->label('Add a new account')
|
80
|
77
|
->icon('heroicon-o-plus-circle')
|
81
|
|
- ->form(fn (Form $form) => $this->getChartForm($form)->operation('create'))
|
82
|
|
- ->fillForm(fn (array $arguments): array => $this->getChartFormDefaults($arguments['subtype']));
|
|
78
|
+ ->form(fn (Form $form) => $this->getAccountForm($form)->operation('create'))
|
|
79
|
+ ->fillForm(fn (array $arguments): array => $this->getAccountFormDefaults($arguments['accountSubtype']));
|
83
|
80
|
}
|
84
|
81
|
|
85
|
|
- private function getChartFormDefaults(int $subtypeId): array
|
|
82
|
+ private function getAccountFormDefaults(int $accountSubtypeId): array
|
86
|
83
|
{
|
87
|
|
- $accountSubtype = AccountSubtype::find($subtypeId);
|
|
84
|
+ $accountSubtype = AccountSubtype::find($accountSubtypeId);
|
88
|
85
|
$generatedCode = AccountCode::generate($accountSubtype);
|
89
|
86
|
|
90
|
87
|
return [
|
91
|
|
- 'subtype_id' => $subtypeId,
|
|
88
|
+ 'subtype_id' => $accountSubtypeId,
|
92
|
89
|
'code' => $generatedCode,
|
93
|
90
|
];
|
94
|
91
|
}
|
95
|
92
|
|
96
|
|
- private function getChartForm(Form $form, bool $useActiveTab = true): Form
|
|
93
|
+ private function getAccountForm(Form $form, bool $useActiveTab = true): Form
|
97
|
94
|
{
|
98
|
95
|
return $form
|
99
|
96
|
->schema([
|
|
@@ -115,7 +112,7 @@ class AccountChart extends Page
|
115
|
112
|
->live()
|
116
|
113
|
->disabledOn('edit')
|
117
|
114
|
->searchable()
|
118
|
|
- ->options($this->getChartSubtypeOptions($useActiveTab))
|
|
115
|
+ ->options($this->getAccountSubtypeOptions($useActiveTab))
|
119
|
116
|
->afterStateUpdated(static function (?string $state, Set $set): void {
|
120
|
117
|
if ($state) {
|
121
|
118
|
$accountSubtype = AccountSubtype::find($state);
|
|
@@ -150,12 +147,12 @@ class AccountChart extends Page
|
150
|
147
|
return false;
|
151
|
148
|
}
|
152
|
149
|
|
153
|
|
- $subtype = $get('subtype_id');
|
154
|
|
- if (empty($subtype)) {
|
|
150
|
+ $accountSubtypeId = $get('subtype_id');
|
|
151
|
+ if (empty($accountSubtypeId)) {
|
155
|
152
|
return false;
|
156
|
153
|
}
|
157
|
154
|
|
158
|
|
- $accountSubtype = AccountSubtype::find($subtype);
|
|
155
|
+ $accountSubtype = AccountSubtype::find($accountSubtypeId);
|
159
|
156
|
|
160
|
157
|
if (! $accountSubtype) {
|
161
|
158
|
return false;
|
|
@@ -168,22 +165,22 @@ class AccountChart extends Page
|
168
|
165
|
})
|
169
|
166
|
->afterStateUpdated(static function ($state, Get $get, Set $set) {
|
170
|
167
|
if ($state) {
|
171
|
|
- $subtypeId = $get('subtype_id');
|
|
168
|
+ $accountSubtypeId = $get('subtype_id');
|
172
|
169
|
|
173
|
|
- if (empty($subtypeId)) {
|
|
170
|
+ if (empty($accountSubtypeId)) {
|
174
|
171
|
return;
|
175
|
172
|
}
|
176
|
173
|
|
177
|
|
- $subtype = AccountSubtype::find($subtypeId);
|
|
174
|
+ $accountSubtype = AccountSubtype::find($accountSubtypeId);
|
178
|
175
|
|
179
|
|
- if (! $subtype) {
|
|
176
|
+ if (! $accountSubtype) {
|
180
|
177
|
return;
|
181
|
178
|
}
|
182
|
179
|
|
183
|
180
|
// Set default bank account type based on account category
|
184
|
|
- if ($subtype->category === AccountCategory::Asset) {
|
|
181
|
+ if ($accountSubtype->category === AccountCategory::Asset) {
|
185
|
182
|
$set('bankAccount.type', BankAccountType::Depository->value);
|
186
|
|
- } elseif ($subtype->category === AccountCategory::Liability) {
|
|
183
|
+ } elseif ($accountSubtype->category === AccountCategory::Liability) {
|
187
|
184
|
$set('bankAccount.type', BankAccountType::Credit->value);
|
188
|
185
|
}
|
189
|
186
|
} else {
|
|
@@ -198,13 +195,13 @@ class AccountChart extends Page
|
198
|
195
|
Select::make('type')
|
199
|
196
|
->label('Bank account type')
|
200
|
197
|
->options(function (Get $get) {
|
201
|
|
- $subtype = $get('../subtype_id');
|
|
198
|
+ $accountSubtypeId = $get('../subtype_id');
|
202
|
199
|
|
203
|
|
- if (empty($subtype)) {
|
|
200
|
+ if (empty($accountSubtypeId)) {
|
204
|
201
|
return [];
|
205
|
202
|
}
|
206
|
203
|
|
207
|
|
- $accountSubtype = AccountSubtype::find($subtype);
|
|
204
|
+ $accountSubtype = AccountSubtype::find($accountSubtypeId);
|
208
|
205
|
|
209
|
206
|
if (! $accountSubtype) {
|
210
|
207
|
return [];
|
|
@@ -287,14 +284,14 @@ class AccountChart extends Page
|
287
|
284
|
->hiddenOn('create');
|
288
|
285
|
}
|
289
|
286
|
|
290
|
|
- private function getChartSubtypeOptions($useActiveTab = true): array
|
|
287
|
+ private function getAccountSubtypeOptions($useActiveTab = true): array
|
291
|
288
|
{
|
292
|
|
- $subtypes = $useActiveTab ?
|
|
289
|
+ $accountSubtypes = $useActiveTab ?
|
293
|
290
|
AccountSubtype::where('category', $this->activeTab)->get() :
|
294
|
291
|
AccountSubtype::all();
|
295
|
292
|
|
296
|
|
- return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
|
297
|
|
- ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
|
|
293
|
+ return $accountSubtypes->groupBy(fn (AccountSubtype $accountSubtype) => $accountSubtype->type->getLabel())
|
|
294
|
+ ->map(fn (Collection $accountSubtypes, string $type) => $accountSubtypes->mapWithKeys(static fn (AccountSubtype $accountSubtype) => [$accountSubtype->id => $accountSubtype->name]))
|
298
|
295
|
->toArray();
|
299
|
296
|
}
|
300
|
297
|
|
|
@@ -303,9 +300,8 @@ class AccountChart extends Page
|
303
|
300
|
return [
|
304
|
301
|
CreateAction::make()
|
305
|
302
|
->button()
|
306
|
|
- ->label('Add new account')
|
307
|
303
|
->model(Account::class)
|
308
|
|
- ->form(fn (Form $form) => $this->getChartForm($form, false)->operation('create')),
|
|
304
|
+ ->form(fn (Form $form) => $this->getAccountForm($form, false)->operation('create')),
|
309
|
305
|
];
|
310
|
306
|
}
|
311
|
307
|
|