resq/src/InvoiceRule/BatteryReplacementWarranty.php

168 lines
4.6 KiB
PHP

<?php
namespace App\InvoiceRule;
use App\InvoiceRuleInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType;
use App\Entity\Battery;
use App\Entity\ServiceOffering;
use App\Entity\ItemType;
use App\Service\PriceTierManager;
class BatteryReplacementWarranty implements InvoiceRuleInterface
{
protected $em;
protected $pt_manager;
public function __construct(EntityManagerInterface $em, PriceTierManager $pt_manager)
{
$this->em = $em;
$this->pt_manager = $pt_manager;
}
public function getID()
{
return 'battery_warranty';
}
public function compute($criteria, &$total)
{
$stype = $criteria->getServiceType();
$pt_id = $criteria->getPriceTier();
$items = [];
if ($stype == $this->getID())
{
// get the entries
$entries = $criteria->getEntries();
foreach($entries as $entry)
{
$batt = $entry['battery'];
$qty = 1;
// check if price tier has item price
$pt_price = $this->getPriceTierItemPrice($pt_id);
if ($pt_price == null)
$price = $this->getServiceTypeFee();
else
$price = $pt_price;
$items[] = [
'service_type' => $this->getID(),
'battery' => $batt,
'qty' => $qty,
'title' => $this->getTitle($batt),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total['total_price'] = bcadd($total['total_price'], $qty_price, 2);
}
}
return $items;
}
public function getServiceTypeFee()
{
$code = 'battery_replacement_warranty_fee';
// find the service fee using the code
// if we can't find the fee, return 0
$fee = $this->em->getRepository(ServiceOffering::class)->findOneBy(['code' => $code]);
if ($fee == null)
return 0;
return $fee->getFee();
}
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. Since this is the
// battery replacement warranty rule, we only check for battery replacement warranty.
$stype = $criteria->getServiceType();
if ($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 getPriceTierItemPrice($pt_id)
{
// price_tier is default
if ($pt_id == 0)
return null;
// find the item type for service offering
$item_type = $this->em->getRepository(ItemType::class)->findOneBy(['code' => 'service_offering']);
if ($item_type == null)
return null;
// find the service offering
$code = 'battery_replacement_warranty_fee';
$service = $this->em->getRepository(ServiceOffering::class)->findOneBy(['code' => $code]);
// check if service is null. If null, return null
if ($service == null)
return null;
$item_type_id = $item_type->getID();
$item_id = $service->getID();
$price = $this->pt_manager->getItemPrice($pt_id, $item_type_id, $item_id);
return $price;
}
protected function getTitle($battery)
{
$title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit';
return $title;
}
}