Files
sterivein/core/lib/Thelia/Coupon/CouponManager.php
Etienne Roudeix 99038e688a Revert "Revert "Merge branch 'cleanmaster' into modules""
This reverts commit b3ac365b45.

Conflicts:
	Readme.md
	core/lib/Thelia/Controller/Admin/AbstractCrudController.php
	core/lib/Thelia/Core/Template/Assets/AssetManagerInterface.php
	core/lib/Thelia/Core/Template/Assets/AsseticAssetManager.php
	core/lib/Thelia/Core/Template/Smarty/SmartyParser.php
	core/lib/Thelia/Core/Template/TemplateDefinition.php
	core/lib/Thelia/Model/Base/Coupon.php
	core/lib/Thelia/Model/Base/CouponQuery.php
	core/lib/Thelia/Model/Base/CouponVersion.php
	core/lib/Thelia/Model/Base/CouponVersionQuery.php
	core/lib/Thelia/Model/Base/OrderCouponQuery.php
2014-01-06 11:25:25 +01:00

253 lines
7.5 KiB
PHP

<?php
/**********************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
namespace Thelia\Coupon;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Condition\Implementation\ConditionInterface;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Model\Coupon;
/**
* Manage how Coupons could interact with a Checkout
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class CouponManager
{
/** @var FacadeInterface Provides necessary value from Thelia */
protected $facade = null;
/** @var ContainerInterface Service Container */
protected $container = null;
/** @var array CouponInterface to process*/
protected $coupons = array();
/** @var array Available Coupons (Services) */
protected $availableCoupons = array();
/** @var array Available Conditions (Services) */
protected $availableConditions = array();
/**
* Constructor
*
* @param ContainerInterface $container Service container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->facade = $container->get('thelia.facade');
$this->coupons = $this->facade->getCurrentCoupons();
}
/**
* Get Discount for the given Coupons
*
* @api
* @return float checkout discount
*/
public function getDiscount()
{
$discount = 0.00;
if (count($this->coupons) > 0) {
$couponsKept = $this->sortCoupons($this->coupons);
$discount = $this->getEffect($couponsKept);
// Just In Case test
$checkoutTotalPrice = $this->facade->getCartTotalPrice();
if ($discount >= $checkoutTotalPrice) {
$discount = $checkoutTotalPrice;
}
}
return $discount;
}
/**
* Check if there is a Coupon removing Postage
* @return bool
*/
public function isCouponRemovingPostage()
{
if (count($this->coupons) == 0) {
return false;
}
$couponsKept = $this->sortCoupons($this->coupons);
/** @var CouponInterface $coupon */
foreach ($couponsKept as $coupon) {
if ($coupon->isRemovingPostage()) {
return true;
}
}
return false;
}
/**
* Sort Coupon to keep
* Coupon not cumulative cancels previous
*
* @param array $coupons CouponInterface to process
*
* @return array Array of CouponInterface sorted
*/
protected function sortCoupons(array $coupons)
{
$couponsKept = array();
/** @var CouponInterface $coupon */
foreach ($coupons as $coupon) {
if (!$coupon->isExpired()) {
if ($coupon->isCumulative()) {
if (isset($couponsKept[0])) {
/** @var CouponInterface $previousCoupon */
$previousCoupon = $couponsKept[0];
if ($previousCoupon->isCumulative()) {
// Add Coupon
$couponsKept[] = $coupon;
} else {
// Reset Coupons, add last
$couponsKept = array($coupon);
}
} else {
// Reset Coupons, add last
$couponsKept = array($coupon);
}
} else {
// Reset Coupons, add last
$couponsKept = array($coupon);
}
}
}
$coupons = $couponsKept;
$couponsKept = array();
/** @var CouponInterface $coupon */
foreach ($coupons as $coupon) {
if ($coupon->isMatching($this->facade)) {
$couponsKept[] = $coupon;
}
}
return $couponsKept;
}
/**
* Process given Coupon in order to get their cumulative effects
*
* @param array $coupons CouponInterface to process
*
* @return float discount
*/
protected function getEffect(array $coupons)
{
$discount = 0.00;
/** @var CouponInterface $coupon */
foreach ($coupons as $coupon) {
$discount += $coupon->exec($this->facade);
}
return $discount;
}
/**
* Add an available CouponManager (Services)
*
* @param CouponInterface $coupon CouponManager
*/
public function addAvailableCoupon(CouponInterface $coupon)
{
$this->availableCoupons[] = $coupon;
}
/**
* Get all available CouponManagers (Services)
*
* @return array
*/
public function getAvailableCoupons()
{
return $this->availableCoupons;
}
/**
* Add an available ConstraintManager (Services)
*
* @param ConditionInterface $condition ConditionInterface
*/
public function addAvailableCondition(ConditionInterface $condition)
{
$this->availableConditions[] = $condition;
}
/**
* Get all available ConstraintManagers (Services)
*
* @return array
*/
public function getAvailableConditions()
{
return $this->availableConditions;
}
/**
* Decrement this coupon quantity
*
* To call when a coupon is consumed
*
* @param \Thelia\Model\Coupon $coupon Coupon consumed
*
* @return int Usage left after decremental
*/
public function decrementQuantity(Coupon $coupon)
{
$ret = -1;
try {
$usageLeft = $coupon->getMaxUsage();
if ($usageLeft > 0) {
$usageLeft--;
$coupon->setMaxUsage($usageLeft);
$coupon->save();
$ret = $usageLeft;
}
} catch(\Exception $e) {
$ret = false;
}
return $ret;
}
}