added category types trait

This commit is contained in:
Cihan Şentürk 2026-03-14 00:50:16 +03:00
parent 38677e65c2
commit c82b4883a7
4 changed files with 88 additions and 62 deletions

View File

@ -35,36 +35,12 @@ class Categories extends Controller
{ {
$type = $request->get('type', Category::ITEM_TYPE); $type = $request->get('type', Category::ITEM_TYPE);
$type_codes = []; $category_types = $this->getTypeCategoryTypes($type);
$hide_code_types = $this->hideCodeCategoryTypes($category_types);
switch ($type) {
case Category::INCOME_TYPE:
$types = $this->getIncomeCategoryTypes();
break;
case Category::EXPENSE_TYPE:
$types = $this->getExpenseCategoryTypes();
break;
case Category::ITEM_TYPE:
$types = $this->getItemCategoryTypes();
break;
case Category::OTHER_TYPE:
$types = $this->getOtherCategoryTypes();
break;
default:
$types = [$type];
}
foreach ($types as $type) {
$config_type = config('type.category.' . $type, []);
$type_codes[$type] = empty($config_type['hide']) || ! in_array('code', $config_type['hide']);
}
$config_type = config('type.category.' . $type, []);
$show_code_field = ! empty($config_type['hide']) && in_array('code', $config_type['hide']) ? false : true;
$categories = collect(); $categories = collect();
Category::type($types) Category::type($category_types)
->enabled() ->enabled()
->orderBy('name') ->orderBy('name')
->get() ->get()
@ -76,7 +52,10 @@ class Categories extends Controller
]); ]);
}); });
$html = view('modals.categories.create', compact('type', 'types', 'categories', 'show_code_field', 'type_codes'))->render(); $type_group = count($category_types) > 1 ? true : false;
$types = $this->getCategoryTypes(group: true, types: $category_types);
$html = view('modals.categories.create', compact('type', 'types', 'categories', 'type_group', 'hide_code_types'))->render();
return response()->json([ return response()->json([
'success' => true, 'success' => true,

View File

@ -81,16 +81,17 @@ class Categories extends Controller
*/ */
public function create() public function create()
{ {
$types = $this->getCategoryTypes(true, true);
$categories = []; $categories = [];
$type_codes = [];
foreach (config('type.category') as $type => $config) { foreach (config('type.category') as $type => $config) {
$type_codes[$type] = empty($config['hide']) || ! in_array('code', $config['hide']);
$categories[$type] = []; $categories[$type] = [];
} }
$type_group = $this->isGroupCategoryType();
$hide_code_types = $this->hideCodeCategoryTypes(array_keys($categories));
$types = $this->getCategoryTypes(group: $type_group);
Category::enabled()->orderBy('name')->get()->each(function ($category) use (&$categories) { Category::enabled()->orderBy('name')->get()->each(function ($category) use (&$categories) {
$categories[$category->type][] = [ $categories[$category->type][] = [
'id' => $category->id, 'id' => $category->id,
@ -99,7 +100,7 @@ class Categories extends Controller
]; ];
}); });
return view('settings.categories.create', compact('types', 'categories', 'type_codes')); return view('settings.categories.create', compact('types', 'categories', 'type_group', 'hide_code_types'));
} }
/** /**
@ -163,20 +164,21 @@ class Categories extends Controller
*/ */
public function edit(Category $category) public function edit(Category $category)
{ {
$types = $this->getCategoryTypes(true, true);
$type_disabled = (Category::where('type', $category->type)->count() == 1) ?: false; $type_disabled = (Category::where('type', $category->type)->count() == 1) ?: false;
$edited_category_id = $category->id; $edited_category_id = $category->id;
$categories = []; $categories = [];
$type_codes = [];
foreach (config('type.category') as $type => $config) { foreach (config('type.category') as $type => $config) {
$type_codes[$type] = empty($config['hide']) || ! in_array('code', $config['hide']);
$categories[$type] = []; $categories[$type] = [];
} }
$type_group = $this->isGroupCategoryType();
$hide_code_types = $this->hideCodeCategoryTypes(array_keys($categories));
$types = $this->getCategoryTypes(group: $type_group);
$skip_categories = []; $skip_categories = [];
$skip_categories[] = $edited_category_id; $skip_categories[] = $edited_category_id;
@ -206,7 +208,7 @@ class Categories extends Controller
$parent_categories = $categories[$category->type] ?? []; $parent_categories = $categories[$category->type] ?? [];
return view('settings.categories.edit', compact('category', 'types', 'type_disabled', 'categories', 'parent_categories', 'type_codes')); return view('settings.categories.edit', compact('category', 'types', 'type_disabled', 'categories', 'parent_categories', 'type_group', 'hide_code_types'));
} }
/** /**

View File

@ -36,6 +36,28 @@ trait Categories
return in_array($type, $this->getOtherCategoryTypes()); return in_array($type, $this->getOtherCategoryTypes());
} }
public function getTypeCategoryTypes(string $type, string $return = 'array'): string|array
{
switch ($type) {
case Category::INCOME_TYPE:
$types = $this->getIncomeCategoryTypes($return);
break;
case Category::EXPENSE_TYPE:
$types = $this->getExpenseCategoryTypes($return);
break;
case Category::ITEM_TYPE:
$types = $this->getItemCategoryTypes($return);
break;
case Category::OTHER_TYPE:
$types = $this->getOtherCategoryTypes($return);
break;
default:
$types = ($return == 'array') ? [$type] : $type;
}
return $types;
}
public function getIncomeCategoryTypes(string $return = 'array'): string|array public function getIncomeCategoryTypes(string $return = 'array'): string|array
{ {
return $this->getCategoryTypesByIndex(Category::INCOME_TYPE, $return); return $this->getCategoryTypesByIndex(Category::INCOME_TYPE, $return);
@ -98,10 +120,46 @@ trait Categories
])->save(); ])->save();
} }
public function getCategoryTypes(bool $translate = true, bool $group = false): array public function isGroupCategoryType(): bool
{ {
$types = []; $setting_category_types = setting('category.type');
$configs = config('type.category');
foreach ($setting_category_types as $type => $category) {
$categories = explode(',', $category);
if (count($categories) > 1) {
return true;
}
}
return false;
}
public function hideCodeCategoryType(string $type, bool $default = true): bool
{
return $this->hideCodeCategoryTypes($type)[$type] ?? $default;
}
public function hideCodeCategoryTypes(string|array $types): array
{
$types = is_string($types) ? explode(',', $types) : $types;
$type_codes = [];
foreach ($types as $type) {
$config_type = config('type.category.' . $type, []);
$type_codes[$type] = ! empty($config_type['hide']) && in_array('code', $config_type['hide']) ? true : false;
}
return $type_codes;
}
public function getCategoryTypes(bool $translate = true, bool $group = false, array $types = []): array
{
$category_types = [];
$configs = empty($types) ? config('type.category') : array_intersect_key(config('type.category'), array_flip($types));
foreach ($configs as $type => $attr) { foreach ($configs as $type => $attr) {
$plural_type = Str::plural($type); $plural_type = Str::plural($type);
@ -114,13 +172,14 @@ trait Categories
if ($group) { if ($group) {
$group_key = $attr['group'] ?? $type; $group_key = $attr['group'] ?? $type;
$types[$group_key][$type] = $translate ? trans_choice($name, 1) : $name;
$category_types[$group_key][$type] = $translate ? trans_choice($name, 1) : $name;
} else { } else {
$types[$type] = $translate ? trans_choice($name, 1) : $name; $category_types[$type] = $translate ? trans_choice($name, 1) : $name;
} }
} }
return $types; return $category_types;
} }
public function getCategoryTabs(): array public function getCategoryTabs(): array

View File

@ -39,27 +39,13 @@ class Category extends Form
$this->name = 'category_id'; $this->name = 'category_id';
} }
switch ($this->type) { $types = $this->getTypeCategoryTypes($this->type);
case Model::INCOME_TYPE: $types_string = implode(',', $types);
$types = $this->getIncomeCategoryTypes();
break;
case Model::EXPENSE_TYPE:
$types = $this->getExpenseCategoryTypes();
break;
case Model::ITEM_TYPE:
$types = $this->getItemCategoryTypes();
break;
case Model::OTHER_TYPE:
$types = $this->getOtherCategoryTypes();
break;
default:
$types = [$this->type];
}
$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:' . implode(',', $types) . ' enabled:1']); $this->remoteAction = route('categories.index', ['search' => 'type:' . $types_string . ' enabled:1']);
$typeLabels = collect($this->getCategoryTypes())->only($types)->all(); $typeLabels = $this->getCategoryTypes(types: $types);
$is_code = false; $is_code = false;