Add invoice rule for price tier. #782
This commit is contained in:
parent
ee033ddd55
commit
70ee7fdd89
2 changed files with 169 additions and 0 deletions
157
src/InvoiceRule/PriceTier.php
Normal file
157
src/InvoiceRule/PriceTier.php
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ class InvoiceCriteria
|
|||
protected $service_charges;
|
||||
protected $flag_taxable;
|
||||
protected $source; // use Ramcar's TransactionOrigin
|
||||
protected $price_tier;
|
||||
|
||||
// entries are battery and trade-in combos
|
||||
protected $entries;
|
||||
|
|
@ -32,6 +33,7 @@ class InvoiceCriteria
|
|||
$this->service_charges = [];
|
||||
$this->flag_taxable = false;
|
||||
$this->source = '';
|
||||
$this->price_tier = 0; // set to default
|
||||
}
|
||||
|
||||
public function setServiceType($stype)
|
||||
|
|
@ -179,4 +181,14 @@ class InvoiceCriteria
|
|||
return $this->source;
|
||||
}
|
||||
|
||||
public function setPriceTier($price_tier)
|
||||
{
|
||||
$this->price_tier = $price_tier;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPriceTier()
|
||||
{
|
||||
return $this->price_tier;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue