Add routes, controller, and template for trade in pricing. #789

This commit is contained in:
Korina Cordero 2024-02-06 04:43:58 -05:00
parent ee033ddd55
commit 4a9dc2a6b2
6 changed files with 355 additions and 0 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,72 @@
<?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
$price_tiers = $em->getRepository(PriceTier::class)->findAll();
// get all the items/battery sizes
$items = $this->getBatterySizes($em);
// get the trade in types
$params = [
'sets' => [
'price_tiers' => $price_tiers,
'trade_in_types' => TradeInType::getCollection(),
],
'items' => $items,
];
// TODO: fix display of prices according to trade in type selected
// need to set a default trade in type to display? check TradeInType::getCollection
return $this->render('trade-in-pricing/form.html.twig', $params);
}
protected function getBatterySizes(EntityManagerInterface $em)
{
// get all battery sizes
$b_sizes = $em->getRepository(BatterySize::class)->findBy([], ['name' => 'asc']);
foreach ($b_sizes as $b_size)
{
$b_size_set[$b_size->getID()] = [
'name' => $b_size->getName(),
'motolite_tiprice' => $b_size->getTIPriceMotolite(),
'premium_tiprice' => $b_size->getTIPricePremium(),
'other_tiprice' => $b_size->getTIPriceOther(),
];
}
return [
'items' => $b_size_set,
];
}
}

117
src/Entity/TradeInPrice.php Normal file
View file

@ -0,0 +1,117 @@
<?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;
// item type
/**
* @ORM\ManyToOne(targetEntity="ItemType", inversedBy="items")
* @ORM\JoinColumn(name="item_type_id", referencedColumnName="id")
*/
protected $item_type;
// 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 setItemType(ItemType $item_type)
{
$this->item_type = $item_type;
return $this;
}
public function getItemType()
{
return $this->item_type;
}
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

@ -0,0 +1,143 @@
{% 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 class="col-md-3">
<div class="m-input-icon m-input-icon--left">
<div class="input-group">
<select class="form-control m-input" id="trade-in-type-select" name="trade_in_type_list">
{% for key, class in sets.trade_in_types %}
<option value="{{ key }}">{{ class }} </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">Price</th>
</tr>
</thead>
<tbody id="table-body">
{% for id, item in items.items %}
<tr>
<td>{{ id }}</td>
<td>{{ item.name }} </td>
<td></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();
init_item_type_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>' + item.item_type + '</td>';
item_html += '<td class="py-1">';
item_html += '<input name="price[' + item.id + ']" class="form-control ca-filter" type="number" value="' + item.price + '" step="0.01">';
item_html += '</td>';
item_html += '</tr>';
}
var table_body = document.getElementById('table-body');
table_body.innerHTML = item_html;
}
</script>
{% endblock %}