From 8ca7292a258a5dc25097d51a9122b7ab82cb66db Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 5 Feb 2024 03:06:06 -0500 Subject: [PATCH] Add processing of trade in items from rider app. #783 --- src/Controller/CAPI/RiderAppController.php | 17 +++++++++------ src/InvoiceRule/BatterySales.php | 11 ++++++---- src/InvoiceRule/TradeIn.php | 24 ++++++++++++++-------- src/Ramcar/InvoiceCriteria.php | 11 ++++++++++ 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/Controller/CAPI/RiderAppController.php b/src/Controller/CAPI/RiderAppController.php index 48d328bd..6dc409d6 100644 --- a/src/Controller/CAPI/RiderAppController.php +++ b/src/Controller/CAPI/RiderAppController.php @@ -23,6 +23,7 @@ use App\Entity\BatterySize; use App\Entity\RiderAPISession; use App\Entity\User; use App\Entity\ApiUser as APIUser; +use App\Entity\JobOrder; use App\Service\RedisClientProvider; use App\Service\RiderCache; @@ -1205,10 +1206,9 @@ class RiderAppController extends ApiController // need to get the existing invoice items using jo id and invoice id $existing_ii = $this->getInvoiceItems($em, $jo_id); - // $this->generateUpdatedInvoice($em, $jo_id, $existing_ii, $trade_in_items); + $this->generateUpdatedInvoice($em, $ic, $jo_id, $existing_ii, $ti_items); $data = []; - return new APIResponse(true, 'Job order updated.', $data); } @@ -1354,7 +1354,7 @@ class RiderAppController extends ApiController return new APIResponse(true, 'Job order service changed.', $data); } - protected function generateUpdatedInvoice(EntityManagerInterface $em, $jo_id, $existing_ii, $trade_in_items) + protected function generateUpdatedInvoice(EntityManagerInterface $em, InvoiceGeneratorInterface $ic, $jo_id, $existing_ii, $trade_in_items) { // get the job order since we need info in the JO for the invoice criteria $jo = $em->getRepository(JobOrder::class)->find($jo_id); @@ -1370,7 +1370,7 @@ class RiderAppController extends ApiController // get the promo id from existing invoice item $promo_id = $existing_ii['promo_id']; - if ($promo_id = null) + if ($promo_id == null) $promo = null; else $promo = $em->getRepository(Promo::class)->find($promo_id); @@ -1404,8 +1404,13 @@ class RiderAppController extends ApiController // add the trade in items to the criteria foreach ($trade_in_items as $ti_item) { - // TODO: need to discuss how to store since what we have is battery size - // and we're supposed to store battery + $batt_size_id = $ti_item['battery_size_id']; + $qty = $ti_item['qty']; + $trade_in_type = $ti_item['trade_in_type']; + + $batt_size = $em->getRepository(BatterySize::class)->find($batt_size_id); + + $icrit->addTradeInEntry($batt_size, $trade_in_type, $qty); } // call generateInvoice diff --git a/src/InvoiceRule/BatterySales.php b/src/InvoiceRule/BatterySales.php index 45b060d1..97551cb7 100644 --- a/src/InvoiceRule/BatterySales.php +++ b/src/InvoiceRule/BatterySales.php @@ -36,18 +36,21 @@ class BatterySales implements InvoiceRuleInterface $entries = $criteria->getEntries(); foreach($entries as $entry) { - $batt = $entry['battery']; $qty = $entry['qty']; $trade_in = null; + // check if entry is for trade in if (isset($entry['trade_in'])) $trade_in = $entry['trade_in']; - $size = $batt->getSize(); - + // entry is a battery purchase if ($trade_in == null) { - // battery purchase + // safe to get entry with battery key since CRM and apps + // will set this for a battery purchase and trade_in will + // will not be set + $batt = $entry['battery']; + $price = $batt->getSellingPrice(); $items[] = [ diff --git a/src/InvoiceRule/TradeIn.php b/src/InvoiceRule/TradeIn.php index 8e3c6063..224bf098 100644 --- a/src/InvoiceRule/TradeIn.php +++ b/src/InvoiceRule/TradeIn.php @@ -21,7 +21,6 @@ class TradeIn implements InvoiceRuleInterface $entries = $criteria->getEntries(); foreach($entries as $entry) { - $batt = $entry['battery']; $qty = $entry['qty']; $trade_in_type = null; @@ -30,7 +29,18 @@ class TradeIn implements InvoiceRuleInterface if ($trade_in_type != null) { - $ti_rate = $this->getTradeInRate($batt, $trade_in_type); + // at this point, entry is a trade in + // need to check if battery (coming from CRM) is set + // or battery_size is set (coming from rider app) + if (isset($entry['battery'])) + { + $battery = $entry['battery']; + $batt_size = $battery->getSize(); + } + else + $batt_size = $entry['battery_size']; + + $ti_rate = $this->getTradeInRate($batt_size, $trade_in_type); $qty_ti = bcmul($ti_rate, $qty, 2); @@ -41,7 +51,7 @@ class TradeIn implements InvoiceRuleInterface $items[] = [ 'qty' => $qty, - 'title' => $this->getTitle($batt, $trade_in_type), + 'title' => $this->getTitle($batt_size, $trade_in_type), 'price' => $price, ]; } @@ -60,10 +70,8 @@ class TradeIn implements InvoiceRuleInterface return null; } - protected function getTradeInRate($battery, $trade_in_type) + protected function getTradeInRate($size, $trade_in_type) { - $size = $battery->getSize(); - switch ($trade_in_type) { case TradeInType::MOTOLITE: @@ -77,9 +85,9 @@ class TradeIn implements InvoiceRuleInterface return 0; } - protected function getTitle($battery, $trade_in_type) + protected function getTitle($battery_size, $trade_in_type) { - $title = 'Trade-in ' . TradeInType::getName($trade_in_type) . ' ' . $battery->getSize()->getName() . ' battery'; + $title = 'Trade-in ' . TradeInType::getName($trade_in_type) . ' ' . $battery_size->getName() . ' battery'; return $title; } diff --git a/src/Ramcar/InvoiceCriteria.php b/src/Ramcar/InvoiceCriteria.php index b27395da..8b1ee18b 100644 --- a/src/Ramcar/InvoiceCriteria.php +++ b/src/Ramcar/InvoiceCriteria.php @@ -108,6 +108,17 @@ class InvoiceCriteria $this->entries[] = $entry; } + public function addTradeInEntry($battery_size, $trade_in, $qty) + { + $entry = [ + 'battery_size' => $battery_size, + 'trade_in' => $trade_in, + 'qty' => $qty + ]; + + $this->entries[] = $entry; + } + public function getEntries() { return $this->entries;