Resolve "Handle under warranty with invoice generator" #842

Merged
jankstudio merged 2 commits from 29-handle-under-warranty-with-invoice-generator into master 2018-02-27 17:43:49 +00:00
3 changed files with 93 additions and 6 deletions

View file

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

View file

@ -7,17 +7,31 @@ use App\Entity\Promo;
class InvoiceCriteria class InvoiceCriteria
{ {
protected $stype;
protected $batteries; protected $batteries;
protected $promos; protected $promos;
protected $trade_in; protected $trade_in;
public function __construct() public function __construct()
{ {
$this->stype = 0;
$this->batteries = []; $this->batteries = [];
$this->promos = []; $this->promos = [];
$this->trade_ins = []; $this->trade_ins = [];
} }
public function setServiceType($stype)
{
// TODO: validate service type
$this->stype = $stype;
return $this;
}
public function getServiceType()
{
return $this->stype;
}
public function addBattery(Battery $battery, $qty = 1) public function addBattery(Battery $battery, $qty = 1)
{ {
for ($i = 0; $i < $qty; $i++) for ($i = 0; $i < $qty; $i++)

View file

@ -5,6 +5,7 @@ namespace App\Service;
use App\Ramcar\InvoiceCriteria; use App\Ramcar\InvoiceCriteria;
use App\Ramcar\TradeInType; use App\Ramcar\TradeInType;
use App\Ramcar\DiscountApply; use App\Ramcar\DiscountApply;
use App\Ramcar\ServiceType;
use App\Entity\Invoice; use App\Entity\Invoice;
use App\Entity\InvoiceItem; use App\Entity\InvoiceItem;
@ -148,6 +149,17 @@ class InvoiceCreator
break; 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['discount'] = $discount;
$total['total_price'] -= $discount; $total['total_price'] -= $discount;
@ -155,10 +167,40 @@ class InvoiceCreator
$invoice->setPromo($promo); $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) public function processCriteria(InvoiceCriteria $criteria)
{ {
// initialize
$invoice = new Invoice(); $invoice = new Invoice();
$total = [ $total = [
'sell_price' => 0.0, 'sell_price' => 0.0,
'vat' => 0.0, 'vat' => 0.0,
@ -168,9 +210,24 @@ class InvoiceCreator
'discount' => 0.0, 'discount' => 0.0,
]; ];
$this->processBatteries($total, $criteria, $invoice); $stype = $criteria->getServiceType();
$this->processTradeIns($total, $criteria, $invoice); error_log($stype);
$this->processDiscount($total, $criteria, $invoice); 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 // TODO: check if any promo is applied
// apply discounts // apply discounts