public function actionCoupon() { if ($_GET['remove'] && !empty($_GET['remove'])) { cart_discount::model()->deleteAllByAttributes(array('discount_ID' => $_GET['remove'], 'cart_ID' => Yii::app()->user->getState('cart_ID'))); Yii::app()->user->setFlash('cart', "voucher has been removed"); $this->redirect(array('index')); exit; } if ($_POST['token'] != md5(Yii::app()->user->getStateKeyPrefix())) { $this->redirect(array('index')); exit; } if (!$_POST['code'] or empty($_POST['code'])) { Yii::app()->user->setFlash('cart', "voucher name can't be blank"); $this->redirect(array('index')); exit; } if (!($discount = discount_entity::model()->findByAttributes(array('discount_name' => $_POST['code'])))) { Yii::app()->user->setFlash('cart', "voucher name not valid"); $this->redirect(array('index')); exit; } $cart = cart::model()->findByPk(Yii::app()->user->getState('cart_ID')); if ($msg = $cart->validateDiscount($_POST['total'], $discount->discount_ID, true)) { Yii::app()->user->setFlash('cart', $msg); $this->redirect(array('index')); exit; } else { $cartDiscount = new cart_discount(); $cartDiscount->cart_ID = $cart->cart_ID; $cartDiscount->discount_ID = $discount->discount_ID; $cartDiscount->save(); } $this->redirect(array('index')); }
public function loadModel() { if ($this->_model == null) { if (isset($_GET['id'])) { $condition = ''; $this->_model = discount_entity::model()->findByPk($_GET['id'], $condition); } if ($this->_model == null) { throw new CHttpException(404, "The requested page does not exist!"); } } return $this->_model; }
public function validateDiscount($orderTotal, $discountID, $checkCartDiscount = false) { $discount = discount_entity::model()->findByPk($discountID); if (!$orderTotal > 0) { return 'cannot add voucher if order is free'; } if (!$discount->discount_active == 1) { return 'this voucher has already been used or is disabled'; } if (!$discount->discount_quantity) { return 'this voucher has expired (usage limit attained)'; } if ($checkCartDiscount and ($this->nbDiscounts($discount->discount_ID) >= $discount->discount_quantity_per_user or order_discount::nbDiscountCustomer(Yii::app()->user->getId(), $discount->discount_ID) + $this->nbDiscounts($discount->discount_ID) >= $discount->discount_quantity_per_user)) { return 'you cannot use this voucher anymore (usage limit attained)'; } if (strtotime($discount->discount_from) > time()) { return 'this voucher is not yet valid'; } if (strtotime($discount->discount_to) < time()) { return 'this voucher has expired'; } if (sizeof($discounts = cart_discount::items($this->cart_ID)) >= 1 and $checkCartDiscount) { if (!$discount->discount_cumulable == 1) { return 'this voucher isn\'t cumulative with other current discounts'; } foreach ($discounts as $row) { if (!$row['discount_cumulable'] == 1) { return 'previous voucher added isn\'t cumulative with other discounts'; } } } if (is_array($discounts) and array_key_exists($discount->discount_ID, $discounts)) { return 'this voucher is already in your cart'; } if ($discount->discount_customer_ID and $discount->discount_customer_ID != Yii::app()->user->getId()) { if (!Yii::app()->user->isGuest) { return 'you cannot use this voucher-try to log in if you own it'; } return 'you cannot use this voucher'; } $onlyProductWithDiscount = true; if (!$discount->discount_cumulable_reduction == 1) { foreach ($this->products as $product) { if (!intval($product['product_reducetion_price']) and !intval($product['product_reducetion_percent'])) { $onlyProductWithDiscount = false; } } } if (!$discount->discount_cumulable_reduction == 1 and $onlyProductWithDiscount) { return 'this voucher isn\'t cumulative on products with reduction'; } $categories = discount_category::items($discount->discount_ID); $returnErrorNoProductCategory = true; foreach ($this->products as $product) { if (count($categories)) { if ($product->productInCategories($categories)) { $returnErrorNoProductCategory = false; } } } if ($returnErrorNoProductCategory) { return 'this discount isn\'t applicable to that product category'; } $minimal = product_entity::decoratePrice($discount->discount_minimal); if ($orderTotal < $minimal) { return 'the total amount of your order isn\'t high enough or this voucher cannot be used with those products'; } return false; }