refactore currency calculations in ProductSaleElements

This commit is contained in:
Julien Chanséaume
2014-04-28 12:07:45 +02:00
parent b86ac88ce4
commit 3bacb0a917
5 changed files with 164 additions and 99 deletions

View File

@@ -17,15 +17,12 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\Currency\CurrencyChangeEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Log\Tlog;
use Thelia\Model\Base\ProductSaleElementsQuery;
use Thelia\Model\Currency;
use Thelia\Model\ProductPrice;
use Thelia\Model\ProductPriceQuery;
use Thelia\Model\CartItem;
use Thelia\Model\CartItemQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Tools\ProductPriceTools;
/**
*
@@ -51,41 +48,23 @@ class Cart extends BaseAction implements EventSubscriberInterface
$append = $event->getAppend();
$quantity = $event->getQuantity();
$currency = $cart->getCurrency();
$defaultCurrency = Currency::getDefaultCurrency();
$productSaleElementsId = $event->getProductSaleElementsId();
$productId = $event->getProduct();
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
$price = 0.0;
$promoPrice = 0.0;
if ($cartItem === null || $newness) {
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId)
->filterByCurrencyId($cart->getCurrencyId())
->findOne();
$productSaleElements = ProductSaleElementsQuery::create()
->findPk($productSaleElementsId);
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
// need to calculate the prices
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId)
->filterByCurrencyId($defaultCurrency->getId())
->findOne();
if (null !== $productPrice) {
$price = $productPrice->getPrice() * $currency->getRate();
$promoPrice = $productPrice->getPromoPrice() * $currency->getRate();
}
} else {
$price = $productPrice->getPrice();
$promoPrice = $productPrice->getPromoPrice();
if (null !== $productSaleElements) {
$productPrices = $productSaleElements->getPricesByCurrency($currency);
$event->setCartItem(
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productSaleElements, $quantity, $productPrices)
);
}
$event->setCartItem(
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productPrice->getProductSaleElements(), $quantity, $price, $promoPrice)
);
}
if ($append && $cartItem !== null) {
@@ -166,71 +145,29 @@ class Cart extends BaseAction implements EventSubscriberInterface
*
* Refresh article's price
*
* @param \Thelia\Model\Base\Cart $cart
* @param \Thelia\Model\Base\Currency $currency
* @param \Thelia\Model\Cart $cart
* @param \Thelia\Model\Currency $currency
*/
public function updateCartPrices(\Thelia\Model\Base\Cart $cart, Currency $currency)
public function updateCartPrices(\Thelia\Model\Cart $cart, Currency $currency)
{
// get default currency
$defaultCurrency = Currency::getDefaultCurrency();
$rate = 1.0;
if ($currency !== $defaultCurrency) {
$rate = $currency->getRate();
}
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY : " . $rate . ' :: ' . $currency->getId() . ' :: ' . $defaultCurrency->getId() );
$price = 0.0;
$promoPrice = 0.0;
// cart item
foreach ($cart->getCartItems() as $cartItem) {
$productSaleElementsId = $cartItem->getProductSaleElementsId();
$productPrice = ProductPriceQuery::create()
->filterByCurrencyId($currency->getId())
->filterByProductSaleElementsId($productSaleElementsId)
->findOne();
$productSaleElements = $cartItem->getProductSaleElements();
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
// we take the default currency and apply the taxe rate
$productPrice = ProductPriceQuery::create()
->filterByCurrencyId($defaultCurrency->getId())
->filterByProductSaleElementsId($productSaleElementsId)
->findOne();
$productPrice = $productSaleElements->getPricesByCurrency($currency);
if (null === $productPrice) {
//Tlog::getInstance()->addDebug("BOOM");
continue;
}
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY DYNAMIC ");
$price = $productPrice->getPrice() * $rate;
$promoPrice = $productPrice->getPromoPrice() * $rate;
} else {
// product price for default currency or manual price for other currency
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY REAL ");
$price = $productPrice->getPrice();
$promoPrice = $productPrice->getPromoPrice();
}
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY : " . $price . " :: " . $promoPrice);
// We have
$cartItem
->setPrice($price)
->setPromoPrice($promoPrice);
->setPrice($productPrice->getPrice())
->setPromoPrice($productPrice->getPromoPrice());
$cartItem->save();
}
// update the currency cart
$cart->setCurrencyId($currency->getId());
$cart->save();
}
@@ -285,16 +222,16 @@ class Cart extends BaseAction implements EventSubscriberInterface
/**
* try to attach a new item to an existing cart
*
* @param \Thelia\Model\Cart $cart
* @param int $productId
* @param \Thelia\Model\ProductSaleElements $productSaleElements
* @param float $quantity
* @param float $price
* @param float $promoPrice
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* @param \Thelia\Model\Cart $cart
* @param int $productId
* @param \Thelia\Model\ProductSaleElements $productSaleElements
* @param float $quantity
* @param ProductPriceTools $productPrices
*
* @return CartItem
*/
protected function doAddItem(EventDispatcherInterface $dispatcher, \Thelia\Model\Cart $cart, $productId, \Thelia\Model\ProductSaleElements $productSaleElements, $quantity, $price, $promoPrice)
protected function doAddItem(EventDispatcherInterface $dispatcher, \Thelia\Model\Cart $cart, $productId, \Thelia\Model\ProductSaleElements $productSaleElements, $quantity, $productPrices)
{
$cartItem = new CartItem();
$cartItem->setDisptacher($dispatcher);
@@ -303,8 +240,8 @@ class Cart extends BaseAction implements EventSubscriberInterface
->setProductId($productId)
->setProductSaleElementsId($productSaleElements->getId())
->setQuantity($quantity)
->setPrice($price)
->setPromoPrice($promoPrice)
->setPrice($productPrices->getPrice())
->setPromoPrice($productPrices->getPromoPrice())
->setPromo($productSaleElements->getPromo())
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30))
->save();
@@ -330,14 +267,4 @@ class Cart extends BaseAction implements EventSubscriberInterface
->findOne();
}
/**
* Returns the session from the current request
*
* @return \Thelia\Core\HttpFoundation\Session\Session
*/
protected function getSession()
{
return $this->request->getSession();
}
}

View File

@@ -17,4 +17,18 @@ use Thelia\Model\Base\ProductPriceQuery as BaseProductPriceQuery;
class ProductPriceQuery extends BaseProductPriceQuery
{
public function findByCurrencyAndProductSaleElements($currencyId, $productSaleElementsId)
{
ArticleQuery::create()->select(array('Id', 'Name'))->find();
$currencyId = $this->getCurrency();
if (null !== $currencyId) {
$currency = CurrencyQuery::create()->findOneById($currencyId);
if (null === $currency) {
throw new \InvalidArgumentException('Cannot found currency id: `' . $currency . '` in product_sale_elements loop');
}
}
}
} // ProductPriceQuery

View File

@@ -3,6 +3,7 @@
namespace Thelia\Model;
use Thelia\Model\Base\ProductSaleElements as BaseProductSaleElements;
use Thelia\Model\Tools\ProductPriceTools;
use Thelia\TaxEngine\Calculator;
class ProductSaleElements extends BaseProductSaleElements
@@ -42,4 +43,51 @@ class ProductSaleElements extends BaseProductSaleElements
return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()), 2);
}
/**
* Get product prices for a specific currency.
*
* When the currency is not the default currency, the product prices for this currency is :
* - calculated according to the product price of the default currency. It happens when no product price exists for
* the currency or when the `from_default_currency` flag is set to `true`
* - set directly in the product price when `from_default_currency` is set to `false`
*
* @param Currency $currency
* @return ProductPriceTools
* @throws \RuntimeException
*/
public function getPricesByCurrency($currency)
{
$defaultCurrency = Currency::getDefaultCurrency();
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($this->getId())
->filterByCurrencyId($currency->getId())
->findOne();
$price = 0.0;
$promoPrice = 0.0;
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
// need to calculate the prices based on the product prices for the default currency
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($this->getId())
->filterByCurrencyId($defaultCurrency->getId())
->findOne();
if (null !== $productPrice) {
$price = $productPrice->getPrice() * $currency->getRate() / $defaultCurrency->getRate();
$promoPrice = $productPrice->getPromoPrice() * $currency->getRate() / $defaultCurrency->getRate();
} else {
throw new \RuntimeException('Cannot find product prices for currency id: `' . $currency->getId() . '`');
}
} else {
$price = $productPrice->getPrice();
$promoPrice = $productPrice->getPromoPrice();
}
$productPriceTools = new ProductPriceTools($price, $promoPrice);
return $productPriceTools;
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Model\Tools;
/**
* Utility class used to store price and promo price for a carte item.
*
* Class ProductPriceTools
* @package Thelia\Model\Tools
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
*/
class ProductPriceTools
{
/**
* The value for the price field.
*
* @var double
*/
protected $price;
/**
* The value for the promoPrice field.
*
* @var double
*/
protected $promoPrice;
public function __construct($price, $promoPrice)
{
$this->price = $price;
$this->promoPrice = $promoPrice;
}
/**
* @param float $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @param float $promoPrice
*/
public function setPromoPrice($promoPrice)
{
$this->promoPrice = $promoPrice;
}
/**
* @return float
*/
public function getPromoPrice()
{
return $this->promoPrice;
}
}

View File

@@ -31,7 +31,7 @@
<thead>
<tr>
<th class="image">&nbsp;</th>
<th class="product">getPrice
<th class="product">
<span class="hidden-xs">{intl l="Product Name"}</span>
<span class="visible-xs">{intl l="Name"}</span>
</th>