Merge pull request #1682 from cuneytsenturk/2.1-dev

Item multiple tax added
This commit is contained in:
Cüneyt Şentürk 2020-12-08 13:43:34 +03:00 committed by GitHub
commit 8e88ebb52b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 143 additions and 6 deletions

View File

@ -33,7 +33,7 @@ class Item extends FormRequest
'name' => 'required|string', 'name' => 'required|string',
'sale_price' => 'required', 'sale_price' => 'required',
'purchase_price' => 'required', 'purchase_price' => 'required',
'tax_id' => 'nullable|integer', 'tax_ids' => 'nullable|array',
'category_id' => 'nullable|integer', 'category_id' => 'nullable|integer',
'enabled' => 'integer|boolean', 'enabled' => 'integer|boolean',
'picture' => $picture, 'picture' => $picture,

View File

@ -4,6 +4,7 @@ namespace App\Jobs\Common;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Common\Item; use App\Models\Common\Item;
use App\Jobs\Common\CreateItemTaxes;
class CreateItem extends Job class CreateItem extends Job
{ {
@ -37,6 +38,8 @@ class CreateItem extends Job
$this->item->attachMedia($media, 'picture'); $this->item->attachMedia($media, 'picture');
} }
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
}); });
return $this->item; return $this->item;

View File

@ -0,0 +1,54 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\ItemTax;
class CreateItemTaxes extends Job
{
protected $item;
protected $request;
/**
* Create a new job instance.
*
* @param $item
* @param $request
*/
public function __construct($item, $request)
{
$this->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;
}
}

View File

@ -28,6 +28,10 @@ class DeleteItem extends Job
$this->authorize(); $this->authorize();
\DB::transaction(function () { \DB::transaction(function () {
$this->deleteRelationships($this->item, [
'taxes'
]);
$this->item->delete(); $this->item->delete();
}); });

View File

@ -4,6 +4,7 @@ namespace App\Jobs\Common;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Common\Item; use App\Models\Common\Item;
use App\Jobs\Common\CreateItemTaxes;
class UpdateItem extends Job class UpdateItem extends Job
{ {
@ -39,6 +40,10 @@ class UpdateItem extends Job
$this->item->attachMedia($media, 'picture'); $this->item->attachMedia($media, 'picture');
} }
$this->deleteRelationships($this->item, ['taxes']);
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
}); });
return $this->item; return $this->item;

View File

@ -30,6 +30,9 @@ class Version210 extends Listener
$this->updateCompanies(); $this->updateCompanies();
Artisan::call('migrate', ['--force' => true]); Artisan::call('migrate', ['--force' => true]);
#todo remove tax_id column
$this->copyItemTax();
} }
protected function updateCompanies() protected function updateCompanies()
{ {
@ -65,4 +68,20 @@ class Version210 extends Listener
setting()->save(); setting()->save();
} }
public function copyItemTax()
{
$items = DB::table('items')->cursor();
foreach ($items as $item) {
DB::table('item_taxes')->insert([
'company_id' => $item->company_id,
'item_id' => $item->id,
'tax_id' => $item->tax_id,
'created_at' => $item->created_at,
'updated_at' => $item->updated_at,
'deleted_at' => $item->deleted_at,
]);
}
}
} }

View File

@ -26,7 +26,7 @@ class Item extends Model
* *
* @var array * @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. * The attributes that should be cast.
@ -46,14 +46,19 @@ class Item extends Model
*/ */
protected $sortable = ['name', 'category', 'sale_price', 'purchase_price', 'enabled']; protected $sortable = ['name', 'category', 'sale_price', 'purchase_price', 'enabled'];
/**
* @var array
*/
public $cloneable_relations = ['taxes'];
public function category() public function category()
{ {
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]); 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() public function bill_items()
@ -81,6 +86,16 @@ class Item extends Model
return $this->id; return $this->id;
} }
/**
* Get the item id.
*
* @return string
*/
public function getTaxIdsAttribute()
{
return $this->taxes()->pluck('tax_id');
}
/** /**
* Scope autocomplete. * Scope autocomplete.
* *

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Common;
use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
class ItemTax extends Model
{
use Cloneable;
protected $table = 'item_taxes';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'item_id', 'tax_id'];
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na'), 'rate' => 0]);
}
}

View File

@ -16,6 +16,17 @@ class CoreV210 extends Migration
Schema::table('failed_jobs', function (Blueprint $table) { Schema::table('failed_jobs', function (Blueprint $table) {
$table->string('uuid')->after('id')->nullable()->unique(); $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() public function down()
{ {
Schema::drop('item_taxes');
// //
} }
} }

View File

@ -19,7 +19,7 @@
<div class="row"> <div class="row">
{{ Form::textGroup('name', trans('general.name'), 'tag') }} {{ 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')) }} {{ Form::textareaGroup('description', trans('general.description')) }}

View File

@ -20,7 +20,7 @@
<div class="row"> <div class="row">
{{ Form::textGroup('name', trans('general.name'), 'tag') }} {{ 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')) }} {{ Form::textareaGroup('description', trans('general.description')) }}