From 1dd51a8dd3827b7d7b769db9927e4c34d64d0749 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Wed, 28 Feb 2018 01:42:09 +0800 Subject: [PATCH] Add support for invoice generation for under warranty #29 --- src/Controller/JobOrderController.php | 20 ++++++++- src/Ramcar/InvoiceCriteria.php | 2 +- src/Service/InvoiceCreator.php | 65 +++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 3906bb3f..79ab23ae 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -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); diff --git a/src/Ramcar/InvoiceCriteria.php b/src/Ramcar/InvoiceCriteria.php index e6379997..4ad00445 100644 --- a/src/Ramcar/InvoiceCriteria.php +++ b/src/Ramcar/InvoiceCriteria.php @@ -23,7 +23,7 @@ class InvoiceCriteria public function setServiceType($stype) { // TODO: validate service type - $this->$stype = $stype; + $this->stype = $stype; return $this; } diff --git a/src/Service/InvoiceCreator.php b/src/Service/InvoiceCreator.php index 5fb6de91..89504d31 100644 --- a/src/Service/InvoiceCreator.php +++ b/src/Service/InvoiceCreator.php @@ -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