resq/src/InvoiceRule/Fuel.php

154 lines
3.9 KiB
PHP

<?php
namespace App\InvoiceRule;
use Doctrine\ORM\EntityManagerInterface;
use App\InvoiceRuleInterface;
use App\Ramcar\FuelType;
use App\Ramcar\ServiceType;
use App\Entity\ServiceOffering;
use App\Entity\CustomerVehicle;
class Fuel implements InvoiceRuleInterface
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getID()
{
return 'fuel';
}
public function compute($criteria, &$total)
{
$stype = $criteria->getServiceType();
$items = [];
if ($stype == $this->getID())
{
$cv = $criteria->getCustomerVehicle();
$fee = $this->getServiceTypeFee($cv);
$ftype = $cv->getFuelType();
// add the service fee to items
$qty = 1;
$items[] = [
'service_type' => $this->getID(),
'qty' => $qty,
'title' => $this->getServiceTitle($ftype),
'price' => $fee,
];
$qty_fee = bcmul($qty, $fee, 2);
$total_price = $qty_fee;
switch ($ftype)
{
case FuelType::GAS:
case FuelType::DIESEL:
$qty = 1;
$price = $this->getFuelFee($ftype);
$items[] = [
'service_type' => $this->getID(),
'qty' => $qty,
'title' => $this->getTitle($ftype),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total_price = bcadd($total_price, $qty_price, 2);
break;
default:
$qty = 1;
$price = 0;
$items[] = [
'service_type' => $this->getID(),
'qty' => $qty,
'title' => $this->getTitle('Unknown'),
'price' => $price,
];
$qty_price = bcmul($price, $qty, 2);
$total_price = bcadd($total_price, $qty_price, 2);
break;
}
$total['total_price'] = bcadd($total['total_price'], $total_price, 2);
}
return $items;
}
public function getServiceTypeFee(CustomerVehicle $cv)
{
// 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';
else
$code = 'fuel_service_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 getFuelFee($fuel_type)
{
$code = '';
if ($fuel_type == FuelType::GAS)
$code = 'fuel_gas_fee';
if ($fuel_type == FuelType::DIESEL)
$code = 'fuel_diesel_fee';
// find the fuel fee for the specific fuel type 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)
{
return null;
}
protected function getTitle($fuel_type)
{
$title = '4L - ' . ucfirst($fuel_type);
return $title;
}
protected function getServiceTitle($fuel_type)
{
$title = 'Service - ' . ServiceType::getName(ServiceType::EMERGENCY_REFUEL);
return $title;
}
}