fixed category and contact form group

This commit is contained in:
Denis Duliçi 2022-07-25 18:40:05 +03:00
parent cc8ca2a740
commit f6c91a2817
3 changed files with 35 additions and 46 deletions

View File

@ -22,8 +22,6 @@ class Category extends Form
*/ */
public function render() public function render()
{ {
$type = $this->type;
if (empty($this->name)) { if (empty($this->name)) {
$this->name = 'category_id'; $this->name = 'category_id';
} }
@ -31,18 +29,20 @@ class Category extends Form
$this->path = route('modals.categories.create', ['type' => $this->type]); $this->path = route('modals.categories.create', ['type' => $this->type]);
$this->remoteAction = route('categories.index', ['search' => 'type:' . $this->type . ' enabled:1']); $this->remoteAction = route('categories.index', ['search' => 'type:' . $this->type . ' enabled:1']);
$this->categories = Model::type($type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->get(); $this->categories = Model::type($this->type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->get();
if (!empty($model) && $model->category && ! $this->categories->has($model->category_id)) { $model = $this->getParentData('model');
$this->categories->put($model->category->id, $model->category->name);
}
if($model = $this->getParentData('model')) { if (! empty($model)) {
$this->selected = $model->category_id; $this->selected = $model->category_id;
if (! $this->categories->has($model->category_id) && ($category = $model->category)) {
$this->categories->put($category->id, $category->name);
}
} }
if (empty($this->selected) && (in_array($type, ['income', 'expense']))) { if (empty($this->selected) && (in_array($this->type, [Model::INCOME_TYPE, Model::EXPENSE_TYPE]))) {
$this->selected = setting('default.' . $type . '_category'); $this->selected = setting('default.' . $this->type . '_category');
} }
return view('components.form.group.category'); return view('components.form.group.category');

View File

@ -4,6 +4,7 @@ namespace App\View\Components\Form\Group;
use App\Abstracts\View\Components\Form; use App\Abstracts\View\Components\Form;
use App\Models\Common\Contact as Model; use App\Models\Common\Contact as Model;
use Illuminate\Support\Str;
class Contact extends Form class Contact extends Form
{ {
@ -26,51 +27,37 @@ class Contact extends Form
*/ */
public function render() public function render()
{ {
$type = $this->type; if (empty($this->name)) {
$this->name = 'contact_id';
}
switch ($type) { $this->setRoutes();
case 'customer':
$this->prepareCustomer(); $this->label = trans_choice('general.' . Str::plural($this->type), 1);
break;
case 'vendor': $this->contacts = Model::type($this->type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->get();
$this->prepareVendor();
break; $model = $this->getParentData('model');
if (! empty($model)) {
$this->selected = $model->contact_id;
if (! $this->contacts->has($model->contact_id) && ($contact = $model->contact)) {
$this->contacts->put($contact->id, $contact->name);
}
} }
return view($this->view); return view($this->view);
} }
protected function prepareCustomer() public function setRoutes(): void
{ {
if (empty($this->name)) { $alias = config('type.contact.' . $this->type . '.alias');
$this->name = 'contact_id'; $prefix = config('type.contact.' . $this->type . '.route.prefix');
}
$this->path = route('modals.customers.create'); $parameters = ['search' => 'enabled:1'];
$this->remoteAction = route('customers.index');
$this->label = trans_choice('general.customers', 1);
$this->contacts = Model::customer()->enabled()->orderBy('name')->take(setting('default.select_limit'))->get(); $this->path = ! empty($alias) ? route("{$alias}.modals.{$prefix}.create") : route("modals.{$prefix}.create");
$this->remoteAction = ! empty($alias) ? route("{$alias}.{$prefix}.index", $parameters) : route("{$prefix}.index", $parameters);
if (! empty($model) && $model->customer && ! $this->contacts->has($model->contact_id)) {
$this->contacts->put($model->customer->id, $model->customer->name);
}
}
protected function prepareVendor()
{
if (empty($this->name)) {
$this->name = 'contact_id';
}
$this->path = route('modals.vendors.create');
$this->remoteAction = route('vendors.index');
$this->label = trans_choice('general.vendors', 1);
$this->contacts = Model::vendor()->enabled()->orderBy('name')->take(setting('default.select_limit'))->get();
if (! empty($model) && $model->vendor && ! $this->contacts->has($model->contact_id)) {
$this->contacts->put($model->vendor->id, $model->vendor->name);
}
} }
} }

View File

@ -41,6 +41,7 @@ return [
// Contacts // Contacts
'contact' => [ 'contact' => [
Contact::CUSTOMER_TYPE => [ Contact::CUSTOMER_TYPE => [
'alias' => '', // core empty but module write own alias
'group' => 'sales', 'group' => 'sales',
'route' => [ 'route' => [
'prefix' => 'customers', // core use with group + prefix, module ex. estimates 'prefix' => 'customers', // core use with group + prefix, module ex. estimates
@ -69,6 +70,7 @@ return [
], ],
Contact::VENDOR_TYPE => [ Contact::VENDOR_TYPE => [
'alias' => '', // core empty but module write own alias
'group' => 'purchases', 'group' => 'purchases',
'route' => [ 'route' => [
'prefix' => 'vendors', // core use with group + prefix, module ex. estimates 'prefix' => 'vendors', // core use with group + prefix, module ex. estimates