87 lines
2.1 KiB
PHP
87 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\InvoiceRule;
|
|
|
|
use App\InvoiceRuleInterface;
|
|
|
|
use App\Ramcar\TradeInType;
|
|
|
|
class TradeIn implements InvoiceRuleInterface
|
|
{
|
|
public function getID()
|
|
{
|
|
return 'trade-in';
|
|
}
|
|
|
|
public function compute($criteria, &$total)
|
|
{
|
|
$items = [];
|
|
|
|
// get the entries
|
|
$entries = $criteria->getEntries();
|
|
foreach($entries as $entry)
|
|
{
|
|
$batt = $entry['battery'];
|
|
$qty = $entry['qty'];
|
|
$trade_in_type = null;
|
|
|
|
if (isset($entry['trade_in']))
|
|
$trade_in_type = $entry['trade_in'];
|
|
|
|
if ($trade_in_type != null)
|
|
{
|
|
$ti_rate = $this->getTradeInRate($batt, $trade_in_type);
|
|
|
|
$qty_ti = bcmul($ti_rate, $qty, 2);
|
|
|
|
$total['ti_rate'] = bcadd($total['ti_rate'], $qty_ti, 2);
|
|
$total['total_price'] = bcsub($total['total_price'], $qty_ti, 2);
|
|
|
|
$price = bcmul($ti_rate, -1, 2);
|
|
|
|
$items[] = [
|
|
'qty' => $qty,
|
|
'title' => $this->getTitle($batt, $trade_in_type),
|
|
'price' => $price,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function validatePromo($criteria, $promo_id)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function validateInvoiceItems($criteria, $invoice_items)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected function getTradeInRate($battery, $trade_in_type)
|
|
{
|
|
$size = $battery->getSize();
|
|
|
|
switch ($trade_in_type)
|
|
{
|
|
case TradeInType::MOTOLITE:
|
|
return $size->getTIPriceMotolite();
|
|
case TradeInType::PREMIUM:
|
|
return $size->getTIPricePremium();
|
|
case TradeInType::OTHER:
|
|
return $size->getTIPriceOther();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function getTitle($battery, $trade_in_type)
|
|
{
|
|
$title = 'Trade-in ' . TradeInType::getName($trade_in_type) . ' ' . $battery->getSize()->getName() . ' battery';
|
|
|
|
return $title;
|
|
}
|
|
}
|
|
|