<?php
/*
  osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License
*/

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();


  class ot_coupon {
    var $title, $output;
    function ot_coupon() {
      $this->code = 'ot_coupon';
      $this->header = MODULE_ORDER_TOTAL_COUPON_HEADER;
      $this->title = MODULE_ORDER_TOTAL_COUPON_TITLE;
      $this->description = MODULE_ORDER_TOTAL_COUPON_DESCRIPTION;
      $this->user_prompt = '';
      $this->enabled =MODULE_ORDER_TOTAL_COUPON_STATUS ;
      $this->sort_order = MODULE_ORDER_TOTAL_COUPON_SORT_ORDER;
      $this->include_shipping = MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING;
      $this->include_tax = MODULE_ORDER_TOTAL_COUPON_INC_TAX;
      $this->calculate_tax = MODULE_ORDER_TOTAL_COUPON_CALC_TAX;
      $this->tax_class  = MODULE_ORDER_TOTAL_COUPON_TAX_CLASS;
      $this->credit_class = true;
      $this->output = array();
	  $this->expire_date="";
	  $this->uses_per_user="";
    }

  function process() {
    global $PHP_SELF, $order, $currencies,$HTTP_SESSION_VARS;
     $order_total=$this->get_order_total(true);         
     $od_amount = $this->calculate_credit($order_total);
	 $this->deduction = $od_amount;
	 if (strtolower($this->calculate_tax) != 'none') $tod_amount = $this->calculate_tax_deduction($order_total, $this->deduction, $this->calculate_tax);
	 // if($this->deduction>$order_total)
	 // $this->deduction=$order_total;

	if ($this->deduction > 0) {
      $order->info['total'] = $order->info['total'] - $this->deduction;
	  if($order->info['total']<0) $order->info['total']=0;
	  if(tep_session_is_registered('email_redeem_code'))
	  	$email_coupon=MODULE_ORDER_TOTAL_EMAIL_COUPON_TITLE . ':' .  $HTTP_SESSION_VARS['email_redeem_code']. ':';
       $this->output[] = array('title' => $this->title . ':' . $this->coupon_code .':' . $email_coupon,
                     'text' => '<b>' . $currencies->format($od_amount) . '</b>',
                     'value' =>$this->deduction);
    }
  }

  function selection_test() {
    return false;
  }


  function pre_confirmation_check($order_total) {
    global $customer_id;
    return $this->calculate_credit($order_total);
    }

  function use_credit_amount() {
    return $output_string;
  }

  function credit_selection() {
      global $customer_id, $currencies, $language;
        $selection_string = '';
		//show_r id used in checkout_single.js.php
        $selection_string .= '<tr id="show_r">' . "\n";
        $selection_string .= '  <td width="20">' .  tep_draw_separator('pixel_trans.gif', '10', '1') .'</td>';
        $selection_string .= '  <td class="main">' . "\n";
        //$image_submit = tep_template_image_submit('button_redeem.gif',IMAGE_REDEEM_VOUCHER,'name="submit_redeem" onClick="javascript:submitFunction();"');
        $image_submit = tep_template_image_button('button_redeem.gif',IMAGE_REDEEM_VOUCHER,'name="submit_redeem" style="cursor:pointer;cursor:hand;" onClick="javascript:submitFunction();"');
        $selection_string .= TEXT_ENTER_COUPON_CODE . tep_draw_input_field('gv_redeem_code','','id="gv_redeem_code"') . '</td>';
        $selection_string .= '  <td align="right">' . $image_submit . '</td>';
        $selection_string .= '  <td width="10">' . tep_draw_separator('pixel_trans.gif', '10', '1') . '</td>';
        $selection_string .= '</tr>' . "\n";
        return $selection_string;
    }


  function collect_posts() {
    global $HTTP_POST_VARS, $customer_id, $currencies, $cc_id,$order;
    if (isset($HTTP_POST_VARS['coupon']) && isset($HTTP_POST_VARS['gv_redeem_code'])) {
// get some info from the coupon table
		$email_coupon=false;
      $coupon_query=tep_db_query("select coupon_id,coupon_code, coupon_amount, coupon_type, coupon_minimum_order,coupon_flag,
                                       uses_per_coupon, uses_per_user, restrict_to_products,
                                       restrict_to_categories from " . TABLE_COUPONS . "
                                       where coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'
                                       and coupon_active='Y'");
     /*   if (tep_db_num_rows($coupon_query)==0 ) {
          tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_INVALID_REDEEM_COUPON), 'SSL'));
        }*/
		// if ($coupon_result['coupon_type'] != 'G') 
		 if (tep_db_num_rows($coupon_query)>0 )  {
			$now_date = getServerDate();
			$date_query=tep_db_query("select coupon_start_date from " . TABLE_COUPONS . "
									where coupon_start_date <= '" . $now_date . "' and
									coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
	
			if (tep_db_num_rows($date_query)==0) {
			  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_STARTDATE_COUPON), 'SSL'));
			}
			$date_query=tep_db_query("select coupon_expire_date,uses_per_user from " . TABLE_COUPONS . "
									where coupon_expire_date >= '" . $now_date . "' and
									coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
			
			if (tep_db_num_rows($date_query)==0) {
			  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_FINISDATE_COUPON), 'SSL'));
			}else if(tep_db_num_rows($date_query)){
				$date_res=tep_db_fetch_array($date_query);
				$this->expire_date=format_date(date('Y-m-d',strtotime($date_res['coupon_expire_date'])));
				
			}
     	 } else {
	  		 $coupon_query=tep_db_query("select c.coupon_id, ce.discount_coupon_code as coupon_code,ce.amount as coupon_amount, c.coupon_type, c.coupon_minimum_order, c.coupon_flag ,
                                       c.uses_per_coupon, c.uses_per_user, c.restrict_to_products,
                                       c.restrict_to_categories from " . TABLE_COUPONS . " c , " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
                                       where ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."' and ce.coupon_id=c.coupon_id  and ce.customer_id='" . $customer_id . "' 
                                       and c.coupon_active='Y'");
			 if (tep_db_num_rows($coupon_query)>0 )  {	
				$now_date = getServerDate();
				$date_query=tep_db_query("select c.coupon_start_date from " . TABLE_COUPONS . " c , " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
										where c.coupon_start_date <= '" . $now_date . "' and c.coupon_id=ce.coupon_id  and  ce.customer_id='" . $customer_id . "' and 
										ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
		
				if (tep_db_num_rows($date_query)==0) {
				  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_STARTDATE_COUPON), 'SSL'));
				}
				$date_query=tep_db_query("select c.coupon_expire_date,c.uses_per_user from " . TABLE_COUPONS . " c , " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
										where c.coupon_expire_date >= '" . $now_date . "' and c.coupon_id=ce.coupon_id  and ce.customer_id='" . $customer_id ."'  and 
										ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
				
				if (tep_db_num_rows($date_query)==0) {
				  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_FINISDATE_COUPON), 'SSL'));
				} else if(tep_db_num_rows($date_query)){
					$date_res=tep_db_fetch_array($date_query);
					$this->expire_date=format_date(date('Y-m-d',strtotime($date_res['coupon_expire_date'])));
				}
				$email_coupon=true;
			 } else  {
				  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_INVALID_REDEEM_COUPON), 'SSL'));
			 }
	 	}
		
		$coupon_result=tep_db_fetch_array($coupon_query);
		$coupon_count = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . "
                                          where coupon_id = '" . $coupon_result['coupon_id']."'");
        $coupon_count_customer = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . "
                                                   where coupon_id = '" . $coupon_result['coupon_id']."' and
                                                   customer_id = '" . $customer_id . "'");
			
        if (tep_db_num_rows($coupon_count)>=$coupon_result['uses_per_coupon'] && $coupon_result['uses_per_coupon'] > 0 ) {
          tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_USES_COUPON . $coupon_result['uses_per_coupon'] . TIMES ), 'SSL'));
        }
		 
        if (tep_db_num_rows($coupon_count_customer)>=$coupon_result['uses_per_user'] && $coupon_result['uses_per_user'] > 0 && $coupon_result['coupon_flag']=='U') {
          tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_USES_USER_COUPON . $coupon_result['uses_per_user'] . TIMES ), 'SSL'));
        }
        if ($coupon_result['coupon_type']=='S') {
          $coupon_amount = $order->info['shipping_cost'];
        } else {
          $coupon_amount = $currencies->format($coupon_result['coupon_amount']) . ' ';
        }
        if ($coupon_result['type']=='P') $coupon_amount = $coupon_result['coupon_amount'] . '% ';
        if ($coupon_result['coupon_minimum_order']>0) $coupon_amount .= 'on orders greater than ' .  $coupon_result['coupon_minimum_order'];
//        if (!tep_session_is_registered('cc_id')) tep_session_register('cc_id');

        $_SESSION['cc_id'] = $coupon_result['coupon_id'];	 
		if($email_coupon) { 
			if(tep_session_is_registered('email_redeem_code'))
				tep_session_unregister('email_redeem_code');
				
				tep_session_register('email_redeem_code');
				//$email_redeem_code=$coupon_result['coupon_code'];
				tep_set_session_value('email_redeem_code' ,$coupon_result['coupon_code']);
		}	else tep_session_unregister('email_redeem_code');
      if ($HTTP_POST_VARS['submit_redeem_coupon_x'] && !$HTTP_POST_VARS['gv_redeem_code']) tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL'));
    }
	else if(isset($HTTP_POST_VARS['coupon']) && !isset($HTTP_POST_VARS['gv_redeem_code'])) {
          tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_INVALID_REDEEM_COUPON), 'SSL'));
	}	
	else{
		tep_session_unregister('cc_id');
	} 

	if($order->info['subtotal']<$coupon_result['coupon_minimum_order'] ){
		  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_LOW_ORDER_TOTAL), 'SSL'));
	}
	
	if($coupon_result['coupon_amount']>$order->info['subtotal']){ 
		  if(!tep_session_is_registered('coupon_exist')){
		  tep_session_register('coupon_exist');
		  $coupon_exist = 1;
		  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_LESSTHAN_COUPON_PRICE), 'SSL'));
		}  
	}
	/*if($coupon_result['coupon_amount']==$order->info['subtotal']){
		  if(!tep_session_is_registered('coupon_exist_equal_amount')){
		  tep_session_register('coupon_exist_equal_amount');
		  $coupon_exist_equal_amount = 1;
		  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_COUPON_PRICE_EQUAL), 'SSL'));
		}  
	}*/
	
  } 
  

function calculate_credit($amount) {
	global $customer_id, $order, $cc_id,$HTTP_SESSION_VARS;
	$cc_id = $_SESSION['cc_id'];
	
	$discount = 0;
	
	if (!$cc_id) return $discount;
	if(!$HTTP_SESSION_VARS['email_redeem_code']) {
		$coupon_query = tep_db_query("select coupon_amount ,coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $cc_id . "'");
	$coupon_result = tep_db_fetch_array($coupon_query);
	$this->coupon_code = $coupon_result['coupon_code'];
	} else	 {
		$coupon_query = tep_db_query("select c.coupon_code as code,ce.amount as coupon_amount,ce.discount_coupon_code as coupon_code from " . TABLE_COUPONS . " c, " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce where ce.coupon_id=c.coupon_id and ce.coupon_id = '" . $cc_id  .  "' and ce.customer_id='" . $customer_id . "' and ce.discount_coupon_code='" . $HTTP_SESSION_VARS['email_redeem_code'] . "'");
		$coupon_result = tep_db_fetch_array($coupon_query);
		$this->coupon_code = $coupon_result['code'];
	}	

	if (tep_db_num_rows($coupon_query)<=0) return $discount;
	
	//$coupon_result = tep_db_fetch_array($coupon_query);
	//$this->coupon_code = $coupon_result['coupon_code'];
	//print_r($coupon_result);
	$coupon_get = tep_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS ." where coupon_code = '". $coupon_result['coupon_code'] . "'");
	$get_result = tep_db_fetch_array($coupon_get);
	//$c_deduct = $get_result['coupon_amount'];
	$c_deduct = $coupon_result['coupon_amount'];
	
	// if coupon type S convert so that shipping amount is subtracted	
	if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
	if ($get_result['coupon_type']=='P') $c_deduct = substr($coupon_result['coupon_amount'],0,-1);
	//if ($get_result['coupon_type']=='P') $c_deduct = substr($get_result['coupon_amount'],0,-1);

	
	// check if order_total > coupon minimum order
	if ($get_result['coupon_minimum_order'] > $this->get_order_total(true)) return $discount;
	
	if ($get_result['coupon_type']!='P'){
		$discount=$c_deduct;
	} else {
		$discount=$amount*$c_deduct/100;
	}
	return $discount;
  }

  function calculate_tax_deduction($amount, $od_amount, $method) {
    global $customer_id, $order, $cc_id, $cart,$HTTP_SESSION_VARS;
    $cc_id = $_SESSION['cc_id'];
	if(!$HTTP_SESSION_VARS['email_redeem_code'])
   		$coupon_query = tep_db_query("select coupon_amount,coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $cc_id . "'");
	 else 
	 	$coupon_query = tep_db_query("select amount as coupon_amount,discount_coupon_code as coupon_code from " . TABLE_COUPONS_DISCOUNT_EMAIL . " where coupon_id = '" . $cc_id .  "' and customer_id='" . $customer_id . "' and discount_coupon_code='" . $HTTP_SESSION_VARS['email_redeem_code'] . "'");
		
    if (tep_db_num_rows($coupon_query) !=0 ) {
      $coupon_result = tep_db_fetch_array($coupon_query);
      $coupon_get = tep_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS . " where coupon_code = '". $coupon_result['coupon_code'] . "'");
	  $get_result = tep_db_fetch_array($coupon_get);
      if ($get_result['coupon_type'] != 'S') {
      if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
        // What to do here.
        // Loop through all products and build a list of all product_ids, price, tax class
        // at the same time create total net amount.
        // then
        // for percentage discounts. simply reduce tax group per product by discount percentage
        // or
        // for fixed payment amount
        // calculate ratio based on total net
        // for each product reduce tax group per product by ratio amount.
        $products = $cart->get_products();
        for ($i=0; $i<sizeof($products); $i++) {
          $t_prid = tep_get_prid($products[$i]['id']);
          $cc_query = tep_db_query("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
          $cc_result = tep_db_fetch_array($cc_query);
          $valid_product = false;
          if ($get_result['restrict_to_products']) {
            $pr_ids = split("[,]", $get_result['restrict_to_products']);
            for ($p = 0; $p < sizeof($pr_ids); $p++) {
              if ($pr_ids[$p] == $t_prid) $valid_product = true;
            }
          }
          if ($get_result['restrict_to_categories']) {
            $cat_ids = split("[,]", $get_result['restrict_to_categories']);
            for ($c = 0; $c < sizeof($cat_ids); $c++) {
              $cat_query = tep_db_query("select products_id from products_to_categories where products_id = '" . $products_id . "' and categories_id = '" . $cat_ids[$i] . "'");
              if (tep_db_num_rows($cat_query) !=0 ) $valid_product = true;
            }
          }
          if ($valid_product) {
            $valid_array[] = array('product_id' => $t_prid,
                                 'products_price' => $products[$i]['final_price'] * $products[$i]['quantity'],
                                 'products_tax_class' => $cc_result['products_tax_class_id']);
            $total_price += $products[$i]['final_price'] * $products[$i]['quantity'];
          }
        }
        if ($valid_product) {
        if ($get_result['coupon_type'] == 'P') {
        //  $ratio = $get_result['coupon_amount']/100;
		  $ratio = $coupon_result['coupon_amount']/100;
        } else {
          $ratio = $od_amount / $total_price;
        }
        if ($get_result['coupon_type'] == 'S') $ratio = 1;
          if ($method=='Credit Note') {
            $tax_rate = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            if ($get_result['coupon_type'] == 'P') {
              $tod_amount = $od_amount / (100 + $tax_rate)* $tax_rate;
            } else {
              $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount/100;
            }
            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
            $order->info['total'] -= $tod_amount;
          } else {
            for ($p=0; $p<sizeof($valid_array); $p++) {
              $tax_rate = tep_get_tax_rate($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
              $tax_desc = tep_get_tax_description($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
              if ($tax_rate > 0) {
                $tod_amount[$tax_desc] += ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio;
                $order->info['tax_groups'][$tax_desc] -= ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio;
                $order->info['total'] -= ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio;
              }
            }
          }
        }
      } else {
        if ($get_result['coupon_type'] =='F') {
          $tod_amount = 0;
          if ($method=='Credit Note') {
            $tax_rate = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tod_amount = $od_amount / (100 + $tax_rate)* $tax_rate;
            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
          } else {
		  	if($amount>0)
	            $ratio1 = $od_amount/$amount;
			if(isset($order->info['tax_groups'])) {
				reset($order->info['tax_groups']);
				while (list($key, $value) = each($order->info['tax_groups'])) {
				  $tax_rate = tep_get_tax_rate_from_desc($key);
				  $net = $tax_rate * $order->info['tax_groups'][$key];
				  if ($net>0) {
					$god_amount = $order->info['tax_groups'][$key] * $ratio1;
					$tod_amount += $god_amount;
					$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
				  }
				}
			}	
          }
          $order->info['total'] -= $tod_amount;
        }
        if ($get_result['coupon_type'] =='P') {
          $tod_amount=0;
          if ($method=='Credit Note') {
            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount/100;
            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
          } else {
		  	if(isset($order->info['tax_groups'])) {
				reset($order->info['tax_groups']);
				while (list($key, $value) = each($order->info['tax_groups'])) {
				  $god_amout=0;
				  $tax_rate = tep_get_tax_rate_from_desc($key);
				  $net = $tax_rate * $order->info['tax_groups'][$key];
				  if ($net>0) {
				  	$god_amount = $order->info['tax_groups'][$key] * $coupon_result['coupon_amount']/100;
					//$god_amount = $order->info['tax_groups'][$key] * $get_result['coupon_amount']/100;
					$tod_amount += $god_amount;
					$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
				  }
				}
			}	
          }
          $order->info['tax'] -= $tod_amount;
        }
      }
    }
    }
    return $tod_amount;
  }

function update_credit_account($i) {
  return false;
 }

 function apply_credit() {
   global $insert_id, $customer_id, $REMOTE_ADDR, $cc_id,$HTTP_SESSION_VARS;
   $cc_id = $_SESSION['cc_id'];
   if($HTTP_SESSION_VARS['email_redeem_code']=='')
   	$HTTP_SESSION_VARS['email_redeem_code']=0;
   if ($this->deduction !=0) {
     tep_db_query("insert into " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, redeem_date, redeem_ip, customer_id, order_id,email_redeem_id) values ('" . $cc_id . "', now(), '" . $REMOTE_ADDR . "', '" . $customer_id . "', '" . $insert_id . "','" . $HTTP_SESSION_VARS['email_redeem_code'] . "')");
   }
   tep_session_unregister('cc_id');
 }

	function get_order_total($include_tax) {
    	global  $order, $cart, $customer_id, $cc_id;
		$cc_id = $_SESSION['cc_id'];
		// get stored total(altered for deposit)
		$order_total = $order->info['total'];

		// Check if gift voucher is in cart and adjust total
		$products = $cart->get_products();
		for ($i=0; $i<sizeof($products); $i++) {
            if ($products[$i]['element_type']!="P") continue;
			$t_prid = tep_get_prid($products[$i]['id']);
			$gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
			$gv_result = tep_db_fetch_array($gv_query);
			if (ereg('^GIFT', addslashes($gv_result['products_model']))) {
				$qty = $cart->get_quantity($t_prid);
				$products_tax = tep_get_tax_rate($gv_result['products_tax_class_id']);
				if ($this->include_tax =='false') {
				   $gv_amount = $gv_result['products_price'] * $qty;
				} else {
				  $gv_amount = ($gv_result['products_price'] + tep_calculate_tax($gv_result['products_price'],$products_tax)) * $qty;
				}
				$order_total=$order_total - $gv_amount;
			}
		}
		
		if ($this->include_tax == 'false') $order_total=$order_total-$order->info['tax'];
		if ($this->include_shipping == 'false') $order_total=$order_total-$order->info['shipping_cost'];
		
		// OK thats fine for global coupons but what about restricted coupons
		// where you can only redeem against certain products/categories.
		// and I though this was going to be easy !!!
   		$coupon_query=tep_db_query("select coupon_code  from " . TABLE_COUPONS . " where coupon_id='".$cc_id."'");
		if (tep_db_num_rows($coupon_query) !=0) {
			$coupon_result=tep_db_fetch_array($coupon_query);
			$coupon_get=tep_db_query("select restrict_to_products,restrict_to_categories,restrict_to_event_categories,restrict_to_sub_categories,restrict_to_events,restrict_to_subscription, restrict_to_service, restrict_to_service_categories from " . TABLE_COUPONS . " where coupon_code='".$coupon_result['coupon_code']."' and (restrict_to_products!='' or restrict_to_categories!='' or restrict_to_event_categories!='' or restrict_to_events!='' or restrict_to_sub_categories!='' or restrict_to_subscription!='' or restrict_to_service_categories!='' or restrict_to_service!='')");
			$get_result=tep_db_fetch_array($coupon_get);
			$in_cat = true;
			if ($get_result['restrict_to_categories']) {
				$cat_ids = split("[,]", $get_result['restrict_to_categories']);
				for ($i = 0; $i <count($cat_ids); $i++) {
					for ($j=0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='P') continue;
						$cat_query = tep_db_query("select products_id from products_to_categories where products_id = '" . $products[$j]['id'] . "' and categories_id = '" . $cat_ids[$i] . "'");
						if (tep_db_num_rows($cat_query) !=0) {
							$total_price += $this->get_product_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_products']) {
				$pr_ids = split("[,]", $get_result['restrict_to_products']);
				for ($i = 0; $i < count($pr_ids); $i++) {
					for ($j = 0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='P') continue;
						if ($products[$j]['id'] == $pr_ids[$i]) {
							$total_price += $this->get_product_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_event_categories']) {
				$cat_ids = split("[,]", $get_result['restrict_to_event_categories']);
				for ($i = 0; $i <count($cat_ids); $i++) {
					for ($j=0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='E') continue;
						$cat_query = tep_db_query("select events_id from events_to_events_categories where events_id = '" . $products[$j]['id'] . "' and events_categories_id = '" . $cat_ids[$i] . "'");
						if (tep_db_num_rows($cat_query) !=0) {
							$total_price += $this->get_event_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_events']) {
				$ev_ids = split("[,]", $get_result['restrict_to_events']);
				for ($i = 0; $i < count($ev_ids); $i++) {
					for ($j = 0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='E') continue;
						if ($products[$j]['id'] == $ev_ids[$i]) {
							$total_price += $this->get_event_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_sub_categories']) {
				$cat_ids = split("[,]", $get_result['restrict_to_sub_categories']);
				for ($i = 0; $i <count($cat_ids); $i++) {
					for ($j=0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='S') continue;
						$cat_query = tep_db_query("select subscription_id from subscription_to_subscription_categories where subscription_id = '" . $products[$j]['id'] . "' and subscription_categories_id = '" . $cat_ids[$i] . "'");
						if (tep_db_num_rows($cat_query) !=0) {
							$total_price += $this->get_subscription_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}

			if ($get_result['restrict_to_subscription']) {
				$su_ids = split("[,]", $get_result['restrict_to_subscription']);
				for ($i = 0; $i < count($su_ids); $i++) {
					for ($j = 0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='S') continue;
						if ($products[$j]['id'] == $su_ids[$i]) {
							$total_price += $this->get_subscription_price($products[$j]['id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_service_categories']) {
				$cat_ids = split("[,]", $get_result['restrict_to_service_categories']);
				for ($i = 0; $i <count($cat_ids); $i++) {
					for ($j=0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='V') continue;
						$cat_query = tep_db_query("select service_id from services_to_services_categories where service_id = '" . $products[$j]['id'] . "' and services_categories_id = '" . $cat_ids[$i] . "'");
						if (tep_db_num_rows($cat_query) !=0) {
							$total_price += $this->get_service_price($products[$j]['sh_id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
			if ($get_result['restrict_to_service']) {
				$se_ids = split("[,]", $get_result['restrict_to_service']);
				for ($i = 0; $i < count($se_ids); $i++) {
					for ($j = 0; $j<count($products); $j++) {
						if ($products[$j]['element_type']!='V') continue;
						if ($products[$j]['id']==$se_ids[$i]) {
							$total_price += $this->get_service_price($products[$j]['sh_id'],$products[$j]['quantity'],$include_tax);
						//	$order->products[$j]['coupon_apply']=1;
						}
					}
				}
			}
	
			if ($this->include_shipping == 'true') $total_price += $order->info['shipping_cost'];
			if(tep_db_num_rows($coupon_get)>0) {
				$order_total = $total_price; 
			 }
		}
		return $order_total;
  }

function get_product_price($product_id,$qty,$include_tax) {
    global $cart, $order;
    $products_id = tep_get_prid($product_id);
     $product_query = tep_db_query("select products_id, products_price, products_tax_class_id,products_price_break,products_weight from " . TABLE_PRODUCTS . " where products_id='" . $product_id . "'");
    if ($product = tep_db_fetch_array($product_query)) {
      $prid = $product['products_id'];

      $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
      $products_price = $cart->get_product_price($products_id,$product,(int)$cart->contents[$products_id]['discount_id']);
      if ($this->include_tax == 'true' && $include_tax) {
        $total_price += ($products_price + tep_calculate_tax($products_price, $products_tax)) * $qty;
      } else {
        $total_price += $products_price * $qty;
      }
// attributes price
      if (isset($cart->contents[$product_id]['attributes'])) {
        reset($cart->contents[$product_id]['attributes']);
        while (list($option, $value) = each($cart->contents[$product_id]['attributes'])) {
          $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $prid . "' and options_id = '" . $option . "' and options_values_id = '" . $value . "'");
          $attribute_price = tep_db_fetch_array($attribute_price_query);
          if ($attribute_price['price_prefix'] == '+') {
            if ($this->include_tax == 'true') {
              $total_price += $qty * ($attribute_price['options_values_price'] + tep_calculate_tax($attribute_price['options_values_price'], $products_tax));
            } else {
              $total_price += $qty * ($attribute_price['options_values_price']);
            }
          } else {
            if ($this->include_tax == 'true' && $include_tax) {
              $total_price -= $qty * ($attribute_price['options_values_price'] + tep_calculate_tax($attribute_price['options_values_price'], $products_tax));
            } else {
              $total_price -= $qty * ($attribute_price['options_values_price']);
            }
          }
        }
      }
    }
    //if ($this->include_shipping == 'true') $total_price += $order->info['shipping_cost'];
	return $total_price;
}

	function get_event_price($events_id,$qty,$include_tax){
		global $cart,$languages_id;
		$result_query = tep_db_query("select e.events_id,ed.events_name,e.session_style,e.events_image,e.events_fees,e.waitfee_vip,e.waitfee_firstcome,e.waitfee_custom,e.events_tax_class_id from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id='" . $events_id . "' and e.events_id=ed.events_id and ed.language_id='" . (int)$languages_id . "'");
		
		$result = tep_db_fetch_array($result_query);
		
		$events_tax = tep_get_tax_rate($result['events_tax_class_id']);
		$events_price=$cart->get_event_price("E" .$events_id,$result,$events_tax);

		if ($this->include_tax == 'true' && $include_tax) {
			$total_price = ($events_price + tep_calculate_tax($events_price, $events_tax)) * $qty;
		} else {
			$total_price = $events_price * $qty;
		}
		return $total_price;
	}
	function get_subscription_price($subscription_id,$qty,$include_tax){
		global $cart,$languages_id;
		$subscription_query = tep_db_query("select subscription_id, subscription_costs, subscription_tax_class_id from " . TABLE_SUBSCRIPTIONS . " where subscription_id = '" . $subscription_id . "'");
		$subscription=tep_db_fetch_array($subscription_query);
		$suid = $subscription['subscription_id'];
		$subscription_tax = tep_get_tax_rate($subscription['subscription_tax_class_id']);
		$subscription_price = $cart->get_subscription_price($suid,$subscription['subscription_costs']);

		if ($this->include_tax == 'true' && $include_tax) {
			$total_price = ($subscription_price + tep_calculate_tax($subscription_price, $subscription_tax)) * $qty;
		} else {
			$total_price = $subscription_price * $qty;
		}
		return $total_price;
	}
	function get_service_price($service_id,$qty,$include_tax){
		global $cart,$languages_id,$languages_id;
		 $pieces = explode('{', $service_id);
		 $ser_id=substr($pieces[0],1);
		$services_query = tep_db_query("select se.service_id, sed.service_name,se.service_image, se.service_costs, se.include_resource_price,se.service_tax_class_id,se.service_total_time,se.booking_type from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed where se.service_id = '" . $ser_id . "' and se.service_id=sed.service_id and sed.language_id='" . $languages_id ."'");
		$services=tep_db_fetch_array($services_query);
		$service_tax_id=$services["service_tax_class_id"];
		$service_price=$cart->get_service_price($service_id,$services,$service_tax_id);
		$service_tax=tep_get_tax_rate($service_tax_id);
		if ($this->include_tax == 'true' && $include_tax) {
			$total_price = ($service_price + tep_calculate_tax($service_price, $service_tax)) * $qty;
		} else {
			$total_price = $service_price * $qty;
		}
		
		return $total_price;
	}
    function check() {
      if (!isset($this->check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_COUPON_STATUS'");
        $this->check = tep_db_num_rows($check_query);
      }
      return $this->check;
    }

    function keys() {
      return array('MODULE_ORDER_TOTAL_COUPON_STATUS', 'MODULE_ORDER_TOTAL_COUPON_SORT_ORDER', 'MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING', 'MODULE_ORDER_TOTAL_COUPON_INC_TAX', 'MODULE_ORDER_TOTAL_COUPON_CALC_TAX', 'MODULE_ORDER_TOTAL_COUPON_TAX_CLASS');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total', 'MODULE_ORDER_TOTAL_COUPON_STATUS', 'true', 'Do you want to display the Discount Coupon value?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_COUPON_SORT_ORDER', '9', 'Sort order of display.', '6', '2', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING', 'true', 'Include Shipping in calculation', '6', '3', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_ORDER_TOTAL_COUPON_INC_TAX', 'true', 'Include Tax in calculation.', '6', '4','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Re-calculate Tax', 'MODULE_ORDER_TOTAL_COUPON_CALC_TAX', 'None', 'Re-Calculate Tax', '6', '5','tep_cfg_select_option(array(\'None\', \'Standard\', \'Credit Note\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_COUPON_TAX_CLASS', '0', 'Use the following tax class when treating Discount Coupon as Credit Note.', '6', '6', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
    }

    function remove() {
      $keys = '';
      $keys_array = $this->keys();
      for ($i=0; $i<sizeof($keys_array); $i++) {
        $keys .= "'" . $keys_array[$i] . "',";
      }
      $keys = substr($keys, 0, -1);

      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
    }
    
    function collect_posts_forajax() {
    global $HTTP_POST_VARS, $customer_id, $currencies, $cc_id,$order,$HTTP_SESSION_VARS;
    tep_set_session_value('coupon_error',"");
     if (isset($HTTP_POST_VARS['coupon']) && isset($HTTP_POST_VARS['gv_redeem_code'])) {
	 $error="";
     // get some info from the coupon table
      $coupon_query=tep_db_query("select coupon_id, coupon_amount,coupon_code, coupon_type, coupon_minimum_order,coupon_flag,
                                       uses_per_coupon, uses_per_user, restrict_to_products,
                                       restrict_to_categories from " . TABLE_COUPONS . "
                                       where coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'
                                       and coupon_active='Y'");
                                       
      
     /* if ($coupon_result['coupon_type'] != 'G') 
        if (tep_db_num_rows($coupon_query)==0 ) {
          $error=ERROR_NO_INVALID_REDEEM_COUPON;
        }*/
		 if (tep_db_num_rows($coupon_query)> 0 ) {
			$now_date = getServerDate();
			$date_query=tep_db_query("select coupon_start_date from " . TABLE_COUPONS . "
									where coupon_start_date <= '" . $now_date . "' and
									coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
			
			if (tep_db_num_rows($date_query)==0) {
			  if(!$error) $error=ERROR_INVALID_STARTDATE_COUPON;
			}
			$date_query=tep_db_query("select coupon_expire_date,uses_per_user from " . TABLE_COUPONS . "
									where coupon_expire_date >= '" . $now_date . "' and
									coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
			
			if (tep_db_num_rows($date_query)==0) {
			  if(!$error) $error=ERROR_INVALID_FINISDATE_COUPON;
			}else if(tep_db_num_rows($date_query)){
				$date_res=tep_db_fetch_array($date_query);
				$this->expire_date=format_date(date('Y-m-d',strtotime($date_res['coupon_expire_date'])));
			}
		} else {
			 $coupon_query=tep_db_query("select c.coupon_id, ce.discount_coupon_code as coupon_code,ce.amount as coupon_amount, c.coupon_type, c.coupon_minimum_order,c.coupon_flag,
                                       c.uses_per_coupon, c.uses_per_user, c.restrict_to_products,
                                       c.restrict_to_categories from " . TABLE_COUPONS . " c, " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
                                       where ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."' and c.coupon_id=ce.coupon_id and ce.customer_id='" . $customer_id . "' 
                                       and coupon_active='Y'"); 
			 if (tep_db_num_rows($coupon_query)> 0 ) {
					$now_date = getServerDate();
					$date_query=tep_db_query("select c.coupon_start_date from " . TABLE_COUPONS .  " c, " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
											where c.coupon_start_date <= '" . $now_date . "' and c.coupon_id=ce.coupon_id and ce.customer_id='" . $customer_id . "'  and 
											ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
					
					if (tep_db_num_rows($date_query)==0) {
					  if(!$error) $error=ERROR_INVALID_STARTDATE_COUPON;
					}
					$date_query=tep_db_query("select c.coupon_expire_date,c.uses_per_user from " . TABLE_COUPONS .  " c, " . TABLE_COUPONS_DISCOUNT_EMAIL . " ce 
											where c.coupon_expire_date >= '" . $now_date . "' and c.coupon_id=ce.coupon_id and ce.customer_id='" . $customer_id . "' and 
											ce.discount_coupon_code='".tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']))."'");
					
					if (tep_db_num_rows($date_query)==0) {
					  if(!$error) $error=ERROR_INVALID_FINISDATE_COUPON;
					}else if(tep_db_num_rows($date_query)){
						$date_res=tep_db_fetch_array($date_query);
						$this->expire_date=format_date(date('Y-m-d',strtotime($date_res['coupon_expire_date'])));
					}
					$email_coupon=true;
				} else {
					 $error=ERROR_NO_INVALID_REDEEM_COUPON;
				}						   
		}
		
		$coupon_result=tep_db_fetch_array($coupon_query);
        $coupon_count = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . "
                                          where coupon_id = '" . $coupon_result['coupon_id']."'");
        $coupon_count_customer = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . "
                                                   where coupon_id = '" . $coupon_result['coupon_id']."' and
                                                   customer_id = '" . $customer_id . "'");
		
        if (tep_db_num_rows($coupon_count)>=$coupon_result['uses_per_coupon'] && $coupon_result['uses_per_coupon'] > 0) {
          if(!$error) $error=ERROR_INVALID_USES_COUPON . $coupon_result['uses_per_coupon'] . TIMES;
        }
		
        if($coupon_result['uses_per_user']){
          $this->uses_per_user=($coupon_result['uses_per_user']-tep_db_num_rows($coupon_count_customer));
		  if($this->uses_per_user<=0) $this->uses_per_user='Nil';
        }else {
			if($this->uses_per_user<0) $this->uses_per_user=0;
        }
        if (tep_db_num_rows($coupon_count_customer)>=$coupon_result['uses_per_user'] && $coupon_result['uses_per_user'] > 0 && $coupon_result['coupon_flag']=='U') {
          if(!$error) $error=ERROR_INVALID_USES_USER_COUPON . $coupon_result['uses_per_user'] . TIMES;
        }
        if ($coupon_result['coupon_type']=='S') {
          $coupon_amount = $order->info['shipping_cost'];
        } else {
          $coupon_amount = $currencies->format($coupon_result['coupon_amount']) . ' ';
        }
		
        if ($coupon_result['type']=='P') $coupon_amount = $coupon_result['coupon_amount'] . '% ';
        if ($coupon_result['coupon_minimum_order']>0) $coupon_amount .= 'on orders greater than ' .  $coupon_result['coupon_minimum_order'];
        $_SESSION['cc_id'] = $coupon_result['coupon_id'];
		if($email_coupon) { 
			if(tep_session_is_registered('email_redeem_code'))
				tep_session_unregister('email_redeem_code');
				
				tep_session_register('email_redeem_code');
				tep_set_session_value('email_redeem_code' ,$coupon_result['coupon_code']);
				
		}	else tep_session_unregister('email_redeem_code');
      
      if ($HTTP_POST_VARS['submit_redeem_coupon_x'] && !$HTTP_POST_VARS['gv_redeem_code']) $error=ERROR_NO_REDEEM_CODE;
    }else if(isset($HTTP_POST_VARS['coupon']) && !isset($HTTP_POST_VARS['gv_redeem_code'])) {
          if(!$error) $error=ERROR_NO_INVALID_REDEEM_COUPON;
	}else{
		tep_session_unregister('cc_id');
	} 

	if($order->info['subtotal']<$coupon_result['coupon_minimum_order'] ){
		  if(!$error) $error=ERROR_LOW_ORDER_TOTAL;
	}
	
	if($coupon_result['coupon_amount']>$order->info['subtotal']){ 
		  if(!tep_session_is_registered('coupon_exist')){
		  tep_session_register('coupon_exist');
		  $coupon_exist = 1;
		  if(!$error) $error=ERROR_LESSTHAN_COUPON_PRICE;
		}  
	}
	/*if($coupon_result['coupon_amount']==(float)$order->info['subtotal']){
		  if(!tep_session_is_registered('coupon_exist_equal_amount')){
		  tep_session_register('coupon_exist_equal_amount');
		  $coupon_exist_equal_amount = 1;
		  if(!$error) $error=ERROR_COUPON_PRICE_EQUAL;
		}  
	}*/
	if($error) tep_set_session_value('coupon_error',$error);
  } 
  
  }

?>