From 406b06e09db53a378702e7b313b4ba182321b604 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 28 Apr 2014 16:54:19 +0200 Subject: [PATCH] use customer permanent discount --- core/lib/Thelia/Action/Cart.php | 17 ++++++++++-- .../lib/Thelia/Core/Template/Loop/Product.php | 19 ++++++++++++- .../Template/Loop/ProductSaleElements.php | 21 ++++++++++++--- core/lib/Thelia/Model/ProductSaleElements.php | 27 ++++++++++++++----- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index a9cdb66dc..56e233fb3 100644 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -48,6 +48,12 @@ class Cart extends BaseAction implements EventSubscriberInterface $append = $event->getAppend(); $quantity = $event->getQuantity(); $currency = $cart->getCurrency(); + $customer = $cart->getCustomer(); + $discount = 0; + + if(null !== $customer && $customer->getDiscount() > 0) { + $discount = $customer->getDiscount(); + } $productSaleElementsId = $event->getProductSaleElementsId(); $productId = $event->getProduct(); @@ -60,7 +66,7 @@ class Cart extends BaseAction implements EventSubscriberInterface ->findPk($productSaleElementsId); if (null !== $productSaleElements) { - $productPrices = $productSaleElements->getPricesByCurrency($currency); + $productPrices = $productSaleElements->getPricesByCurrency($currency, $discount); $event->setCartItem( $this->doAddItem($event->getDispatcher(), $cart, $productId, $productSaleElements, $quantity, $productPrices) ); @@ -151,11 +157,18 @@ class Cart extends BaseAction implements EventSubscriberInterface public function updateCartPrices(\Thelia\Model\Cart $cart, Currency $currency) { + $customer = $cart->getCustomer(); + $discount = 0; + + if(null !== $customer && $customer->getDiscount() > 0) { + $discount = $customer->getDiscount(); + } + // cart item foreach ($cart->getCartItems() as $cartItem) { $productSaleElements = $cartItem->getProductSaleElements(); - $productPrice = $productSaleElements->getPricesByCurrency($currency); + $productPrice = $productSaleElements->getPricesByCurrency($currency, $discount); $cartItem ->setPrice($productPrice->getPrice()) diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index 8b500faac..70348626b 100644 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -457,12 +457,19 @@ class Product extends BaseI18nLoop implements PropelSearchLoopInterface, SearchL } $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); + /** @var \Thelia\Core\Security\SecurityContext $securityContext */ + $securityContext = $this->container->get('thelia.securityContext'); foreach ($loopResult->getResultDataCollection() as $product) { $loopResultRow = new LoopResultRow($product); $price = $product->getVirtualColumn('price'); + + if ($securityContext->hasCustomerUser() && $securityContext->getCustomerUser()->getDiscount() > 0) { + $price = $price * (1-($securityContext->getCustomerUser()->getDiscount()/100)); + } + try { $taxedPrice = $product->getTaxedPrice( $taxCountry, @@ -472,6 +479,10 @@ class Product extends BaseI18nLoop implements PropelSearchLoopInterface, SearchL $taxedPrice = null; } $promoPrice = $product->getVirtualColumn('promo_price'); + + if ($securityContext->hasCustomerUser() && $securityContext->getCustomerUser()->getDiscount() > 0) { + $promoPrice = $promoPrice * (1-($securityContext->getCustomerUser()->getDiscount()/100)); + } try { $taxedPromoPrice = $product->getTaxedPromoPrice( $taxCountry, @@ -938,6 +949,8 @@ class Product extends BaseI18nLoop implements PropelSearchLoopInterface, SearchL $loopResult = new LoopResult($results); $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); + /** @var \Thelia\Core\Security\SecurityContext $securityContext */ + $securityContext = $this->container->get('thelia.securityContext'); foreach ($loopResult->getResultDataCollection() as $product) { @@ -945,10 +958,14 @@ class Product extends BaseI18nLoop implements PropelSearchLoopInterface, SearchL $price = $product->getRealLowestPrice(); + if ($securityContext->hasCustomerUser() && $securityContext->getCustomerUser()->getDiscount() > 0) { + $price = $price * (1-($securityContext->getCustomerUser()->getDiscount()/100)); + } + try { $taxedPrice = $product->getTaxedPrice( $taxCountry, - $product->getRealLowestPrice() + $price ); } catch (TaxEngineException $e) { $taxedPrice = null; diff --git a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php index bf1d8f599..225a4e9b8 100644 --- a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php +++ b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php @@ -135,27 +135,40 @@ class ProductSaleElements extends BaseLoop implements PropelSearchLoopInterface public function parseResults(LoopResult $loopResult) { $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); + /** @var \Thelia\Core\Security\SecurityContext $securityContext */ + $securityContext = $this->container->get('thelia.securityContext'); + $discount = 0; + + if ($securityContext->hasCustomerUser() && $securityContext->getCustomerUser()->getDiscount() > 0) { + $discount = $securityContext->getCustomerUser()->getDiscount(); + } foreach ($loopResult->getResultDataCollection() as $PSEValue) { $loopResultRow = new LoopResultRow($PSEValue); - $price = $PSEValue->getPrice(); + $price = $PSEValue->getPrice('price_PRICE', $discount); try { $taxedPrice = $PSEValue->getTaxedPrice( - $taxCountry + $taxCountry, + 'price_PRICE', + $discount ); } catch (TaxEngineException $e) { $taxedPrice = null; } - $promoPrice = $PSEValue->getPromoPrice(); + + $promoPrice = $PSEValue->getPromoPrice('price_PROMO_PRICE', $discount); try { $taxedPromoPrice = $PSEValue->getTaxedPromoPrice( - $taxCountry + $taxCountry, + 'price_PROMO_PRICE', + $discount ); } catch (TaxEngineException $e) { $taxedPromoPrice = null; } + $loopResultRow ->set("ID" , $PSEValue->getId()) ->set("QUANTITY" , $PSEValue->getQuantity()) diff --git a/core/lib/Thelia/Model/ProductSaleElements.php b/core/lib/Thelia/Model/ProductSaleElements.php index f2b960343..05398c041 100644 --- a/core/lib/Thelia/Model/ProductSaleElements.php +++ b/core/lib/Thelia/Model/ProductSaleElements.php @@ -8,10 +8,14 @@ use Thelia\TaxEngine\Calculator; class ProductSaleElements extends BaseProductSaleElements { - public function getPrice($virtualColumnName = 'price_PRICE') + public function getPrice($virtualColumnName = 'price_PRICE', $discount = 0) { try { $amount = $this->getVirtualColumn($virtualColumnName); + + if ($discount > 0) { + $amount = $amount * (1-($discount/100)); + } } catch (PropelException $e) { throw new PropelException("Virtual column `$virtualColumnName` does not exist in ProductSaleElements::getPrice"); } @@ -19,10 +23,14 @@ class ProductSaleElements extends BaseProductSaleElements return $amount; } - public function getPromoPrice($virtualColumnName = 'price_PROMO_PRICE') + public function getPromoPrice($virtualColumnName = 'price_PROMO_PRICE', $discount = 0) { try { $amount = $this->getVirtualColumn($virtualColumnName); + + if ($discount > 0) { + $amount = $amount * (1-($discount/100)); + } } catch (PropelException $e) { throw new PropelException("Virtual column `$virtualColumnName` does not exist in ProductSaleElements::getPromoPrice"); } @@ -30,18 +38,18 @@ class ProductSaleElements extends BaseProductSaleElements return $amount; } - public function getTaxedPrice(Country $country) + public function getTaxedPrice(Country $country , $virtualColumnName = 'price_PRICE', $discount = 0) { $taxCalculator = new Calculator(); - return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPrice()), 2); + return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPrice($virtualColumnName, $discount)), 2); } - public function getTaxedPromoPrice(Country $country) + public function getTaxedPromoPrice(Country $country, $virtualColumnName = 'price_PROMO_PRICE', $discount = 0) { $taxCalculator = new Calculator(); - return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()), 2); + return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice($virtualColumnName, $discount)), 2); } /** @@ -56,7 +64,7 @@ class ProductSaleElements extends BaseProductSaleElements * @return ProductPriceTools * @throws \RuntimeException */ - public function getPricesByCurrency($currency) + public function getPricesByCurrency(Currency $currency, $discount = 0) { $defaultCurrency = Currency::getDefaultCurrency(); @@ -85,6 +93,11 @@ class ProductSaleElements extends BaseProductSaleElements $promoPrice = $productPrice->getPromoPrice(); } + if ($discount > 0) { + $price = $price * (1-($discount/100)); + $promoPrice = $promoPrice * (1-($discount/100)); + } + $productPriceTools = new ProductPriceTools($price, $promoPrice); return $productPriceTools;