withholding tax

This commit is contained in:
Denis Duliçi 2020-07-22 15:11:31 +03:00
parent 2b0c2e4846
commit 010d0bb3fb
13 changed files with 635 additions and 280 deletions

View File

@ -3,8 +3,7 @@
namespace App\Abstracts;
use App\Abstracts\Model;
use App\Models\Banking\Transaction;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
@ -159,8 +158,14 @@ abstract class DocumentModel extends Model
{
$amount = $this->amount;
$this->totals->where('code', 'tax')->each(function ($tax) use(&$amount) {
$amount -= $tax->amount;
$this->totals->where('code', 'tax')->each(function ($total) use(&$amount) {
$tax = Tax::name($total->name)->first();
if (!empty($tax) && ($tax->type == 'withholding')) {
return;
}
$amount -= $total->amount;
});
return $amount;

View File

@ -25,6 +25,7 @@ class Taxes extends Controller
'fixed' => trans('taxes.fixed'),
'normal' => trans('taxes.normal'),
'inclusive' => trans('taxes.inclusive'),
'withholding' => trans('taxes.withholding'),
'compound' => trans('taxes.compound'),
];
@ -52,6 +53,7 @@ class Taxes extends Controller
'fixed' => trans('taxes.fixed'),
'normal' => trans('taxes.normal'),
'inclusive' => trans('taxes.inclusive'),
'withholding' => trans('taxes.withholding'),
'compound' => trans('taxes.compound'),
];
@ -99,6 +101,7 @@ class Taxes extends Controller
'fixed' => trans('taxes.fixed'),
'normal' => trans('taxes.normal'),
'inclusive' => trans('taxes.inclusive'),
'withholding' => trans('taxes.withholding'),
'compound' => trans('taxes.compound'),
];

View File

@ -87,9 +87,23 @@ class CreateBillItem extends Job
$item_tax_total += $tax_amount;
break;
case 'withholding':
$tax_amount = 0 - $item_discounted_amount * ($tax->rate / 100);
$item_taxes[] = [
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'tax_id' => $tax_id,
'name' => $tax->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
break;
default:
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
$tax_amount = $item_discounted_amount * ($tax->rate / 100);
$item_taxes[] = [
'company_id' => $this->bill->company_id,
@ -167,7 +181,7 @@ class CreateBillItem extends Job
foreach ($item_taxes as $item_tax) {
$item_tax['bill_item_id'] = $bill_item->id;
$item_tax['amount'] = round($item_tax['amount'], $precision);
$item_tax['amount'] = round(abs($item_tax['amount']), $precision);
BillItemTax::create($item_tax);
}

View File

@ -3,7 +3,6 @@
namespace App\Jobs\Purchase;
use App\Abstracts\Job;
use App\Models\Purchase\Bill;
use App\Models\Purchase\BillTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
@ -96,7 +95,7 @@ class CreateBillItemsAndTotals extends Job
'bill_id' => $this->bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => round($tax['amount'], $precision),
'amount' => round(abs($total['amount']), $precision),
'sort_order' => $sort_order,
]);
@ -117,7 +116,7 @@ class CreateBillItemsAndTotals extends Job
$total['code'] = 'extra';
}
$total['amount'] = round($total['amount'], $precision);
$total['amount'] = round(abs($total['amount']), $precision);
BillTotal::create($total);

View File

@ -87,9 +87,23 @@ class CreateInvoiceItem extends Job
$item_tax_total += $tax_amount;
break;
case 'withholding':
$tax_amount = 0 - $item_discounted_amount * ($tax->rate / 100);
$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;
default:
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
$tax_amount = $item_discounted_amount * ($tax->rate / 100);
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
@ -167,7 +181,7 @@ class CreateInvoiceItem extends Job
foreach ($item_taxes as $item_tax) {
$item_tax['invoice_item_id'] = $invoice_item->id;
$item_tax['amount'] = round($item_tax['amount'], $precision);
$item_tax['amount'] = round(abs($item_tax['amount']), $precision);
InvoiceItemTax::create($item_tax);
}

View File

@ -95,7 +95,7 @@ class CreateInvoiceItemsAndTotals extends Job
'invoice_id' => $this->invoice->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => round($tax['amount'], $precision),
'amount' => round(abs($tax['amount']), $precision),
'sort_order' => $sort_order,
]);
@ -116,7 +116,7 @@ class CreateInvoiceItemsAndTotals extends Job
$total['code'] = 'extra';
}
$total['amount'] = round($total['amount'], $precision);
$total['amount'] = round(abs($total['amount']), $precision);
InvoiceTotal::create($total);

View File

@ -54,6 +54,50 @@ class Tax extends Model
return $query->where('rate', '=', $rate);
}
public function scopeNotRate($query, $rate)
{
return $query->where('rate', '<>', $rate);
}
public function scopeType($query, $types)
{
if (empty($types)) {
return $query;
}
return $query->whereIn($this->table . '.type', (array) $types);
}
public function scopeFixed($query)
{
return $query->where($this->table . '.type', '=', 'fixed');
}
public function scopeNormal($query)
{
return $query->where($this->table . '.type', '=', 'normal');
}
public function scopeInclusive($query)
{
return $query->where($this->table . '.type', '=', 'inclusive');
}
public function scopeCompound($query)
{
return $query->where($this->table . '.type', '=', 'compound');
}
public function scopeWithholding($query)
{
return $query->where($this->table . '.type', '=', 'withholding');
}
public function scopeNotWithholding($query)
{
return $query->where($this->table . '.type', '<>', 'withholding');
}
/**
* Convert rate to double.
*

View File

@ -32,7 +32,7 @@ class TaxSummary extends Report
public function setTables()
{
$taxes = Tax::enabled()->where('rate', '<>', '0')->orderBy('name')->pluck('name')->toArray();
$taxes = Tax::enabled()->notWithholding()->notRate(0)->orderBy('name')->pluck('name')->toArray();
$this->tables = array_combine($taxes, $taxes);
}

787
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@ $company = $user->companies()->first();
$factory->define(Tax::class, function (Faker $faker) use ($company) {
setting()->setExtraColumns(['company_id' => $company->id]);
$types = ['normal', 'inclusive', 'compound', 'fixed'];
$types = ['normal', 'inclusive', 'compound', 'fixed', 'withholding'];
return [
'company_id' => $company->id,
@ -32,3 +32,5 @@ $factory->state(Tax::class, 'inclusive', ['type' => 'inclusive']);
$factory->state(Tax::class, 'compound', ['type' => 'compound']);
$factory->state(Tax::class, 'fixed', ['type' => 'fixed']);
$factory->state(Tax::class, 'withholding', ['type' => 'withholding']);

View File

@ -176,10 +176,11 @@ const app = new Vue({
case 'fixed':
item_tax_total += tax.rate * item.quantity;
break;
case 'withholding':
item_tax_total += 0 - item_discounted_total * (tax.rate / 100);
break;
default:
let item_tax_amount = (item_discounted_total / 100) * tax.rate;
item_tax_total += item_tax_amount;
item_tax_total += item_discounted_total * (tax.rate / 100);
break;
}
}

View File

@ -177,10 +177,11 @@ const app = new Vue({
case 'fixed':
item_tax_total += tax.rate * item.quantity;
break;
case 'withholding':
item_tax_total += 0 - item_discounted_total * (tax.rate / 100);
break;
default:
let item_tax_amount = (item_discounted_total / 100) * tax.rate;
item_tax_total += item_tax_amount;
item_tax_total += item_discounted_total * (tax.rate / 100);
break;
}
}
@ -225,7 +226,7 @@ const app = new Vue({
// set global total variable.
this.totals.sub = sub_total;
this.totals.tax = tax_total;
this.totals.tax = Math.abs(tax_total);
this.totals.item_discount = line_item_discount_total;
// Apply discount to total

View File

@ -8,4 +8,5 @@ return [
'inclusive' => 'Inclusive',
'compound' => 'Compound',
'fixed' => 'Fixed',
'withholding' => 'Withholding',
];