diff --git a/src/Controller/TradeInPricingController.php b/src/Controller/TradeInPricingController.php index f9c0dcb4..001d565b 100644 --- a/src/Controller/TradeInPricingController.php +++ b/src/Controller/TradeInPricingController.php @@ -31,37 +31,108 @@ class TradeInPricingController extends Controller 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); - // 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); } + /** + * @IsGranted("trade_in_pricing.update") + */ + public function tradeInPrices(EntityManagerInterface $em, $pt_id, $ti_type) + { + $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' => $actul_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([], ['name' => 'asc']); + $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_tiprice' => $b_size->getTIPriceMotolite(), - 'premium_tiprice' => $b_size->getTIPricePremium(), - 'other_tiprice' => $b_size->getTIPriceOther(), + 'motolite_tip' => $b_size->getTIPriceMotolite(), + 'premium_tip' => $b_size->getTIPricePremium(), + 'other_tip' => $b_size->getTIPriceOther(), ]; } diff --git a/src/Entity/PriceTier.php b/src/Entity/PriceTier.php index 1e2599a5..c56cd0bb 100644 --- a/src/Entity/PriceTier.php +++ b/src/Entity/PriceTier.php @@ -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; + } + } diff --git a/src/Entity/TradeInPrice.php b/src/Entity/TradeInPrice.php index de5d1216..26f31b47 100644 --- a/src/Entity/TradeInPrice.php +++ b/src/Entity/TradeInPrice.php @@ -26,13 +26,6 @@ class TradeInPrice */ 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") @@ -67,17 +60,6 @@ class TradeInPrice 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; diff --git a/templates/trade-in-pricing/form.html.twig b/templates/trade-in-pricing/form.html.twig index 77198fe2..fc878824 100644 --- a/templates/trade-in-pricing/form.html.twig +++ b/templates/trade-in-pricing/form.html.twig @@ -34,17 +34,6 @@ -
-
-
- -
-
-
@@ -57,7 +46,9 @@ ID Name - Price + Motolite + Premium + Other @@ -65,7 +56,15 @@ {{ id }} {{ item.name }} - + + + + + + + + + {% endfor %} @@ -89,7 +88,6 @@ initialize(); function initialize() { init_price_tier_dropdown(); - init_item_type_dropdown(); } function init_price_tier_dropdown() { @@ -128,7 +126,6 @@ function update_table(data) { item_html += ''; item_html += '' + item.id + ''; item_html += '' + item.name + ''; - item_html += '' + item.item_type + ''; item_html += ''; item_html += ''; item_html += '';