Add support for invoice generation for under warranty #29

This commit is contained in:
Kendrick Chan 2018-02-28 01:42:09 +08:00
parent 13adca3721
commit 1dd51a8dd3
3 changed files with 80 additions and 7 deletions

View file

@ -1284,6 +1284,12 @@ class JobOrderController extends BaseController
{
// return error if there's a problem, false otherwise
// check service type
$stype = $criteria->getServiceType();
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW)
return null;
if (empty($promo_id))
return false;
@ -1299,6 +1305,11 @@ class JobOrderController extends BaseController
protected function invoiceBatteries($em, InvoiceCriteria $criteria, $items)
{
// check service type
$stype = $criteria->getServiceType();
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY)
return null;
// return error if there's a problem, false otherwise
if (!empty($items))
{
@ -1332,14 +1343,20 @@ class JobOrderController extends BaseController
public function generateInvoice(Request $req, InvoiceCreator $ic)
{
error_log('generating invoice...');
$error = false;
$stype = $req->request->get('stype');
$items = $req->request->get('items');
$promo_id = $req->request->get('promo');
// instantiate invoice criteria
$criteria = new InvoiceCriteria();
$criteria->setServiceType($stype);
$em = $this->getDoctrine()->getManager();
/*
// if it's a jumpstart or troubleshoot only, we know what to charge already
if ($stype == ServiceType::JUMPSTART_TROUBLESHOOT)
{
@ -1363,9 +1380,8 @@ class JobOrderController extends BaseController
'invoice' => $invoice
]);
}
*/
// instantiate invoice criteria
$criteria = new InvoiceCriteria();
$error = $this->invoicePromo($em, $criteria, $promo_id);

View file

@ -23,7 +23,7 @@ class InvoiceCriteria
public function setServiceType($stype)
{
// TODO: validate service type
$this->$stype = $stype;
$this->stype = $stype;
return $this;
}

View file

@ -5,6 +5,7 @@ namespace App\Service;
use App\Ramcar\InvoiceCriteria;
use App\Ramcar\TradeInType;
use App\Ramcar\DiscountApply;
use App\Ramcar\ServiceType;
use App\Entity\Invoice;
use App\Entity\InvoiceItem;
@ -148,6 +149,17 @@ class InvoiceCreator
break;
}
// if discount is higher than 0, display in invoice
if ($discount > 0)
{
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle('Promo discount')
->setQuantity(1)
->setPrice(-1 * $discount);
$invoice->addItem($item);
}
$total['discount'] = $discount;
$total['total_price'] -= $discount;
@ -155,10 +167,40 @@ class InvoiceCreator
$invoice->setPromo($promo);
}
public function processJumpstart(&$total, $invoice)
{
// add troubleshooting fee
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle('Troubleshooting fee')
->setQuantity(1)
->setPrice(150.00);
$invoice->addItem($item);
$total['sell_price'] = 150.00;
$total['vat_ex_price'] = 150.00;
$total['total_price'] = 150.00;
}
public function processWarranty(&$total, InvoiceCriteria $criteria, $invoice)
{
error_log('processing warranty');
$batteries = $criteria->getBatteries();
foreach ($batteries as $batt)
{
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle($batt->getModel()->getName() . ' ' . $batt->getSize()->getName() . ' - Service Unit')
->setQuantity(1)
->setPrice(0.00);
$invoice->addItem($item);
}
}
public function processCriteria(InvoiceCriteria $criteria)
{
// initialize
$invoice = new Invoice();
$total = [
'sell_price' => 0.0,
'vat' => 0.0,
@ -168,9 +210,24 @@ class InvoiceCreator
'discount' => 0.0,
];
$this->processBatteries($total, $criteria, $invoice);
$this->processTradeIns($total, $criteria, $invoice);
$this->processDiscount($total, $criteria, $invoice);
$stype = $criteria->getServiceType();
error_log($stype);
switch ($stype)
{
case ServiceType::JUMPSTART_TROUBLESHOOT:
$this->processJumpstart($total, $invoice);
break;
case ServiceType::BATTERY_REPLACEMENT_NEW:
$this->processBatteries($total, $criteria, $invoice);
$this->processTradeIns($total, $criteria, $invoice);
$this->processDiscount($total, $criteria, $invoice);
break;
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
$this->processWarranty($total, $criteria, $invoice);
break;
}
// TODO: check if any promo is applied
// apply discounts