From dedcb9493abdbb4177326ad588e35925d4294b59 Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Mon, 22 Oct 2018 16:57:35 +0300 Subject: [PATCH] Bill item tax multible files added --- app/Http/Controllers/Expenses/Bills.php | 128 ++++++++++++------ app/Models/Expense/Bill.php | 5 + app/Models/Expense/BillItem.php | 24 ++++ app/Models/Expense/BillItemTax.php | 47 +++++++ ...22_000000_create_bill_item_taxes_table.php | 39 ++++++ .../views/expenses/bills/create.blade.php | 43 +++++- resources/views/expenses/bills/edit.blade.php | 39 +++++- resources/views/expenses/bills/item.blade.php | 10 +- 8 files changed, 277 insertions(+), 58 deletions(-) create mode 100644 app/Models/Expense/BillItemTax.php create mode 100644 database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index f1b021550..fba2db439 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -169,7 +169,6 @@ class Bills extends Controller if ($request['item']) { foreach ($request['item'] as $item) { - unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -183,18 +182,32 @@ class Bills extends Controller $item_object->save(); } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $bill_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $bill_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -203,29 +216,33 @@ class Bills extends Controller $bill_item['sku'] = $item_sku; $bill_item['quantity'] = (double) $item['quantity']; $bill_item['price'] = (double) $item['price']; - $bill_item['tax'] = $tax; - $bill_item['tax_id'] = $tax_id; + $bill_item['tax'] = $item_tax; + $bill_item['tax_id'] = 0;//$tax_id; $bill_item['total'] = (double) $item['price'] * (double) $item['quantity']; - BillItem::create($bill_item); + $bill_item_created = BillItem::create($bill_item); - // Set taxes - if (isset($tax_object)) { - if (isset($taxes) && array_key_exists($tax_object->id, $taxes)) { - $taxes[$tax_object->id]['amount'] += $tax; - } else { - $taxes[$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; + if ($bill_item_taxes) { + foreach ($bill_item_taxes as $bill_item_tax) { + $bill_item_tax['invoice_item_id'] = $bill_item_created->id; + + BillItemTax::create($bill_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) { + $taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount']; + } else { + $taxes[$bill_item_tax['tax_id']] = [ + 'name' => $bill_item_tax['name'], + 'amount' => $bill_item_tax['amount'] + ]; + } } } // Calculate totals - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $bill_item['total']; - - unset($tax_object); } } @@ -398,18 +415,32 @@ class Bills extends Controller $item_sku = $item_object->sku; } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $bill_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $bill_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -418,25 +449,32 @@ class Bills extends Controller $bill_item['sku'] = $item_sku; $bill_item['quantity'] = (double) $item['quantity']; $bill_item['price'] = (double) $item['price']; - $bill_item['tax'] = $tax; - $bill_item['tax_id'] = $tax_id; + $bill_item['tax'] = $item_tax; + $bill_item['tax_id'] = 0;//$tax_id; $bill_item['total'] = (double) $item['price'] * (double) $item['quantity']; - if (isset($tax_object)) { - if (isset($taxes) && array_key_exists($tax_object->id, $taxes)) { - $taxes[$tax_object->id]['amount'] += $tax; - } else { - $taxes[$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; - } - } - - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $bill_item['total']; - BillItem::create($bill_item); + $bill_item_created = BillItem::create($bill_item); + + if ($bill_item_taxes) { + foreach ($bill_item_taxes as $bill_item_tax) { + $bill_item_tax['invoice_item_id'] = $bill_item_created->id; + + BillItemTax::create($bill_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) { + $taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount']; + } else { + $taxes[$bill_item_tax['tax_id']] = [ + 'name' => $bill_item_tax['name'], + 'amount' => $bill_item_tax['amount'] + ]; + } + } + } } } diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index 3ae017282..3258163e1 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -82,6 +82,11 @@ class Bill extends Model return $this->hasMany('App\Models\Expense\BillItem'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Expense\BillItemTax'); + } + public function payments() { return $this->hasMany('App\Models\Expense\BillPayment'); diff --git a/app/Models/Expense/BillItem.php b/app/Models/Expense/BillItem.php index ae2c026c0..b40f6fdf5 100644 --- a/app/Models/Expense/BillItem.php +++ b/app/Models/Expense/BillItem.php @@ -29,6 +29,11 @@ class BillItem extends Model return $this->belongsTo('App\Models\Common\Item'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Expense\BillItemTax', 'bill_item_id', 'id'); + } + public function tax() { return $this->belongsTo('App\Models\Setting\Tax'); @@ -66,4 +71,23 @@ class BillItem extends Model { $this->attributes['tax'] = (double) $value; } + + /** + * Convert tax to double. + * + * @param string $value + * @return void + */ + public function getTaxIdAttribute($value) + { + $tax_ids = []; + + if (!empty($value)) { + $tax_ids[] = $value; + + return $tax_ids; + } + + return $this->itemTaxes->pluck('tax_id'); + } } diff --git a/app/Models/Expense/BillItemTax.php b/app/Models/Expense/BillItemTax.php new file mode 100644 index 000000000..8f7e2862a --- /dev/null +++ b/app/Models/Expense/BillItemTax.php @@ -0,0 +1,47 @@ +belongsTo('App\Models\Expense\Bill'); + } + + public function item() + { + return $this->belongsTo('App\Models\Common\Item'); + } + + public function tax() + { + return $this->belongsTo('App\Models\Setting\Tax'); + } + + /** + * Convert amount to double. + * + * @param string $value + * @return void + */ + public function setAmountAttribute($value) + { + $this->attributes['amount'] = (double) $value; + } +} diff --git a/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php b/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php new file mode 100644 index 000000000..7338aca0d --- /dev/null +++ b/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php @@ -0,0 +1,39 @@ +increments('id'); + $table->integer('company_id'); + $table->integer('bill_id'); + $table->integer('bill_item_id'); + $table->integer('tax_id'); + $table->string('name'); + $table->double('amount', 15, 4)->default('0.0000'); + $table->timestamps(); + $table->softDeletes(); + + $table->index('company_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('bill_item_taxes'); + } +} diff --git a/resources/views/expenses/bills/create.blade.php b/resources/views/expenses/bills/create.blade.php index bf912403c..be9bb813f 100644 --- a/resources/views/expenses/bills/create.blade.php +++ b/resources/views/expenses/bills/create.blade.php @@ -130,7 +130,7 @@ {{ Form::recurring('create') }} - {{ Form::fileGroup('attachment', trans('general.attachment'),[]) }} + {{ Form::fileGroup('attachment', trans('general.attachment')) }} {{ Form::hidden('vendor_name', old('vendor_name'), ['id' => 'vendor_name']) }} {{ Form::hidden('vendor_email', old('vendor_email'), ['id' => 'vendor_email']) }} @@ -169,7 +169,6 @@ @push('scripts')