Bill item tax multible files added

This commit is contained in:
cuneytsenturk 2018-10-22 16:57:35 +03:00
parent 8fcfccc4b0
commit dedcb9493a
8 changed files with 277 additions and 58 deletions

View File

@ -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']
];
}
}
}
}
}

View File

@ -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');

View File

@ -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');
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\Currencies;
class BillItemTax extends Model
{
use Currencies;
protected $table = 'bill_item_taxes';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'bill_item_id', 'tax_id', 'name', 'amount'];
public function bill()
{
return $this->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;
}
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateBillItemTaxesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bill_item_taxes', function (Blueprint $table) {
$table->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');
}
}

View File

@ -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')
<script type="text/javascript">
var item_row = '{{ $item_row }}';
var tax_row = '0';
$(document).on('click', '#button-add-item', function (e) {
var currency_code = $('#currency_code').val();
@ -190,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>';
}
}
});
@ -249,6 +256,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>';
}
}
});
@ -270,6 +285,24 @@
placeholder : '{{ trans('general.form.no_file_selected') }}'
});
$(document).on('click', '#tax-add-new', function(e){
tax_name = $('.select2-search__field').val();
$('#modal-create-tax').remove();
$.ajax({
url: '{{ url("modals/taxes/create") }}',
type: 'GET',
dataType: 'JSON',
data: {name: tax_name},
success: function(json) {
if (json['success']) {
$('body').append(json['html']);
}
}
});
});
var autocomplete_path = "{{ url('common/items/autocomplete') }}";
$(document).on('click', '.form-control.typeahead', function() {
@ -365,8 +398,8 @@
totalItem();
});
var focus = false;
var focus = false;
$(document).on('focusin', '#items .input-price', function(){
focus = true;
});
@ -439,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) {

View File

@ -112,7 +112,7 @@
{{ Form::recurring('edit', $bill) }}
{{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}
{{ Form::fileGroup('attachment', trans('general.attachment')) }}
{{ Form::hidden('vendor_name', old('customer_name', null), ['id' => 'vendor_name']) }}
{{ Form::hidden('vendor_email', old('vendor_email', null), ['id' => 'vendor_email']) }}
@ -170,6 +170,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>';
}
}
});
@ -192,6 +200,24 @@
});
});
$(document).on('click', '#tax-add-new', function(e){
tax_name = $('.select2-search__field').val();
$('#modal-create-tax').remove();
$.ajax({
url: '{{ url("modals/taxes/create") }}',
type: 'GET',
dataType: 'JSON',
data: {name: tax_name},
success: function(json) {
if (json['success']) {
$('body').append(json['html']);
}
}
});
});
$(document).ready(function(){
$(".input-price").maskMoney({
thousands : '{{ $currency->thousands_separator }}',
@ -231,6 +257,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>';
}
}
});
@ -373,7 +407,6 @@
});
var focus = false;
$(document).on('focusin', '#items .input-price', function(){
focus = true;
});
@ -446,7 +479,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) {

View File

@ -7,7 +7,7 @@
</td>
@stack('actions_td_end')
@stack('name_td_start')
<td {!! $errors->has('item.' . $item_row . '.name') ? 'class="has-error"' : '' !!}">
<td {!! $errors->has('item.' . $item_row . '.name') ? 'class="has-error"' : '' !!}>
@stack('name_input_start')
<input value="{{ empty($item) ? '' : $item->name }}" class="form-control typeahead" required="required" placeholder="{{ trans('general.form.enter', ['field' => trans_choice('bills.item_name', 1)]) }}" name="item[{{ $item_row }}][name]" type="text" id="item-name-{{ $item_row }}" autocomplete="off">
<input value="{{ empty($item) ? '' : $item->item_id }}" name="item[{{ $item_row }}][item_id]" type="hidden" id="item-id-{{ $item_row }}">
@ -16,7 +16,7 @@
</td>
@stack('name_td_end')
@stack('quantity_td_start')
<td {{ $errors->has('item.' . $item_row . '.quantity') ? 'class="has-error"' : '' }}">
<td {{ $errors->has('item.' . $item_row . '.quantity') ? 'class="has-error"' : '' }}>
@stack('quantity_input_start')
<input value="{{ empty($item) ? '' : $item->quantity }}" class="form-control text-center" required="required" name="item[{{ $item_row }}][quantity]" type="text" id="item-quantity-{{ $item_row }}">
{!! $errors->first('item.' . $item_row . '.quantity', '<p class="help-block">:message</p>') !!}
@ -24,7 +24,7 @@
</td>
@stack('quantity_td_end')
@stack('price_td_start')
<td {{ $errors->has('item.' . $item_row . 'price') ? 'class="has-error"' : '' }}">
<td {{ $errors->has('item.' . $item_row . 'price') ? 'class="has-error"' : '' }}>
@stack('price_input_start')
<input value="{{ empty($item) ? '' : $item->price }}" class="form-control text-right input-price" required="required" name="item[{{ $item_row }}][price]" type="text" id="item-price-{{ $item_row }}">
<input value="{{ $currency->code }}" name="item[{{ $item_row }}][currency]" type="hidden" id="item-currency-{{ $item_row }}">
@ -33,9 +33,9 @@
</td>
@stack('price_td_end')
@stack('taxes_td_start')
<td {{ $errors->has('item.' . $item_row . '.tax_id') ? 'class="has-error"' : '' }}">
<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)])]) !!}
{!! 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>