Compare commits

...

6 commits

10 changed files with 560 additions and 11 deletions

View file

@ -672,6 +672,11 @@ catalyst_auth:
acls:
- id: item_pricing.update
label: Update
- id: trade_in_pricing
label: Trade In Pricing
acls:
- id: trade_in_pricing.update
label: Update
api:
user_entity: "App\\Entity\\ApiUser"

View file

@ -306,3 +306,7 @@ catalyst_menu:
acl: item_pricing.update
label: Item Pricing
parent: item
- id: trade_in_pricing
acl: trade_in_pricing.update
label: Trade In Pricing
parent: item

View file

@ -0,0 +1,14 @@
trade_in_pricing:
path: /trade-in-pricing
controller: App\Controller\TradeInPricingController::index
methods: [GET]
trade_in_pricing_update:
path: /trade-in-pricing
controller: App\Controller\TradeInPricingController::formSubmit
methods: [POST]
trade_in_pricing_prices:
path: /trade-in-pricing/{pt_id}/prices
controller: App\Controller\TradeInPricingController::tradeInPrices
methods: [GET]

View file

@ -0,0 +1,214 @@
<?php
namespace App\Controller;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Catalyst\MenuBundle\Annotation\Menu;
use App\Entity\PriceTier;
use App\Entity\BatterySize;
use App\Entity\TradeInPrice;
use App\Ramcar\TradeInType;
class TradeInPricingController extends Controller
{
/**
* @Menu(selected="trade_in_pricing")
* @IsGranted("trade_in_pricing.update")
*/
public function index (EntityManagerInterface $em)
{
// get all the price tiers
// default price tier so battery sizes data should come from battery size
// not from price tier
$price_tiers = $em->getRepository(PriceTier::class)->findAll();
// get all the items/battery sizes
$items = $this->getBatterySizes($em);
$params = [
'sets' => [
'price_tiers' => $price_tiers,
],
'items' => $items,
];
return $this->render('trade-in-pricing/form.html.twig', $params);
}
/**
* @Menu(selected="trade_in_pricing")
* @IsGranted("trade_in_pricing.update")
*/
public function formSubmit(Request $req, EntityManagerInterface $em)
{
$pt_id = $req->request->get('price_tier_id');
// get the trade in prices
$motolite_tips = $req->request->get('motolite_tip');
$premium_tips = $req->request->get('premium_tip');
$other_tips = $req->request->get('other_tip');
// get the battery sizes
$bsizes = $em->getRepository(BatterySize::class)->findBy([], ['id' => 'asc']);
// on default price tier
if ($pt_id == 0)
{
// default price tier, update battery size trade in prices
// NOTE: battery size trade in prices are stored as decimal
foreach ($bsizes as $bsize)
{
$bsize_id = $bsize->getID();
if (isset($motolite_tips[$bsize_id]))
$bsize->setTIPriceMotolite($motolite_tips[$bsize_id]);
if (isset($premium_tips[$bsize_id]))
$bsize->setTIPricePremium($premium_tips[$bsize_id]);
if (isset($other_tips[$bsize_id]))
$bsize->setTIPriceOther($other_tips[$bsize_id]);
}
}
else
{
// get the price tier
$price_tier = $em->getRepository(PriceTier::class)->find($pt_id);
$pt_tips = $price_tier->getTradeInPrices();
// clear the tier's trade in prices
foreach ($pt_tips as $pt_tip)
{
$em->remove($pt_tip);
}
// update the tier's trade in prices
foreach ($bsizes as $bsize)
{
$bsize_id = $bsize->getID();
$new_tip = new TradeInPrice();
$new_tip->setPriceTier($price_tier)
->setItemID($bsize_id);
$new_tip->addMetaInfo(TradeInType::MOTOLITE, $motolite_tips[$bsize_id] * 100);
$new_tip->addMetaInfo(TradeInType::PREMIUM, $premium_tips[$bsize_id] * 100);
$new_tip->addMetaInfo(TradeInType::OTHER, $other_tips[$bsize_id] * 100);
// save
$em->persist($new_tip);
}
}
$em->flush();
return $this->redirectToRoute('trade_in_pricing');
}
/**
* @IsGranted("trade_in_pricing.update")
*/
public function tradeInPrices(EntityManagerInterface $em, $pt_id)
{
$pt_prices = [];
// check if default prices are needed
if ($pt_id != 0)
{
// get the price tier
$pt = $em->getRepository(PriceTier::class)->find($pt_id);
// get the trade in prices under the price tier
$pt_trade_ins = $pt->getTradeInPrices();
foreach ($pt_trade_ins as $pt_trade_in)
{
$meta_info = $pt_trade_in->getAllMetaInfo();
error_log(print_r($meta_info, true));
$pt_prices[$pt_trade_in->getItemID()] = $meta_info;
}
}
// get the prices from battery size
$bsizes = $em->getRepository(BatterySize::class)->findBy([], ['id' => 'asc']);
$data_items = [];
foreach ($bsizes as $bsize)
{
$bsize_id = $bsize->getID();
$name = $bsize->getName();
// get the default trade in prices
$motolite_tip = $bsize->getTIPriceMotolite();
$premium_tip = $bsize->getTIPricePremium();
$other_tip = $bsize->getTiPriceOther();
// check if tier has price for battery size
if (isset($pt_prices[$bsize_id]))
{
$meta_info = $pt_prices[$bsize_id];
$pt_motolite_tip = $meta_info['motolite'];
$pt_premium_tip = $meta_info['premium'];
$pt_other_tip = $meta_info['other'];
// actual prices
$motolite_tip = number_format($pt_motolite_tip / 100, 2, '.', '');
$premium_tip = number_format($pt_premium_tip / 100, 2, '.', '');
$other_tip = number_format($pt_other_tip / 100, 2, '.', '');
}
$actual_motolite_tip = $motolite_tip;
$actual_premium_tip = $premium_tip;
$actual_other_tip = $other_tip;
$data_items[] = [
'id' => $bsize_id,
'name' => $name,
'motolite_tip' => $actual_motolite_tip,
'premium_tip' => $actual_premium_tip,
'other_tip' => $actual_other_tip,
];
}
// response
return new JsonResponse([
'items' => $data_items,
]);
}
protected function getBatterySizes(EntityManagerInterface $em)
{
// get all battery sizes
$b_sizes = $em->getRepository(BatterySize::class)->findBy([], ['id' => 'asc']);
foreach ($b_sizes as $b_size)
{
$b_size_set[$b_size->getID()] = [
'name' => $b_size->getName(),
'motolite_tip' => $b_size->getTIPriceMotolite(),
'premium_tip' => $b_size->getTIPricePremium(),
'other_tip' => $b_size->getTIPriceOther(),
];
}
return [
'items' => $b_size_set,
];
}
}

View file

@ -38,10 +38,17 @@ class PriceTier
*/
protected $item_prices;
// trade in prices under a price tier
/**
* @ORM\OneToMany(targetEntity="TradeInPrice", mappedBy="price_tier")
*/
protected $trade_in_prices;
public function __construct()
{
$this->supported_areas = new ArrayCollection();
$this->items = new ArrayCollection();
$this->trade_in_prices = new ArrayCollection();
}
public function getID()
@ -85,4 +92,9 @@ class PriceTier
return $this->item_prices;
}
public function getTradeInPrices()
{
return $this->trade_in_prices;
}
}

View file

@ -0,0 +1,99 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="trade_in_price")
*/
class TradeInPrice
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="PriceTier", inversedBy="item_prices")
* @ORM\JoinColumn(name="price_tier_id", referencedColumnName="id")
*/
protected $price_tier;
// battery size id, loosely coupled
/**
* @ORM\Column(type="integer")
*/
protected $item_id;
// metadata with trade in type as key
// and trade in price as value
/**
* @ORM\Column(type="json")
*/
protected $meta_info;
public function __construct()
{
$this->meta_info = [];
}
public function getID()
{
return $this->id;
}
public function setPriceTier(PriceTier $price_tier)
{
$this->price_tier = $price_tier;
return $this;
}
public function getPriceTier()
{
return $this->price_tier;
}
public function setItemID($item_id)
{
$this->item_id = $item_id;
return $this;
}
public function getItemID()
{
return $this->item_id;
}
public function addMetaInfo($id, $value)
{
$this->meta_info[$id] = $value;
return $this;
}
public function deleteMetadataInfo($id)
{
unset($this->meta_info[$id]);
return $this;
}
public function getMetaInfo($id)
{
// return null if we don't have it
if (!isset($this->meta_info[$id]))
return null;
return $this->meta_info[$id];
}
public function getAllMetaInfo()
{
return $this->meta_info;
}
}

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;

View file

@ -0,0 +1,146 @@
{% extends 'base.html.twig' %}
{% block body %}
<!-- BEGIN: Subheader -->
<div class="m-subheader">
<div class="d-flex align-items-center">
<div class="mr-auto">
<h3 class="m-subheader__title">Trade-In Pricing</h3>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<div class="row">
<div class="col-xl-12">
<div class="m-portlet m-portlet--mobile">
<div class="m-portlet__body">
<div class="m-form m-form--label-align-right m--margin-top-20 m--margin-bottom-30">
<div class="row align-items-center">
<div class="col-xl-12">
<div class="form-group m-form__group row align-items-center">
<div class="col-md-2">
<label>Trade-In Prices for </label>
</div>
<div class="col-md-3">
<div class="m-input-icon m-input-icon--left">
<div class="input-group">
<select class="form-control m-input" id="price-tier-select" name="price_tier_list">
<option value="0">Default Price Tier</option>
{% for price_tier in sets.price_tiers %}
<option value="{{ price_tier.getID }}">{{ price_tier.getName }} </option>
{% endfor %}
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<form id="row-form" class="m-form m-form--fit m-form--label-align-right" method="post" action="{{ url('trade_in_pricing_update') }}">
<input id="price-tier-id" type="hidden" name="price_tier_id" value="0">
<div style="padding-left: 25px; padding-right: 25px;">
<table class="table">
<thead>
<tr>
<th style="width: 100px">ID</th>
<th>Name</th>
<th style="width: 180px">Motolite</th>
<th style="width: 180px">Premium</th>
<th style="width: 180px">Other</th>
</tr>
</thead>
<tbody id="table-body">
{% for id, item in items.items %}
<tr>
<td>{{ id }}</td>
<td>{{ item.name }} </td>
<td class="py-1">
<input name="motolite_tip[{{ id }}]" class="form-control ca-filter" type="number" value="{{ item.motolite_tip }}" step="0.01">
</td>
<td class="py-1">
<input name="premium_tip[{{ id }}]" class="form-control ca-filter" type="number" value="{{ item.premium_tip }}" step="0.01">
</td>
<td class="py-1">
<input name="other_tip[{{ id }}]" class="form-control ca-filter" type="number" value="{{ item.other_tip }}" step="0.01">
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="">
<input type="submit" class="btn btn-primary" value="Update Price">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
initialize();
function initialize() {
init_price_tier_dropdown();
}
function init_price_tier_dropdown() {
var pt_dropdown = document.getElementById('price-tier-select');
pt_dropdown.addEventListener('change', function(e) {
load_prices(e.target.value);
});
}
function load_prices(price_tier_id) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
// process response
if (this.readyState == 4 && this.status == 200) {
// update form
update_table(JSON.parse(req.responseText));
var pt_field = document.getElementById('price-tier-id');
pt_field.value = price_tier_id;
} else {
// console.log('could not load tier prices');
}
}
var url_pattern = '{{ url('trade_in_pricing_prices', {'pt_id': '--id--'}) }}';
var url = url_pattern.replace('--id--', price_tier_id);
console.log(url);
req.open('GET', url, true);
req.send();
}
function update_table(data) {
console.log(data);
var item_html = '';
for (var i in data.items) {
var item = data.items[i];
// console.log(item);
item_html += '<tr>';
item_html += '<td>' + item.id + '</td>';
item_html += '<td>' + item.name + '</td>';
item_html += '<td class="py-1">';
item_html += '<input name="motolite_tip[' + item.id + ']" class="form-control ca-filter" type="number" value="' + item.motolite_tip + '" step="0.01">';
item_html += '</td>';
item_html += '<td class="py-1">';
item_html += '<input name="premium_tip[' + item.id + ']" class="form-control ca-filter" type="number" value="' + item.premium_tip + '" step="0.01">';
item_html += '</td>';
item_html += '<td class="py-1">';
item_html += '<input name="other_tip[' + item.id + ']" class="form-control ca-filter" type="number" value="' + item.other_tip + '" step="0.01">';
item_html += '</td>';
item_html += '</tr>';
}
var table_body = document.getElementById('table-body');
table_body.innerHTML = item_html;
}
</script>
{% endblock %}