diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index da8862d54..9de27bf58 100644 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -148,7 +148,7 @@ class Coupon extends BaseAction implements EventSubscriberInterface $this->request->getSession()->setConsumedCoupons($consumedCoupons); } - + $totalDiscount = $this->couponManager->getDiscount(); $this->request diff --git a/core/lib/Thelia/Coupon/Type/AbstractRemove.php b/core/lib/Thelia/Coupon/Type/AbstractRemove.php new file mode 100644 index 000000000..f96a660e9 --- /dev/null +++ b/core/lib/Thelia/Coupon/Type/AbstractRemove.php @@ -0,0 +1,129 @@ + + */ +abstract class AbstractRemove extends CouponAbstract implements AmountAndPercentageCouponInterface +{ + /** + * Set the value of specific coupon fields. + * + * @param Array $effects the Coupon effects params + */ + public abstract function setFieldsValue($effects); + + /** + * Get the discount for a specific cart item. + * + * @param CartItem $cartItem the cart item + * @return float the discount value + */ + public abstract function getCartItemDiscount($cartItem); + + /** + * @inheritdoc + */ + public function set( + FacadeInterface $facade, + $code, + $title, + $shortDescription, + $description, + array $effects, + $isCumulative, + $isRemovingPostage, + $isAvailableOnSpecialOffers, + $isEnabled, + $maxUsage, + \DateTime $expirationDate, + $freeShippingForCountries, + $freeShippingForModules, + $perCustomerUsageCount + ) + { + parent::set( + $facade, $code, $title, $shortDescription, $description, $effects, + $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate, + $freeShippingForCountries, + $freeShippingForModules, + $perCustomerUsageCount + ); + + $this->setFieldsValue($effects); + + return $this; + } + /** + * @inheritdoc + */ + public function exec() + { + // This coupon subtracts the specified amount from the order total + // for each product of the selected categories. + $discount = 0; + + $cartItems = $this->facade->getCart()->getCartItems(); + + /** @var CartItem $cartItem */ + foreach ($cartItems as $cartItem) { + if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) { + $categories = $cartItem->getProduct()->getCategories(); + + /** @var Category $category */ + foreach ($categories as $category) { + + if (in_array($category->getId(), $this->category_list)) { + $discount += $this->getCartItemDiscount($cartItem); + + break; + } + } + } + } + + return $discount; + } + + /** + * @inheritdoc + */ + public function drawBaseBackOfficeInputs($templateName, $otherFields) + { + return $this->facade->getParser()->render($templateName, $otherFields); + } + + /** + * @inheritdoc + */ + public function getBaseFieldList($otherFields) + { + return array_merge($otherFields); + } + + /** + * @inheritdoc + */ + public function checkBaseCouponFieldValue($fieldName, $fieldValue) + { + return $fieldValue; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnAttributeValues.php b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnAttributeValues.php index eee1db95c..c0c6f3844 100644 --- a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnAttributeValues.php +++ b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnAttributeValues.php @@ -23,7 +23,7 @@ use Thelia\Model\CartItem; * @package Coupon * @author Franck Allimant */ -abstract class AbstractRemoveOnAttributeValues extends CouponAbstract +abstract class AbstractRemoveOnAttributeValues extends CouponAbstract implements AmountAndPercentageCouponInterface { const ATTRIBUTES_AV_LIST = 'attribute_avs'; const ATTRIBUTE = 'attribute_id'; @@ -35,7 +35,7 @@ abstract class AbstractRemoveOnAttributeValues extends CouponAbstract * Set the value of specific coupon fields. * @param Array $effects the Coupon effects params */ - protected abstract function setFieldsValue($effects); + public abstract function setFieldsValue($effects); /** * Get the discount for a specific cart item. @@ -43,7 +43,7 @@ abstract class AbstractRemoveOnAttributeValues extends CouponAbstract * @param CartItem $cartItem the cart item * @return float the discount value */ - protected abstract function getCartItemDiscount($cartItem); + public abstract function getCartItemDiscount($cartItem); /** * @inheritdoc @@ -122,7 +122,13 @@ abstract class AbstractRemoveOnAttributeValues extends CouponAbstract } /** - * @inheritdoc + * Renders the template which implements coupon specific user-input, + * using the provided template file, and a list of specific input fields. + * + * @param string $templateName the path to the template + * @param array $otherFields the list of additional fields fields + * + * @return string the rendered template. */ public function drawBaseBackOfficeInputs($templateName, $otherFields) { @@ -141,7 +147,7 @@ abstract class AbstractRemoveOnAttributeValues extends CouponAbstract /** * @inheritdoc */ - protected function getBaseFieldList($otherFields) + public function getBaseFieldList($otherFields) { return array_merge($otherFields, [self::ATTRIBUTE, self::ATTRIBUTES_AV_LIST]); } @@ -149,7 +155,7 @@ abstract class AbstractRemoveOnAttributeValues extends CouponAbstract /** * @inheritdoc */ - protected function checkBaseCouponFieldValue($fieldName, $fieldValue) + public function checkBaseCouponFieldValue($fieldName, $fieldValue) { if ($fieldName === self::ATTRIBUTE) { if (empty($fieldValue)) { diff --git a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnCategories.php b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnCategories.php index 6d6622126..174c6f7c3 100644 --- a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnCategories.php +++ b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnCategories.php @@ -22,7 +22,7 @@ use Thelia\Model\CartItem; * @package Coupon * @author Franck Allimant */ -abstract class AbstractRemoveOnCategories extends CouponAbstract +abstract class AbstractRemoveOnCategories extends CouponAbstract implements AmountAndPercentageCouponInterface { const CATEGORIES_LIST = 'categories'; @@ -33,7 +33,7 @@ abstract class AbstractRemoveOnCategories extends CouponAbstract * * @param Array $effects the Coupon effects params */ - protected abstract function setFieldsValue($effects); + public abstract function setFieldsValue($effects); /** * Get the discount for a specific cart item. @@ -41,7 +41,7 @@ abstract class AbstractRemoveOnCategories extends CouponAbstract * @param CartItem $cartItem the cart item * @return float the discount value */ - protected abstract function getCartItemDiscount($cartItem); + public abstract function getCartItemDiscount($cartItem); /** * @inheritdoc @@ -125,7 +125,7 @@ abstract class AbstractRemoveOnCategories extends CouponAbstract /** * @inheritdoc */ - protected function getBaseFieldList($otherFields) + public function getBaseFieldList($otherFields) { return array_merge($otherFields, [self::CATEGORIES_LIST]); } @@ -133,7 +133,7 @@ abstract class AbstractRemoveOnCategories extends CouponAbstract /** * @inheritdoc */ - protected function checkBaseCouponFieldValue($fieldName, $fieldValue) + public function checkBaseCouponFieldValue($fieldName, $fieldValue) { if ($fieldName === self::CATEGORIES_LIST) { if (empty($fieldValue)) { diff --git a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnProducts.php b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnProducts.php index 2bb5ecea8..c06df1f29 100644 --- a/core/lib/Thelia/Coupon/Type/AbstractRemoveOnProducts.php +++ b/core/lib/Thelia/Coupon/Type/AbstractRemoveOnProducts.php @@ -22,7 +22,7 @@ use Thelia\Model\CartItem; * @package Coupon * @author Franck Allimant */ -abstract class AbstractRemoveOnProducts extends CouponAbstract +abstract class AbstractRemoveOnProducts extends CouponAbstract implements AmountAndPercentageCouponInterface { const CATEGORY_ID = 'category_id'; const PRODUCTS_LIST = 'products'; @@ -35,7 +35,7 @@ abstract class AbstractRemoveOnProducts extends CouponAbstract * * @param Array $effects the Coupon effects params */ - protected abstract function setFieldsValue($effects); + public abstract function setFieldsValue($effects); /** * Get the discount for a specific cart item. @@ -43,7 +43,7 @@ abstract class AbstractRemoveOnProducts extends CouponAbstract * @param CartItem $cartItem the cart item * @return float the discount value */ - protected abstract function getCartItemDiscount($cartItem); + public abstract function getCartItemDiscount($cartItem); /** * @inheritdoc @@ -129,7 +129,7 @@ abstract class AbstractRemoveOnProducts extends CouponAbstract /** * @inheritdoc */ - protected function getBaseFieldList($otherFields) + public function getBaseFieldList($otherFields) { return array_merge($otherFields, [self::CATEGORY_ID, self::PRODUCTS_LIST]); } @@ -137,7 +137,7 @@ abstract class AbstractRemoveOnProducts extends CouponAbstract /** * @inheritdoc */ - protected function checkBaseCouponFieldValue($fieldName, $fieldValue) + public function checkBaseCouponFieldValue($fieldName, $fieldValue) { if ($fieldName === self::CATEGORY_ID) { if (empty($fieldValue)) { diff --git a/core/lib/Thelia/Coupon/Type/AmountAndPercentageCouponInterface.php b/core/lib/Thelia/Coupon/Type/AmountAndPercentageCouponInterface.php new file mode 100644 index 000000000..7bc28857f --- /dev/null +++ b/core/lib/Thelia/Coupon/Type/AmountAndPercentageCouponInterface.php @@ -0,0 +1,64 @@ + + * + */ +interface AmountAndPercentageCouponInterface +{ + + /** + * Set the value of specific coupon fields. + * @param Array $effects the Coupon effects params + */ + public function setFieldsValue($effects); + + /** + * Get the discount for a specific cart item. + * + * @param CartItem $cartItem the cart item + * @return float the discount value + */ + public function getCartItemDiscount($cartItem); + + + /** + * Renders the template which implements coupon specific user-input, + * using the provided template file, and a list of specific input fields. + * + * @param string $templateName the path to the template + * @param array $otherFields the list of additional fields fields + * + * @return string the rendered template. + */ + public function drawBaseBackOfficeInputs($templateName, $otherFields); + + /** + * @inheritdoc + */ + public function getBaseFieldList($otherFields); + + /** + * + */ + public function checkBaseCouponFieldValue($fieldName, $fieldValue); +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/AmountCouponTrait.php b/core/lib/Thelia/Coupon/Type/AmountCouponTrait.php new file mode 100644 index 000000000..e743f7b4d --- /dev/null +++ b/core/lib/Thelia/Coupon/Type/AmountCouponTrait.php @@ -0,0 +1,91 @@ + + * @package Thelia\Coupon\Type + */ +Trait AmountCouponTrait { + + // The amount is already defined in CouponAbstract, and should not be redefined here. + // protected $amount = 0; + + /** + * Should return the amount field name, defined in the parent class. + * + * @return string the percentage field name + */ + protected abstract function getAmountFieldName(); + + /** + * @inheritdoc + */ + protected function setFieldsValue($effects) { + $this->amount = $effects[$this->getAmountFieldName()]; + } + + /** + * @inheritdoc + */ + public function getCartItemDiscount($cartItem) { + return $cartItem->getQuantity() * $this->amount; + } + + /** + * @inheritdoc + */ + public function callDrawBackOfficeInputs($templateName) + { + return $this->drawBaseBackOfficeInputs($templateName, [ + 'amount_field_name' => $this->makeCouponFieldName($this->getAmountFieldName()), + 'amount_value' => $this->amount + ]); + } + + /** + * @inheritdoc + */ + protected function getFieldList() + { + return $this->getBaseFieldList([$this->getAmountFieldName()]); + } + + /** + * @inheritdoc + */ + protected function checkCouponFieldValue($fieldName, $fieldValue) + { + $this->checkBaseCouponFieldValue($fieldName, $fieldValue); + + if ($fieldName === $this->getAmountFieldName()) { + + if (floatval($fieldValue) < 0) { + throw new \InvalidArgumentException( + Translator::getInstance()->trans( + 'Value %val for Discount Amount is invalid. Please enter a positive value.', + [ '%val' => $fieldValue] + ) + ); + } + } + + return $fieldValue; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 6d57b0d38..71b986fcb 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -499,4 +499,4 @@ abstract class CouponAbstract implements CouponInterface return $effects; } -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/PercentageCouponTrait.php b/core/lib/Thelia/Coupon/Type/PercentageCouponTrait.php new file mode 100644 index 000000000..0b05d126b --- /dev/null +++ b/core/lib/Thelia/Coupon/Type/PercentageCouponTrait.php @@ -0,0 +1,90 @@ + + * @package Thelia\Coupon\Type + */ +Trait PercentageCouponTrait { + + public $percentage = 0; + + /** + * Should return the percentage field name, defined in the parent class. + * + * @return string the percentage field name + */ + protected abstract function getPercentageFieldName(); + + /** + * @inheritdoc + */ + protected function setFieldsValue($effects) { + $this->percentage = $effects[$this->getPercentageFieldName()]; + } + + /** + * @inheritdoc + */ + public function getCartItemDiscount($cartItem) { + return $cartItem->getQuantity() * $cartItem->getPrice() * ($this->percentage / 100); + } + + /** + * @inheritdoc + */ + public function callDrawBackOfficeInputs($templateName) + { + return $this->drawBaseBackOfficeInputs($templateName, [ + 'percentage_field_name' => $this->makeCouponFieldName($this->getPercentageFieldName()), + 'percentage_value' => $this->percentage, + ]); + } + + /** + * @inheritdoc + */ + protected function getFieldList() + { + return $this->getBaseFieldList([$this->getPercentageFieldName()]); + } + + /** + * @inheritdoc + */ + protected function checkCouponFieldValue($fieldName, $fieldValue) + { + $this->checkBaseCouponFieldValue($fieldName, $fieldValue); + + if ($fieldName === $this->getPercentageFieldName()) { + + $pcent = floatval($fieldValue); + + if ($pcent <= 0 || $pcent > 100) { + throw new \InvalidArgumentException( + Translator::getInstance()->trans( + 'Value %val for Percent Discount is invalid. Please enter a positive value between 1 and 100.', + [ '%val' => $fieldValue] + ) + ); + } + } + + return $fieldValue; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/RemoveAmountOnAttributeValues.php b/core/lib/Thelia/Coupon/Type/RemoveAmountOnAttributeValues.php index 523365d08..495c908dc 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveAmountOnAttributeValues.php +++ b/core/lib/Thelia/Coupon/Type/RemoveAmountOnAttributeValues.php @@ -22,23 +22,14 @@ use Thelia\Core\Translation\Translator; */ class RemoveAmountOnAttributeValues extends AbstractRemoveOnAttributeValues { + use AmountCouponTrait; + /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_amount_on_attribute_av'; - /** - * @inheritdoc - */ - protected function setFieldsValue($effects) { - // Nothing to do, the amount is processed by CouponAbstract. + protected function getAmountFieldName() { + return self::AMOUNT_FIELD_NAME; } - - /** - * @inheritdoc - */ - public function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $this->amount; - } - /** * @inheritdoc */ @@ -70,39 +61,6 @@ class RemoveAmountOnAttributeValues extends AbstractRemoveOnAttributeValues */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-amount-on-attributes.html', [ - 'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME), - 'amount_value' => $this->amount - ]); - } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::AMOUNT_FIELD_NAME]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::AMOUNT_FIELD_NAME) { - - if (floatval($fieldValue) < 0) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Discount Amount is invalid. Please enter a positive value.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-amount-on-attributes.html'); } } diff --git a/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php b/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php index e5f3070b0..8843a07d8 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php +++ b/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php @@ -25,21 +25,13 @@ use Thelia\Model\Category; */ class RemoveAmountOnCategories extends AbstractRemoveOnCategories { + use AmountCouponTrait; + /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_amount_on_categories'; - /** - * @inheritdoc - */ - protected function setFieldsValue($effects) { - // Nothing to do, the amount is processed by CouponAbstract. - } - - /** - * @inheritdoc - */ - public function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $this->amount; + protected function getAmountFieldName() { + return self::AMOUNT_FIELD_NAME; } /** @@ -73,39 +65,6 @@ class RemoveAmountOnCategories extends AbstractRemoveOnCategories */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-amount-on-categories.html', [ - 'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME), - 'amount_value' => $this->amount - ]); - } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::AMOUNT_FIELD_NAME]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::AMOUNT_FIELD_NAME) { - - if (floatval($fieldValue) < 0) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Discount Amount is invalid. Please enter a positive value.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-amount-on-categories.html'); } } diff --git a/core/lib/Thelia/Coupon/Type/RemoveAmountOnProducts.php b/core/lib/Thelia/Coupon/Type/RemoveAmountOnProducts.php index 1061171e6..4d6d05906 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveAmountOnProducts.php +++ b/core/lib/Thelia/Coupon/Type/RemoveAmountOnProducts.php @@ -25,21 +25,13 @@ use Thelia\Model\Product; */ class RemoveAmountOnProducts extends AbstractRemoveOnProducts { + use AmountCouponTrait; + /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_amount_on_products'; - /** - * @inheritdoc - */ - protected function setFieldsValue($effects) { - // Nothing to do: amount is processed by CouponAbstract - } - - /** - * @inheritdoc - */ - protected function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $this->amount; + protected function getAmountFieldName() { + return self::AMOUNT_FIELD_NAME; } /** @@ -75,39 +67,6 @@ class RemoveAmountOnProducts extends AbstractRemoveOnProducts */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-amount-on-products.html', [ - 'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME), - 'amount_value' => $this->amount - ]); - } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::AMOUNT_FIELD_NAME]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::AMOUNT_FIELD_NAME) { - - if (floatval($fieldValue) < 0) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Discount Amount is invalid. Please enter a positive value.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-amount-on-products.html'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/RemovePercentageOnAttributeValues.php b/core/lib/Thelia/Coupon/Type/RemovePercentageOnAttributeValues.php index 26c55022c..509472e9d 100644 --- a/core/lib/Thelia/Coupon/Type/RemovePercentageOnAttributeValues.php +++ b/core/lib/Thelia/Coupon/Type/RemovePercentageOnAttributeValues.php @@ -24,23 +24,16 @@ class RemovePercentageOnAttributeValues extends AbstractRemoveOnAttributeValues { const PERCENTAGE = 'percentage'; - public $percentage = 0; + use PercentageCouponTrait; /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_percentage_on_attribute_av'; - /** + /** * @inheritdoc */ - protected function setFieldsValue($effects) { - $this->percentage = $effects[self::PERCENTAGE]; - } - - /** - * @inheritdoc - */ - public function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage; + protected function getPercentageFieldName() { + return self::PERCENTAGE; } /** @@ -74,41 +67,6 @@ class RemovePercentageOnAttributeValues extends AbstractRemoveOnAttributeValues */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-percentage-on-attributes.html', [ - 'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE), - 'percentage_value' => $this->percentage, - ]); - } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::PERCENTAGE]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::PERCENTAGE) { - - $pcent = floatval($fieldValue); - - if ($pcent <= 0 || $pcent > 100) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Percent Discount is invalid. Please enter a positive value between 1 and 100.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-percentage-on-attributes.html'); } } diff --git a/core/lib/Thelia/Coupon/Type/RemovePercentageOnCategories.php b/core/lib/Thelia/Coupon/Type/RemovePercentageOnCategories.php index dc5a533bd..0106a1ead 100644 --- a/core/lib/Thelia/Coupon/Type/RemovePercentageOnCategories.php +++ b/core/lib/Thelia/Coupon/Type/RemovePercentageOnCategories.php @@ -22,25 +22,18 @@ use Thelia\Model\Category; */ class RemovePercentageOnCategories extends AbstractRemoveOnCategories { - const PERCENTAGE = 'percentage'; + const PERCENTAGE = 'percentage'; + + use PercentageCouponTrait; /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_percentage_on_categories'; - protected $percentage = 0; - /** * @inheritdoc */ - protected function setFieldsValue($effects) { - $this->percentage = $effects[self::PERCENTAGE]; - } - - /** - * @inheritdoc - */ - public function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage; + protected function getPercentageFieldName() { + return self::PERCENTAGE; } /** @@ -74,42 +67,6 @@ class RemovePercentageOnCategories extends AbstractRemoveOnCategories */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-percentage-on-categories.html', [ - 'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE), - 'percentage_value' => $this->percentage, - ]); + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-percentage-on-categories.html'); } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::PERCENTAGE]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::PERCENTAGE) { - - $pcent = floatval($fieldValue); - - if ($pcent <= 0 || $pcent > 100) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Percent Discount is invalid. Please enter a positive value between 1 and 100.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; - } - } diff --git a/core/lib/Thelia/Coupon/Type/RemovePercentageOnProducts.php b/core/lib/Thelia/Coupon/Type/RemovePercentageOnProducts.php index 332d0feed..df48abf31 100644 --- a/core/lib/Thelia/Coupon/Type/RemovePercentageOnProducts.php +++ b/core/lib/Thelia/Coupon/Type/RemovePercentageOnProducts.php @@ -25,26 +25,18 @@ use Thelia\Model\Product; */ class RemovePercentageOnProducts extends AbstractRemoveOnProducts { - const PERCENTAGE = 'percentage'; + const PERCENTAGE = 'percentage'; + + use PercentageCouponTrait; /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_percentage_on_products'; - public $percentage = 0; - /** * @inheritdoc */ - protected function setFieldsValue($effects) { - - $this->percentage = $effects[self::PERCENTAGE]; - } - - /** - * @inheritdoc - */ - protected function getCartItemDiscount($cartItem) { - return $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage; + protected function getPercentageFieldName() { + return self::PERCENTAGE; } /** @@ -80,41 +72,6 @@ class RemovePercentageOnProducts extends AbstractRemoveOnProducts */ public function drawBackOfficeInputs() { - return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-percentage-on-products.html', [ - 'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE), - 'percentage_value' => $this->percentage, - ]); - } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return $this->getBaseFieldList([self::PERCENTAGE]); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - $this->checkBaseCouponFieldValue($fieldName, $fieldValue); - - if ($fieldName === self::PERCENTAGE) { - - $pcent = floatval($fieldValue); - - if ($pcent <= 0 || $pcent > 100) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Percent Discount is invalid. Please enter a positive value between 1 and 100.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-percentage-on-products.html'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php index 7ded8108c..64fcfe033 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php @@ -18,14 +18,20 @@ use Thelia\Core\Translation\Translator; * Allow to remove an amount from the checkout total * * @package Coupon - * @author Guillaume MOREL + * @author Guillaume MOREL , Franck Allimant * */ -class RemoveXAmount extends CouponAbstract +class RemoveXAmount extends AbstractRemove { + use AmountCouponTrait; + /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_x_amount'; + protected function getAmountFieldName() { + return self::AMOUNT_FIELD_NAME; + } + /** * @inheritdoc */ @@ -52,43 +58,19 @@ class RemoveXAmount extends CouponAbstract return $toolTip; } + /** + * @inheritdoc + */ + public function exec() + { + return $this->amount; + } + /** * @inheritdoc */ public function drawBackOfficeInputs() { - return $this->facade->getParser()->render('coupon/type-fragments/remove-x-amount.html', [ - 'fieldName' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME), - 'value' => $this->amount - ]); + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-x-amount.html'); } - - /** - * @inheritdoc - */ - protected function getFieldList() - { - return [self::AMOUNT_FIELD_NAME]; - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - if ($fieldName === self::AMOUNT_FIELD_NAME) { - - if (floatval($fieldValue) < 0) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Disount Amount is invalid. Please enter a positive value.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; - } - -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php index 554e3f5da..2799cf948 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php @@ -17,81 +17,23 @@ use Thelia\Coupon\FacadeInterface; /** * @package Coupon - * @author Guillaume MOREL + * @author Guillaume MOREL , Franck Allimant * */ -class RemoveXPercent extends CouponAbstract +class RemoveXPercent extends AbstractRemove { const INPUT_PERCENTAGE_NAME = 'percentage'; + use PercentageCouponTrait; + /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_x_percent'; - /** @var float Percentage removed from the Cart */ - protected $percentage = 0; - /** * @inheritdoc */ - public function set( - FacadeInterface $facade, - $code, - $title, - $shortDescription, - $description, - array $effects, - $isCumulative, - $isRemovingPostage, - $isAvailableOnSpecialOffers, - $isEnabled, - $maxUsage, - \DateTime $expirationDate, - $freeShippingForCountries, - $freeShippingForModules, - $perCustomerUsageCount - ) - { - parent::set( - $facade, $code, $title, $shortDescription, $description, $effects, - $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate, - $freeShippingForCountries, - $freeShippingForModules, - $perCustomerUsageCount - ); - - $this->percentage = $effects[self::INPUT_PERCENTAGE_NAME]; - - return $this; - } - - /** - * @inheritdoc - */ - public function exec() - { - return round($this->facade->getCartTotalTaxPrice($this->isAvailableOnSpecialOffers()) * $this->percentage/100, 2); - } - - /** - * @inheritdoc - */ - protected function checkCouponFieldValue($fieldName, $fieldValue) - { - if ($fieldName === self::INPUT_PERCENTAGE_NAME) { - - $pcent = floatval($fieldValue); - - if ($pcent <= 0 || $pcent > 100) { - throw new \InvalidArgumentException( - Translator::getInstance()->trans( - 'Value %val for Percent Discount is invalid. Please enter a positive value between 1 and 100.', - [ '%val' => $fieldValue] - ) - ); - } - } - - return $fieldValue; + protected function getPercentageFieldName() { + return self::INPUT_PERCENTAGE_NAME; } /** @@ -123,19 +65,16 @@ class RemoveXPercent extends CouponAbstract /** * @inheritdoc */ - public function drawBackOfficeInputs() + public function exec() { - return $this->facade->getParser()->render('coupon/type-fragments/remove-x-percent.html', [ - 'fieldName' => $this->makeCouponFieldName(self::INPUT_PERCENTAGE_NAME), - 'value' => $this->percentage - ]); + return round($this->facade->getCartTotalTaxPrice($this->isAvailableOnSpecialOffers()) * $this->percentage/100, 2); } /** * @inheritdoc */ - protected function getFieldList() + public function drawBackOfficeInputs() { - return [self::INPUT_PERCENTAGE_NAME]; + return $this->callDrawBackOfficeInputs('coupon/type-fragments/remove-x-percent.html'); } } diff --git a/templates/backOffice/default/coupon/type-fragments/base-remove-on-attributes.html b/templates/backOffice/default/coupon/type-fragments/base-remove-on-attributes.html index 073cdedbf..c809002b8 100644 --- a/templates/backOffice/default/coupon/type-fragments/base-remove-on-attributes.html +++ b/templates/backOffice/default/coupon/type-fragments/base-remove-on-attributes.html @@ -2,7 +2,7 @@
- + + {loop type="currency" name="get-symbol" default_only="true"}
{$SYMBOL}
diff --git a/templates/backOffice/default/coupon/type-fragments/remove-x-percent.html b/templates/backOffice/default/coupon/type-fragments/remove-x-percent.html index f06b42a6a..25fe083b0 100644 --- a/templates/backOffice/default/coupon/type-fragments/remove-x-percent.html +++ b/templates/backOffice/default/coupon/type-fragments/remove-x-percent.html @@ -3,7 +3,7 @@
- +
%