resq/src/InvoiceRule/Fuel.php
2023-07-05 01:36:44 -04:00

133 lines
3.4 KiB
PHP

<?php
namespace App\InvoiceRule;
use App\InvoiceRuleInterface;
use App\Ramcar\FuelType;
use App\Ramcar\ServiceType;
class Fuel implements InvoiceRuleInterface
{
public function getID()
{
return 'fuel';
}
public function compute($criteria, &$total)
{
$stype = $criteria->getServiceType();
$items = [];
if ($stype == $this->getID())
{
// check if customer vehicle has a motolite battery
$cv = $criteria->getCustomerVehicle();
if ($cv->hasMotoliteBattery())
$fee = 0;
else
$fee = $this->getServiceTypeFee();
$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()
{
// TODO: we need to to put this somewhere like in .env
// so that if any changes are to be made, we just edit the file
// instead of the code
return 300;
}
public function getFuelFee($fuel_type)
{
// TODO: we need to to put this somewhere like in .env
// so that if any changes are to be made, we just edit the file
// instead of the code
if ($fuel_type == FuelType::GAS)
{
// gas fuel fee
return 320;
}
else
{
// diesel fuel fee
return 340;
}
}
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;
}
}