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