82 lines
2 KiB
PHP
82 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Invoice;
|
|
|
|
use App\InvoiceInterface;
|
|
|
|
class Tax implements InvoiceInterface
|
|
{
|
|
const TAX_RATE = 0.12;
|
|
|
|
public function check($criteria)
|
|
{
|
|
if (!empty($criteria->isTaxable()))
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public function getTemplate()
|
|
{
|
|
return 'invoice/tax.html.twig';
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return 'tax';
|
|
}
|
|
|
|
public function compute($items, &$total)
|
|
{
|
|
foreach ($items as $item)
|
|
{
|
|
$price = $item['price'];
|
|
$qty = $item['qty'];
|
|
|
|
if (isset($items['service_type']))
|
|
{
|
|
if ($items['service_type'] == 'battery_new')
|
|
{
|
|
// battery purchase
|
|
if (isset($item['battery']))
|
|
{
|
|
// battery purchase
|
|
$vat = $this->getTaxAmount($price);
|
|
|
|
$qty_price = bcmul($price, $qty, 2);
|
|
$price_minus_vat = bcsub($price, $vat, 2);
|
|
$qty_price_minus_vat = bcmul($price_minus_vat, $qty, 2);
|
|
|
|
$total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2);
|
|
$total['vat'] = bcadd($total['vat'], $qty_price, 2);
|
|
$total['vat_ex_price'] = bcadd($total['vat_ex_price'], $qty_price_minus_vat, 2);
|
|
|
|
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
// everything else
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getTaxAmount($price)
|
|
{
|
|
$vat_ex_price = $this->getTaxExclusivePrice($price);
|
|
$tax_amount = bcsub($price, $vat_ex_price, 2);
|
|
|
|
return $tax_amount;
|
|
}
|
|
|
|
protected function getTaxExclusivePrice($price)
|
|
{
|
|
$tax_ex_price = bcdiv($price, bcadd(1, self::TAX_RATE, 2), 2);
|
|
return $tax_ex_price;
|
|
}
|
|
|
|
}
|
|
|