/** * Calculate discount amount for Order * * @author Dmitry Semenov <*****@*****.**> * * @param float * @param array * * @return float */ public function getDiscount($code, $plan = null) { $message = ''; $discount = 0; if (empty($code)) { return array('discount' => $discount, 'message' => 'No promotional code provided.'); } $plan = \MembershipPlanModel::model()->findByPk($plan); $plan = \MembershipPlanModel::recalculateSalesPrices(array($plan)); $plan = $plan[0]; if (!$plan) { return array('success' => false, 'message' => 'Billing plan not found.'); } $criteria = new \CDbCriteria(); $criteria->addColumnCondition(array('t.code' => $code)); $coupon = self::model()->find($criteria); if (!$coupon) { return array('success' => false, 'message' => 'Coupon not found.'); } if (!$coupon->canUse($plan->id)) { return array('success' => false, 'message' => 'Can`t use this coupon with selected billing plan.'); } if (!$coupon->isInTime()) { return array('success' => false, 'message' => 'This coupon is out of date.'); } switch ($coupon->discountType) { case PromoCodeType::DISCOUNT_TYPE_DOLLAR_VALUE: return array('success' => true, 'message' => "Your discount is \$" . $coupon->discountValue . " and total cost is \$" . round($coupon->discountValue < $plan->price ? $plan->price - $coupon->discountValue : 0, 2), 'discountedPrice' => round($coupon->discountValue < $plan->price ? $plan->price - $coupon->discountValue : 0, 2)); break; case PromoCodeType::DISCOUNT_TYPE_PERCENTAGE: $discountedPrice = $plan->price - $coupon->discountValue / 100 * $plan->price; if ($coupon->discountValue >= 100) { $discountedPrice = 0; } return array('success' => true, 'message' => "Your discount is " . $coupon->discountValue . "% and total cost is \$" . round($discountedPrice, 2), 'discountedPrice' => round($discountedPrice, 2)); break; } }