Skip to content

Commit

Permalink
Add dynamic category type for modules..
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Jan 11, 2021
1 parent 7bdbaa9 commit 381ce21
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
34 changes: 33 additions & 1 deletion app/Abstracts/View/Components/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,43 @@ public function getHideFromConfig($type, $config_key)

return $hide;
}

public function getClassFromConfig($type, $config_key)
{
$class_key = 'type.' . $type . '.class.' . $config_key;

return config($class_key, '');
}

public function getCategoryFromConfig($type)
{
$category_type = '';

// if set config trasnlation config_key
if ($category_type = config('type.' . $type . '.category_type')) {
return $category_type;
}

switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$category_type = 'expense';
break;
case 'item':
$category_type = 'item';
break;
case 'other':
$category_type = 'other';
break;
case 'transfer':
$category_type = 'transfer';
break;
default:
$category_type = 'income';
break;
}

return $category_type;
}
}
22 changes: 21 additions & 1 deletion app/Abstracts/View/Components/DocumentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ abstract class DocumentForm extends Base
public $document;

/** Advanced Component Start */
/** @var string */
public $categoryType;

/** @var bool */
public $hideRecurring;

Expand Down Expand Up @@ -192,7 +195,7 @@ abstract class DocumentForm extends Base
public function __construct(
$type, $document = false,
/** Advanced Component Start */
bool $hideRecurring = false, bool $hideCategory = false, bool $hideAttachment = false,
string $categoryType = '', bool $hideRecurring = false, bool $hideCategory = false, bool $hideAttachment = false,
/** Advanced Component End */
/** Company Component Start */
bool $hideLogo = false, bool $hideDocumentTitle = false, bool $hideDocumentSubheading = false, bool $hideCompanyEdit = false,
Expand Down Expand Up @@ -220,6 +223,7 @@ public function __construct(
$this->document = $document;

/** Advanced Component Start */
$this->categoryType = $this->getCategoryType($type, $categoryType);
$this->hideRecurring = $hideRecurring;
$this->hideCategory = $hideCategory;
$this->hideAttachment = $hideAttachment;
Expand Down Expand Up @@ -341,6 +345,22 @@ protected function getRouteCancel($type, $routeCancel)
return 'invoices.index';
}

protected function getCategoryType($type, $categoryType)
{
if (!empty($categoryType)) {
return $categoryType;
}

if ($category_type = config('type.' . $type . '.category_type')) {
return $category_type;
}

// set default type
$type = Document::INVOICE_TYPE;

return config('type.' . $type . '.category_type');
}

protected function getContacts($type, $contacts)
{
if (!empty($contacts)) {
Expand Down
29 changes: 1 addition & 28 deletions app/View/Components/Documents/Form/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Advanced extends Component
*/
public function render()
{
$category_type = $this->getCategoryType();
$category_type = $this->categoryType;

if ($category_type) {
$categories = Category::$category_type()->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
Expand All @@ -24,31 +24,4 @@ public function render()

return view('components.documents.form.advanced', compact('categories', 'category_type'));
}

protected function getCategoryType()
{
$type = '';

switch ($this->type) {
case 'bill':
case 'expense':
case 'purchase':
$type = 'expense';
break;
case 'item':
$type = 'item';
break;
case 'other':
$type = 'other';
break;
case 'transfer':
$type = 'transfer';
break;
default:
$type = 'income';
break;
}

return $type;
}
}
2 changes: 2 additions & 0 deletions config/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'issued_at' => 'invoices.invoice_date',
'due_at' => 'invoices.due_date',
],
'category_type' => 'income',
'contact_type' => 'customer', // use contact type
'hide' => [], // for document items
'class' => [],
Expand All @@ -45,6 +46,7 @@
'issued_at' => 'bills.bill_date',
'due_at' => 'bills.due_date',
],
'category_type' => 'expense',
'contact_type' => 'vendor',
'hide' => [],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
@if (!$hideCategory)
{{ Form::selectRemoteAddNewGroup('category_id', trans_choice('general.categories', 1), 'folder', $categories, setting('default.' . $category_type . '_category'), ['required' => 'required', 'path' => route('modals.categories.create') . '?type=' . $category_type, 'remote_action' => route('categories.index'). '?type=' . $category_type], 'col-md-12') }}
{{ Form::selectRemoteAddNewGroup('category_id', trans_choice('general.categories', 1), 'folder', $categories, setting('default.' . $categoryType . '_category'), ['required' => 'required', 'path' => route('modals.categories.create') . '?type=' . $categoryType, 'remote_action' => route('categories.index'). '?type=' . $categoryType], 'col-md-12') }}
@endif

@if (!$hideAttachment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<x-documents.form.advanced
type="{{ $type }}"
:document="$document"
category-type="{{ $categoryType }}"
hide-recurring="{{ $hideRecurring }}"
hide-category="{{ $hideCategory }}"
hide-attachment="{{ $hideAttachment }}"
Expand Down

0 comments on commit 381ce21

Please sign in to comment.