Invoice create invocie item multible tax
This commit is contained in:
parent
39e6e0b07c
commit
7f9239e602
@ -323,22 +323,25 @@ class Items extends Controller
|
||||
$price = (double) $item['price'];
|
||||
$quantity = (double) $item['quantity'];
|
||||
|
||||
$item_tax_total= 0;
|
||||
$item_tax_total = 0;
|
||||
$item_sub_total = ($price * $quantity);
|
||||
|
||||
if (!empty($item['tax_id'])) {
|
||||
$tax = Tax::find($item['tax_id']);
|
||||
foreach ($item['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
$item_tax_total = (($price * $quantity) / 100) * $tax->rate;
|
||||
$item_tax = (($price * $quantity) / 100) * $tax->rate;
|
||||
|
||||
// Apply discount to tax
|
||||
if ($discount) {
|
||||
$item_tax = $item_tax - ($item_tax * ($discount / 100));
|
||||
}
|
||||
|
||||
$item_tax_total += $item_tax;
|
||||
}
|
||||
}
|
||||
|
||||
$sub_total += $item_sub_total;
|
||||
|
||||
// Apply discount to tax
|
||||
if ($discount) {
|
||||
$item_tax_total = $item_tax_total - ($item_tax_total * ($discount / 100));
|
||||
}
|
||||
|
||||
$tax_total += $item_tax_total;
|
||||
|
||||
$items[$key] = money($item_sub_total, $currency_code, true)->format();
|
||||
|
@ -14,6 +14,7 @@ use App\Models\Income\Customer;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoiceItem;
|
||||
use App\Models\Income\InvoiceItemTax;
|
||||
use App\Models\Income\InvoiceTotal;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\InvoiceStatus;
|
||||
@ -218,18 +219,32 @@ class Invoices extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$tax = $tax_id = 0;
|
||||
$item_tax = 0;
|
||||
$item_taxes = [];
|
||||
$invoice_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));
|
||||
}
|
||||
|
||||
$invoice_item_taxes[] = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax_object->name,
|
||||
'amount' => $tax,
|
||||
];
|
||||
|
||||
$item_tax += $tax;
|
||||
}
|
||||
}
|
||||
|
||||
@ -238,29 +253,33 @@ class Invoices extends Controller
|
||||
$invoice_item['sku'] = $item_sku;
|
||||
$invoice_item['quantity'] = (double) $item['quantity'];
|
||||
$invoice_item['price'] = (double) $item['price'];
|
||||
$invoice_item['tax'] = $tax;
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['tax'] = $item_tax;
|
||||
$invoice_item['tax_id'] = 0;//(int) $item_taxes;
|
||||
$invoice_item['total'] = (double) $item['price'] * (double) $item['quantity'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
$invoice_item_created = InvoiceItem::create($invoice_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 ($invoice_item_taxes) {
|
||||
foreach ($invoice_item_taxes as $invoice_item_tax) {
|
||||
$invoice_item_tax['invoice_item_id'] = $invoice_item_created->id;
|
||||
|
||||
InvoiceItemTax::create($invoice_item_tax);
|
||||
|
||||
// Set taxes
|
||||
if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$invoice_item_tax['tax_id']] = [
|
||||
'name' => $invoice_item_tax['name'],
|
||||
'amount' => $invoice_item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $tax;
|
||||
$tax_total += $item_tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
unset($tax_object);
|
||||
}
|
||||
}
|
||||
|
||||
@ -439,18 +458,32 @@ class Invoices extends Controller
|
||||
$item_sku = $item_object->sku;
|
||||
}
|
||||
|
||||
$tax = $tax_id = 0;
|
||||
$item_tax = 0;
|
||||
$item_taxes = [];
|
||||
$invoice_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));
|
||||
}
|
||||
|
||||
$invoice_item_taxes[] = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax_object->name,
|
||||
'amount' => $tax,
|
||||
];
|
||||
|
||||
$item_tax += $tax;
|
||||
}
|
||||
}
|
||||
|
||||
@ -460,24 +493,31 @@ class Invoices extends Controller
|
||||
$invoice_item['quantity'] = (double) $item['quantity'];
|
||||
$invoice_item['price'] = (double) $item['price'];
|
||||
$invoice_item['tax'] = $tax;
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['tax_id'] = 0;//$tax_id;
|
||||
$invoice_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
|
||||
];
|
||||
$invoice_item_created = InvoiceItem::create($invoice_item);
|
||||
|
||||
if ($invoice_item_taxes) {
|
||||
foreach ($invoice_item_taxes as $invoice_item_tax) {
|
||||
$invoice_item_tax['invoice_item_id'] = $invoice_item_created->id;
|
||||
|
||||
InvoiceItemTax::create($invoice_item_tax);
|
||||
|
||||
// Set taxes
|
||||
if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$invoice_item_tax['tax_id']] = [
|
||||
'name' => $invoice_item_tax['name'],
|
||||
'amount' => $invoice_item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$tax_total += $tax;
|
||||
$tax_total += $item_tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,6 +83,11 @@ class Invoice extends Model
|
||||
return $this->hasMany('App\Models\Income\InvoiceItem');
|
||||
}
|
||||
|
||||
public function itemTaxes()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\InvoiceItemTax');
|
||||
}
|
||||
|
||||
public function histories()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\InvoiceHistory');
|
||||
|
47
app/Models/Income/InvoiceItemTax.php
Normal file
47
app/Models/Income/InvoiceItemTax.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Income;
|
||||
|
||||
use App\Models\Model;
|
||||
use App\Traits\Currencies;
|
||||
|
||||
class InvoiceItemTax extends Model
|
||||
{
|
||||
|
||||
use Currencies;
|
||||
|
||||
protected $table = 'invoice_item_taxes';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'invoice_id', 'invoice_item_id', 'tax_id', 'name', 'amount'];
|
||||
|
||||
public function invoice()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Income\Invoice');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateInvoiceItemTaxesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('invoice_item_taxes', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('company_id');
|
||||
$table->integer('invoice_id');
|
||||
$table->integer('invoice_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('invoice_item_taxes');
|
||||
}
|
||||
}
|
8
public/css/app.css
vendored
8
public/css/app.css
vendored
@ -765,3 +765,11 @@ input[type="number"] {
|
||||
border-color: #6da252;
|
||||
border-radius: 0;
|
||||
}
|
||||
|
||||
#tax-add-new {
|
||||
padding: 6px 12px;
|
||||
}
|
||||
|
||||
#items .select2-search__field {
|
||||
padding-left: 15px;
|
||||
}
|
@ -189,6 +189,14 @@
|
||||
placeholder: {
|
||||
id: '-1', // the value of the option
|
||||
text: "{{ trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)]) }}"
|
||||
},
|
||||
escapeMarkup: function (markup) {
|
||||
return markup;
|
||||
},
|
||||
language: {
|
||||
noResults: function () {
|
||||
return '<span id="tax-add-new"><i class="fa fa-plus"> {{ trans('general.title.new', ['type' => trans_choice('general.tax_rates', 1)]) }}</span>';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -464,7 +472,7 @@
|
||||
url: '{{ url("common/items/totalItem") }}',
|
||||
type: 'POST',
|
||||
dataType: 'JSON',
|
||||
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
|
||||
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select').serialize(),
|
||||
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
|
||||
success: function(data) {
|
||||
if (data) {
|
||||
|
@ -35,7 +35,7 @@
|
||||
@stack('taxes_td_start')
|
||||
<td {{ $errors->has('item.' . $item_row . '.tax_id') ? 'class="has-error"' : '' }}>
|
||||
@stack('tax_id_input_start')
|
||||
{!! Form::select('item[' . $item_row . '][tax_id][]', $taxes, empty($item) ? setting('general.default_tax') : $item->tax_id, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control tax-select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)]), 'multiple' => 'true']) !!}
|
||||
{!! Form::select('item[' . $item_row . '][tax_id][]', $taxes, empty($item) ? setting('general.default_tax') : $item->tax_id, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control tax-select2', 'multiple' => 'true']) !!}
|
||||
{!! $errors->first('item.' . $item_row . '.tax_id', '<p class="help-block">:message</p>') !!}
|
||||
@stack('tax_id_input_end')
|
||||
</td>
|
||||
|
Loading…
x
Reference in New Issue
Block a user