Pushed forward the refactoring, using traits

This commit is contained in:
Franck Allimant
2014-06-12 17:00:17 +02:00
parent b34f1c35c7
commit e9a0661938
20 changed files with 461 additions and 412 deletions

View File

@@ -0,0 +1,129 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CartItem;
/**
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
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;
}
}

View File

@@ -23,7 +23,7 @@ use Thelia\Model\CartItem;
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
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)) {

View File

@@ -22,7 +22,7 @@ use Thelia\Model\CartItem;
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
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)) {

View File

@@ -22,7 +22,7 @@ use Thelia\Model\CartItem;
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
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)) {

View File

@@ -0,0 +1,64 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Condition\ConditionCollection;
use Thelia\Coupon\FacadeInterface;
/**
* Represents a Coupon ready to be processed in a Checkout process
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
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);
}

View File

@@ -0,0 +1,91 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Thelia\Core\Translation\Translator;
/**
* A trait to manage a coupon which removes a constant amount from the order total.
* Should be used on coupons classes which implements AmountAndPercentageCouponInterface
*
* Class AmountCouponTrait
* @author Franck Allimant <franck@cqfdev.fr>
* @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;
}
}

View File

@@ -0,0 +1,90 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
/**
* A trait to manage a coupon which removes a percentage of cart items from the order total.
* Should be used on coupons classes which implements AmountAndPercentageCouponInterface
*
* Class PercentageCouponTrait
* @author Franck Allimant <franck@cqfdev.fr>
* @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;
}
}

View File

@@ -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');
}
}

View File

@@ -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');
}
}

View File

@@ -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');
}
}

View File

@@ -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');
}
}

View File

@@ -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;
}
}

View File

@@ -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');
}
}

View File

@@ -18,14 +18,20 @@ use Thelia\Core\Translation\Translator;
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
* @author Guillaume MOREL <gmorel@openstudio.fr>, Franck Allimant <franck@cqfdev.fr>
*
*/
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;
}
}

View File

@@ -17,81 +17,23 @@ use Thelia\Coupon\FacadeInterface;
/**
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
* @author Guillaume MOREL <gmorel@openstudio.fr>, Franck Allimant <franck@cqfdev.fr>
*
*/
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');
}
}

View File

@@ -2,7 +2,7 @@
<div class="form-group input-coupon-attribute-id">
<label for="coupon-category-id">{intl l="Select attribute :"} = {$attribute_value}</label>
<label for="coupon-category-id">{intl l="Select attribute :"}</label>
<select required class="form-control" id="coupon-attribute-id" name="{$attribute_field_name}">
<option value="0">{intl l="Please select..."}</option>

View File

@@ -2,7 +2,7 @@
<label for="coupon-amount" class="control-label">{intl l='Discount amount'}</label>
<div class="input-group">
<input id="coupon-amount" type="money" class="form-control" name="{$fieldName}" value="{$value}" placeholder="{intl l="Amount, e.g. 12.50"}">
<input id="coupon-amount" type="money" class="form-control" name="{$amount_field_name}" value="{$amount_value}" placeholder="{intl l="Amount, e.g. 12.50"}">
{loop type="currency" name="get-symbol" default_only="true"}
<div class="input-group-addon">{$SYMBOL}</div>

View File

@@ -3,7 +3,7 @@
<label for="coupon-percent" class="control-label">{intl l='Percent Discount'}</label>
<div class="input-group">
<input id="coupon-percent" class="form-control" name="{$fieldName}" type="text" value="{$value}"/>
<input id="coupon-percent" class="form-control" name="{$percentage_field_name}" type="text" value="{$percentage_value}"/>
<div class="input-group-addon">%</div>
</div>
</div>