From c074be895ff9f15ede43bb45958dc5a2de891862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Thu, 18 Feb 2021 11:54:01 +0300 Subject: [PATCH] make category types extendible --- app/Http/Controllers/Settings/Categories.php | 23 +--- app/Traits/Categories.php | 28 +++++ config/type.php | 119 ++++++++++++------- 3 files changed, 107 insertions(+), 63 deletions(-) create mode 100644 app/Traits/Categories.php diff --git a/app/Http/Controllers/Settings/Categories.php b/app/Http/Controllers/Settings/Categories.php index 727df6cb1..6bdc533e5 100644 --- a/app/Http/Controllers/Settings/Categories.php +++ b/app/Http/Controllers/Settings/Categories.php @@ -11,9 +11,11 @@ use App\Jobs\Setting\CreateCategory; use App\Jobs\Setting\DeleteCategory; use App\Jobs\Setting\UpdateCategory; use App\Models\Setting\Category; +use App\Traits\Categories as Helper; class Categories extends Controller { + use Helper; /** * Display a listing of the resource. @@ -26,12 +28,7 @@ class Categories extends Controller $transfer_id = Category::transfer(); - $types = collect([ - 'expense' => trans_choice('general.expenses', 1), - 'income' => trans_choice('general.incomes', 1), - 'item' => trans_choice('general.items', 1), - 'other' => trans_choice('general.others', 1), - ]); + $types = $this->getCategoryTypes(); return $this->response('settings.categories.index', compact('categories', 'types', 'transfer_id')); } @@ -53,12 +50,7 @@ class Categories extends Controller */ public function create() { - $types = [ - 'expense' => trans_choice('general.expenses', 1), - 'income' => trans_choice('general.incomes', 1), - 'item' => trans_choice('general.items', 1), - 'other' => trans_choice('general.others', 1), - ]; + $types = $this->getCategoryTypes(); return view('settings.categories.create', compact('types')); } @@ -128,12 +120,7 @@ class Categories extends Controller */ public function edit(Category $category) { - $types = [ - 'expense' => trans_choice('general.expenses', 1), - 'income' => trans_choice('general.incomes', 1), - 'item' => trans_choice('general.items', 1), - 'other' => trans_choice('general.others', 1), - ]; + $types = $this->getCategoryTypes(); $type_disabled = (Category::where('type', $category->type)->count() == 1) ?: false; diff --git a/app/Traits/Categories.php b/app/Traits/Categories.php new file mode 100644 index 000000000..1b7ba68c9 --- /dev/null +++ b/app/Traits/Categories.php @@ -0,0 +1,28 @@ + $attr) { + $plural_type = Str::plural($type); + + $name = $attr['translation']['prefix'] . '.' . $plural_type; + + if (!empty($attr['alias'])) { + $name = $attr['alias'] . '::' . $name; + } + + $types[$type] = trans_choice($name, 1); + } + + return $types; + } +} diff --git a/config/type.php b/config/type.php index 920ce1c4a..b24e0e361 100644 --- a/config/type.php +++ b/config/type.php @@ -6,86 +6,115 @@ return [ // Documents Document::INVOICE_TYPE => [ - 'alias' => '', // core empty but module write own alias - 'group' => 'sales', // controller folder name for permission and route + 'alias' => '', // core empty but module write own alias + 'group' => 'sales', // controller folder name for permission and route 'route' => [ - 'prefix' => 'invoices', // core use with group + prefix, module ex. estimates - 'parameter' => 'invoice', // sales/invoices/{parameter}/edit - //'create' => 'invoices.create', // if you change route, you can write full path + 'prefix' => 'invoices', // core use with group + prefix, module ex. estimates + 'parameter' => 'invoice', // sales/invoices/{parameter}/edit + //'create' => 'invoices.create', // if you change route, you can write full path ], 'permission' => [ - 'prefix' => 'invoices', // this controller file name. - //'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission + 'prefix' => 'invoices', // this controller file name. + //'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission ], 'translation' => [ - 'prefix' => 'invoices', // this translation file name. - 'add_contact' => 'general.customers', // - 'issued_at' => 'invoices.invoice_date', - 'due_at' => 'invoices.due_date', + 'prefix' => 'invoices', // this translation file name. + 'add_contact' => 'general.customers', // + 'issued_at' => 'invoices.invoice_date', + 'due_at' => 'invoices.due_date', ], - 'category_type' => 'income', - 'transaction_type' => 'income', - 'contact_type' => 'customer', // use contact type - 'hide' => [], // for document items - 'class' => [], + 'category_type' => 'income', + 'transaction_type' => 'income', + 'contact_type' => 'customer', // use contact type + 'hide' => [], // for document items + 'class' => [], ], Document::BILL_TYPE => [ - 'alias' => '', - 'group' => 'purchases', + 'alias' => '', + 'group' => 'purchases', 'route' => [ - 'prefix' => 'bills', - 'parameter' => 'bill', - //'create' => 'bilss.create', + 'prefix' => 'bills', + 'parameter' => 'bill', + //'create' => 'bilss.create', ], 'permission' => [ - 'prefix' => 'bills', - //'create' => 'create-purchases-bills', + 'prefix' => 'bills', + //'create' => 'create-purchases-bills', ], - 'translation' => [ - 'prefix' => 'bills', - 'issued_at' => 'bills.bill_date', - 'due_at' => 'bills.due_date', + 'translation' => [ + 'prefix' => 'bills', + 'issued_at' => 'bills.bill_date', + 'due_at' => 'bills.due_date', ], - 'category_type' => 'expense', - 'transaction_type' => 'expense', - 'contact_type' => 'vendor', - 'hide' => [], + 'category_type' => 'expense', + 'transaction_type' => 'expense', + 'contact_type' => 'vendor', + 'hide' => [], ], // Contacts 'customer' => [ - 'group' => 'sales', + 'group' => 'sales', 'permission' => [ - 'prefix' => 'customers', - //'create' => 'create-sales-customers', + 'prefix' => 'customers', + //'create' => 'create-sales-customers', ], ], 'vendor' => [ - 'group' => 'purchases', + 'group' => 'purchases', 'permission' => [ - 'prefix' => 'vendors', - //'create' => 'create-purchases-vendors', + 'prefix' => 'vendors', + //'create' => 'create-purchases-vendors', ], ], // Transactions 'income' => [ - 'group' => 'sales', + 'group' => 'sales', 'permission' => [ - 'prefix' => 'revenues', - //'create' => 'create-sales-revenues', + 'prefix' => 'revenues', + //'create' => 'create-sales-revenues', ], - 'contact_type' => 'customer', + 'contact_type' => 'customer', ], 'expense' => [ - 'group' => 'purchases', + 'group' => 'purchases', 'permission' => [ - 'prefix' => 'payments', - //'create' => 'create-purchases-payments', + 'prefix' => 'payments', + //'create' => 'create-purchases-payments', ], - 'contact_type' => 'vendor', + 'contact_type' => 'vendor', ], + + // Categories + 'category' => [ + 'income' => [ + 'alias' => '', + 'translation' => [ + 'prefix' => 'general', + ], + ], + 'expense' => [ + 'alias' => '', + 'translation' => [ + 'prefix' => 'general', + ], + ], + 'item' => [ + 'alias' => '', + 'translation' => [ + 'prefix' => 'general', + ], + ], + 'other' => [ + 'alias' => '', + 'translation' => [ + 'prefix' => 'general', + ], + ], + ], + ];