175 lines
4.5 KiB
PHP
175 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\InvoiceRule;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\InvoiceRuleInterface;
|
|
|
|
use App\Ramcar\ServiceType;
|
|
|
|
use App\Entity\ServiceOffering;
|
|
use App\Entity\CustomerVehicle;
|
|
use App\Entity\ItemType;
|
|
|
|
use App\Service\PriceTierManager;
|
|
|
|
class Overheat 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 'overheat';
|
|
}
|
|
|
|
public function compute($criteria, &$total)
|
|
{
|
|
$stype = $criteria->getServiceType();
|
|
$has_coolant = $criteria->hasCoolant();
|
|
$pt_id = $criteria->getPriceTier();
|
|
|
|
$items = [];
|
|
|
|
if ($stype == $this->getID())
|
|
{
|
|
$cv = $criteria->getCustomerVehicle();
|
|
|
|
// check if price tier has item price
|
|
$pt_price = $this->getPriceTierItemPrice($pt_id, $cv);
|
|
|
|
if ($pt_price == null)
|
|
$price = $this->getServiceTypeFee($cv);
|
|
else
|
|
|
|
$price = $pt_price;
|
|
|
|
// add the service fee to items
|
|
$qty = 1;
|
|
$items[] = [
|
|
'service_type' => $this->getID(),
|
|
'qty' => $qty,
|
|
'title' => $this->getServiceTitle(),
|
|
'price' => $price,
|
|
];
|
|
|
|
$qty_fee = bcmul($qty, $price, 2);
|
|
$total_price = $qty_fee;
|
|
|
|
if ($has_coolant)
|
|
{
|
|
$coolant_fee = $this->getCoolantFee();
|
|
$items[] = [
|
|
'service_type' => $this->getID(),
|
|
'qty' => $qty,
|
|
'title' => $this->getServiceCoolantTitle(),
|
|
'price' => $coolant_fee,
|
|
];
|
|
|
|
$qty_price = bcmul($coolant_fee, $qty, 2);
|
|
$total_price = bcadd($total_price, $qty_price, 2);
|
|
}
|
|
|
|
$total['total_price'] = bcadd($total['total_price'], $total_price, 2);
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function getServiceTypeFee(CustomerVehicle $cv)
|
|
{
|
|
$code = 'overheat_fee';
|
|
|
|
// check if customer vehicle has a motolite battery
|
|
// if yes, set the code to the motolite user service fee
|
|
if ($cv->hasMotoliteBattery())
|
|
$code = 'motolite_user_service_fee';
|
|
|
|
// find the service fee for overheat 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 getCoolantFee()
|
|
{
|
|
$code = 'coolant_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);
|
|
|
|
if ($fee == null)
|
|
return 0;
|
|
|
|
return $fee->getFee();
|
|
}
|
|
|
|
public function validatePromo($criteria, $promo_id)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function validateInvoiceItems($criteria, $invoice_items)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected function getPriceTierItemPrice($pt_id, CustomerVehicle $cv)
|
|
{
|
|
// 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 = 'overheat_fee';
|
|
|
|
// check if customer vehicle has a motolite battery
|
|
// if yes, set the code to the motolite user service fee
|
|
if ($cv->hasMotoliteBattery())
|
|
$code = 'motolite_user_service_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 getServiceTitle()
|
|
{
|
|
$title = 'Service - ' . ServiceType::getName(ServiceType::OVERHEAT_ASSISTANCE);
|
|
|
|
return $title;
|
|
}
|
|
|
|
protected function getServiceCoolantTitle()
|
|
{
|
|
$title = '4L Coolant';
|
|
|
|
return $title;
|
|
}
|
|
}
|