From 67bf7bb9898de032b8828c524ad4fe24e9f5e570 Mon Sep 17 00:00:00 2001 From: franck Date: Mon, 23 Sep 2013 18:36:55 +0200 Subject: [PATCH] Finished product combination basic function --- core/lib/Thelia/Action/Product.php | 45 +- .../Thelia/Config/Resources/routing/admin.xml | 12 +- .../Controller/Admin/BaseAdminController.php | 3 +- .../Controller/Admin/ProductController.php | 34 +- .../Event/ProductCreateCombinationEvent.php | 33 +- .../Event/ProductDeleteCombinationEvent.php | 47 ++ .../Template/Loop/ProductSaleElements.php | 1 + .../default/includes/product-details-tab.html | 472 +++++++++--------- 8 files changed, 387 insertions(+), 260 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/ProductDeleteCombinationEvent.php diff --git a/core/lib/Thelia/Action/Product.php b/core/lib/Thelia/Action/Product.php index 095f2d32e..cecc1df39 100644 --- a/core/lib/Thelia/Action/Product.php +++ b/core/lib/Thelia/Action/Product.php @@ -56,7 +56,6 @@ use Thelia\Model\FeatureProductQuery; use Thelia\Model\ProductCategoryQuery; use Thelia\Core\Event\ProductSetTemplateEvent; use Thelia\Model\AttributeCombinationQuery; -use Thelia\Core\Template\Loop\ProductSaleElements; use Thelia\Model\ProductSaleElementsQuery; use Propel\Runtime\ActiveQuery\PropelQuery; use Thelia\Core\Event\ProductDeleteCategoryEvent; @@ -66,6 +65,9 @@ use Thelia\Model\AttributeCombination; use Thelia\Core\Event\ProductCreateCombinationEvent; use Propel\Runtime\Propel; use Thelia\Model\Map\ProductTableMap; +use Thelia\Core\Event\ProductDeleteCombinationEvent; +use Thelia\Model\ProductPrice; +use Thelia\Model\ProductSaleElements; class Product extends BaseAction implements EventSubscriberInterface { @@ -367,19 +369,31 @@ class Product extends BaseAction implements EventSubscriberInterface $con->beginTransaction(); try { + $product = $event->getProduct(); - if ($event->getUseDefaultPricing()) { - // Get the default pricing - $salesElement = ProductSaleElementsQuery::create()->filterByIsDefault(true)->findOne(); - } - else { - // We have to create a new ProductSaleElement - echo "no default !!!!"; - exit; - } + // Create an empty product sale element + $salesElement = new ProductSaleElements(); - if (null == $salesElement) - throw new \LogicException("Cannot create or get the product sales element for this new combination."); + $salesElement + ->setProduct($product) + ->setRef($product->getRef()) + ->setPromo(0) + ->setNewness(0) + ->setWeight(0) + ->setIsDefault(false) + ->save($con) + ; + + // Create an empty product price in the default currency + $product_price = new ProductPrice(); + + $product_price + ->setProductSaleElements($salesElement) + ->setPromoPrice(0) + ->setPrice(0) + ->setCurrencyId($event->getCurrencyId()) + ->save($con) + ; $combinationAttributes = $event->getAttributeAvList(); @@ -412,6 +426,13 @@ class Product extends BaseAction implements EventSubscriberInterface } } + public function deleteProductCombination(ProductDeleteCombinationEvent $event) { + + if (null !== $pse = ProductSaleElementsQuery::create()->findPk($event->getProductSaleElementId())) { + $pse->delete(); + } + } + /** * {@inheritDoc} */ diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 6127b2160..0d7327e4c 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -190,7 +190,7 @@ Thelia\Controller\Admin\ProductController::loadGeneralAjaxTabAction - + Thelia\Controller\Admin\ProductController::loadRelatedAjaxTabAction @@ -278,6 +278,16 @@ Thelia\Controller\Admin\ProductController::deleteCombinationAction + + Thelia\Controller\Admin\ProductController::updateCombinationAction + + + + Thelia\Controller\Admin\ProductController::updateDefaultPriceAction + + + + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index 346f4b55f..fa505b5c3 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -42,6 +42,7 @@ use Thelia\Log\Tlog; use Symfony\Component\Routing\Router; use Thelia\Model\Admin; use Thelia\Core\Security\Token\CookieTokenProvider; +use Thelia\Model\CurrencyQuery; class BaseAdminController extends BaseController { @@ -258,7 +259,7 @@ class BaseAdminController extends BaseController // Return the new language if a change is required. if (null !== $edit_currency_id = $this->getRequest()->get('edit_currency_id', null)) { - if (null !== $edit_currency = LangQuery::create()->findOneById($edit_currency_id)) { + if (null !== $edit_currency = CurrencyQuery::create()->findOneById($edit_currency_id)) { return $edit_currency; } } diff --git a/core/lib/Thelia/Controller/Admin/ProductController.php b/core/lib/Thelia/Controller/Admin/ProductController.php index f46bda291..342af6b14 100644 --- a/core/lib/Thelia/Controller/Admin/ProductController.php +++ b/core/lib/Thelia/Controller/Admin/ProductController.php @@ -56,6 +56,7 @@ use Thelia\Model\ProductSaleElementsQuery; use Thelia\Model\AttributeCombination; use Thelia\Model\AttributeAv; use Thelia\Core\Event\ProductCreateCombinationEvent; +use Thelia\Core\Event\ProductDeleteCombinationEvent; /** * Manages products @@ -753,8 +754,8 @@ class ProductController extends AbstractCrudController $event = new ProductCreateCombinationEvent( $this->getExistingObject(), - $this->getRequest()->get('use_default_princing', 0), - $this->getRequest()->get('combination_attributes', array()) + $this->getRequest()->get('combination_attributes', array()), + $this->getCurrentEditionCurrency()->getId() ); try { @@ -764,8 +765,33 @@ class ProductController extends AbstractCrudController // Any error return $this->errorPage($ex); } -echo "done!"; -exit; + $this->redirectToEditionTemplate(); } + + + /** + * A a new combination to a product + */ + public function deleteCombinationAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.products.update")) return $response; + + $event = new ProductDeleteCombinationEvent( + $this->getExistingObject(), + $this->getRequest()->get('product_sale_element_id',0) + ); + + try { + $this->dispatch(TheliaEvents::PRODUCT_DELETE_COMBINATION, $event); + } + catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + + $this->redirectToEditionTemplate(); + } + } diff --git a/core/lib/Thelia/Core/Event/ProductCreateCombinationEvent.php b/core/lib/Thelia/Core/Event/ProductCreateCombinationEvent.php index 092dc7f82..322451199 100644 --- a/core/lib/Thelia/Core/Event/ProductCreateCombinationEvent.php +++ b/core/lib/Thelia/Core/Event/ProductCreateCombinationEvent.php @@ -22,31 +22,19 @@ /*************************************************************************************/ namespace Thelia\Core\Event; - use Thelia\Model\Product; + class ProductCreateCombinationEvent extends ProductEvent { - protected $use_default_pricing; protected $attribute_av_list; + protected $currency_id; - public function __construct(Product $product, $use_default_pricing, $attribute_av_list) + public function __construct(Product $product, $attribute_av_list, $currency_id) { parent::__construct($product); - $this->use_default_pricing = $use_default_pricing; $this->attribute_av_list = $attribute_av_list; - } - - public function getUseDefaultPricing() - { - return $this->use_default_pricing; - } - - public function setUseDefaultPricing($use_default_pricing) - { - $this->use_default_pricing = $use_default_pricing; - - return $this; + $this->currency_id = $currency_id; } public function getAttributeAvList() @@ -60,4 +48,17 @@ class ProductCreateCombinationEvent extends ProductEvent return $this; } + + public function getCurrencyId() + { + return $this->currency_id; + } + + public function setCurrencyId($currency_id) + { + $this->currency_id = $currency_id; + + return $this; + } + } diff --git a/core/lib/Thelia/Core/Event/ProductDeleteCombinationEvent.php b/core/lib/Thelia/Core/Event/ProductDeleteCombinationEvent.php new file mode 100644 index 000000000..3cc9a25ce --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductDeleteCombinationEvent.php @@ -0,0 +1,47 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; +use Thelia\Model\Product; + +class ProductDeleteCombinationEvent extends ProductEvent +{ + protected $product_sale_element_id; + + public function __construct(Product $product, $product_sale_element_id) + { + parent::__construct($product); + + $this->product_sale_element_id = $product_sale_element_id; + } + + public function getProductSaleElementId() + { + return $this->product_sale_element_id; + } + + public function setProductSaleElementId($product_sale_element_id) + { + $this->product_sale_element_id = $product_sale_element_id; + } +} diff --git a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php index bf8ccf512..e25164456 100755 --- a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php +++ b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php @@ -175,6 +175,7 @@ class ProductSaleElements extends BaseLoop ->set("QUANTITY" , $PSEValue->getQuantity()) ->set("IS_PROMO" , $PSEValue->getPromo() === 1 ? 1 : 0) ->set("IS_NEW" , $PSEValue->getNewness() === 1 ? 1 : 0) + ->set("IS_DEFAULT" , $PSEValue->getIsDefault() === 1 ? 1 : 0) ->set("WEIGHT" , $PSEValue->getWeight()) ->set("PRICE" , $price) ->set("PRICE_TAX" , $taxedPrice - $price) diff --git a/templates/admin/default/includes/product-details-tab.html b/templates/admin/default/includes/product-details-tab.html index 238b7d987..13e241e59 100644 --- a/templates/admin/default/includes/product-details-tab.html +++ b/templates/admin/default/includes/product-details-tab.html @@ -1,259 +1,290 @@
- {form name="thelia.admin.product.details.modification"} -
+ {loop name="product.sales.elements.test" type="product_sale_elements" product=$product_id currency=$edit_currency_id backend_context="1"} + {/loop} - {include - file = "includes/inner-form-toolbar.html" - hide_submit_buttons = false - show_currencies = true + {elseloop rel="product.sales.elements.test"} + {form name="thelia.admin.product.details.modification"} + - page_url = "{url path='/admin/products/update' product_id=$ID}" - close_url = "{url path='/admin/categories' category_id=$DEFAULT_CATEGORY}" - } + {include + file = "includes/inner-form-toolbar.html" + hide_submit_buttons = false + show_currencies = true - {* Be sure to get the product ID, even if the form could not be validated *} - + page_url = "{url path='/admin/products/update' product_id=$ID}" + close_url = "{url path='/admin/categories' category_id=$DEFAULT_CATEGORY}" + } - + {* Be sure to get the product ID, even if the form could not be validated *} + - {form_hidden_fields form=$form} + - {form_field form=$form field='id'} - - {/form_field} + {form_hidden_fields form=$form} - {form_field form=$form field='success_url'} - - {/form_field} + {form_field form=$form field='id'} + + {/form_field} - {loop type="currency" name="product-currency" id=$edit_currency_id backend_context="1"} + {form_field form=$form field='success_url'} + + {/form_field} - {$currency_symbol = $SYMBOL} - {$currency_name = $NAME} + {loop type="currency" name="product-currency" id=$edit_currency_id backend_context="1"} - {form_field form=$form field='currency'} - - {/form_field} - {/loop} + {$currency_symbol = $SYMBOL} + {$currency_name = $NAME} -

{intl l='Default pricing'}

+ {form_field form=$form field='currency'} + + {/form_field} + {/loop} -

{intl l="The default pricing is used with product that do not have any combinations. It is also used for product with combinations which share the same pricing information"}

+

{intl l='Default pricing'}

-
+

{intl l="The default pricing is used with product that do not have any combinations. It is also used for product with combinations which share the same pricing information"}

- {* -- Pricing ------------------------------------------------------- *} +
-
-
-

{intl l='Pricing'}

+ {* -- Pricing ------------------------------------------------------- *} -

+
+
+

{intl l='Pricing'}

- {form_field form=$form field='price'} -
- +

-
- - {$currency_symbol} -
-
- {/form_field} + {form_field form=$form field='price'} +
+ - {form_field form=$form field='tax_rule'} -
- -
- -
+
+ + {$currency_symbol} +
+
+ {/form_field} -
- {/form_field} + {form_field form=$form field='tax_rule'} +
+ +
+ +
- {form_field form=$form field='price_with_tax'} -
- -
- - {$currency_symbol} -
-
- {/form_field} +
+ {/form_field} - {module_include location='product_details_pricing_form'} -
+ {form_field form=$form field='price_with_tax'} +
+ +
+ + {$currency_symbol} +
+
+ {/form_field} + + {module_include location='product_details_pricing_form'} +
+
+ + + {* -- Promotion ------------------------------------------------- *} + +
+
+

{intl l='Promotion'}

+ + {form_field form=$form field='sale_price'} +
+ + +
+ + {$currency_symbol} +
+
+ {/form_field} + + {form_field form=$form field='onsale'} +
+
+ +
+
+ {/form_field} + + {form_field form=$form field='isnew'} +
+
+ +
+
+ {/form_field} + + {module_include location='product_details_promotion_form'} +
+
+ + + {* -- Shipping -------------------------------------------------- *} + +
+
+

{intl l='Shipping'}

+ + {form_field form=$form field='weight'} +
+ + +
+ + {intl l="Kg"} +
+
+ {/form_field} + + {module_include location='product_details_shipping_form'} + +

{intl l='Quantity'}

+ + {form_field form=$form field='quantity'} +
+ + +
+ +
+
+ {/form_field} + + {module_include location='product_details_quantity_form'} +
+
- - - {* -- Promotion ------------------------------------------------- *} - -
-
-

{intl l='Promotion'}

- - {form_field form=$form field='sale_price'} -
- - -
- - {$currency_symbol} -
-
- {/form_field} - - {form_field form=$form field='onsale'} -
-
- -
-
- {/form_field} - - {form_field form=$form field='isnew'} -
-
- -
-
- {/form_field} - - {module_include location='product_details_promotion_form'} -
-
- - - {* -- Shipping -------------------------------------------------- *} - -
-
-

{intl l='Shipping'}

- - {form_field form=$form field='weight'} -
- - -
- - {intl l="Kg"} -
-
- {/form_field} - - {module_include location='product_details_shipping_form'} - -

{intl l='Quantity'}

- - {form_field form=$form field='quantity'} -
- - -
- -
-
- {/form_field} - - {module_include location='product_details_quantity_form'} -
-
-
- - {/form} - + + {/form} + {/elseloop} {* -- Attribute combinations -------------------------------------------- *} - {module_include location='product_before_combinations'} -
-
- - + {module_include location='product_before_combinations'} - - - - - - - - - - - - - - +
- {intl l='Attribute Combinations'} + {ifloop rel="product.sales.elements"} +
- {module_include location='product_combinations_list_caption'} + {include + file = "includes/inner-form-toolbar.html" + hide_submit_buttons = false + show_currencies = true + page_url = "{url path='/admin/products/update' product_id=$ID}" + close_url = "{url path='/admin/categories' category_id=$DEFAULT_CATEGORY}" + } - {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.products.update"} - - - - {/loop} -
{intl l='Attributes'}{intl l='Quantity'}{intl l='Price
w/o taxes (%currency)' currency=$currency_symbol}
{intl l='Price
w/ taxes (%currency)' currency=$currency_symbol}
{intl l='Weight (Kg)'}{intl l='Is new'}{intl l='On sale'}{intl l='Sale price
w/o taxes (%currency)' currency=$currency_symbol}
{intl l='Sale price
w/ taxes (%currency)' currency=$currency_symbol}
{intl l='Actions'}
+ - {loop name="product.sales.elements" type="product_sale_elements" product=$product_id currency=$edit_currency_id} - - + {module_include location='product_combinations_list_caption'} - - - - + {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.products.update"} + + + + {/loop} + - + + + + + + + + + + + + + + + - + + {loop name="product.sales.elements" type="product_sale_elements" product=$product_id currency=$edit_currency_id backend_context="1"} + + - - + + + + + + + + + + + + + + + + + {/loop} + +
+ {intl l='Attribute Combinations'} -
- {loop name="product.sales.elements.combinations" type="attribute_combination" product_sale_elements=$ID} - {$ATTRIBUTE_TITLE}  - {/loop} - -
- -
-
{intl l='Attributes'}{intl l='Quantity'}{intl l='Price
w/o taxes (%currency)' currency=$currency_symbol}
{intl l='Price
w/ taxes (%currency)' currency=$currency_symbol}
{intl l='Weight (Kg)'}{intl l='Default'}{intl l='Is new'}{intl l='On sale'}{intl l='Sale price
w/o taxes (%currency)' currency=$currency_symbol}
{intl l='Sale price
w/ taxes (%currency)' currency=$currency_symbol}
{intl l='Actions'}
-
- -
-
+ {loop name="product.sales.elements.combinations" type="attribute_combination" product_sale_elements=$ID backend_context="1"} + {$ATTRIBUTE_TITLE}  + {/loop} + + + + + + + + +
+ + {module_include location='product_after_combinations'} + + + {/ifloop} + + {elseloop rel="product.sales.elements"} +

{intl l='Attribute Combinations'}

+ +
+ {intl + l='This product has no combination. The default price is used. Click here to create a new combination' + url='#combination_creation_dialog' + } +
+ {/elseloop} + + {module_include location='product_after_combinations'} - - - - - {/loop} - -
- {module_include location='product_after_combinations'} -
- {* -- Adding a new combination ------------------------------------------------- *} {* Capture the dialog body, to pass it to the generic dialog *} @@ -314,17 +345,6 @@
- {form_field form=$form field='isnew'} -
-
- -
-
- {/form_field} - {/capture} {include @@ -351,7 +371,7 @@ - + {module_include location='category_delete_form'} @@ -364,6 +384,6 @@ dialog_title = {intl l="Delete a combunation"} dialog_message = {intl l="Do you really want to delete this combination ?"} - form_action = {url path='/admin/product/combination/delete'} - form_content = {$smarty.capture.combination_delete_dialog nofilter} + form_action = {url path='/admin/product/combination/delete'} + form_content = {$smarty.capture.combination_delete_dialog nofilter} }