<?php
/*

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce
  
  

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
  http://www.openfreeway.org

Copyright 2007 ZacWare Pty. Ltd
*/
 // Check to ensure this file is included in osConcer!
//this are to be testing 
 
defined('_FEXEC') or die(); 
  class order {
    var $info, $totals, $products, $customer, $delivery;
    function __construct($order_id='',$retrieve_temp=false) {
      $this->info = array();
      $this->totals = array();
      $this->products = array();
      $this->customer = array();
      $this->delivery = array();
	  if (tep_not_null($order_id)) {
        $this->query($order_id,$retrieve_temp);
      } else {
        //$this->cart();
      }
    }
	
    function query($order_id,$retrieve_temp=false) {
	global $products_ordered, $currencies,$ordered_product, $order_totals_str;
	$ordered_product=""; 
 
		
      $order_query = tep_db_query("select o.customers_id,o.customers_name, o.customers_company, o.customers_language, o.customers_street_address, o.customers_suburb, o.customers_city, o.customers_postcode, o.customers_state, o.customers_country, o.customers_telephone, o.customers_email_address, o.customers_second_email_address,o.customers_second_telephone,o.customers_fax,
                                   o.customers_address_format_id, o.delivery_name, o.delivery_company, o.delivery_street_address, o.delivery_suburb, o.delivery_city, o.delivery_postcode, o.delivery_state, o.delivery_country, o.delivery_address_format_id, o.billing_name, o.billing_company, o.billing_street_address, o.billing_suburb, o.billing_city, o.billing_postcode, o.billing_state, o.billing_country, o.billing_address_format_id, o.payment_method, o.payment_info,o.cc_type, o.cc_owner, o.cc_number, o.cc_expires, o.cc_cvv_number,o.currency,o.shipping_date, o.currency_value,o.date_purchased, o.orders_status, o.last_modified, o.payment_return1, o.payment_return2, o.reference_id, c.customers_firstname, c.customers_lastname,o.ip_address from " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c where o.orders_id = '" . (int)$order_id . "' and o.customers_id=c.customers_id");
	  
	  $order = tep_db_fetch_array($order_query);
	 

	   $totals_query = tep_db_query("select title, text,class, value from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order_id . "' order by sort_order");
      while ($totals = tep_db_fetch_array($totals_query)) {
        $this->totals[] = array('title' => $totals['title'],
								'class'=>$totals['class'],
                                'text' => $totals['text'],
								'value' => $totals['value']);
						
      }
	  	$order_total_query = tep_db_query("select text from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order_id . "' and class = 'ot_total'");
      $order_total = tep_db_fetch_array($order_total_query);
	  	$order_tax_query = tep_db_query("select text from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order_id . "' and class = 'ot_tax'");
      $order_tax = tep_db_fetch_array($order_tax_query);
		// get the comments
		$comments_query=tep_db_query("SELECT comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id='" . (int)$order_id . "' order by date_added desc limit 1");
		$comments="";
		$comments_result=tep_db_fetch_array($comments_query);
		$comments=$comments_result["comments"];
      $this->info = array('currency' => $order['currency'],
                          'currency_value' => $order['currency_value'],
                          'payment_method' => $order['payment_method'],
						  'payment_info'=>$order['payment_info'],
                          'cc_type' => $order['cc_type'],
                          'cc_owner' => $order['cc_owner'],
                          'cc_number' => $order['cc_number'],
                          'cc_expires' => $order['cc_expires'],
			  			  'cc_cvv_number'=> $order['cc_cvv_number'],
  						  'status_id'=>$order['orders_status'],
						  'comments'=>$comments,
						  'payment_return1' => $order['payment_return1'],
						  'payment_return2' => $order['payment_return2'],
						  'reference_id' => $order['reference_id'],
                          'date_purchased' => $order['date_purchased'],
                          'date_purchasing' => $order['date_purchasing'],
  						  'shipping_date' => $order['shipping_date'],
						  'total'=>strip_tags($order_total['text']),
						  'orders_status' => $order['orders_status'],
						  'ip_address'=>$order['ip_address'],
                          'last_modified' => $order['last_modified'],
						  'shipping_method' => ((substr($shipping_method['title'], -1) == ':') ? substr(strip_tags($shipping_method['title']), 0, -1) : strip_tags($shipping_method['title'])));
						  
      $this->customer = array('id'=> $order['customers_id'],
	  						  'firstname'=>$order['customers_firstname'],
							  'lastname'=>$order['customers_lastname'],
                              'name' => $order['customers_name'],
                              'company' => $order['customers_company'],
							  'language' => $order['customers_language'],
                              'street_address' => $order['customers_street_address'],
                              'suburb' => $order['customers_suburb'],
                              'city' => $order['customers_city'],
                              'postcode' => $order['customers_postcode'],
                              'state' => $order['customers_state'],
                              'country' => $order['customers_country'],
                              'format_id' => $order['customers_address_format_id'],
                              'telephone' => $order['customers_telephone'],
			    			  'second_telephone'=>$order['customers_second_telephone'],
			   			      'second_email_address'=>$order['customers_second_email_address'],
			   			      'fax'=>$order['customers_fax'],
                              'email_address' => $order['customers_email_address']);
							  
							

      $this->delivery = array('name' => $order['delivery_name'],
	  						  'customer_email' => $order['delivery_email'],//new added 14-08-17
                              'company' => $order['delivery_company'],
                              'street_address' => $order['delivery_street_address'],
                              'suburb' => $order['delivery_suburb'],
                              'city' => $order['delivery_city'],
                              'postcode' => $order['delivery_postcode'],
			                  'customer_phone' => $order['delivery_phone'],
                              'state' => $order['delivery_state'],
                              'country' => $order['delivery_country'],
                              'format_id' => $order['delivery_address_format_id']);

      if (empty($this->delivery['name']) && empty($this->delivery['street_address'])) 
	  {
        $this->delivery = false;
      }

      $this->billing = array('name' => $order['billing_name'],
                             'company' => $order['billing_company'],
							 'customer_email' => $order['billing_email'],//new added 14-08-17
                             'street_address' => $order['billing_street_address'],
                             'suburb' => $order['billing_suburb'],
                             'city' => $order['billing_city'],
                             'postcode' => $order['billing_postcode'],
							 'customer_phone' => $order['billing_phone'],
                             'state' => $order['billing_state'],
                             'country' => $order['billing_country'],
                             'format_id' => $order['billing_address_format_id']);
							 
							
      $index = 0;
	  $pcnt=0;
	 if(!$retrieve_temp){
		  $orders_products_query = tep_db_query("select orders_products_id, products_id, products_name,  products_model, products_price, products_tax, products_quantity, final_price,products_type,events_type,orders_products_status,products_sku,support_packs_type,discount_text,discount_id, categories_name, concert_venue, concert_date, concert_time from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$order_id . "' order by products_model,products_name");

	 }else{
	      $orders_products_query = tep_db_query("select orders_products_id, products_id, products_name,date_format(products_model,'%Y-%m-%d') as products_model,products_price, products_tax, products_quantity, final_price,products_type,events_type,products_sku from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id = '" . (int)$order_id . "' order by products_model,products_name");
	 }	  	  
	  	$products_ordered = '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
		$products_ordered.= '<tr class="textTableSubHead" height="21"><td width="5">&nbsp;' . TEXT_QUANTITY .'</td><td colspan="2">'.TEXT_PRODUCTS_NAME.'</td><Td>SKU</td><td align="center" width="100">Total</td></tr>';
		  while ($orders_products = tep_db_fetch_array($orders_products_query)) 
		  {
		  	$discount_whole_text='';
	  		if ($orders_products["discount_id"]>0 && $orders_products["discount_text"]!=''){
	  			$discount_whole_text=TEXT_DISCOUNT_APPLIED . '&nbsp;' . $orders_products["discount_text"] .'<br>' . TEXT_ORIGINAL_AMOUNT . '&nbsp;' . $currencies->format(tep_add_tax($orders_products['products_price'],$orders_products['products_tax'])*$orders_products['products_quantity']) . '<br>' . TEXT_DISCOUNT . '&nbsp;' .$currencies->format(tep_add_tax($orders_products['products_price']-$orders_products['final_price'],$orders_products['products_tax'])*$orders_products['products_quantity']) . '<br>';
			}

		  //if(!$retrieve_temp)
			$this->products[$index] = array('id'=>$orders_products['products_id'],
											'orders_products_id'=>$orders_products['orders_products_id'],
											'qty' => $orders_products['products_quantity'],
											'name' => $orders_products['products_name'],
											'model' => $orders_products['products_model'],
											'orders_products_status'=>$orders_products['orders_products_status'],
											'tax' => $orders_products['products_tax'],
											'is_printable' => $orders_products['is_printable'],
											'price' => $orders_products['products_price'],
											'final_price' => $orders_products['final_price'],
											'products_type'=>$orders_products['products_type'],
											'events_type'=>$orders_products['events_type'],
											'products_sku' =>$orders_products['products_sku'],
											'discount_whole_text'=>$discount_whole_text,
											'discount_text' => $orders_products["discount_text"],
											'categories_name'=> $orders_products['categories_name'],
											'concert_venue'=> $orders_products['concert_venue'],
											'concert_date'=> $orders_products['concert_date'],
											'concert_time'=> $orders_products['concert_time'],
											'support_packs_type'=>$orders_products['support_packs_type'] //Support Packs
									);
										
			$subindex = 0;


			if($this->products[$index]['products_type']=='P'){

				$product_query = tep_db_query("Select product_mode,datediff(download_last_date,CURDATE()) as download_last_date,download_link,downloads_per_customer  from " . TABLE_PRODUCTS . " where products_id='" . (int)$orders_products['products_id'] . "'");
				if($prd_result=tep_db_fetch_array($product_query))
				{
					$this->products[$index]['product_mode'] = $prd_result['product_mode'];
					$this->products[$index]['download_link'] = $prd_result['download_link'];
					$this->products[$index]['download_last_date'] = $prd_result['download_last_date'];
					$this->products[$index]['downloads_per_customer'] = $prd_result['downloads_per_customer'];
				}
				$product_query=tep_db_query("select products_image_1 from ".TABLE_PRODUCTS." where products_id='".(int)$this->products[$index]["id"]."'");
				$product_result=tep_db_fetch_array($product_query);
				
				$products_ordered .= '<tr class="textTableContent" height="56">' . 
										'<td valign="top"  width="60"><br>&nbsp;'.$this->products[$index]['qty'].' x </td>' .'</td>' . 
										'<td width="75" valign="top"><br>'.tep_product_email_image($product_result["products_image_1"],$this->products[$index]['name'],'style="width:50px;height:50px"').'</td>' .
										'<td valign="top"><br>&nbsp;&nbsp;'.$this->products[$index]['name'] . ($this->products[$index]['model']!=""?' (' . $this->products[$index]['model'] . ')':'') . '<br>' . $products_ordered_attributes . '</td>' . 
										'<td valign="top"><br>'. $sku .'</td>' .
										'<td valign="top" align="right" class="textMain"><br>'.$currencies->display_price($this->products[$index]['final_price'], $this->products[$index]['tax'], $this->products[$index]['qty'],true).'&nbsp;</td>' .
									'</tr>' .
									'<tr>' .
										'<td colspan="5" height="1" class="textBorder">' . tep_draw_separator('pixel_trans.gif',10,1) . '</td>' . 
									'</tr>';
														
										
			}
			$this->products[$index]['sku'] = $sku;

			$res_index=0;			

	
			$products_tax = $this->products[$index]['tax'];
			$products_tax_description = $this->products[$index]['tax_description'];
			
			
			$shown_price = tep_add_tax($this->products[$index]['final_price'] , $this->products[$index]['tax'])  * $this->products[$index]['qty'];
			$this->info['subtotal']+=$shown_price;
			
			if (DISPLAY_PRICE_WITH_TAX == 'true') {
				$this->info['tax'] += $shown_price - ($shown_price / (($products_tax < 10) ? "1.0" . str_replace('.', '', $products_tax) : "1." . str_replace('.', '', $products_tax)));
				if (isset($this->info['tax_groups']["$products_tax_description"])) {
					$this->info['tax_groups']["$products_tax_description"] += $shown_price - ($shown_price / (($products_tax < 10) ? "1.0" . str_replace('.', '', $products_tax) : "1." . str_replace('.', '', $products_tax)));
				} else {
					$this->info['tax_groups']["$products_tax_description"] = $shown_price - ($shown_price / (($products_tax < 10) ? "1.0" . str_replace('.', '', $products_tax) : "1." . str_replace('.', '', $products_tax)));
				}
			} else {
				$this->info['tax'] += ($products_tax / 100) * $shown_price;
				if (isset($this->info['tax_groups']["$products_tax_description"])) {
					$this->info['tax_groups']["$products_tax_description"] += ($products_tax / 100) * $shown_price;
				} else {
					$this->info['tax_groups']["$products_tax_description"] = ($products_tax / 100) * $shown_price;
				}
			}
			$index++;
			$pcnt++;
			}
	  $products_ordered.="</table>";
	  

	if (DISPLAY_PRICE_WITH_TAX == 'true') {
		$this->info['total'] = $this->info['subtotal'];
	} else {
		$this->info['total'] = $this->info['subtotal'] + $this->info['tax'];
	}
	$this->info['total']=tep_round($this->info['total'],2);

	}
 function get_order_country_id($country_name) {
    $country_id_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_name = '" . tep_db_input($country_name) . "'");
    if (!tep_db_num_rows($country_id_query)) {
      return 0;
    }
    else {
      $country_id_row = tep_db_fetch_array($country_id_query);
      return $country_id_row['countries_id'];
    }
  }	
 function get_order_country_name($country_id) {
    $country_name_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_name = '" . tep_db_input($country_id) . "'");
    if (!tep_db_num_rows($country_name_query)) {
      return 0;
    }
    else {
      $country_name_row = tep_db_fetch_array($country_name_query);
      return $country_name_row['countries_name'];
    }
  }	

  }
  
  ?>