159 lines
5.2 KiB
PHP
159 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Symfony\Component\Security\Core\Security;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\Ramcar\InvoiceCriteria;
|
|
use App\Ramcar\InvoiceStatus;
|
|
|
|
use App\Entity\Invoice;
|
|
use App\Entity\InvoiceItem;
|
|
use App\Entity\User;
|
|
use App\Entity\Battery;
|
|
|
|
class InvoiceManager
|
|
{
|
|
protected $security;
|
|
protected $em;
|
|
protected $validator;
|
|
protected $inv_batt;
|
|
protected $inv_trade;
|
|
protected $inv_tax;
|
|
|
|
public function __construct(Security $service, EntityManagerInterface $em, ValidatorInterface $validator,
|
|
InvoiceBatteryManager $inv_batt, InvoiceTradeInManager $inv_trade, InvoiceTaxManager $inv_tax)
|
|
{
|
|
$this->security = $security;
|
|
$this->em = $em;
|
|
$this->validator = $validator;
|
|
$this->inv_batt = $inv_batt;
|
|
$this->inv_trade = $inv_trade;
|
|
$this->inv_tax = $inv_tax;
|
|
}
|
|
|
|
public function generateInvoice(InvoiceCriteria $criteria)
|
|
{
|
|
// initialize
|
|
$invoice = new Invoice();
|
|
$total = [
|
|
'sell_price' => 0.0,
|
|
'vat' => 0.0,
|
|
'vat_ex_price' => 0.0,
|
|
'ti_rate' => 0.0,
|
|
'total_price' => 0.0,
|
|
'discount' => 0.0,
|
|
];
|
|
|
|
$stype = $criteria->getServiceType();
|
|
$cv = $criteria->getCustomerVehicle();
|
|
$has_coolant = $criteria->hasCoolant();
|
|
|
|
switch($stype)
|
|
{
|
|
case ServiceType::JUMPSTART_TROUBLESHOOT:
|
|
$this->processJumpstart($total, $invoice);
|
|
break;
|
|
case ServiceType::JUMPSTART_WARRANTY:
|
|
$this->processJumpstartWarranty($total, $invoice);
|
|
break;
|
|
case ServiceType::BATTERY_REPLACEMENT_NEW:
|
|
$this->processEntries($total, $criteria, $invoice);
|
|
$this->processDiscount($total, $criteria, $invoice, $cust_tag_info);
|
|
break;
|
|
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
|
|
$this->processWarranty($total, $criteria, $invoice);
|
|
break;
|
|
case ServiceType::POST_RECHARGED:
|
|
$this->processRecharge($total, $invoice);
|
|
break;
|
|
case ServiceType::POST_REPLACEMENT:
|
|
$this->processReplacement($total, $invoice);
|
|
break;
|
|
case ServiceType::TIRE_REPAIR:
|
|
$this->processTireRepair($total, $invoice, $cv);
|
|
break;
|
|
case ServiceType::OVERHEAT_ASSISTANCE:
|
|
$this->processOverheat($total, $invoice, $cv, $has_coolant);
|
|
break;
|
|
case ServiceType::EMERGENCY_REFUEL:
|
|
$ftype = $criteria->getCustomerVehicle()->getFuelType();
|
|
$this->processRefuel($total, $invoice, $cv);
|
|
break;
|
|
}
|
|
|
|
// process promos here?
|
|
|
|
// get current user
|
|
$user = $this->security->getUser();
|
|
// check if user is User or APIUser
|
|
if ($user instanceof User)
|
|
{
|
|
$invoice->setCreatedBy($user);
|
|
}
|
|
|
|
$invoice->setTotalPrice($total['total_price'])
|
|
->setVATExclusivePrice($total['vat_ex_price'])
|
|
->setVAT($total['vat'])
|
|
->setDiscount($total['discount'])
|
|
->setTradeIn($total['ti_rate'])
|
|
->setStatus(InvoiceStatus::DRAFT);
|
|
|
|
return $invoice;
|
|
}
|
|
|
|
// process battery sales and trade-ins
|
|
protected function processEntries(&$total, InvoiceCriteria $criteria, Invoice $invoice)
|
|
{
|
|
$entries = $criteria->getEntries();
|
|
|
|
$con_batts = [];
|
|
$con_tis = [];
|
|
foreach($entries as $entry)
|
|
{
|
|
// batteries purchased should have trade_in set to null
|
|
$batt = $entry['battery'];
|
|
$qty = $entry['qty'];
|
|
$trade_in = $entry['trade_in'];
|
|
$size = $batt->getSize();
|
|
|
|
// consolidate batteries
|
|
$batt_id = $batt->getID();
|
|
if (!isset($con_batts[$batt_id]))
|
|
$con_batts[$batt->getID()] = [
|
|
'batt' => $batt,
|
|
'qty' => 0
|
|
];
|
|
$con_batts[$batt_id]['qty']++;
|
|
|
|
// no trade-in
|
|
if ($trade_in == null)
|
|
continue;
|
|
|
|
// consolidate trade-ins
|
|
// need to set battery for trade in so we know what battery is for trade in
|
|
// possible that battery purchased is not the same battery for trade in
|
|
$ti_key = $size->getID() . '|' . $trade_in;
|
|
if (!isset($con_tis[$ti_key]))
|
|
$con_tis[$ti_key] = [
|
|
'batt' => $batt,
|
|
'size' => $size,
|
|
'trade_in' => $trade_in,
|
|
'qty' => 0
|
|
];
|
|
$con_tis[$ti_key]['qty']++;
|
|
}
|
|
|
|
$inv_batt_entries = $this->inv_batt->processBatteries($con_batts, $total);
|
|
$inv_trade_in_entries = $this->inv_trade->processTradeIns($con_tis, $total);
|
|
|
|
// check if we need to compute tax
|
|
if ($criteria->isTaxable())
|
|
{
|
|
$this->inv_tax->computeTax($inv_batt_entries, $total);
|
|
}
|
|
}
|
|
}
|