diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index cdaa3424a..036502c68 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -29,6 +29,7 @@ use Thelia\Constraint\ConstraintFactory; use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\TheliaEvents; +use Thelia\Core\HttpFoundation\Request; use Thelia\Coupon\CouponFactory; use Thelia\Coupon\CouponManager; use Thelia\Coupon\Type\CouponInterface; @@ -101,10 +102,24 @@ class Coupon extends BaseAction implements EventSubscriberInterface /** @var CouponInterface $coupon */ $coupon = $couponFactory->buildCouponFromCode($event->getCode()); - $isValid = $coupon->isMatching(); if ($isValid) { + /** @var Request $request */ + $request = $this->container->get('request'); + $consumedCoupons = $request->getSession()->getConsumedCoupons(); + + if (!isset($consumedCoupons) || !$consumedCoupons) { + $consumedCoupons = array(); + } + + // Prevent accumulation of the same Coupon on a Checkout + $consumedCoupons[$event->getCode()] = $event->getCode(); + + $request->getSession()->setConsumedCoupons($consumedCoupons); + $totalDiscount = $couponManager->getDiscount(); + + // @todo modify Cart total discount } $event->setIsValid($isValid); diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index ddb32cf65..cc92905b1 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -230,6 +230,8 @@ + + diff --git a/core/lib/Thelia/Constraint/ConstraintValidator.php b/core/lib/Thelia/Constraint/ConstraintValidator.php index edacee317..325faed22 100644 --- a/core/lib/Thelia/Constraint/ConstraintValidator.php +++ b/core/lib/Thelia/Constraint/ConstraintValidator.php @@ -52,7 +52,7 @@ class ConstraintValidator * * @return bool */ - public function test(CouponRuleCollection $rules) + public function isMatching(CouponRuleCollection $rules) { $isMatching = true; /** @var CouponRuleInterface $rule */ diff --git a/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php index 9f1bf23a0..30bc86ad6 100644 --- a/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForTotalAmountManager.php @@ -25,7 +25,6 @@ namespace Thelia\Constraint\Rule; use Symfony\Component\Intl\Exception\NotImplementedException; use Symfony\Component\Translation\Translator; -use Thelia\Constraint\ConstraintValidator; use Thelia\Coupon\CouponAdapterInterface; use Thelia\Constraint\Validator\PriceParam; use Thelia\Constraint\Validator\RuleValidator; @@ -168,13 +167,12 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract return false; } - $constrainValidator = new ConstraintValidator(); - $constraint1 =$constrainValidator->variableOpComparison( + $constraint1 = $this->constraintValidator->variableOpComparison( $this->adapter->getCartTotalPrice(), $this->operators[self::INPUT1], $this->values[self::INPUT1] ); - $constraint2 =$constrainValidator->variableOpComparison( + $constraint2 = $this->constraintValidator->variableOpComparison( $this->adapter->getCheckoutCurrency(), $this->operators[self::INPUT2], $this->values[self::INPUT2] diff --git a/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php b/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php index 26c7a8aeb..572d39b1d 100644 --- a/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php +++ b/core/lib/Thelia/Constraint/Rule/AvailableForXArticlesManager.php @@ -126,8 +126,7 @@ class AvailableForXArticlesManager extends CouponRuleAbstract */ public function isMatching() { - $constrainValidator = new ConstraintValidator(); - $constraint1 =$constrainValidator->variableOpComparison( + $constraint1 = $this->constraintValidator->variableOpComparison( $this->adapter->getNbArticlesInCart(), $this->operators[self::INPUT1], $this->values[self::INPUT1] diff --git a/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php index 1cab6c48b..942e48d1f 100644 --- a/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php +++ b/core/lib/Thelia/Constraint/Rule/CouponRuleAbstract.php @@ -82,6 +82,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface { $this->adapter = $adapter; $this->translator = $adapter->getTranslator(); + $this->constraintValidator = $adapter->getConstraintValidator(); } // /** diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 9718e655d..8f3d3381b 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -495,7 +495,7 @@ class CouponController extends BaseAdminController $couponConsumeEvent ); - var_dump('test'); + var_dump('test', $couponConsumeEvent->getCode(), $couponConsumeEvent->getDiscount(), $couponConsumeEvent->getIsValid()); exit(); } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index f2eaadc4d..bce8e712a 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -220,4 +220,29 @@ class Session extends BaseSession { return $this->get("thelia.delivery_id"); } + + + /** + * Set consumed coupons by the Customer + * + * @param array $couponsCode An array of Coupon code + * + * @return $this + */ + public function setConsumedCoupons(array $couponsCode) + { + $this->set('thelia.consumed_coupons', $couponsCode); + + return $this; + } + + /** + * Get Customer consumed coupons + * + * @return array $couponsCode An array of Coupon code + */ + public function getConsumedCoupons() + { + return $this->get('thelia.consumed_coupons'); + } } diff --git a/core/lib/Thelia/Coupon/CouponAdapterInterface.php b/core/lib/Thelia/Coupon/CouponAdapterInterface.php index 134d061be..b2c168186 100644 --- a/core/lib/Thelia/Coupon/CouponAdapterInterface.php +++ b/core/lib/Thelia/Coupon/CouponAdapterInterface.php @@ -155,4 +155,18 @@ interface CouponAdapterInterface */ public function getMainCurrency(); + /** + * Return request + * + * @return Request + */ + public function getRequest(); + + /** + * Return Constraint Validator + * + * @return ConstraintValidator + */ + public function getConstraintValidator(); + } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/CouponBaseAdapter.php b/core/lib/Thelia/Coupon/CouponBaseAdapter.php index 5d308509e..f9fae8651 100644 --- a/core/lib/Thelia/Coupon/CouponBaseAdapter.php +++ b/core/lib/Thelia/Coupon/CouponBaseAdapter.php @@ -27,6 +27,7 @@ use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\TranslatorInterface; +use Thelia\Constraint\ConstraintValidator; use Thelia\Core\HttpFoundation\Request; use Thelia\Core\Security\SecurityContext; use Thelia\Coupon\Type\CouponInterface; @@ -155,6 +156,11 @@ class CouponBaseAdapter implements CouponAdapterInterface */ public function getCurrentCoupons() { + // @todo implement +// $consumedCoupons = $this->getRequest()->getSession()->getConsumedCoupons(); + // @todo convert coupon code to coupon Interface + + $couponFactory = $this->container->get('thelia.coupon.factory'); // @todo get from cart @@ -250,4 +256,14 @@ class CouponBaseAdapter implements CouponAdapterInterface { return $this->container->get('request'); } + + /** + * Return Constraint Validator + * + * @return ConstraintValidator + */ + public function getConstraintValidator() + { + return $this->container->get('thelia.constraint.validator'); + } } diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index 93cdd21fe..d8575c3e6 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -183,6 +183,7 @@ class CouponManager $discount = 0.00; /** @var CouponInterface $coupon */ foreach ($coupons as $coupon) { + // @todo modify Cart with discount for each cart item $discount += $coupon->getDiscount($this->adapter); } diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 647635024..45e1427f1 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -26,6 +26,7 @@ namespace Thelia\Coupon\Type; use Symfony\Component\Intl\Exception\NotImplementedException; use Thelia\Constraint\ConstraintManager; use Thelia\Constraint\ConstraintValidator; +use Thelia\Core\Translation\Translator; use Thelia\Coupon\CouponAdapterInterface; use Thelia\Coupon\CouponRuleCollection; use Thelia\Coupon\RuleOrganizerInterface; @@ -44,9 +45,6 @@ use Thelia\Exception\InvalidRuleException; */ abstract class CouponAbstract implements CouponInterface { - /** @var string Service Id */ - protected $serviceId = null; - /** @var CouponAdapterInterface Provide necessary value from Thelia */ protected $adapter = null; @@ -62,9 +60,19 @@ abstract class CouponAbstract implements CouponInterface /** @var ConstraintValidator Constraint validator */ protected $constraintValidator = null; + + + /** @var string Service Id */ + protected $serviceId = null; + + /** @var float Amount that will be removed from the Checkout (Coupon Effect) */ + protected $amount = 0; + /** @var string Coupon code (ex: XMAS) */ protected $code = null; + + /** @var string Coupon title (ex: Coupon for XMAS) */ protected $title = null; @@ -74,6 +82,8 @@ abstract class CouponAbstract implements CouponInterface /** @var string Coupon description */ protected $description = null; + + /** @var bool if Coupon is enabled */ protected $isEnabled = false; @@ -86,9 +96,6 @@ abstract class CouponAbstract implements CouponInterface /** @var bool if Coupon is removing postage */ protected $isRemovingPostage = false; - /** @var float Amount that will be removed from the Checkout (Coupon Effect) */ - protected $amount = 0; - /** @var int Max time a Coupon can be used (-1 = unlimited) */ protected $maxUsage = -1; @@ -105,6 +112,7 @@ abstract class CouponAbstract implements CouponInterface { $this->adapter = $adapter; $this->translator = $adapter->getTranslator(); + $this->constraintValidator = $adapter->getConstraintValidator(); } /** @@ -220,17 +228,6 @@ abstract class CouponAbstract implements CouponInterface return $this; } - /** - * Check if the current Coupon is matching its conditions (Rules) - * Thelia variables are given by the CouponAdapterInterface - * - * @return bool - */ - public function isMatching() - { - return $this->constraintValidator->test($this->rules); - } - /** * Return Coupon expiration date * @@ -302,4 +299,16 @@ abstract class CouponAbstract implements CouponInterface } + /** + * Check if the current Coupon is matching its conditions (Rules) + * Thelia variables are given by the CouponAdapterInterface + * + * @return bool + */ + public function isMatching() + { + return $this->constraintValidator->isMatching($this->rules); + } + + } diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index f0426298f..be76c1878 100644 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -39,6 +39,27 @@ use Thelia\Coupon\CouponRuleCollection; */ interface CouponInterface { + /** + * Get I18n name + * + * @return string + */ + public function getName(); + + /** + * Get I18n tooltip + * + * @return string + */ + public function getToolTip(); + + /** + * Get Coupon Manager service Id + * + * @return string + */ + public function getServiceId(); + /** * Set Coupon * @@ -114,18 +135,7 @@ interface CouponInterface */ public function isRemovingPostage(); - /** - * Return effects generated by the coupon - * A positive value - * - * Effects could also affect something else than the final Checkout price - * CouponAdapter $adapter could be use to directly pass a Session value - * some would wish to modify - * Hence affecting a wide variety of Thelia elements - * - * @return float Amount removed from the Total Checkout - */ - public function getDiscount(); + /** * Return condition to validate the Coupon or not @@ -134,14 +144,6 @@ interface CouponInterface */ public function getRules(); - /** - * Check if the current Coupon is matching its conditions (Rules) - * Thelia variables are given by the CouponAdapterInterface - * - * @return bool - */ - public function isMatching(); - /** * Replace the existing Rules by those given in parameter * If one Rule is badly implemented, no Rule will be added @@ -191,25 +193,26 @@ interface CouponInterface */ public function isExpired(); - /** - * Get I18n name - * - * @return string - */ - public function getName(); /** - * Get I18n tooltip + * Return effects generated by the coupon + * A positive value * - * @return string + * Effects could also affect something else than the final Checkout price + * CouponAdapter $adapter could be use to directly pass a Session value + * some would wish to modify + * Hence affecting a wide variety of Thelia elements + * + * @return float Amount removed from the Total Checkout */ - public function getToolTip(); + public function getDiscount(); /** - * Get Coupon Manager service Id + * Check if the current Coupon is matching its conditions (Rules) + * Thelia variables are given by the CouponAdapterInterface * - * @return string + * @return bool */ - public function getServiceId(); + public function isMatching(); } diff --git a/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php b/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php index 01e9dde69..17b6b49e1 100644 --- a/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php +++ b/core/lib/Thelia/Tests/Constraint/ConstraintValidatorTest.php @@ -79,7 +79,7 @@ class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase $rules = new CouponRuleCollection(); $rules->add($rule1); - $isValid = $ConstraintValidator->test($rules); + $isValid = $ConstraintValidator->isMatching($rules); $expected = true; $actual =$isValid; @@ -113,7 +113,7 @@ class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase $rules = new CouponRuleCollection(); $rules->add($rule1); - $isValid = $ConstraintValidator->test($rules); + $isValid = $ConstraintValidator->isMatching($rules); $expected = false; $actual =$isValid; @@ -160,7 +160,7 @@ class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase $rules->add($rule1); $rules->add($rule2); - $isValid = $ConstraintValidator->test($rules); + $isValid = $ConstraintValidator->isMatching($rules); $expected = true; $actual =$isValid; @@ -207,7 +207,7 @@ class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase $rules->add($rule1); $rules->add($rule2); - $isValid = $ConstraintValidator->test($rules); + $isValid = $ConstraintValidator->isMatching($rules); $expected = false; $actual =$isValid;