<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 

  class editOrderCart {
    var $contents, $total, $weight, $cartID, $content_type,$orders_id,$attendees;
    function editOrderCart($orders_id) {
      $this->orders_id=(int)$orders_id;
	  $this->get_attendees();
	  
	  $this->depo = array();
    }

	function restore_contents() {
		if (tep_session_is_registered('edit_order')==true && (int)$HTTP_SESSION_VARS['edit_order']!=$this->orders_id){
			$this->reset($HTTP_SESSION_VARS['edit_order']);
			tep_session_unregister('edit_order');
		}    
		$this->reset($this->orders_id);
		$products_query=tep_db_query("SELECT orders_id,products_id,products_model,products_name,products_price,final_price,products_tax,products_quantity,products_quantity,products_type,events_type,waitlist_type,events_fees,waitlist_orders_id,orders_products_id from " . TABLE_ORDERS_PRODUCTS . " where orders_id='" . $this->orders_id . "'");
		
		while($products_detail=tep_db_fetch_array($products_query)){
		   //service details 
		   $service_order_query=tep_db_query("SELECT * from " . TABLE_ORDERS_SERVICES . " where orders_id='" . $this->orders_id . "' and orders_products_id='".(int)$products_detail['orders_products_id']."'");
		   //subscription details
		   $subscription_order_query=tep_db_query("SELECT * from " . TABLE_ORDERS_SUBSCRIPTIONS . " where orders_id='" . $this->orders_id . "' and orders_products_id='".(int)$products_detail['orders_products_id']."'");
		   // service details 
			tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$products_detail);
			$insert_id=tep_db_insert_id();
			// service details
				$service_order_detail=tep_db_fetch_array($service_order_query);
				$service_order_detail['orders_products_id']=$insert_id;
				tep_db_perform(TABLE_ORDERS_EDIT_SERVICES,$service_order_detail);
			//subscription details
			$subscription_order_detail=tep_db_fetch_array($subscription_order_query);
			$subscription_order_detail['orders_products_id']=$insert_id;
			tep_db_perform(TABLE_ORDERS_EDIT_SUBSCRIPTIONS,$subscription_order_detail);
			//service details
			$attributes_query=tep_db_query("SELECT orders_products_id,orders_id,products_options,products_options_values,options_values_price,price_prefix from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id='" . $this->orders_id . "' and orders_products_id='" . $products_detail['orders_products_id'] . "'");
			while($attributes_detail=tep_db_fetch_array($attributes_query)){
				$attributes_detail['orders_products_id']=$insert_id;
				tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES,$attributes_detail);
			}
		}
	}

    function store_final_contents($detail_only=false) {
       global $currencies,$coupon_class,$HTTP_POST_VARS;       
       $check_products=array();
       $old_order_query=tep_db_query("SELECT products_id,products_type,products_name from " . TABLE_ORDERS_PRODUCTS . " where orders_id='" . $this->orders_id . "'");
       while($old_order_result=tep_db_fetch_array($old_order_query)){
           $check_products[$old_order_result['products_type'] . $old_order_result['products_id']]['status']='N';
           $check_products[$old_order_result['products_type'] . $old_order_result['products_id']]['name']=$old_order_result['products_name'] . ($old_order_result['products_type']=='E'? '(' . tep_get_session_date($old_order_result['products_id']) .')':'');
       }
       
       tep_db_query("delete from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . $this->orders_id . "'");
       tep_db_query("delete from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . $this->orders_id . "'");
       tep_db_query("delete from " . TABLE_ORDERS_SERVICES . " where orders_id = '" . $this->orders_id . "'");
       
		$tax=0.0;
		$updated_subtotal=0.0;
		$updated_tax=0.0;
		$updated_total=0.0;
		$add_product_string='';
		$delete_product_string='';
		$order_query=tep_db_query("SELECT currency,currency_value from " . TABLE_ORDERS . " where orders_id='" . $this->orders_id . "'");
		$order_info=tep_db_fetch_array($order_query);
		$prev_order=0;
		$products_query=tep_db_query("SELECT orders_id,orders_products_id,products_id,products_model,products_name,products_price,final_price,products_tax,products_quantity,products_type,events_type,waitlist_type,events_fees,waitlist_orders_id,products_sku from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id='" . $this->orders_id . "'");
		//echo "SELECT orders_id,orders_products_id,products_id,products_model,products_name,products_price,final_price,products_tax,products_quantity,products_type,events_type,waitlist_type,events_fees,waitlist_orders_id from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id='" . $this->orders_id . "'"; 
		while($products_detail=tep_db_fetch_array($products_query)){
			$prev_order=(int)$products_detail['waitlist_orders_id'];
			$orders_products_id=$products_detail["orders_products_id"];
			//service details
			$service_order_query=tep_db_query("SELECT * from " . TABLE_ORDERS_EDIT_SERVICES . " where orders_id='" . $this->orders_id . "' and orders_products_id='".(int)$orders_products_id."'");
			//subscription details
			$subscription_order_query=tep_db_query("SELECT * from " . TABLE_ORDERS_EDIT_SUBSCRIPTIONS . " where orders_id='" . $this->orders_id . "' and orders_products_id='".(int)$orders_products_id."'");
			//service details
			unset($products_detail["orders_products_id"]);
			tep_db_perform(TABLE_ORDERS_PRODUCTS,$products_detail);
			
			$insert_id=tep_db_insert_id();
			if ($prev_order>0) {
				tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='C', waitlist_type='' where orders_id='" . $prev_order . "' and products_id='" . $products_detail['products_id'] . "'");
			}
			//subscriptions
			 $subscription_order_detail=tep_db_fetch_array($subscription_order_query);
			 $subscription_order_detail['orders_products_id']=$insert_id;
			 tep_db_perform(TABLE_ORDERS_SUBSCRIPTIONS,$subscription_order_detail);
			//service
			 $service_order_detail=tep_db_fetch_array($service_order_query);
			 $service_order_detail['orders_products_id']=$insert_id;
			 tep_db_perform(TABLE_ORDERS_SERVICES,$service_order_detail);
			//service
			$attributes_id="";
			$attributes_query=tep_db_query("SELECT orders_id,orders_products_id,products_options,products_options_id,products_options_values_id,products_options_values,options_values_price,price_prefix from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_id='" . $this->orders_id . "' and orders_products_id='" . $orders_products_id . "'");
			while($attributes_detail=tep_db_fetch_array($attributes_query)){
				$attributes_detail['orders_products_id']=$insert_id;
				$attributes_detail['products_type']=$products_detail['products_type'];
				if($attributes_detail['products_options_id'] == "") $attributes_detail['products_options_id']="0";
				if($attributes_detail['products_options_values_id'] == "") $attributes_detail['products_options_values_id']="0";
				tep_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES,$attributes_detail);
				$attributes_id.=$attributes_detail['products_options_id'] . '{' . $attributes_detail['products_options_values_id'] . '}-';
			}
			if($attributes_id!=""){
				$attributes_id=substr($attributes_id,0,-1);
				$attributes_id=tep_get_sorted_attribute_ids($attributes_id);
			}
				tep_db_query("UPDATE " . TABLE_PRODUCTS_STOCK . " set products_quantity=products_quantity-" . $products_detail['products_quantity'] . ",products_ordered=products_ordered+" . $products_detail['products_quantity']  . " where products_id='" . $products_detail['products_id'] . "' and attributes_id='" . $attributes_id . "'");
				tep_db_query("update " . TABLE_PRODUCTS_STOCK . " set attribute_status=0 where products_quantity<=0 and products_id=" . $products_detail['products_id'] . " and attributes_id='" . $attributes_id . "'");
			tep_db_query("UPDATE " . TABLE_PRODUCTS . " set products_quantity=products_quantity-" . $products_detail['products_quantity'] . ",products_ordered=products_ordered+" . $products_detail['products_quantity']  . " where products_id='" . $products_detail['products_id'] . "'");

			$tax=(($products_detail['products_tax'] / 100) * ($products_detail['products_quantity'] * $products_detail['final_price']));
			$updated_subtotal += (($products_detail['products_quantity'] * $products_detail['final_price'])+$tax);
			$updated_tax += $tax;
			if (isset($check_products[$products_detail['products_type'] . $products_detail['products_id']])==false){
				$add_product_string.=$products_detail['products_name'] . ($products_detail['products_type']=='E'? '(' . tep_get_session_date($products_detail['products_id']) .')':'') . '<br>';
			} else{
				$check_products[$products_detail['products_type'] . $products_detail['products_id']]['status']='Y';
			}
		}
		$updated_subtotal=tep_get_rounded_amount($updated_subtotal);
		$updated_total=$updated_subtotal;

		
		$total_change_string='';
		if (!$detail_only){
			$old_totals_query=tep_db_query("SELECT title,text,class from " . TABLE_ORDERS_TOTAL . " where orders_id='" . $this->orders_id ."' order by sort_order");
			while($old_totals=tep_db_fetch_array($old_totals_query)){
			if ($old_totals['class']=='ot_subtotal')
				$total_change_string.= '<i>' . $old_totals['title'] . '</i> from ' . $old_totals['text'] . ' to ' . $currencies->format($updated_subtotal,true,$order_info['currency'],$order_info['currency_value']) . '<br>';
			else if ($old_totals['class']=='ot_tax')
				$total_change_string.= '<i>' . $old_totals['title'] . '</i> from ' . $old_totals['text'] . ' to ' . $currencies->format($updated_tax,true,$order_info['currency'],$order_info['currency_value']) . '<br>';
			else if ($old_totals['class']=='ot_total')
				$total_change_string.= '<i>' . $old_totals['title'] . '</i> from ' . $old_totals['text'] . ' to <b>' . $currencies->format($updated_total,true,$order_info['currency'],$order_info['currency_value'] . '</b>');
			} 
				reset($check_products);
				while (list($key,$value)=each($check_products)){
					if ($value['status']=='N')
					$delete_product_string.=$value['name'] .'<br>';
				}
					//-------------------
				$coupon_class->process();
				$size = sizeof($coupon_class->output);
				$get_order_total = $coupon_class->get_order_total;
			
			  $order_total=$updated_subtotal;
			  $deduction=$coupon_class->pre_confirmation_check($order_total);
			  
			  if($HTTP_POST_VARS['coupon']==1){
			  		if($get_order_total < $deduction) $order_total = $order_total - $get_order_total;
			 	 	if($get_order_total > $deduction) $order_total = $order_total - $deduction;
			  		if ($order_total <= 0 )  $order_total=0; 
			  }
			  
			  $updated_total=$order_total;
			  $coupon_value = 0;
			//-------------------------	
			if($deduction>0)
			{
				//$coupon_class->process();
				//$size = sizeof($coupon_class->output);
			
				$output_string=""; 
                for ($i=0; $i<$size; $i++) {
    				tep_db_query("INSERT into " . TABLE_ORDERS_TOTAL . " (orders_id,title,text,value,sort_order,class) values('" . $this->orders_id . "','" . $coupon_class->output[$i]['title'] . "','" . $coupon_class->output[$i]['text'] . "','" . $deduction . "','4','ot_coupon')");
					$coupon_value = $coupon_class->output[$i]['text'];
					$i=$size;
				}
			}
			$updated_total -= $coupon_value;
			if($updated_total<0)$updated_total=0;
			tep_db_query("UPDATE " . TABLE_ORDERS_TOTAL . " set value='" . number_format($updated_subtotal,'2','.','') . "', text='" . $currencies->format($updated_subtotal,true,$order_info['currency'],$order_info['currency_value']) . "' where orders_id='" . $this->orders_id . "' and class='ot_subtotal'");
			tep_db_query("UPDATE " . TABLE_ORDERS_TOTAL . " set value='" . number_format($updated_tax,'2','.','') . "', text='" . $currencies->format($updated_tax,true,$order_info['currency'],$order_info['currency_value']) . "' where orders_id='" . $this->orders_id . "' and class='ot_tax'");
			tep_db_query("UPDATE " . TABLE_ORDERS_TOTAL . " set value='" . number_format($updated_total,'2','.','') . "', text='<b>" .  $currencies->format($updated_total,true,$order_info['currency'],$order_info['currency_value']) . "</b>' where orders_id='" . $this->orders_id . "' and class='ot_total'");
		}
		$this->reset($this->orders_id);
		$final_string="";
		if ($add_product_string!="")
			$final_string.='<u>Products Added</u><br>' . $add_product_string . '<br>';
		if ($delete_product_string!="")
			$final_string.='<u>Products Removed</u><br>' . $delete_product_string . '<br>';
		if ($total_change_string!="")
			$final_string.='<u>Total Changes</u><br>' . $total_change_string .'<br>';
		
		return $final_string;
    }

    function reset($oID) {

      $this->contents = array();
      $this->total = 0;
      $this->weight = 0;
      $this->content_type = false;
     
      tep_db_query("delete from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id = '" . (int)$oID . "'");
      tep_db_query("delete from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$oID . "'");
		tep_db_query("delete from " . TABLE_ORDERS_EDIT_SERVICES . " where orders_id = '" . (int)$oID . "'");
		tep_db_query("delete from " . TABLE_ORDERS_EDIT_SUBSCRIPTIONS . " where orders_id = '" . (int)$oID . "'");
	  tep_db_query("delete from " . TABLE_ORDERS_EDIT_ATTENDEES . " where orders_id='" . (int)$oID . "'");
    }
	
	// Modified multiple attendees
	function set_attendees($attendees){
		$this->attendees=$attendees;
		$this->save_attendees();
	}
	function save_attendees(){
		$attendees=$this->attendees;
		tep_db_query("DELETE from " . TABLE_ORDERS_EDIT_ATTENDEES . " where orders_id='" .  $this->orders_id . "'");
		for ($icnt=0;$icnt<count($attendees);$icnt++){
			tep_db_perform(TABLE_ORDERS_EDIT_ATTENDEES,$attendees[$icnt]);
		}
	}
	function remove_attendees(){
		global $customer_id;
		tep_db_query("DELETE from " . TABLE_ORDERS_EDIT_ATTENDEES . " where orders_id='" .  $this->orders_id . "'");
		$attendees=array();
	}
	function get_attendees(){
		$query=tep_db_query("SELECT * from " . TABLE_ORDERS_EDIT_ATTENDEES . " where orders_id='" .  $this->orders_id . "' order by attendee_id");
		$attendees=array();
		if (tep_db_num_rows($query)>0){
			while($result=tep_db_fetch_array($query)){
				$attendees[]=$result;
			}
			$this->attendees=$attendees;
		}
	}
	
	function get_attendee_names($break='<br>'){
		$result="";
		$attendees=$this->attendees;
		for ($icnt=0;$icnt<count($attendees);$icnt++){
			$result.=$attendees[$icnt]["first_name"] . ' ' . $attendees[$icnt]["last_name"] . $break;
		}
		if ($result!=""){
			$result=substr($result,0,-(strlen($break)));
		}
		return $result;
	}
	function check_attendees(){
		$attendees=&$this->attendees;
		if (count($attendees)<=0) return false;
		$valid=true;
		for ($icnt=0;$icnt<count($attendees);$icnt++){
			if ($attendees[$icnt]["first_name"]==""){
				$valid=false;
				break;
			}
		}
		return $valid;
	}
	}
	// Modified multiple attendees}
	?>
