From fe5121cf71f605bc8e8b5887e847f4053e361d5c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 5 Jun 2023 01:55:04 -0400 Subject: [PATCH] Add methods to process generateInvoice ajax call. Add test cases. #744 --- src/Command/TestInvoiceManagerCommand.php | 181 ++++++++++++++++++++++ src/Controller/JobOrderController.php | 2 + src/Service/InvoiceManager.php | 56 ++++++- 3 files changed, 234 insertions(+), 5 deletions(-) diff --git a/src/Command/TestInvoiceManagerCommand.php b/src/Command/TestInvoiceManagerCommand.php index 6c971156..85745c1d 100644 --- a/src/Command/TestInvoiceManagerCommand.php +++ b/src/Command/TestInvoiceManagerCommand.php @@ -17,6 +17,7 @@ use App\Ramcar\ServiceType; use App\Entity\Battery; use App\Entity\CustomerVehicle; use App\Entity\Promo; +use App\Entity\JobOrder; class TestInvoiceManagerCommand extends Command { @@ -138,6 +139,22 @@ class TestInvoiceManagerCommand extends Command $this->testTireRepairWithServiceFeeWithoutTax(); $this->testTireRepairWithoutServiceFeeWithoutTax(); + // test creation of invoice criteria when JO is submitted + $this->testGenerateInvoiceCriteria(); + $this->testGenerateInvoiceCriteriaInvalidPromo(); + $this->testGenerateInvoiceCriteriaInvalidBattery(); + + // test generateDraftInvoice call from ajax call + $this->testGenerateDraftInvoiceNoErrorWithTradeIn(); + $this->testGenerateDraftInvoiceNoErrorNoTradeIn(); + $this->testGenerateDraftInvoiceInvalidBattery(); + $this->testGenerateDraftInvoiceInvalidPromo(); + $this->testGenerateDraftInvoiceNonBatterySales(); + $this->testGenerateDraftInvoiceBatterySalesNoPromo(); + + // test generateInvoiceInvoice call from ajax call + $this->testGenerateInvoice(); + return 0; } @@ -1948,4 +1965,168 @@ class TestInvoiceManagerCommand extends Command } } + // test creation of invoice criteria when JO is submitted + protected function testGenerateInvoiceCriteria() + { + // create JO, set service type and customer vehicle + $jo = new JobOrder(); + + $jo->setServiceType(ServiceType::BATTERY_REPLACEMENT_NEW); + + // set customer vehicle + $cv_id = 1306604; + $cv = $this->em->getRepository(CustomerVehicle::class)->find($cv_id); + + $jo->setCustomerVehicle($cv); + + // create error_array + $error_array = []; + + // create array of invoice items + $invoice_items = [[ + 'battery' => 1038, + 'quantity' => 1, + 'trade_in' => '', + ], [ + 'battery' => 1038, + 'quantity' => 1, + 'trade_in' => 'premium' + ]]; + + // error_log(print_r(json_encode($invoice_items), true)); + // promo id + $promo_id = 10; + + $invoice = $this->inv_manager->generateInvoiceCriteria($jo, $promo_id, $invoice_items, $error_array); + + if ($invoice != null) + { + error_log('TEST GENERATEINVOICECRITERIA:'); + error_log('INVOICE'); + error_log('TOTAL PRICE: ' . $invoice->getTotalPrice()); + error_log('VAT EXCLUSIVE PRICE: ' . $invoice->getVATExclusivePrice()); + error_log('VAT: ' . $invoice->getVAT()); + error_log('DISCOUNT: ' . $invoice->getDiscount()); + error_log('TRADE-IN: ' . $invoice->getTradeIn()); + error_log('STATUS: ' . $invoice->getStatus()); + + $invoice_items = $invoice->getItems(); + + foreach ($invoice_items as $invoice_item) + { + error_log($invoice_item->getTitle() . ' ' . $invoice_item->getQuantity() . ' ' . $invoice_item->getPrice()); + } + } + } + + protected function testGenerateInvoiceCriteriaInvalidPromo() + { + } + + protected function testGenerateInvoiceCriteriaInvalidBattery() + { + } + + // test generateDraftInvoice call from ajax call + protected function testGenerateDraftInvoiceNoErrorWithTradeIn() + { + $criteria = new InvoiceCriteria(); + + $criteria->setServiceType(ServiceType::BATTERY_REPLACEMENT_NEW); + $criteria->setIsTaxable(); + + $promo_id = 1; + $service_charges = []; + + // create array of invoice items + $items = [[ + 'battery' => 1038, + 'quantity' => 1, + 'trade_in' => '', + ], [ + 'battery' => 1038, + 'quantity' => 1, + 'trade_in' => 'premium' + ]]; + + $error = $this->inv_manager->generateDraftInvoice($criteria, $promo_id, $service_charges, $items); + + if ($error) + error_log('TEST GENERATE DRAFT INVOICE NO ERROR WITH TRADE IN: Errors found when generating draft invoice' . print_r($error, true)); + else + error_log('TEST GENERATE DRAFT INVOICE NO ERROR WITH TRADE IN: No errors here'); + } + + protected function testGenerateDraftInvoiceNoErrorNoTradeIn() + { + $criteria = new InvoiceCriteria(); + + $criteria->setServiceType(ServiceType::BATTERY_REPLACEMENT_NEW); + $criteria->setIsTaxable(); + + $promo_id = 1; + $service_charges = []; + + // create array of invoice items + $items = [[ + 'battery' => 1038, + 'quantity' => 1, + 'trade_in' => '', + ]]; + + $error = $this->inv_manager->generateDraftInvoice($criteria, $promo_id, $service_charges, $items); + + if ($error) + error_log('TEST GENERATE DRAFT INVOICE NO ERROR NO TRADE IN: Errors found when generating draft invoice' . print_r($error, true)); + else + error_log('TEST GENERATE DRAFT INVOICE NO ERROR NO TRADE IN: No errors here'); + } + + protected function testGenerateDraftInvoiceInvalidBattery() + { + $criteria = new InvoiceCriteria(); + + $criteria->setServiceType(ServiceType::BATTERY_REPLACEMENT_NEW); + $criteria->setIsTaxable(); + + $promo_id = 1; + $service_charges = []; + + // create array of invoice items + $items = [[ + 'battery' => 1, + 'quantity' => 1, + 'trade_in' => '', + ], [ + 'battery' => 2, + 'quantity' => 1, + 'trade_in' => 'premium' + ]]; + + $error = $this->inv_manager->generateDraftInvoice($criteria, $promo_id, $service_charges, $items); + + if ($error) + error_log('TEST GENERATE DRAFT INVOICE BATTERY ERROR: Errors found when generating draft invoice: ' . print_r($error, true)); + else + error_log('TEST GENERATE DRAFT INVOICE BATTERY ERROR: No errors here'); + } + + protected function testGenerateDraftInvoiceInvalidPromo() + { + } + + protected function testGenerateDraftInvoiceNonBatterySales() + { + } + + protected function testGenerateDraftInvoiceBatterySalesNoPromo() + { + } + + + // test generateInvoiceInvoice call from ajax call + protected function testGenerateInvoice() + { + } + } diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 485709e4..c51a34c9 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -724,6 +724,8 @@ class JobOrderController extends Controller $cvid = $req->request->get('cvid'); $service_charges = $req->request->get('service_charges', []); + // TODO: set if taxable here + $em = $this->getDoctrine()->getManager(); // get customer vehicle diff --git a/src/Service/InvoiceManager.php b/src/Service/InvoiceManager.php index 0990db76..41d8dde9 100644 --- a/src/Service/InvoiceManager.php +++ b/src/Service/InvoiceManager.php @@ -14,6 +14,7 @@ use App\Ramcar\InvoiceStatus; use App\Ramcar\ServiceType; use App\Ramcar\TradeInType; +use App\Entity\Invoice as Inv; use App\Entity\InvoiceItem; use App\Entity\User; use App\Entity\Battery; @@ -82,6 +83,7 @@ class InvoiceManager return 0.12; } + // this is called when JO is submitted public function generateInvoiceCriteria($jo, $promo_id, $invoice_items, &$error_array) { // instantiate the invoice criteria @@ -112,8 +114,45 @@ class InvoiceManager $invoice_data = $this->compute($criteria, $rules); - $invoice = $createInvoice($invoice_data); + $invoice = $this->createInvoice($invoice_data); + + return $invoice; } + + return null; + } + + // this is called by JobOrderController when JS script generateInvoice is called + public function generateDraftInvoice($criteria, $promo_id, $service_charges, $items) + { + error_log('generateDraftInvoice'); + $ierror = $this->validatePromo($criteria, $promo_id); + + error_log(print_r($ierror, true)); + + if (!$ierror && !empty($items)) + { + error_log('calling validateInvoiceItems'); + // validate the invoice items (batteries and trade ins) + $ierror = $this->validateInvoiceItems($criteria, $items); + } + + return $ierror; + } + + // this is called by JobOrderController when JS script generateInvoice is called + public function generateInvoice($criteria) + { + // no need to validate since generateDraftInvoice was called before this was called + // generate the invoice + // check which rules to apply + $rules = $this->check($criteria); + + $invoice_data = $this->compute($criteria, $rules); + + $invoice = $this->createInvoice($invoice_data); + + return $invoice; } // check what rules to use given the criteria @@ -264,6 +303,8 @@ class InvoiceManager // check if this is a valid promo $promo = $this->em->getRepository(Promo::class)->find($promo_id); + error_log('promo checkpoint'); + if (empty($promo)) return 'Invalid promo specified.'; @@ -274,17 +315,19 @@ class InvoiceManager protected function validateInvoiceItems($criteria, $invoice_items) { + error_log('validate invoice items'); // check service type. Only battery sales and battery warranty should have invoice items $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)) + if (!empty($invoice_items)) { // check if this is a valid battery - foreach ($items as $item) + foreach ($invoice_items as $item) { + error_log($item['battery']); $battery = $this->em->getRepository(Battery::class)->find($item['battery']); if (empty($battery)) @@ -313,7 +356,7 @@ class InvoiceManager protected function createInvoice($invoice_data) { - $invoice = new Invoice(); + $invoice = new Inv(); // get current user $user = $this->security->getUser(); @@ -332,7 +375,10 @@ class InvoiceManager if (isset($data['promo'])) $promo = $data['promo']; - $invoice->addItem($invoice_item); + foreach ($invoice_items as $invoice_item) + { + $invoice->addItem($invoice_item); + } $invoice->setTotalPrice($total['total_price']) ->setVATExclusivePrice($total['vat_ex_price'])