Merge pull request #3348 from CihanSenturk/add-category-tabs

Improve category tabs
This commit is contained in:
Cüneyt Şentürk 2026-03-14 09:33:42 +00:00 committed by GitHub
commit 729834a0ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 144 additions and 126 deletions

View File

@ -53,6 +53,7 @@ class Categories extends BulkAction
public function edit($request)
{
$selected = $this->getSelectedInput($request);
$types = $this->getCategoryTypes();
return $this->response('bulk-actions.settings.categories.edit', compact('selected', 'types'));

View File

@ -35,36 +35,12 @@ class Categories extends Controller
{
$type = $request->get('type', Category::ITEM_TYPE);
$type_codes = [];
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;
$category_types = $this->getTypeCategoryTypes($type);
$hide_code_types = $this->hideCodeCategoryTypes($category_types);
$categories = collect();
Category::type($types)
Category::type($category_types)
->enabled()
->orderBy('name')
->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([
'success' => true,

View File

@ -81,16 +81,17 @@ class Categories extends Controller
*/
public function create()
{
$types = $this->getCategoryTypes(true, true);
$categories = [];
$type_codes = [];
foreach (config('type.category') as $type => $config) {
$type_codes[$type] = empty($config['hide']) || ! in_array('code', $config['hide']);
$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) {
$categories[$category->type][] = [
'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)
{
$types = $this->getCategoryTypes(true, true);
$type_disabled = (Category::where('type', $category->type)->count() == 1) ?: false;
$edited_category_id = $category->id;
$categories = [];
$type_codes = [];
foreach (config('type.category') as $type => $config) {
$type_codes[$type] = empty($config['hide']) || ! in_array('code', $config['hide']);
$categories[$type] = [];
}
$type_group = $this->isGroupCategoryType();
$hide_code_types = $this->hideCodeCategoryTypes(array_keys($categories));
$types = $this->getCategoryTypes(group: $type_group);
$skip_categories = [];
$skip_categories[] = $edited_category_id;
@ -206,7 +208,7 @@ class Categories extends Controller
$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

@ -26,7 +26,7 @@ class AddExpenseCategories extends Listener
}
// send true for add limit on search and filter..
$event->class->filters['categories'] = $this->getExpenseCategories(true);
$event->class->filters['categories'] = $this->getExpenseCategories();
$event->class->filters['routes']['categories'] = ['categories.index', 'search=type:' . $this->getExpenseCategoryTypes('string') . ' enabled:1'];
$event->class->filters['multiple']['categories'] = true;
}

View File

@ -26,7 +26,7 @@ class AddIncomeCategories extends Listener
}
// send true for add limit on search and filter..
$event->class->filters['categories'] = $this->getIncomeCategories(true);
$event->class->filters['categories'] = $this->getIncomeCategories();
$event->class->filters['routes']['categories'] = ['categories.index', 'search=type:' . $this->getIncomeCategoryTypes('string') . ' enabled:1'];
$event->class->filters['multiple']['categories'] = true;
}

View File

@ -27,8 +27,10 @@ class AddIncomeExpenseCategories extends Listener
return;
}
$event->class->filters['categories'] = $this->getIncomeExpenseCategories(true);
$event->class->filters['routes']['categories'] = ['categories.index', 'search=type:' . implode(',', array_merge($this->getIncomeCategoryTypes(), $this->getExpenseCategoryTypes())) . ' enabled:1'];
$types = array_merge($this->getIncomeCategoryTypes(), $this->getExpenseCategoryTypes());
$event->class->filters['categories'] = $this->getIncomeExpenseCategories();
$event->class->filters['routes']['categories'] = ['categories.index', 'search=type:' . implode(',', $types) . ' enabled:1'];
$event->class->filters['multiple']['categories'] = true;
}

View File

@ -292,10 +292,9 @@ class Category extends Model
*/
public function getDisplayNameAttribute(): string
{
$typeConfig = config('type.category.' . $this->type, []);
$hideCode = isset($typeConfig['hide']) && in_array('code', $typeConfig['hide']);
$hideCode = $this->hideCodeCategoryType($this->type);
$typeNames = $this->getCategoryTypes();
$typeName = $typeNames[$this->type] ?? ucfirst($this->type);
$prefix = (!$hideCode && $this->code) ? $this->code . ' - ' : '';

View File

@ -36,6 +36,28 @@ trait Categories
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
{
return $this->getCategoryTypesByIndex(Category::INCOME_TYPE, $return);
@ -98,10 +120,46 @@ trait Categories
])->save();
}
public function getCategoryTypes(bool $translate = true, bool $group = false): array
public function isGroupCategoryType(): bool
{
$types = [];
$configs = config('type.category');
$setting_category_types = setting('category.type');
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) {
$plural_type = Str::plural($type);
@ -114,13 +172,14 @@ trait Categories
if ($group) {
$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 {
$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

View File

@ -57,7 +57,7 @@ trait Import
event(new ImportViewCreated($import));
return [
$import->view,
$import->view,
$import->data
];
}
@ -103,7 +103,7 @@ trait Import
{
$id = isset($row['contact_id']) ? $row['contact_id'] : null;
$type = !empty($type) ? $type : (!empty($row['type']) ? (($row['type'] == Transaction::INCOME_TYPE) ? 'customer' : 'vendor') : 'customer');
$type = !empty($type) ? $type : (!empty($row['type']) ? (($row['type'] == Transaction::INCOME_TYPE) ? Contact::CUSTOMER_TYPE : Contact::VENDOR_TYPE) : Contact::CUSTOMER_TYPE);
if (empty($row['contact_id']) && !empty($row['contact_email'])) {
$id = $this->getContactIdFromEmail($row, $type);

View File

@ -39,27 +39,13 @@ class Category extends Form
$this->name = 'category_id';
}
switch ($this->type) {
case Model::INCOME_TYPE:
$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];
}
$types = $this->getTypeCategoryTypes($this->type);
$types_string = implode(',', $types);
$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;

View File

@ -228,7 +228,7 @@ export default {
<div class="swiper-button-prev bg-body text-white flex items-center justify-center left-0">
<span class="material-icons text-purple text-4xl">chevron_left</span>
</div>
`;
`;
item.querySelector('[data-tabs-swiper]').innerHTML = html;
slides_view = Number(item.getAttribute('data-swiper')) != 0 ? Number(item.getAttribute('data-swiper')) : slides_view;
@ -301,6 +301,28 @@ export default {
this.form[key] = event.target.files[0];
},
isCategoryCodeFieldVisible() {
if (!this.form || !this.form.type || !this.form.type_codes) {
return false;
}
let type_codes = this.form.type_codes;
if (typeof type_codes === 'string') {
try {
type_codes = JSON.parse(type_codes);
} catch (e) {
return false;
}
}
if (typeof type_codes[this.form.type] === 'undefined') {
return true;
}
return !Boolean(type_codes[this.form.type]);
},
// Bulk Action Select all
onSelectAllBulkAction() {
this.bulk_action.selectAll();

View File

@ -31,31 +31,15 @@ const app = new Vue({
bulk_action: new BulkAction('categories'),
categoriesBasedTypes: null,
selected_type: true,
type_codes: {},
show_code_field: false
}
},
mounted() {
if (typeof type_codes !== 'undefined') {
this.type_codes = type_codes;
if (this.form.type) {
this.show_code_field = this.type_codes[this.form.type] || false;
}
}
},
methods: {
changeCategories(event) {
updateParentCategories(event) {
if (event === '') {
return;
}
if (this.type_codes[event] !== undefined) {
this.show_code_field = this.type_codes[event];
}
if (typeof JSON.parse(this.form.categories)[event] === 'undefined') {
this.categoriesBasedTypes = [];

View File

@ -27,7 +27,7 @@
>
<template #option="{option}">
<div class="flex items-center">
<span class="w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
<span class="{{ (! $group) ? 'ltr:ml-2 rtl:mr-2 ' : '' }}w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
@if ($option_field['value'] == 'title')
<span>@{{ option.option.title }}</span>
@ -63,7 +63,7 @@
>
<template #option="{option}">
<div class="flex items-center">
<span class="w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
<span class="{{ (! $group) ? 'ltr:ml-2 rtl:mr-2 ' : '' }}w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
@if ($option_field['value'] == 'title')
<span>@{{ option.option.title }}</span>
@ -99,7 +99,7 @@
>
<template #option="{option}">
<div class="flex items-center">
<span class="w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
<span class="{{ (! $group) ? 'ltr:ml-2 rtl:mr-2 ' : '' }}w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
@if ($option_field['value'] == 'title')
<span>@{{ option.option.title }}</span>
@ -129,7 +129,7 @@
>
<template #option="{option}">
<div class="flex items-center">
<span class="w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
<span class="{{ (! $group) ? 'ltr:ml-2 rtl:mr-2 ' : '' }}w-5 h-4 rounded-full" :style="{backgroundColor: option.option.color_hex_code}"></span>
@if ($option_field['value'] == 'title')
<span>@{{ option.option.title }}</span>

View File

@ -6,14 +6,14 @@
<x-form.group.select name="parent_id" label="{{ trans('general.parent') . ' ' . trans_choice('general.categories', 1) }}" :options="$categories" not-required sort-options="false" searchable form-group-class="col-span-6" />
@if (!empty($types) && count($types) > 1)
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" value="{{ $type }}" form-group-class="col-span-6" change="changeCategories" />
@if (! empty($types) && count($types) > 1)
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" value="{{ $type }}" form-group-class="col-span-6" :group="$type_group" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" form-group-class="col-span-6" v-show="show_code_field" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" form-group-class="col-span-6" v-show="isCategoryCodeFieldVisible()" />
@else
<x-form.input.hidden name="type" value="{{ $type }}" />
@if ($show_code_field)
@if (empty($hide_code_types[$type]) || ! $hide_code_types[$type])
<x-form.group.text name="code" label="{{ trans('general.code') }}" form-group-class="col-span-6" />
@endif
@endif
@ -21,12 +21,6 @@
<x-form.group.textarea name="description" label="{{ trans('general.description') }}" not-required />
<x-form.input.hidden name="enabled" value="1" />
<x-form.input.hidden name="type_codes" value="{{ json_encode($hide_code_types) }}" />
</div>
</x-form>
<script type="text/javascript">
if (typeof type_codes === 'undefined') {
var type_codes = {!! json_encode($type_codes) !!};
}
</script>

View File

@ -22,15 +22,16 @@
<x-form.group.color name="color" label="{{ trans('general.color') }}" />
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" :selected="config('general.types')" change="changeCategories" group />
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" :selected="config('general.types')" change="updateParentCategories" :group="$type_group" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" v-show="show_code_field" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" v-show="isCategoryCodeFieldVisible()" />
<x-form.group.select name="parent_id" label="{{ trans('general.parent') . ' ' . trans_choice('general.categories', 1) }}" :options="[]" not-required dynamicOptions="categoriesBasedTypes" sort-options="false" v-disabled="selected_type" />
<x-form.group.textarea name="description" label="{{ trans('general.description') }}" not-required />
<x-form.input.hidden name="categories" value="{{ json_encode($categories) }}" />
<x-form.input.hidden name="type_codes" value="{{ json_encode($hide_code_types) }}" />
</x-slot>
</x-form.section>
@ -43,11 +44,5 @@
</x-form.container>
</x-slot>
@push('scripts_start')
<script type="text/javascript">
var type_codes = {!! json_encode($type_codes) !!};
</script>
@endpush
<x-script folder="settings" file="categories" />
</x-layouts.admin>

View File

@ -14,22 +14,22 @@
<x-slot name="body">
<x-form.group.text name="name" label="{{ trans('general.name') }}" />
<x-form.group.color name="color" label="{{ trans('general.color') }}" />
@if ($type_disabled)
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" v-disabled="true" group />
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" v-disabled="true" :group="$type_group" />
<input type="hidden" name="type" value="{{ $category->type }}" />
@else
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" change="changeCategories" group />
<x-form.group.select name="type" label="{{ trans_choice('general.types', 1) }}" :options="$types" change="updateParentCategories" :group="$type_group" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" v-show="show_code_field" />
<x-form.group.text name="code" label="{{ trans('general.code') }}" v-show="isCategoryCodeFieldVisible()" />
<x-form.group.select name="parent_id" label="{{ trans('general.parent') . ' ' . trans_choice('general.categories', 1) }}" :options="$parent_categories" not-required dynamicOptions="categoriesBasedTypes" sort-options="false" />
<x-form.input.hidden name="parent_category_id" value="{{ $category->parent_id }}" />
<x-form.input.hidden name="categories" value="{{ json_encode($categories) }}" />
<x-form.input.hidden name="type_codes" value="{{ json_encode($hide_code_types) }}" />
@endif
<x-form.group.textarea name="description" label="{{ trans('general.description') }}" not-required />
@ -37,7 +37,7 @@
</x-form.section>
@if (! $type_disabled)
<x-form.group.switch name="enabled" label="{{ trans('general.enabled') }}" />
<x-form.group.switch name="enabled" label="{{ trans('general.enabled') }}" />
@endif
@can('update-settings-categories')
@ -51,11 +51,5 @@
</x-form.container>
</x-slot>
@push('scripts_start')
<script type="text/javascript">
var type_codes = {!! json_encode($type_codes) !!};
</script>
@endpush
<x-script folder="settings" file="categories" />
</x-layouts.admin>

View File

@ -136,8 +136,8 @@
@endif
<x-table.td class="{{ $name_class }}">
<div class="flex items-center">
@if ($item->sub_categories->count())
@if ($item->sub_categories->count())
<div class="flex items-center">
<x-tooltip id="tooltip-category-{{ $item->id }}" placement="bottom" message="{{ trans('categories.collapse') }}">
<button
type="button"
@ -152,7 +152,7 @@
<div class="flex items-center font-bold">
{{ $item->name }}
</div>
</div>
</div>
@else
<div class="flex items-center">
<span class="material-icons text-{{ $item->color }}" class="text-3xl" style="color:{{ $item->color }};">circle</span>

View File

@ -114,6 +114,7 @@
@php
$sub_category->load(['sub_categories']);
@endphp
@include('settings.categories.sub_category', ['parent_category' => $parent_category, 'sub_category' => $sub_category, 'tree_level' => $tree_level, 'hide_code_column' => $hide_code_column, 'name_class' => $name_class])
@endforeach
@endif