Example #1
0
		/**
		 * Validate discount rules data
		 *
		 * Method will validate all the discount rules POST data
		 *
		 * @access private
		 * @param string &$error The referenced string to store the error in, if any were found
		 * @return bool TRUE if POST data is valid, FALSE if there were errors
		 */
		private function ValidateDiscountRulesData(&$error)
		{
			$discounts = $this->GetDiscountRulesData(0);

			// Check to see if we have anything to validate
			if (empty($discounts)) {
				return true;
			}

			// Variable to check for overlapping
			$overlap = array(
						0 => array(),
						1 => array()
						);

			// This is to check for the previous quantities
			$prevMax = null;

			// OK, we have some, now check each rule
			foreach ($discounts as $key => $discount) {

				// Check first to see if these are empty records. If so then just continue
				if ($discount['quantitymin'] == '' && $discount['quantitymax'] == '' && $discount['amount'] == '') {
					continue;
				}

				if ($discount['quantitymin'] == '') {
					$error = sprintf(GetLang('DiscountRulesQuantityMinRequired'), $key+1);
					return false;
				}

				if (!isId($discount['quantitymin']) && $discount['quantitymin'] !== '*') {
					$error = sprintf(GetLang('DiscountRulesQuantityMinInvalid'), $key+1);
					return false;
				}

				if ($discount['quantitymax'] == '') {
					$error = sprintf(GetLang('DiscountRulesQuantityMaxRequired'), $key+1);
					return false;
				}

				if (!isId($discount['quantitymax']) && $discount['quantitymax'] !== '*') {
					$error = sprintf(GetLang('DiscountRulesQuantityMaxInvalid'), $key+1);
					return false;
				}

				// Check to see if the min is still lower than the maximum quantity
				if ($discount['quantitymin'] !== '*' && $discount['quantitymax'] !== '*' && $discount['quantitymin'] > $discount['quantitymax']) {
					$error = sprintf(GetLang('DiscountRulesQuantityMinHigher'), $key+1);
					return false;
				}

				// Both min and max values cannot be astrix
				if ($discount['quantitymin'] == '*' && $discount['quantitymax'] == '*') {
					$error = sprintf(GetLang('DiscountRulesQuantityBothAstrix'), $key+1);
					return false;
				}

				// Check to see if the previous max and current min quantities are both astrixes
				if (!is_null($prevMax) && $prevMax == '*' && $discount['quantitymin'] == '*') {
					$error = sprintf(GetLang('DiscountRulesQuantityMinPrevMaxAstrix'), $key+1);
					return false;
				}

				// Check for overlapping
				if ($discount['quantitymin'] !== '*' && Store_Number::isOverlapping($discount['quantitymin'], $overlap) == 1) {
					$error = sprintf(GetLang('DiscountRulesQuantityMinOverlap'), $key+1);
					return false;
				}
				if ($discount['quantitymax'] !== '*' && Store_Number::isOverlapping($discount['quantitymin'], $overlap) == 1) {
					$error = sprintf(GetLang('DiscountRulesQuantityMinOverlap'), $key+1);
					return false;
				}

				// Check those values for our next loop
				if ($discount['quantitymin'] !== '*') {
					$overlap[0][] = $discount['quantitymin'];
				} else {
					$overlap[0][] = '';
				}

				if ($discount['quantitymax'] !== '*') {
					$overlap[1][] = $discount['quantitymax'];
				} else {
					$overlap[1][] = '';
				}

				$type = isc_strtolower(isc_html_escape($discount['type']));

				// Do we have the currect type?
				if ($type !== 'price' && $type !== 'percent' && $type !== 'fixed') {
					$error = sprintf(GetLang('DiscountRulesTypeInvalid'), $key+1);
					return false;
				}

				if ($discount['amount'] == '') {
					$error = sprintf(GetLang('DiscountRulesAmountRequired'), $key+1);
					return false;
				}

				// Do we have a valit price/percentage?
				if (!isId($discount['amount']) && CPrice($discount['amount']) == '') {
					$error = sprintf(GetLang('DiscountRulesAmountInvalid'), $key+1);
					return false;
				}

				// Now we do some checking compared againt the product price
				switch ($type) {
					case 'price':
						if (DefaultPriceFormat($discount['amount']) >= DefaultPriceFormat($_POST['prodPrice'])) {
							$error = sprintf(GetLang('DiscountRulesAmountPriceInvalid'), $key+1);
							return false;
						}
						break;

					case 'percent':
						if ((int)$discount['amount'] >= 100) {
							$error = sprintf(GetLang('DiscountRulesAmountPercentInvalid'), $key+1);
							return false;
						} else if (strpos($discount['amount'], '.') !== false) {
							$error = sprintf(GetLang('DiscountRulesAmountPercentIsFloat'), $key+1);
							return false;
						}
						break;

					case 'fixed':
						if ($discount['amount'] >= $_POST['prodPrice']) {
							$error = sprintf(GetLang('DiscountRulesAmountFixedInvalid'), $key+1);
							return false;
						}
						break;
				}

				// Store value to be used as previous value next time
				$prevMax = $discount['quantitymax'];
			}

			return true;
		}