116 lines
2.8 KiB
PHP
116 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\InvoiceRule;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\InvoiceRuleInterface;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
use App\Ramcar\DiscountApply;
|
|
|
|
use App\Entity\Promo;
|
|
|
|
class DiscountType implements InvoiceRuleInterface
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return 'discount';
|
|
}
|
|
|
|
public function compute($criteria, &$total)
|
|
{
|
|
$items = [];
|
|
|
|
$promos = $criteria->getPromos();
|
|
|
|
if (empty($promos))
|
|
return $items;
|
|
|
|
// NOTE: only get first promo because only one is applicable anyway
|
|
$promo = $promos[0];
|
|
|
|
$rate = $promo->getDiscountRate();
|
|
$apply_to = $promo->getDiscountApply();
|
|
|
|
switch ($apply_to)
|
|
{
|
|
case DiscountApply::SRP:
|
|
$discount = bcmul($total['sell_price'], $rate, 2);
|
|
break;
|
|
case DiscountApply::OPL:
|
|
// $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2);
|
|
// $discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2);
|
|
$num1 = bcdiv(1.5, 0.7, 9);
|
|
$num1_rate = bcmul($num1, $rate, 9);
|
|
$multiplier = bcsub(1, $num1_rate, 9);
|
|
|
|
$discount = bcmul($total['sell_price'], $multiplier, 2);
|
|
break;
|
|
}
|
|
|
|
// if discount is higher than 0, add to items
|
|
if ($discount > 0)
|
|
{
|
|
$qty = 1;
|
|
$price = bcmul(-1, $discount, 2);
|
|
|
|
$items[] = [
|
|
'promo' => $promo,
|
|
'title' => $this->getTitle(),
|
|
'qty' => $qty,
|
|
'price' => $price,
|
|
];
|
|
}
|
|
|
|
$total['discount'] = $discount;
|
|
$total['total_price'] = bcsub($total['total_price'], $discount, 2);
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function validatePromo($criteria, $promo_id)
|
|
{
|
|
// return error if there's a problem, false otherwise
|
|
// check service type
|
|
$stype = $criteria->getServiceType();
|
|
|
|
// discount/promo only applies for battery sales
|
|
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
return null;
|
|
|
|
if (empty($promo_id))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// check if this is a valid promo
|
|
$promo = $this->em->getRepository(Promo::class)->find($promo_id);
|
|
|
|
if (empty($promo))
|
|
return 'Invalid promo specified.';
|
|
|
|
$criteria->addPromo($promo);
|
|
return false;
|
|
}
|
|
|
|
public function validateInvoiceItems($criteria, $invoice_items)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected function getTitle()
|
|
{
|
|
$title = 'Promo discount';
|
|
|
|
return $title;
|
|
}
|
|
}
|
|
|