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,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;
}
}