127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\InvoiceRule;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\InvoiceRuleInterface;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
|
|
use App\Entity\ServiceOffering;
|
|
|
|
class Tax implements InvoiceRuleInterface
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return 'tax';
|
|
}
|
|
|
|
public function compute($criteria, &$total)
|
|
{
|
|
// check if taxable
|
|
if (!$criteria->isTaxable())
|
|
{
|
|
// nothing to compute
|
|
return [];
|
|
}
|
|
|
|
$tax_rate = $this->getTaxRate();
|
|
|
|
$is_battery_sales = false;
|
|
$total_price = 0;
|
|
|
|
// compute tax per item if service type is battery sales
|
|
$stype = $criteria->getServiceType();
|
|
|
|
if ($stype == ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
{
|
|
// get the entries
|
|
$entries = $criteria->getEntries();
|
|
|
|
foreach($entries as $entry)
|
|
{
|
|
// check if entry is trade-in
|
|
if (isset($entry['trade_in']))
|
|
{
|
|
// continue to next entry
|
|
continue;
|
|
}
|
|
|
|
$battery = $entry['battery'];
|
|
$qty = $entry['qty'];
|
|
|
|
$price = $battery->getSellingPrice();
|
|
|
|
$vat = $this->getTaxAmount($price, $tax_rate);
|
|
|
|
$qty_price = bcmul($price, $qty, 2);
|
|
$qty_vat = bcmul($vat, $qty, 2);
|
|
$price_minus_vat = bcsub($price, $vat, 2);
|
|
$qty_price_minus_vat = bcmul($price_minus_vat, $qty, 2);
|
|
|
|
$total['vat'] = bcadd($total['vat'], $qty_vat, 2);
|
|
$total['vat_ex_price'] = bcadd($total['vat_ex_price'], $qty_price_minus_vat, 2);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// compute VAT after adding all item costs, if service type is not battery sales
|
|
$total_price = $total['total_price'];
|
|
|
|
$vat_ex_price = $this->getTaxExclusivePrice($total_price, $tax_rate);
|
|
$vat = bcsub($total_price, $vat_ex_price, 2);
|
|
|
|
$total['vat_ex_price'] = $vat_ex_price;
|
|
$total['vat'] = $vat;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
public function validatePromo($criteria, $promo_id)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function validateInvoiceItems($criteria, $invoice_items)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected function getTaxAmount($price, $tax_rate)
|
|
{
|
|
$vat_ex_price = $this->getTaxExclusivePrice($price, $tax_rate);
|
|
$tax_amount = bcsub($price, $vat_ex_price, 2);
|
|
|
|
return $tax_amount;
|
|
}
|
|
|
|
protected function getTaxExclusivePrice($price, $tax_rate)
|
|
{
|
|
$tax_ex_price = bcdiv($price, bcadd(1, $tax_rate, 2), 2);
|
|
return $tax_ex_price;
|
|
}
|
|
|
|
protected function getTaxRate()
|
|
{
|
|
$code = 'tax';
|
|
|
|
// find the service fee using the code
|
|
// if we can't find the fee, return 0
|
|
$fee = $this->em->getRepository(ServiceOffering::class)->findOneBy(['code' => $code]);
|
|
|
|
if ($fee == null)
|
|
return 0;
|
|
|
|
return $fee->getFee();
|
|
}
|
|
}
|
|
|