Akaunting/app/View/Components/Form/Group/Tax.php

74 lines
1.7 KiB
PHP
Raw Normal View History

2022-06-01 07:15:55 +00:00
<?php
namespace App\View\Components\Form\Group;
use App\Abstracts\View\Components\Form;
2022-08-05 07:59:23 +00:00
use App\Models\Setting\Tax as Model;
2022-06-01 07:15:55 +00:00
class Tax extends Form
{
public $type = 'tax';
public $path;
2023-10-03 08:06:08 +00:00
public $remoteAction;
2022-06-01 07:15:55 +00:00
public $field;
2022-08-05 07:59:23 +00:00
public $taxes;
2022-06-01 07:15:55 +00:00
2023-10-03 08:06:08 +00:00
public $currency;
public $change;
2022-06-01 07:15:55 +00:00
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
if (empty($this->name)) {
2022-08-05 07:59:23 +00:00
$this->name = 'tax_id';
2022-06-01 07:15:55 +00:00
}
2022-08-05 07:59:23 +00:00
$this->path = route('modals.taxes.create');
2023-10-03 08:06:08 +00:00
$this->remoteAction = route('taxes.index', ['search' => 'enabled:1']);
2022-06-01 07:15:55 +00:00
$this->field = [
2022-08-05 07:59:23 +00:00
'key' => 'id',
'value' => 'title'
2022-06-01 07:15:55 +00:00
];
2023-10-03 08:06:08 +00:00
$this->taxes = Model::enabled()->orderBy('name')->get();
2022-08-05 07:59:23 +00:00
2023-10-03 08:06:08 +00:00
$model = $this->getParentData('model');
$tax_id = old('tax_ids', old('tax.id', old('tax_id', null)));
2022-08-05 07:59:23 +00:00
if (! empty($tax_id)) {
2023-10-03 08:06:08 +00:00
$this->selected = ($this->multiple) ? (array) $tax_id : $tax_id;
2022-08-05 07:59:23 +00:00
2023-10-03 08:06:08 +00:00
foreach ($tax_id as $id) {
if (! $this->taxes->has($id)) {
$tax = Model::find($id);
2022-08-05 07:59:23 +00:00
2023-10-03 08:06:08 +00:00
$this->taxes->put($tax->id, $tax->title);
}
2022-08-05 07:59:23 +00:00
}
}
2022-06-01 07:15:55 +00:00
2023-10-03 08:06:08 +00:00
if (! empty($model) && ! empty($model->{$this->name})) {
$this->selected = $model->{$this->name};
}
if ($this->selected === null && $this->multiple) {
$this->selected = (setting('default.tax')) ? [setting('default.tax')] : null;
} else if ($this->selected === null) {
$this->selected = setting('default.tax', null);
2022-06-01 07:15:55 +00:00
}
return view('components.form.group.tax');
}
}