Modify trade-in invoice rule to use price tiers. #789

This commit is contained in:
Korina Cordero 2024-02-08 04:32:34 -05:00
parent 962dbe1672
commit a0d9d1287a
3 changed files with 66 additions and 11 deletions

View file

@ -6,8 +6,17 @@ use App\InvoiceRuleInterface;
use App\Ramcar\TradeInType;
use App\Service\PriceTierManager;
class TradeIn implements InvoiceRuleInterface
{
protected $pt_manager;
public function __construct(PriceTierManager $pt_manager)
{
$this->pt_manager = $pt_manager;
}
public function getID()
{
return 'trade-in';
@ -17,10 +26,14 @@ class TradeIn implements InvoiceRuleInterface
{
$items = [];
$pt = $criteria->getPriceTier();
// get the entries
$entries = $criteria->getEntries();
foreach($entries as $entry)
{
// TODO: this might need some changes once merged with the
// rider app changes
$batt = $entry['battery'];
$qty = $entry['qty'];
$trade_in_type = null;
@ -30,7 +43,7 @@ class TradeIn implements InvoiceRuleInterface
if ($trade_in_type != null)
{
$ti_rate = $this->getTradeInRate($batt, $trade_in_type);
$ti_rate = $this->getTradeInRate($batt, $trade_in_type, $pt);
$qty_ti = bcmul($ti_rate, $qty, 2);
@ -60,21 +73,27 @@ class TradeIn implements InvoiceRuleInterface
return null;
}
protected function getTradeInRate($battery, $trade_in_type)
protected function getTradeInRate($battery, $trade_in_type, $pt_id)
{
$size = $battery->getSize();
$size_id = $size->getID();
switch ($trade_in_type)
$tip = $this->pt_manager->getTradeInPrice($pt_id, $size_id, $trade_in_type);
if ($tip == null)
{
case TradeInType::MOTOLITE:
return $size->getTIPriceMotolite();
case TradeInType::PREMIUM:
return $size->getTIPricePremium();
case TradeInType::OTHER:
return $size->getTIPriceOther();
switch ($trade_in_type)
{
case TradeInType::MOTOLITE:
return $size->getTIPriceMotolite();
case TradeInType::PREMIUM:
return $size->getTIPricePremium();
case TradeInType::OTHER:
return $size->getTIPriceOther();
}
}
return 0;
return $tip;
}
protected function getTitle($battery, $trade_in_type)

View file

@ -55,7 +55,7 @@ class InvoiceManager implements InvoiceGeneratorInterface
new InvoiceRule\Fuel($this->em, $this->pt_manager),
new InvoiceRule\TireRepair($this->em, $this->pt_manager),
new InvoiceRule\DiscountType($this->em),
new InvoiceRule\TradeIn(),
new InvoiceRule\TradeIn($this->pt_manager),
new InvoiceRule\Tax($this->em, $this->pt_manager),
];
}

View file

@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\PriceTier;
use App\Entity\TradeInPrice;
class PriceTierManager
{
@ -51,6 +52,41 @@ class PriceTierManager
return $actual_price;
}
public function getTradeInPrice($pt_id, $battery_size_id, $trade_in_type)
{
// find the trade in price, given the price tier and battery size id
$db_conn = $this->em->getConnection();
$tip_sql = 'SELECT tip.meta_info AS metainfo
FROM trade_in_price tip
WHERE tip.price_tier_id = :pt_id
AND tip.item_id = :battery_size_id';
$tip_stmt = $db_conn->prepare($tip_sql);
$tip_stmt->bindValue('pt_id', $pt_id);
$tip_stmt->bindValue('battery_size_id', $battery_size_id);
$tip_result = $tip_stmt->executeQuery();
$actual_tip = null;
// go through the rows
while ($row = $tip_result->fetchAssociative())
{
// get the price
$meta_info = $row['metainfo'];
$metadata = json_decode($meta_info, true);
if (isset($metadata[$trade_in_type]))
{
$tip = $metadata[$trade_in_type];
$actual_tip = number_format($tip / 100, 2, '.', '');
}
}
return $actual_tip;
}
public function getPriceTier(Point $coordinates)
{
$price_tier_id = 0;