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.
106 lines
3.2 KiB
106 lines
3.2 KiB
<?php
|
|
|
|
use App\Domains\Finance\Actions\LedgerConfiguration\AdjustAccountClassification;
|
|
use App\Domains\Finance\Actions\LedgerConfiguration\RegisterChartOfAccount;
|
|
use App\Domains\Finance\DTOs\LedgerConfiguration\AdjustAccountClassificationDTO;
|
|
use App\Domains\Finance\DTOs\LedgerConfiguration\RegisterChartOfAccountDTO;
|
|
use App\Domains\Finance\Models\ChartOfAccount;
|
|
use App\Livewire\Concerns\WithModal;
|
|
use App\Livewire\Concerns\WithToast;
|
|
use App\Livewire\Forms\Finance\ChartOfAccountForm;
|
|
use Illuminate\Support\Collection;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
new class extends Component
|
|
{
|
|
use WithModal;
|
|
use WithToast;
|
|
|
|
public ChartOfAccountForm $form;
|
|
|
|
#[Locked]
|
|
public ?string $id = null;
|
|
|
|
#[Locked]
|
|
public string $mode = 'create';
|
|
|
|
protected string $resourceName = 'chart_of_account';
|
|
|
|
public function updating($property, $value): void
|
|
{
|
|
if ($property === 'form.parent_id') {
|
|
$props = explode('.', $property);
|
|
$props = implode('->', $props);
|
|
$this->{$props} = $value;
|
|
|
|
$coa = $this->chartOfAccounts->where('ulid', $value)->first();
|
|
if ($coa) {
|
|
$this->form->code = $coa->code + 1;
|
|
$this->form->classification = $coa->classification;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function show(int|string $id): void
|
|
{
|
|
$this->id = $id;
|
|
$this->mode = 'update';
|
|
$coa = $this->coa->only(['name', 'code', 'classification', 'status', 'parent_id']);
|
|
$coa['parent_id'] = $this->chartOfAccounts
|
|
->where('id', $coa['parent_id'])
|
|
->first()
|
|
?->ulid;
|
|
$this->form->fill($coa);
|
|
}
|
|
|
|
#[Computed]
|
|
public function coa(): ChartOfAccount
|
|
{
|
|
return $this->id ? ChartOfAccount::where('ulid', $this->id)->first() : new ChartOfAccount;
|
|
}
|
|
|
|
#[Computed]
|
|
public function chartOfAccounts(): Collection
|
|
{
|
|
return ChartOfAccount::query()
|
|
->select(['id', 'name', 'code', 'ulid', 'classification'])
|
|
->get();
|
|
}
|
|
|
|
public function save(RegisterChartOfAccount $register, AdjustAccountClassification $adjust): void
|
|
{
|
|
$this->form->validate();
|
|
|
|
$parentId = $this->chartOfAccounts
|
|
->where('ulid', $this->form->parent_id)
|
|
->first()
|
|
->id;
|
|
|
|
if ($this->mode === 'create') {
|
|
$register->execute(new RegisterChartOfAccountDTO(
|
|
code: $this->form->code,
|
|
name: $this->form->name,
|
|
classification: $this->form->classification,
|
|
status: $this->form->status,
|
|
parentId: $parentId,
|
|
));
|
|
} elseif ($this->mode == 'update') {
|
|
$adjust->execute($this->coa, new AdjustAccountClassificationDTO(
|
|
code: $this->form->code,
|
|
classification: $this->form->classification,
|
|
parentId: $parentId,
|
|
));
|
|
}
|
|
|
|
$this->dispatch('hide-chartofaccount-form-modal');
|
|
$this->js("LaravelDataTables['chartofaccount-table'].ajax.reload(null, false)");
|
|
}
|
|
|
|
public function hide(): void
|
|
{
|
|
$this->form->reset();
|
|
$this->reset('id', 'mode');
|
|
}
|
|
};
|
|
|