Skip to content

Commit

Permalink
Tax rate change workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Nov 6, 2018
1 parent f3a7055 commit 79ebd9d
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 61 deletions.
66 changes: 62 additions & 4 deletions app/Http/Controllers/Common/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,78 @@ public function totalItem(TRequest $request)
$quantity = (double) $item['quantity'];

$item_tax_total = 0;
$item_tax_amount = 0;

$item_sub_total = ($price * $quantity);
$item_discount_total = $item_sub_total;

// Apply discount to item
if ($discount) {
$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
}

if (!empty($item['tax_id'])) {
$calculates = $compounds = $taxes = [];

foreach ($item['tax_id'] as $tax_id) {
$tax = Tax::find($tax_id);

$item_tax = (($price * $quantity) / 100) * $tax->rate;
switch ($tax->type) {
case 'calculate':
$calculates[] = $tax;
break;
case 'compound':
$compounds[] = $tax;
break;
case 'normal':
default:
$taxes[] = $tax;

$item_tax_amount = ($item_discount_total / 100) * $tax->rate;

$item_tax_total += $item_tax_amount;
break;
}
}

// Apply discount to tax
if ($calculates) {
if ($discount) {
$item_tax = $item_tax - ($item_tax * ($discount / 100));
$item_tax_total = 0;

if ($taxes) {
foreach ($taxes as $tax) {
$item_tax_amount = ($item_sub_total / 100) * $tax->rate;

$item_tax_total += $item_tax_amount;
}
}

foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_sub_total + $item_tax_total;

$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);

$item_sub_total = $item_sub_and_tax_total - $item_tax_total;

$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
}
} else {
foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_discount_total + $item_tax_total;

$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);

$item_sub_total = $item_sub_and_tax_total - $item_tax_total;

$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
}
}
}

$item_tax_total += $item_tax;
if ($compounds) {
foreach ($compounds as $compound) {
$item_tax_total += (($item_discount_total + $item_tax_total) / 100) * $compound->rate;
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions app/Http/Controllers/Settings/Taxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function show()
*/
public function create()
{
return view('settings.taxes.create');
$types = [
'normal' => trans('taxes.normal'),
'calculate' => trans('taxes.calculate'),
'compound' => trans('taxes.compound'),
];

return view('settings.taxes.create', compact('types'));
}

/**
Expand Down Expand Up @@ -68,7 +74,13 @@ public function store(Request $request)
*/
public function edit(Tax $tax)
{
return view('settings.taxes.edit', compact('tax'));
$types = [
'normal' => trans('taxes.normal'),
'calculate' => trans('taxes.calculate'),
'compound' => trans('taxes.compound'),
];

return view('settings.taxes.edit', compact('tax', 'types'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/Income/CreateInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ protected function addTotals($invoice, $request, $taxes, $sub_total, $discount_t
'sort_order' => $sort_order,
]);
}
}
}
113 changes: 84 additions & 29 deletions app/Jobs/Income/CreateInvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function handle()
$item_sku = '';

$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];

$item_discount_amount = $item_amount;

// Apply discount to tax
if ($this->discount) {
$item_discount_amount = $item_amount * ($this->discount / 100);
}

if (!empty($item_id)) {
$item_object = Item::find($item_id);
Expand Down Expand Up @@ -85,32 +93,72 @@ public function handle()
$item_sku = $this->data['sku'];
}

$item_tax = 0;
$tax_amount = 0;
$item_taxes = [];
$invoice_item_taxes = [];
$item_tax_total = 0;

if (!empty($this->data['tax_id'])) {
$calculates = $compounds = [];

foreach ((array) $this->data['tax_id'] as $tax_id) {
$tax_object = Tax::find($tax_id);
$tax = Tax::find($tax_id);

switch ($tax->type) {
case 'calculate':
$calculates[] = $tax;
break;
case 'compound':
$compounds[] = $tax;
break;
case 'normal':
default:
$tax_amount = ($item_discount_amount / 100) * $tax->rate;

$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];

$item_tax_total += $tax_amount;
break;
}
}

$item_taxes[] = $tax_id;
if ($calculates) {
foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_discount_amount + $item_tax_total;

$tax = (((double) $this->data['price'] * (double) $this->data['quantity']) / 100) * $tax_object->rate;
$item_tax_total = $tax_amount = $item_sub_and_tax_total - ($item_sub_and_tax_total / (1 + ($calculate->rate / 100)));

$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $calculate->id,
'name' => $calculate->name,
'amount' => $tax_amount,
];

// Apply discount to tax
if ($this->discount) {
$tax = $tax - ($tax * ($this->discount / 100));
$item_amount = $item_sub_and_tax_total - $item_tax_total;
}
}

if ($compounds) {
foreach ($compounds as $compound) {
$tax_amount = (($item_discount_amount + $item_tax_total) / 100) * $compound->rate;

$invoice_item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $tax_id,
'name' => $tax_object->name,
'amount' => $tax,
];
$item_tax_total += $tax_amount;

$item_tax += $tax;
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $compound->id,
'name' => $compound->name,
'amount' => $tax_amount,
];
}
}
}

Expand All @@ -122,29 +170,36 @@ public function handle()
'sku' => $item_sku,
'quantity' => (double) $this->data['quantity'],
'price' => (double) $this->data['price'],
'tax' => $item_tax,
'tax_id' => 0, // (int) $item_taxes;
'total' => (double) $this->data['price'] * (double) $this->data['quantity'],
'tax' => $item_tax_total,
'tax_id' => 0,
'total' => $item_amount,
]);

if ($invoice_item_taxes) {
foreach ($invoice_item_taxes as $invoice_item_tax) {
$invoice_item_tax['invoice_item_id'] = $invoice_item->id;
if (!empty($this->data['tax_id'])) {
// set item_taxes for
$invoice_item->item_taxes = $item_taxes;
$invoice_item->calculates = $calculates;
$invoice_item->compounds = $compounds;
}

if ($item_taxes) {
foreach ($item_taxes as $item_tax) {
$item_tax['invoice_item_id'] = $invoice_item->id;

InvoiceItemTax::create($invoice_item_tax);
InvoiceItemTax::create($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'];
if (isset($taxes) && array_key_exists($item_tax['tax_id'], $taxes)) {
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
} else {
$taxes[$invoice_item_tax['tax_id']] = [
'name' => $invoice_item_tax['name'],
'amount' => $invoice_item_tax['amount']
$taxes[$item_tax['tax_id']] = [
'name' => $item_tax['name'],
'amount' => $item_tax['amount']
];
}
}
}

return $invoice_item;
}
}
}
4 changes: 4 additions & 0 deletions app/Models/Income/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ public function getDiscountAttribute()
*/
public function getPaidAttribute()
{
if (empty($this->amount)) {
return false;
}

$paid = 0;
$reconciled = $reconciled_amount = 0;

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Setting/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Tax extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'rate', 'calculate', 'compound', 'enabled'];
protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled'];

/**
* Sortable columns.
Expand Down
6 changes: 2 additions & 4 deletions database/migrations/2018_11_05_000000_add_tax_columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class AddTaxColumns extends Migration
public function up()
{
Schema::table('taxes', function ($table) {
$table->boolean('calculate')->default(0);
$table->boolean('compound')->default(0);
$table->string('type')->default('normal');
});
}

Expand All @@ -26,8 +25,7 @@ public function down()
{
Schema::table('taxes', function ($table) {
$table->dropColumn([
'calculate',
'compound',
'type',
]);
});
}
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-GB/taxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'rate' => 'Rate',
'rate_percent' => 'Rate (%)',
'normal' => 'Normal',
'calculate' => 'Calculate as VAT/GST',
'compound' => 'Compound',

Expand Down
14 changes: 3 additions & 11 deletions resources/views/settings/taxes/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}

{{ Form::radioGroup('calculate', trans('taxes.calculate')) }}

{{ Form::radioGroup('compound', trans('taxes.compound')) }}
{{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', $types, null, []) }}

{{ Form::radioGroup('enabled', trans('general.enabled')) }}
</div>
Expand All @@ -36,17 +34,11 @@
$(document).ready(function() {
$('#enabled_1').trigger('click');
$('#calculate_0').trigger('click');
$('#compound_0').trigger('click');
$('#name').focus();
$("#calculate").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('taxes.calculate')]) }}"
});
$("#compound").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('taxes.compound')]) }}"
$("#type").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('general.type')]) }}"
});
});
</script>
Expand Down
12 changes: 3 additions & 9 deletions resources/views/settings/taxes/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}

{{ Form::radioGroup('calculate', trans('taxes.calculate')) }}

{{ Form::radioGroup('compound', trans('taxes.compound')) }}
{{ Form::selectGroup('type', trans('general.type'), 'bars', $types, null, []) }}

{{ Form::radioGroup('enabled', trans('general.enabled')) }}
</div>
Expand All @@ -44,12 +42,8 @@
$(document).ready(function() {
$('#name').focus();
$("#calculate").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('taxes.calculate')]) }}"
});
$("#compound").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('taxes.compound')]) }}"
$("#type").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans('general.type')]) }}"
});
});
</script>
Expand Down

0 comments on commit 79ebd9d

Please sign in to comment.