resq/src/Invoice/TradeIn.php
2023-05-30 05:54:16 -04:00

94 lines
2.1 KiB
PHP

<?php
namespace App\Invoice;
use App\InvoiceInterface;
use App\Ramcar\TradeInType;
class TradeIn implements InvoiceInterface
{
public function check($criteria)
{
// compare specified type ids with trade in entry of criteria
$entries = $criteria->getEntries();
foreach($entries as $entry)
{
if ($entry['trade_in'])
{
// just need to find one trade-in entry
return true;
}
}
return false;
}
public function getTemplate()
{
return 'invoice/trade_in.html.twig';
}
public function getID()
{
return 'trade_in';
}
public function compute($criteria, $stype_fees)
{
$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);
$price = bcmul($ti_rate, -1, 2);
$items[] = [
'qty' => $qty,
'title' => $this->getTitle($batt, $trade_in_type),
'price' => $price,
];
}
}
return $items;
}
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;
}
}