Finished PSE management for products with no combinations
This commit is contained in:
@@ -67,11 +67,11 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface
|
||||
->findOne($con);
|
||||
|
||||
if ($salesElement == null) {
|
||||
// Create a new product sale element
|
||||
// Create a new default product sale element
|
||||
$salesElement = $event->getProduct()->createDefaultProductSaleElement($con, 0, 0, $event->getCurrencyId(), true);
|
||||
}
|
||||
else {
|
||||
// This one is the default
|
||||
// This (new) one is the default
|
||||
$salesElement->setIsDefault(true)->save($con);
|
||||
}
|
||||
|
||||
@@ -122,6 +122,9 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface
|
||||
|
||||
try {
|
||||
|
||||
// Update the product's tax rule
|
||||
$event->getProduct()->setTaxRuleId($event->getTaxRuleId())->save($con);
|
||||
|
||||
// If product sale element is not defined, create it.
|
||||
if ($salesElement == null) {
|
||||
$salesElement = new ProductSaleElements();
|
||||
@@ -158,11 +161,25 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface
|
||||
;
|
||||
}
|
||||
|
||||
$productPrice
|
||||
->setPromoPrice($event->getSalePrice())
|
||||
->setPrice($event->getPrice())
|
||||
->save($con)
|
||||
;
|
||||
// Check if we have to store the price
|
||||
$productPrice->setFromDefaultCurrency($event->getFromDefaultCurrency());
|
||||
|
||||
if ($event->getFromDefaultCurrency() == 0) {
|
||||
// Store the price
|
||||
$productPrice
|
||||
->setPromoPrice($event->getSalePrice())
|
||||
->setPrice($event->getPrice())
|
||||
;
|
||||
}
|
||||
else {
|
||||
// Do not store the price.
|
||||
$productPrice
|
||||
->setPromoPrice(0)
|
||||
->setPrice(0)
|
||||
;
|
||||
}
|
||||
|
||||
$productPrice->save($con);
|
||||
|
||||
// Store all the stuff !
|
||||
$con->commit();
|
||||
|
||||
@@ -189,6 +189,23 @@ class ProductController extends AbstractCrudController
|
||||
return $event->hasProduct();
|
||||
}
|
||||
|
||||
protected function updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency) {
|
||||
|
||||
// Get price for default currency
|
||||
$priceForDefaultCurrency = ProductPriceQuery::create()
|
||||
->filterByCurrency($defaultCurrency)
|
||||
->filterByProductSaleElements($saleElement)
|
||||
->findOne()
|
||||
;
|
||||
|
||||
if ($priceForDefaultCurrency !== null) {
|
||||
$productPrice
|
||||
->setPrice($priceForDefaultCurrency->getPrice() * $currentCurrency->getRate())
|
||||
->setPromoPrice($priceForDefaultCurrency->getPromoPrice() * $currentCurrency->getRate())
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
protected function hydrateObjectForm($object)
|
||||
{
|
||||
$defaultPseData = $combinationPseData = array();
|
||||
@@ -198,18 +215,35 @@ class ProductController extends AbstractCrudController
|
||||
->filterByProduct($object)
|
||||
->find();
|
||||
|
||||
$defaultCurrency = Currency::getDefaultCurrency();
|
||||
$currentCurrency = $this->getCurrentEditionCurrency();
|
||||
|
||||
foreach($saleElements as $saleElement) {
|
||||
|
||||
// Get the product price for the current currency
|
||||
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByCurrency($this->getCurrentEditionCurrency())
|
||||
->filterByCurrency($currentCurrency)
|
||||
->filterByProductSaleElements($saleElement)
|
||||
->findOne()
|
||||
;
|
||||
|
||||
if ($productPrice == null) {
|
||||
// No one exists ?
|
||||
if ($productPrice === null) {
|
||||
$productPrice = new ProductPrice();
|
||||
|
||||
// If the current currency is not the default one, calculate the price
|
||||
// using default currency price and current currency rate
|
||||
if ($currentCurrency->getId() != $defaultCurrency->getId()) {
|
||||
|
||||
$productPrice->setFromDefaultCurrency(true);
|
||||
|
||||
$this->updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency);
|
||||
}
|
||||
}
|
||||
|
||||
// Caclulate prices if we have to use the rate * defaulkt currency price
|
||||
if ($productPrice->getFromDefaultCurrency() == true) {
|
||||
$this->updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency);
|
||||
}
|
||||
|
||||
$isDefaultPse = count($saleElement->getAttributeCombinations()) == 0;
|
||||
@@ -236,7 +270,6 @@ class ProductController extends AbstractCrudController
|
||||
"isdefault" => $saleElement->getIsDefault() > 0 ? 1 : 0,
|
||||
"ean_code" => $saleElement->getEanCode()
|
||||
);
|
||||
|
||||
}
|
||||
else {
|
||||
}
|
||||
@@ -246,9 +279,13 @@ class ProductController extends AbstractCrudController
|
||||
|
||||
$combinationPseForm = new ProductSaleElementUpdateForm($this->getRequest(), "form", $combinationPseData);
|
||||
$this->getParserContext()->addForm($combinationPseForm);
|
||||
|
||||
var_dump($defaultPseData);
|
||||
|
||||
var_dump($combinationPseData);
|
||||
}
|
||||
|
||||
// Prepare the data that will hydrate the form
|
||||
// Prepare the data that will hydrate the form(s)
|
||||
$data = array(
|
||||
'id' => $object->getId(),
|
||||
'ref' => $object->getRef(),
|
||||
@@ -260,8 +297,6 @@ class ProductController extends AbstractCrudController
|
||||
'visible' => $object->getVisible(),
|
||||
'url' => $object->getRewrittenUrl($this->getCurrentEditionLocale()),
|
||||
'default_category' => $object->getDefaultCategoryId()
|
||||
|
||||
// A terminer pour les prix
|
||||
);
|
||||
|
||||
// Setup the object form
|
||||
@@ -881,7 +916,8 @@ class ProductController extends AbstractCrudController
|
||||
->setIsnew($data['isnew'])
|
||||
->setIsdefault($data['isdefault'])
|
||||
->setEanCode($data['ean_code'])
|
||||
->setTaxrule($data['tax_rule'])
|
||||
->setTaxRuleId($data['tax_rule'])
|
||||
->setFromDefaultCurrency($data['use_exchange_rate'])
|
||||
;
|
||||
|
||||
$this->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $event);
|
||||
|
||||
@@ -41,7 +41,8 @@ class ProductSaleElementUpdateEvent extends ProductSaleElementEvent
|
||||
protected $isnew;
|
||||
protected $isdefault;
|
||||
protected $ean_code;
|
||||
protected $taxrule;
|
||||
protected $tax_rule_id;
|
||||
protected $from_default_currency;
|
||||
|
||||
public function __construct(Product $product, $product_sale_element_id)
|
||||
{
|
||||
@@ -196,16 +197,27 @@ class ProductSaleElementUpdateEvent extends ProductSaleElementEvent
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTaxrule()
|
||||
public function getTaxRuleId()
|
||||
{
|
||||
return $this->taxrule;
|
||||
return $this->tax_rule_id;
|
||||
}
|
||||
|
||||
public function setTaxrule($taxrule)
|
||||
public function setTaxRuleId($tax_rule_id)
|
||||
{
|
||||
$this->taxrule = $taxrule;
|
||||
$this->tax_rule_id = $tax_rule_id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFromDefaultCurrency()
|
||||
{
|
||||
return $this->from_default_currency;
|
||||
}
|
||||
|
||||
public function setFromDefaultCurrency($from_default_currency)
|
||||
{
|
||||
$this->from_default_currency = $from_default_currency;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -668,6 +668,7 @@ class Product extends BaseI18nLoop
|
||||
->set("PREVIOUS" , $previous != null ? $previous->getId() : -1)
|
||||
->set("NEXT" , $next != null ? $next->getId() : -1)
|
||||
->set("DEFAULT_CATEGORY" , $default_category_id)
|
||||
->set("TAX_RULE_ID" , $product->getTaxRuleId())
|
||||
|
||||
;
|
||||
|
||||
|
||||
@@ -28,4 +28,4 @@ class ProductDefaultSaleElementUpdateForm extends ProductSaleElementUpdateForm
|
||||
{
|
||||
return "thelia_product_default_sale_element_update_form";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ class ProductSaleElementUpdateForm extends BaseForm
|
||||
"label_attr" => array("for" => "quantity_field")
|
||||
))
|
||||
->add("sale_price", "number", array(
|
||||
"label" => Translator::getInstance()->trans("Sale price without taxes *"),
|
||||
"label" => Translator::getInstance()->trans("Sale price without taxes"),
|
||||
"label_attr" => array("for" => "price_with_tax_field")
|
||||
))
|
||||
->add("sale_price_with_tax", "number", array(
|
||||
@@ -96,7 +96,7 @@ class ProductSaleElementUpdateForm extends BaseForm
|
||||
"label" => Translator::getInstance()->trans("Is it the default product sale element ?"),
|
||||
"label_attr" => array("for" => "isdefault_field")
|
||||
))
|
||||
->add("ean_code", "integer", array(
|
||||
->add("ean_code", "text", array(
|
||||
"label" => Translator::getInstance()->trans("EAN Code"),
|
||||
"label_attr" => array("for" => "ean_code_field")
|
||||
))
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
<select id="tax_rule_field" required="required" name="{$name}" class="form-control">
|
||||
<option value="">{intl l="Select a tax tule"}</option>
|
||||
{loop name="tax" type="tax-rule" backend_context="1"}
|
||||
<option value="{$ID}" {if $IS_DEFAULT}selected="selected"{/if}>{$TITLE}</option>
|
||||
<option value="{$ID}" {if $ID == $TAX_RULE_ID}selected="selected"{/if}>{$TITLE}</option>
|
||||
{/loop}
|
||||
</select>
|
||||
</div>
|
||||
@@ -280,7 +280,7 @@
|
||||
<select id="tax_rule_field" required="required" name="{$name}" class="form-control">
|
||||
<option value="">{intl l="Select a tax tule"}</option>
|
||||
{loop name="tax" type="tax-rule" backend_context="1"}
|
||||
<option value="{$ID}" {if $IS_DEFAULT}selected="selected"{/if}>{$TITLE}</option>
|
||||
<option value="{$ID}" {if $ID == $TAX_RULE_ID}selected="selected"{/if}>{$TITLE}</option>
|
||||
{/loop}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user