Modify trade-in invoice rule to use price tiers. #789
This commit is contained in:
parent
962dbe1672
commit
a0d9d1287a
3 changed files with 66 additions and 11 deletions
|
|
@ -6,8 +6,17 @@ use App\InvoiceRuleInterface;
|
||||||
|
|
||||||
use App\Ramcar\TradeInType;
|
use App\Ramcar\TradeInType;
|
||||||
|
|
||||||
|
use App\Service\PriceTierManager;
|
||||||
|
|
||||||
class TradeIn implements InvoiceRuleInterface
|
class TradeIn implements InvoiceRuleInterface
|
||||||
{
|
{
|
||||||
|
protected $pt_manager;
|
||||||
|
|
||||||
|
public function __construct(PriceTierManager $pt_manager)
|
||||||
|
{
|
||||||
|
$this->pt_manager = $pt_manager;
|
||||||
|
}
|
||||||
|
|
||||||
public function getID()
|
public function getID()
|
||||||
{
|
{
|
||||||
return 'trade-in';
|
return 'trade-in';
|
||||||
|
|
@ -17,10 +26,14 @@ class TradeIn implements InvoiceRuleInterface
|
||||||
{
|
{
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|
||||||
|
$pt = $criteria->getPriceTier();
|
||||||
|
|
||||||
// get the entries
|
// get the entries
|
||||||
$entries = $criteria->getEntries();
|
$entries = $criteria->getEntries();
|
||||||
foreach($entries as $entry)
|
foreach($entries as $entry)
|
||||||
{
|
{
|
||||||
|
// TODO: this might need some changes once merged with the
|
||||||
|
// rider app changes
|
||||||
$batt = $entry['battery'];
|
$batt = $entry['battery'];
|
||||||
$qty = $entry['qty'];
|
$qty = $entry['qty'];
|
||||||
$trade_in_type = null;
|
$trade_in_type = null;
|
||||||
|
|
@ -30,7 +43,7 @@ class TradeIn implements InvoiceRuleInterface
|
||||||
|
|
||||||
if ($trade_in_type != null)
|
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);
|
$qty_ti = bcmul($ti_rate, $qty, 2);
|
||||||
|
|
||||||
|
|
@ -60,21 +73,27 @@ class TradeIn implements InvoiceRuleInterface
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTradeInRate($battery, $trade_in_type)
|
protected function getTradeInRate($battery, $trade_in_type, $pt_id)
|
||||||
{
|
{
|
||||||
$size = $battery->getSize();
|
$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:
|
switch ($trade_in_type)
|
||||||
return $size->getTIPriceMotolite();
|
{
|
||||||
case TradeInType::PREMIUM:
|
case TradeInType::MOTOLITE:
|
||||||
return $size->getTIPricePremium();
|
return $size->getTIPriceMotolite();
|
||||||
case TradeInType::OTHER:
|
case TradeInType::PREMIUM:
|
||||||
return $size->getTIPriceOther();
|
return $size->getTIPricePremium();
|
||||||
|
case TradeInType::OTHER:
|
||||||
|
return $size->getTIPriceOther();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return $tip;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTitle($battery, $trade_in_type)
|
protected function getTitle($battery, $trade_in_type)
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ class InvoiceManager implements InvoiceGeneratorInterface
|
||||||
new InvoiceRule\Fuel($this->em, $this->pt_manager),
|
new InvoiceRule\Fuel($this->em, $this->pt_manager),
|
||||||
new InvoiceRule\TireRepair($this->em, $this->pt_manager),
|
new InvoiceRule\TireRepair($this->em, $this->pt_manager),
|
||||||
new InvoiceRule\DiscountType($this->em),
|
new InvoiceRule\DiscountType($this->em),
|
||||||
new InvoiceRule\TradeIn(),
|
new InvoiceRule\TradeIn($this->pt_manager),
|
||||||
new InvoiceRule\Tax($this->em, $this->pt_manager),
|
new InvoiceRule\Tax($this->em, $this->pt_manager),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||||
|
|
||||||
use App\Entity\PriceTier;
|
use App\Entity\PriceTier;
|
||||||
|
use App\Entity\TradeInPrice;
|
||||||
|
|
||||||
class PriceTierManager
|
class PriceTierManager
|
||||||
{
|
{
|
||||||
|
|
@ -51,6 +52,41 @@ class PriceTierManager
|
||||||
return $actual_price;
|
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)
|
public function getPriceTier(Point $coordinates)
|
||||||
{
|
{
|
||||||
$price_tier_id = 0;
|
$price_tier_id = 0;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue