<?php
class Coupon {
	var $data    = array();
	var $product = array();
	var $status  = false;
	
	function Coupon(&$locator) {
		$this->cart     =& $locator->get('cart');
		$this->customer =& $locator->get('customer');
		$this->database =& $locator->get('database');
		$this->language =& $locator->get('language');
		$this->session  =& $locator->get('session');
	    // 构造coupon实例，数据为空，状态不可用, 如果数据不为空的话，当前客户的totals里将有优惠劵数据		
	}
	
	// 使用优惠劵函数
	function isValid($coupon_id) {
				
		// 查找数据库中是否有客户输入的优惠劵代码，已经使用者邮箱是否匹配，优惠劵状态是否可用
		if (!$coupon_id)
		{
			return $this->status=false;
		}
		$coupon = $this->database->getRow("select * from coupon where coupon_id =$coupon_id");

		if ($coupon)
			{				
				$this->status=$coupon['status']>0?true:false;
			} 
		else
		{
			$this->status=false;
		}
	    		
		return $this->status;
	}
	
	// 使用优惠劵函数
	function set($code) {
				
		// 查找数据库中是否有客户输入的优惠劵代码，已经使用者邮箱是否匹配，优惠劵状态是否可用
		$sql = "select * from coupon where code = '?' and email = '?' and usable_times > 0";
		$coupon_info = $this->database->getRow($this->database->parse($sql, trim($code), $this->customer->getEmail() ));
 /*       echo $code."=".$coupon_info['discount'].">".$order_amount."<br />"; */
		
		// 如果可以找到完全匹配的优惠劵，并且优惠劵面值必须小于订单金额，否则打折后的订单总额将为负数
		if ($coupon_info)
			{				
			    // 输出调试信息
/*				echo "<script language=\"JavaScript\">alert(\"Tips: Use Coupon Successfully@library/cart/coupon.php line 28\");</script>";*/	
			
			    // 把Coupon实例的当前数据换成客户设置的优惠劵数据				
				$this->data = $coupon_info;
				// 把Coupon实例的状态设置成有效
				$this->status = true;
				// 优惠劵一经使用成功就失效, 将status置为0， 并且记录优惠劵被使用时的时间 (网站Apache服务器系统时间）
//   			date_default_timezone_set('Asia/Hong_Kong');
//              return $this->database->query("update coupon set status = 0, usable_times=0, date_added = '".date('Y-m-d H:i:s',time())."' where coupon_id = $coupon_info['coupon_id']");
				
			} 	
		else
		{			
			// 优惠劵使用不成功	
			$this->status = false;
/*			echo "<script language=\"JavaScript\">alert(\"Tips: Use Coupon Failure@library/cart/coupon.php line 42\");</script>";*/			
		}
		
		return $this->status;
	}
		
			
	function redeem($coupon_id, $customer_id, $order_id) {
		date_default_timezone_set('Asia/Hong_Kong');
		$this->database->query("insert coupon_redeem set coupon_id = '" . (int)$coupon_id . "', customer_id = '" . (int)$customer_id . "', order_id = '" . (int)$order_id . "', date_added = '".date('Y-m-d H:i:s',time())."'");
	}

	function getId() {
		return (isset($this->data['coupon_id']) ? $this->data['coupon_id'] : NULL);
	}
		
	function getName() {
		return (isset($this->data['name']) ? $this->data['name'] : NULL);
	}
	
	function getDescription() {
		return (isset($this->data['description']) ? $this->data['description'] : NULL);
	}
		
	function getCode() {
		return (isset($this->data['code']) ? $this->data['code'] : NULL);
	}

	function getDiscount() {
	
		return round(@$this->data['discount'], 2);
	
	}
		
	function getShipping() {
		return (isset($this->data['shipping']) ? $this->data['shipping'] : NULL);
	}
		
	function hasProduct() {
		if ($this->product) {
			$data = array();

			foreach ($this->product as $result) {
				$data[] = $result['product_id'];
			}

			foreach ($this->cart->getProducts() as $result) {
				if (in_array($result['product_id'], $data)) {
					return TRUE;
				}
			}
		} else {
			return TRUE;
		}
	}
}
?>