157 lines
4.1 KiB
PHP
157 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\InvoiceRule;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\InvoiceRuleInterface;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
use App\Ramcar\TradeInType;
|
|
|
|
use App\Entity\PriceTier as PTEntity;
|
|
|
|
class PriceTier implements InvoiceRuleInterface
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return 'price_tier';
|
|
}
|
|
|
|
public function compute($criteria, &$total)
|
|
{
|
|
$pt_id = $criteria->getPriceTier();
|
|
|
|
// get the service type
|
|
$service_type = $criteria->getServiceType();
|
|
|
|
// get price tier
|
|
$pt = $em->getRepository(PTEntity::class)->find($pt_id);
|
|
|
|
// price tier is default
|
|
if ($pt == null)
|
|
{
|
|
// check if service type is battery sales
|
|
if ($service_type == ServiceType::BATTERY_REPLACEMENT_NEW)
|
|
{
|
|
$items = $this->processBatteryEntries($criteria, $total);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// get items in price tier
|
|
$pt_items = $pt->getItemPrices();
|
|
|
|
foreach ($pt_items as $pt_item)
|
|
{
|
|
// make item price hash
|
|
$pt_prices[$pt_item->getItemID()] = $pt_item->getPrice();
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function validatePromo($criteria, $promo_id)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function validateInvoiceItems($criteria, $invoice_items)
|
|
{
|
|
// check service type. Only battery sales and battery warranty should have invoice items.
|
|
$stype = $criteria->getServiceType();
|
|
if (($stype != ServiceType::BATTERY_REPLACEMENT_NEW) &&
|
|
($stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY))
|
|
return null;
|
|
|
|
// return error if there's a problem, false otherwise
|
|
if (!empty($invoice_items))
|
|
{
|
|
// check if this is a valid battery
|
|
foreach ($invoice_items as $item)
|
|
{
|
|
$battery = $this->em->getRepository(Battery::class)->find($item['battery']);
|
|
|
|
if (empty($battery))
|
|
{
|
|
$error = 'Invalid battery specified.';
|
|
return $error;
|
|
}
|
|
|
|
// quantity
|
|
$qty = $item['quantity'];
|
|
if ($qty < 1)
|
|
continue;
|
|
|
|
// if this is a trade in, add trade in
|
|
if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in']))
|
|
$trade_in = $item['trade_in'];
|
|
else
|
|
$trade_in = null;
|
|
|
|
$criteria->addEntry($battery, $trade_in, $qty);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function processBatteryEntries($criteria, &$total)
|
|
{
|
|
$items = [];
|
|
|
|
// get the entries
|
|
$entries = $criteria->getEntries();
|
|
foreach($entries as $entry)
|
|
{
|
|
$batt = $entry['battery'];
|
|
$qty = $entry['qty'];
|
|
$trade_in = null;
|
|
|
|
if (isset($entry['trade_in']))
|
|
$trade_in = $entry['trade_in'];
|
|
|
|
$size = $batt->getSize();
|
|
|
|
if ($trade_in == null)
|
|
{
|
|
// battery purchase
|
|
$price = $batt->getSellingPrice();
|
|
|
|
$items[] = [
|
|
'service_type' => $this->getID(),
|
|
'battery' => $batt,
|
|
'qty' => $qty,
|
|
'title' => $this->getTitle($criteria->getServiceType(), $batt),
|
|
'price' => $price,
|
|
];
|
|
|
|
$qty_price = bcmul($price, $qty, 2);
|
|
|
|
$total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2);
|
|
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
protected function getTitle($service_type, $battery)
|
|
{
|
|
$title ='';
|
|
|
|
// TODO: check for service type
|
|
$title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName();
|
|
|
|
return $title;
|
|
}
|
|
|
|
}
|