Location: PHPKode > projects > HikaShop > back/classes/discount.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class hikashopDiscountClass extends hikashopClass{
	var $tables = array('discount');
	var $pkeys = array('discount_id');
	var $namekeys = array('');
	var $toggle = array('discount_published'=>'discount_id');
	function saveForm(){
		$discount = null;
		$discount->discount_id = hikashop::getCID('discount_id');
		$formData = JRequest::getVar( 'data', array(), '', 'array' );
		foreach($formData['discount'] as $column => $value){
			hikashop::secureField($column);
			$discount->$column = strip_tags($value);
		}
		if(!empty($discount->discount_start)){
			$discount->discount_start=hikashop::getTime($discount->discount_start);
		}
		if(!empty($discount->discount_end)){
			$discount->discount_end=hikashop::getTime($discount->discount_end);
		}
		$status = $this->save($discount);
		return $status;
	}
	function save(&$discount){
		if(empty($discount->discount_id)){
			if(!empty($discount->discount_type) && $discount->discount_type=='coupon' && empty($discount->discount_code)){
				return false;
			}
		} 
		$status = parent::save($discount);
		return $status;
	}
	function load($coupon){
		$filters = array('discount_code='.$this->database->Quote($coupon),'discount_type=\'coupon\'','discount_published=1');
		$query = 'SELECT * FROM '.hikashop::table('discount').' WHERE '.implode(' AND ',$filters);
		$this->database->setQuery($query);
		return $this->database->loadObject();
	}
	function loadAndCheck($coupon_code,&$total,$zones,&$products,$display_error=true){
		$coupon = $this->load($coupon_code);
		return $this->check($coupon,$total,$zones,$products,$display_error);
	}
	function check(&$coupon,&$total,$zones,&$products,$display_error=true){
		$user = hikashop::get('class.user');
		$currency = hikashop::get('class.currency');
		$error_message = '';
		if(empty($coupon)){
			$error_message = JText::_('COUPON_NOT_VALID');
		}elseif($coupon->discount_start>time()){
			$error_message = JText::_('COUPON_NOT_YET_USABLE');
		}elseif($coupon->discount_end && $coupon->discount_end<time()){
			$error_message = JText::_('COUPON_EXPIRED');
		}else{
			if(hikashop::level(2)){
				if(!empty($coupon->discount_access)){
					$my = JFactory::getUser();
					if($coupon->discount_access != 'all' AND ($coupon->discount_access == 'none' OR empty($my->gid) OR empty($my->id) OR !hikashop::isAllowed($coupon->discount_access))){
			    		$error_message = JText::_('COUPON_NOT_FOR_YOU');
			    	}
				}
			}
			if(empty($error_message) && hikashop::level(1)){
				if(!empty($coupon->discount_quota) && $coupon->discount_quota<=$coupon->discount_used_times){
					$error_message = JText::_('QUOTA_REACHED_FOR_COUPON');
				}else{
					if($coupon->discount_zone_id){
						$class = hikashop::get('class.zone');
						$zone = $class->get($coupon->discount_zone_id);
						if(!in_array($zone->zone_namekey,$zones)){
							$error_message = JText::_('COUPON_NOT_AVAILABLE_IN_YOUR_ZONE');
						}
					}
					if(empty($error_message) && $coupon->discount_product_id){
						$ids = array_keys($products);
						if(!in_array($coupon->discount_product_id,$ids)){
							$error_message = JText::_('COUPON_NOT_FOR_THOSE_PRODUCTS');
						}
					}
					if(empty($error_message) && $coupon->discount_category_id){
						$ids = array_keys($products);
						$database =& JFactory::getDBO();
						if($coupon->discount_category_childs){
							$categoryClass = hikashop::get('class.category');
							$category = $categoryClass->get((int)$coupon->discount_category_id);
							$filters = array('b.category_left >= '.$category->category_left ,'b.category_right <= '.$category->category_right,'b.category_published=1','b.category_type=\'product\'','a.product_id IN ('.implode(',',$ids).')');
							hikashop::addACLFilters($filters,'category_access','b');
							$select = 'SELECT a.product_id FROM '.hikashop::table('category').' AS b LEFT JOIN '.hikashop::table('product_category').' AS a ON b.category_id=a.category_id WHERE '.implode(' AND ',$filters);
						}else{
							$filters = array('b.category_id = '.(int)$coupon->discount_category_id ,'a.product_id IN ('.implode(',',$ids).')');
							hikashop::addACLFilters($filters,'category_access','b');
							$select = 'SELECT a.product_id FROM '.hikashop::table('category').' AS b LEFT JOIN '.hikashop::table('product_category').' AS a ON b.category_id=a.category_id WHERE '.implode(' AND ',$filters);
						}
						$database->setQuery($select);
						$id = $database->loadResult();
						if(!$id){
							$error_message = JText::_('COUPON_NOT_FOR_PRODUCTS_IN_THOSE_CATEGORIES');
						}
					}
					if(empty($error_message) && bccomp($coupon->discount_minimum_order,0,5)){
						$currency->convertCoupon($coupon,$total->prices[0]->price_currency_id);
						if($coupon->discount_minimum_order>$total->prices[0]->price_value){
							$error_message = JText::sprintf('ORDER_NOT_EXPENSIVE_ENOUGH_FOR_COUPON',$currency->format($coupon->discount_minimum_order,$coupon->discount_currency_id));
						}
					}
				}
			}
		}
		if(!empty($error_message)){
			$class = hikashop::get('class.cart');
			$class->update('',0,0,'coupon');
			if($display_error){
				JRequest::setVar('coupon_error_message',$error_message);
			}
			return null;
		}
		JRequest::setVar('coupon_error_message','');
		$currency->convertCoupon($coupon,$total->prices[0]->price_currency_id);
		if(PHP_VERSION < 5){
			$coupon->total = $total;
		}else{
			$coupon->total->prices = array(clone(reset($total->prices)));
		}
		$currency->addCoupon($coupon->total,$coupon);
		return $coupon;
	}
}
Return current item: HikaShop