From 032555507bee3b058e0d2918affb81feca97177e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Tue, 8 Dec 2020 13:30:06 +0300 Subject: [PATCH] Item multiple tax added --- app/Jobs/Common/CreateItem.php | 3 ++ app/Jobs/Common/CreateItemTaxes.php | 54 +++++++++++++++++++ app/Jobs/Common/DeleteItem.php | 4 ++ app/Jobs/Common/UpdateItem.php | 5 ++ app/Models/Common/Item.php | 21 ++++++-- app/Models/Common/ItemTax.php | 25 +++++++++ .../2020_10_13_000000_core_v210.php | 12 +++++ resources/views/common/items/create.blade.php | 2 +- resources/views/common/items/edit.blade.php | 2 +- 9 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 app/Jobs/Common/CreateItemTaxes.php create mode 100644 app/Models/Common/ItemTax.php diff --git a/app/Jobs/Common/CreateItem.php b/app/Jobs/Common/CreateItem.php index 121ac4995..2b754639b 100644 --- a/app/Jobs/Common/CreateItem.php +++ b/app/Jobs/Common/CreateItem.php @@ -4,6 +4,7 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Models\Common\Item; +use App\Jobs\Common\CreateItemTaxes; class CreateItem extends Job { @@ -37,6 +38,8 @@ class CreateItem extends Job $this->item->attachMedia($media, 'picture'); } + + $this->dispatch(new CreateItemTaxes($this->item, $this->request)); }); return $this->item; diff --git a/app/Jobs/Common/CreateItemTaxes.php b/app/Jobs/Common/CreateItemTaxes.php new file mode 100644 index 000000000..e0d808d6d --- /dev/null +++ b/app/Jobs/Common/CreateItemTaxes.php @@ -0,0 +1,54 @@ +item = $item; + $this->request = $request; + } + + /** + * Execute the job. + * + * @return Item + */ + public function handle() + { + \DB::transaction(function () { + // This controller for BC < 2.1 + if (!empty($this->request['tax_id'])) { + ItemTax::create([ + 'company_id' => $this->item->company_id, + 'item_id' => $this->item->id, + 'tax_id' => $this->request['tax_id'] + ]); + } else { + foreach ($this->request['tax_ids'] as $tax_id) { + ItemTax::create([ + 'company_id' => $this->item->company_id, + 'item_id' => $this->item->id, + 'tax_id' => $tax_id + ]); + } + } + }); + + return $this->item->taxes; + } +} diff --git a/app/Jobs/Common/DeleteItem.php b/app/Jobs/Common/DeleteItem.php index 8b194adc1..41cbb1608 100644 --- a/app/Jobs/Common/DeleteItem.php +++ b/app/Jobs/Common/DeleteItem.php @@ -28,6 +28,10 @@ class DeleteItem extends Job $this->authorize(); \DB::transaction(function () { + $this->deleteRelationships($this->item, [ + 'taxes' + ]); + $this->item->delete(); }); diff --git a/app/Jobs/Common/UpdateItem.php b/app/Jobs/Common/UpdateItem.php index 1ebc52a53..6c038b9c1 100644 --- a/app/Jobs/Common/UpdateItem.php +++ b/app/Jobs/Common/UpdateItem.php @@ -4,6 +4,7 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Models\Common\Item; +use App\Jobs\Common\CreateItemTaxes; class UpdateItem extends Job { @@ -39,6 +40,10 @@ class UpdateItem extends Job $this->item->attachMedia($media, 'picture'); } + + $this->deleteRelationships($this->item, ['taxes']); + + $this->dispatch(new CreateItemTaxes($this->item, $this->request)); }); return $this->item; diff --git a/app/Models/Common/Item.php b/app/Models/Common/Item.php index e00cf7372..7c66d2d01 100644 --- a/app/Models/Common/Item.php +++ b/app/Models/Common/Item.php @@ -26,7 +26,7 @@ class Item extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'tax_id', 'enabled']; + protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled']; /** * The attributes that should be cast. @@ -46,14 +46,19 @@ class Item extends Model */ protected $sortable = ['name', 'category', 'sale_price', 'purchase_price', 'enabled']; + /** + * @var array + */ + public $cloneable_relations = ['taxes']; + public function category() { return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]); } - public function tax() + public function taxes() { - return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na'), 'rate' => 0]); + return $this->hasMany('App\Models\Common\ItemTax'); } public function bill_items() @@ -81,6 +86,16 @@ class Item extends Model return $this->id; } + /** + * Get the item id. + * + * @return string + */ + public function getTaxIdsAttribute() + { + return $this->taxes()->pluck('tax_id'); + } + /** * Scope autocomplete. * diff --git a/app/Models/Common/ItemTax.php b/app/Models/Common/ItemTax.php new file mode 100644 index 000000000..f963c9eaa --- /dev/null +++ b/app/Models/Common/ItemTax.php @@ -0,0 +1,25 @@ +belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na'), 'rate' => 0]); + } +} diff --git a/database/migrations/2020_10_13_000000_core_v210.php b/database/migrations/2020_10_13_000000_core_v210.php index 509b671a9..774aaf1a5 100644 --- a/database/migrations/2020_10_13_000000_core_v210.php +++ b/database/migrations/2020_10_13_000000_core_v210.php @@ -16,6 +16,17 @@ class CoreV210 extends Migration Schema::table('failed_jobs', function (Blueprint $table) { $table->string('uuid')->after('id')->nullable()->unique(); }); + + Schema::create('item_taxes', function (Blueprint $table) { + $table->increments('id'); + $table->integer('company_id'); + $table->integer('item_id'); + $table->integer('tax_id'); + $table->timestamps(); + $table->softDeletes(); + + $table->index(['company_id', 'item_id']); + }); } /** @@ -25,6 +36,7 @@ class CoreV210 extends Migration */ public function down() { + Schema::drop('item_taxes'); // } } diff --git a/resources/views/common/items/create.blade.php b/resources/views/common/items/create.blade.php index 62023f1a7..278e5c324 100644 --- a/resources/views/common/items/create.blade.php +++ b/resources/views/common/items/create.blade.php @@ -19,7 +19,7 @@
{{ Form::textGroup('name', trans('general.name'), 'tag') }} - {{ Form::selectAddNewGroup('tax_id', trans_choice('general.taxes', 1), 'percentage', $taxes, setting('default.tax'), ['path' => route('modals.taxes.create'), 'field' => ['key' => 'id', 'value' => 'title']]) }} + {{ Form::multiSelectAddNewGroup('tax_ids', trans_choice('general.taxes', 1), 'percentage', $taxes, [setting('default.tax')], ['path' => route('modals.taxes.create'), 'field' => ['key' => 'id', 'value' => 'title']]) }} {{ Form::textareaGroup('description', trans('general.description')) }} diff --git a/resources/views/common/items/edit.blade.php b/resources/views/common/items/edit.blade.php index 5c2676b28..3a1ad4eb2 100644 --- a/resources/views/common/items/edit.blade.php +++ b/resources/views/common/items/edit.blade.php @@ -20,7 +20,7 @@
{{ Form::textGroup('name', trans('general.name'), 'tag') }} - {{ Form::selectAddNewGroup('tax_id', trans_choice('general.taxes', 1), 'percentage', $taxes, $item->tax_id, ['path' => route('modals.taxes.create'), 'field' => ['key' => 'id', 'value' => 'title']]) }} + {{ Form::multiSelectAddNewGroup('tax_ids', trans_choice('general.taxes', 1), 'percentage', $taxes, $item->tax_ids, ['path' => route('modals.taxes.create'), 'field' => ['key' => 'id', 'value' => 'title']]) }} {{ Form::textareaGroup('description', trans('general.description')) }}