<?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 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
	define('FPDF_FONTPATH','font/');
	require('includes/fpdf.php');
    include(DIR_WS_CLASSES . 'order.php');
	require(DIR_WS_CLASSES . 'currencies.php');
//	require('includes/functions/freeway_general.php');
	
	
	$currencies = new currencies();
	$product_ids=isset($HTTP_GET_VARS["product_ids"])?$HTTP_GET_VARS["product_ids"]:-1;
	$order_ids=isset($HTTP_GET_VARS["order_ids"])?$HTTP_GET_VARS["order_ids"]:-1;
	$cat_id=isset($HTTP_GET_VARS["cat_id"])?$HTTP_GET_VARS["cat_id"]:0;
	//$date_clause=isset($HTTP_GET_VARS["date_clause"])?$HTTP_GET_VARS["date_clause"]:'';
	$start_date=isset($HTTP_GET_VARS['start_date'])?$HTTP_GET_VARS['start_date']:'';
	$end_date=isset($HTTP_GET_VARS['end_date'])?$HTTP_GET_VARS['end_date']:'';
	$date_clause = " and (date_format(o.date_purchased,'%Y-%m-%d')>='". $start_date ."' and date_format(o.date_purchased,'%Y-%m-%d')<='" . $end_date . "')";
	//$date_clause=str_replace("\'","'",$date_clause);
	if($product_ids!="-1" && $order_ids!="-1")
		$order_sql="select cus.customers_firstname,cus.customers_lastname,cus.customers_id,o.orders_id,o.date_purchased,ord.products_id,o.customers_email_address,ord.products_quantity,ord.products_name,ord.orders_products_id,ord.orders_products_status as status from " .  TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " cus," . TABLE_ORDERS_PRODUCTS . " ord where ord.orders_id=o.orders_id and o.customers_id=cus.customers_id and ord.products_type='P' and ord.orders_products_status<3 and o.orders_status=2 and (ord.products_id in ( " . $product_ids . ") or ord.orders_products_id in(" . $order_ids.  "))" . $date_clause . " order by cus.customers_id,o.orders_id";
	elseif($product_ids!="-1")
		$order_sql="select cus.customers_firstname,cus.customers_lastname,cus.customers_id,o.orders_id,o.date_purchased,ord.products_id,o.customers_email_address,ord.products_quantity,ord.products_name,ord.orders_products_id,ord.orders_products_status as status from " .  TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " cus," . TABLE_ORDERS_PRODUCTS . " ord where ord.orders_id=o.orders_id and o.customers_id=cus.customers_id and ord.products_type='P' and ord.orders_products_status<3  and o.orders_status=2 and ord.products_id in ( " . $product_ids . ")" . $date_clause . " order by cus.customers_id,o.orders_id";
	elseif($order_ids!="-1")
		$order_sql="select cus.customers_firstname,cus.customers_lastname,cus.customers_id,o.orders_id,o.date_purchased,ord.products_id,o.customers_email_address,ord.products_quantity,ord.products_name,ord.orders_products_id,ord.orders_products_status as status from " .  TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " cus," . TABLE_ORDERS_PRODUCTS . " ord where ord.orders_id=o.orders_id and o.customers_id=cus.customers_id and ord.products_type='P' and ord.orders_products_status<3  and o.orders_status=2 and ord.orders_products_id in ( " . $order_ids. ")" . $date_clause . " order by cus.customers_id,o.orders_id";
	elseif($cat_id>0) //products of category
		$order_sql="select cus.customers_firstname,cus.customers_lastname,cus.customers_id,o.orders_id,o.date_purchased,ord.products_id,o.customers_email_address,ord.products_quantity,ord.products_name,ord.orders_products_id,ord.orders_products_status as status from " .  TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " cus," . TABLE_ORDERS_PRODUCTS . " ord," . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p2c.products_id=ord.products_id and p2c.categories_id='" . $cat_id . "' and ord.orders_id=o.orders_id and o.customers_id=cus.customers_id and ord.products_type='P' and ord.orders_products_status<3  and o.orders_status=2 " . $date_clause . " order by cus.customers_id,o.orders_id";
	else		//all products of all category
		$order_sql="select cus.customers_firstname,cus.customers_lastname,cus.customers_id,o.orders_id,o.date_purchased,ord.products_id,o.customers_email_address,ord.products_quantity,ord.products_name,ord.orders_products_id,ord.orders_products_status as status from " .  TABLE_ORDERS . " o," . TABLE_CUSTOMERS . " cus," . TABLE_ORDERS_PRODUCTS . " ord where ord.orders_id=o.orders_id and o.customers_id=cus.customers_id and ord.products_type='P' and ord.orders_products_status<3  and o.orders_status=2  " . $date_clause . " order by cus.customers_id,o.orders_id";
	$found_results=false;
		
	$customer_query=tep_db_query($order_sql);
	$customer_orders=tep_db_num_rows($customer_query);
	if ($customer_orders) $found_results=true;
	// set page setup default width and height
	$pdf=new FPDF("P","mm","A4");
	
	$unit=72/25.4;
	$page_width=595.28;
	$pos_left=5;
	$pos_top=5;
	$content=array();
	$invoice_width=125;
	$invoice_height=55;
	$page_name="images/reports_invoice_". $login_id ."_"  .time(). ".pdf";
	//$oscomm_logo_image=DIR_FS_CATALOG_IMAGES . 'freeway.png';
	//$oscomm_logo_image= DIR_FS_ADMIN . DIR_WS_IMAGES . 'freeway.png';DIR_FS_TEMPLATES
	$oscomm_logo_image= DIR_FS_TEMPLATES.DEFAULT_TEMPLATE.'/'.DIR_WS_IMAGES . COMPANY_LOGO;
	//echo DIR_FS_TEMPLATES.DEFAULT_TEMPLATE.'/'.DIR_WS_IMAGES . "<br>" . 'freeway.png';
	if ($found_results){
		$prev_order_id=0;
		$prev_product_id=0;
		$orders_products_id="";
		while($customer_result=tep_db_fetch_array($customer_query))
		{
			$o_id=$customer_result["orders_id"];
			$order = new order($o_id);
			$orders_det[]=array("id"=>$o_id,
							"date"=>$customer_result["date_purchased"],
							"payment_detail"=>get_payment_detail($order->info['status_id']),
							"payment_method"=>$order->info['payment_method'],
							"customer_format_id"=>$order->customer['format_id'],
							"customer_add" =>$order->customer,
							"delivery_format_id"=>$order->delivery['format_id'],
							"delivery_add"=>$order->delivery
							);
			for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
			  if($order->products[$i]['id']==$customer_result['products_id'])
				{
					//check attributes is found
						$att_keys="";
						$att_query=tep_db_query("select products_options_id,products_options_values_id from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_products_id=".$order->products[$i]['orders_products_id'] . " order by products_options_id");
						if(tep_db_num_rows($att_query)>0){
							while($attribs=tep_db_fetch_array($att_query)){
								$att_keys.=$attribs['products_options_id']."{".$attribs['products_options_values_id']."}-"; 
							}	
						}
						$att_keys=substr($att_keys,0,-1);
					//check attributes is found   tep_get_sku_ids($customer_result['products_id'],$att_keys)
				   $ord_tot=tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']) * $order->products[$i]['qty'];
					$details[$o_id][]=array("products_id"=>$row_p["products_id"],
											"orders_id" =>$o_id,
											"quantity"=>$order->products[$i]['qty'],
											"item"=>$order->products[$i]['qty'] . ' x ' . $order->products[$i]['name'],
											"sku"=>$order->products[$i]['sku'],
											"tax"=>$order->products[$i]['tax'],
											"price"=>$order->products[$i]['final_price'],
											"total_ex"=>$order->products[$i]['final_price'] * $order->products[$i]['qty'],
											"total_inc"=>$ord_tot,
											"currency"=>$order->info['currency'],
											"currency_value"=>$order->info['currency_value'],
											"orders_products_id"=>$order->products[$i]['orders_products_id'],
											"shipping_value"=>$order->products[$i]['shipping_value']
											);
				  	if($orders_products_id!="")
				  	 	$orders_products_id.=",";
				  	$orders_products_id.=$customer_result['orders_products_id'];
				  	break;
				}
			}
		}
		for($icnt=0,$max_ord=sizeof($orders_det);$icnt<$max_ord;$icnt++)
		{
		  if(strpos("," . $orders_printed . ",","," . $orders_det[$icnt]['id'] . ",")===false)
		  {
			$pdf->AddPage();
			$pdf->SetFont('Arial','B',12);
			$pdf->SetTextColor(114,114,114);
			$pdf->SetLineWidth(0);
			$pos_left=20;
			$pos_top=20;
			// create bar image
			if (file_exists($oscomm_logo_image)){
				$info_logo=getimagesize($oscomm_logo_image);
				$info_logo[0]=($info_logo[0]/$unit)*74/100;
				$info_logo[1]=($info_logo[1]/$unit)*74/100;
				$logo_width=$info_logo[0];
			}
			$pos_width=$invoice_width-$logo_width-2;
			$content_width=$pdf->w/2 ;//$invoice_width-$logo_width;
			//echo $pdf->width . ' ' . $logo_width;
			
			if (isset($info_logo))
				$pdf->Image($oscomm_logo_image,($pdf->w-$logo_width-15),$pos_top);
			$store_add=split('<br>',str_replace("\n","<br>",STORE_NAME_ADDRESS));
			for($it=0,$max=sizeof($store_add);$it<$max;$it++)
			{
				$pdf->Text($pos_left,$pos_top,$store_add[$it]);
				$pos_top+=5;
			}
			$pos_top+=5;
			$pdf->SetFont('Arial','B',14);
			$pdf->Text($content_width-15,$pos_top,TEXT_TAX_INVOICE);
			$pos_top+=5;
			$pdf->SetTextColor(0,0,0);
			$pdf->SetFont('Arial','B',10);
			$pdf->Text($pos_left,$pos_top,TEXT_ORDER);
			$pdf->Text($content_width+30,$pos_top,TEXT_ORDER_DATE);
			$pdf->Text($content_width+30+$pdf->GetStringWidth(TEXT_ORDER_DATE)+2,$pos_top,format_date($orders_det[$icnt]["date"]) . ' ' . date('h:i:s A',strtotime($orders_det[$icnt]["date"])));
			$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_ORDER)+2,$pos_top,$orders_det[$icnt]['id'] );
			$pdf->Text($pos_left,$pos_top+7,TEXT_PAYMENT_METHOD);
			//=====modified text===============================================
			//=================================================================
			$payment_method=strtolower($orders_det[$icnt]['payment_method']);
			if(strpos($payment_method,"<img")===false){
				$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_PAYMENT_METHOD)+2,$pos_top+7,$orders_det[$icnt]['payment_method']);
				$pos_top+=7;
			}
			else  //having image tag
			{
				$payment_text=substr($orders_det[$icnt]['payment_method'],0,strpos($payment_method,"<img"));
				$payment_text=str_replace("&nbsp;"," ",$payment_text);
				$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_PAYMENT_METHOD)+2,$pos_top+7,$payment_text);
				$image_tag=substr($orders_det[$icnt]['payment_method'],strpos($payment_method,"<img"));
				$img_details=split(" ",$image_tag);
				$image_width=80; //default values;
				$image_ht=30;
				for($k=0;$k<count($img_details);$k++)
				{
					if(strpos($img_details[$k],"src=")!==false)
					{
						$image_name=str_replace("src=","",$img_details[$k]);
						$image_name=str_replace('"',"",$image_name);
					}
					if(strpos($img_details[$k],"width=")!==false)
					{
						$image_width=str_replace("width=","",$img_details[$k]);
						$image_width=str_replace('"',"",$image_width);
					}
					if(strpos($img_details[$k],"height=")!==false)
					{
						$image_ht=str_replace("height=","",$img_details[$k]);
						$image_ht=str_replace('"',"",$image_ht);
					}
				}
				$image_width=($image_width/$unit)*74/100+10;
				$image_ht=($image_ht/$unit)*74/100+5;
				if($image_name!="" && file_exists(DIR_FS_ADMIN . $image_name))
				{
					//$pdf->Image($image_name,$pos_left+$pdf->GetStringWidth(TEXT_PAYMENT_METHOD)+2+$pdf->GetStringWidth($payment_text)+1,$pos_top+4,$image_width,$image_ht);
					$pos_top+=$image_ht-5;
				}
				$pos_top+=7;
			}
			//==================end==================================

			$pdf->SetFont('Arial','',10);
			
		//	$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_ORDER)+2,$pos_top,$orders_det[$icnt]['id'] );
		//	$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_PAYMENT_METHOD)+2,$pos_top+7,$orders_det[$icnt]['payment_method']);
		//	$pdf->Text($content_width+30+$pdf->GetStringWidth(TEXT_ORDER_DATE)+2,$pos_top,format_date(strftime($orders_det[$icnt]["date"])) . ' ' . strftime('%X',$orders_det[$icnt]["date"]));
		//	$pos_top+=14;
			//Getting the comments
			$comment_query= tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id =" . $orders_det[$icnt]['id']);
			$comment = tep_db_fetch_array($comment_query);
			//--------
			$add_arr=split("<br>",tep_address_format($orders_det[$icnt]['customer_format_id'], $orders_det[$icnt]['customer_add'], 1, '', '<br>'));
			$address_label=tep_address_format($orders_det[$icnt]['delivery_format_id'], $orders_det[$icnt]['delivery_add'], 1, '', '<br>');
			$add_arr_length = count($add_arr)-1;
			$temp = split(',',$add_arr[$add_arr_length]); 
			$add_arr[$add_arr_length] = $temp[0];
			$add_arr[$add_arr_length+1] = $temp[1];
			$pdf->SetDrawColor(153,152,152);
			$pdf->SetFillColor(240,241,241);
			if($address_label!="")
			{ 
			    if($comment['comments']!=""){
				$pos_top+=5;
				$pdf->SetFont('Arial','B',10);
				$pdf->Text($pos_left,$pos_top,TEXT_ORDER_COMMENTS);
				$pdf->SetFont('Arial','',10);
				$replace_array = array("</tr>","&nbsp;","&nbsp");
				$replaced_by = array("<br>"," ","");
				 $comment['comments'] = str_replace($replace_array,$replaced_by,$comment['comments']);
				 $strip_comment= strip_tags($comment['comments'],'<br>');
				 $split_comment = spliti('<br>',$strip_comment);
				 for($i=0;$i<count($split_comment);$i=$i+1){
				   $pdf->Text($pos_left+31,$pos_top,html_entity_decode($split_comment[$i])); 
				   $pos_top+=4; 
				 }
				}
				$pos_top+=3;
				$pdf->Rect($pos_left,$pos_top,35,sizeof($add_arr)*4+5,'FD');
				$lable_arr=split("<br>",$address_label);
				$lable_arr_length = count($lable_arr)-1;
				$temp = split(',',$lable_arr[$lable_arr_length]);
				$lable_arr[$lable_arr_length] = $temp[0];
				$lable_arr[$lable_arr_length+1] = $temp[1];
				$pdf->Rect($content_width+50,$pos_top,35,sizeof($lable_arr)*4+5,'FD'); 
			}
			$pdf->SetLineWidth(5);
			$pdf->SetDrawColor(201,201,201);
			$pdf->Line($pos_left+3,$pos_top+3,$pos_left-3+35,$pos_top+3);
			$pdf->Line($content_width+50+3,$pos_top+3,$content_width+50-3+35,$pos_top+3);
			$pdf->SetFont('Arial','B',8);
			$pdf->SetTextColor(0,0,0);
			$pdf->Text($pos_left+1,$pos_top+4,TEXT_SOLD_TO);
			$pdf->Text($content_width+50+1,$pos_top+4,TEXT_SHIP_TO);
			$pos_top+=8;
			$pdf->SetFont('Arial','',8);
			for($it=0,$max=sizeof($add_arr);$it<$max;$it++)
			{ 
				$pdf->Text($pos_left+1,$pos_top,$add_arr[$it]);
				$pos_top+=4;
			}
			$pos_top=$pos_top-(4*$max);
			if(isset($lable_arr))
			{
			    for($it=0,$max=sizeof($lable_arr);$it<$max;$it++)
				{
					$pdf->Text($content_width+50+1,$pos_top,$lable_arr[$it]);
					$pos_top+=4;
				}
			}
			$n = sizeof($details[$orders_det[$icnt]['id']]);
			$r_count=$n;
			$rect_top=$pos_top;
			$pdf->SetLineWidth(5);
			$pdf->SetDrawColor(201,201,201);
			$pdf->Line($pos_left+3,$pos_top+3,$pos_left-3+$invoice_width+45,$pos_top+3);
			$pdf->Text($pos_left+3,$pos_top+4,TEXT_ITEMS);
			//$pdf->Text($pos_left+60,$pos_top+4,TEXT_SKU);
			$pdf->Text($pos_left+85,$pos_top+4,TEXT_TAX);
			$pdf->Text($pos_left+110,$pos_top+4,TEXT_PRICE);
			$pdf->Text($pos_left+130,$pos_top+4,TEXT_TOTAL_EX);
			$pdf->Text($pos_left+155,$pos_top+4,TEXT_TOTAL_INC);
			$pdf->SetFont('Arial','',8);
			$pos_top+=4;
			$inv_subtotal=0;
			$inv_tax=0;
			$inv_shipping=0;
			
			
			
			for ($i = 0 ; $i < $n; $i++) {
			

			
				  $row=$details[$orders_det[$icnt]['id']][$i];
				  
				  $product_name=split("\n",wordwrap($row['item'],30,"\n"));
				  $pdf->SetDrawColor(240,241,241);
				  $pdf->SetLineWidth(5);
				  $pdf->Line($pos_left+3,$pos_top+5,$pos_left-3+$invoice_width+45,$pos_top+5);
				  $pdf->Text($pos_left+2,$pos_top+5,$product_name[0]);
				  $pdf->Text($pos_left+60,$pos_top+5,get_discount_details($row['orders_id'],$row['orders_products_id']));
				  $pdf->Text($pos_left+86,$pos_top+5,tep_display_tax_value($row['tax']) . '%');
				  $pdf->Text($pos_left+110,$pos_top+5,$currencies->format($row['price'], true, $row['currency'], $row['currency_value']));
				  $pdf->Text($pos_left+130,$pos_top+5,$currencies->format($row['total_ex'],true, $row['currency'], $row['currency_value']));
				  $pdf->Text($pos_left+155,$pos_top+5,$currencies->format($row['total_inc'],true, $row['currency'], $row['currency_value']));
				
				 // $pdf->Text(print_r(get_discount_details($o_id,$details[$o_id][$i]['products_id'])));
//print_r(get_discount_details($row['orders_id'],$row['orders_products_id']));
				
				
				  $pos_top+=4;
				  if (count($product_name)>1){
				  	for ($tcnt=1,$tn=count($product_name);$tcnt<$tn;$tcnt++){
					  $pdf->SetDrawColor(240,241,241);
					  $pdf->SetLineWidth(5);
					  $pdf->Line($pos_left+3,$pos_top+5,$pos_left-3+$invoice_width+45,$pos_top+5);
						$pdf->Text($pos_left+2,$pos_top+5,$product_name[$tcnt]);
						$pos_top+=4;
						$r_count=$r_count+1;
					}
				  }
				  $att_query=tep_db_query("select products_options,products_options_values from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_products_id=".$row['orders_products_id'] . " order by products_options_id");
				  if(tep_db_num_rows($att_query)>0)
				  {
				    //$pos_top+=5;
				  	while($att=tep_db_fetch_array($att_query))
					{
						$pos_top+=4;
						$pdf->SetDrawColor(240,241,241);
				  		$pdf->SetLineWidth(5);
				  		$pdf->Line($pos_left+2,$pos_top,$pos_left-2+$invoice_width+45,$pos_top);
						$pdf->Text($pos_left+5,$pos_top,$att['products_options'] . ' : ' . $att['products_options_values'] );
						$r_count=$r_count+1;
					}
				  }
				  if($row['sku']!=""){
					  $pdf->SetDrawColor(240,241,241);
					  $pdf->SetLineWidth(5);
					  $pdf->Line($pos_left+3,$pos_top+5,$pos_left-3+$invoice_width+45,$pos_top+5);
					  $pdf->Text($pos_left+3,$pos_top+5,TEXT_SKU . ":" . $row['sku']);
					  $r_count=$r_count+1;
					  $pos_top+=4;
				  }
				  $inv_subtotal+=$row['total_inc'];
				  $inv_tax+=$row['quantity'] * ($row['price'] * $row['tax']/100);
				  $inv_shipping+=$row['shipping_value'];
			}
			$pdf->SetLineWidth('');
			$pdf->SetDrawColor(153,152,152);
			$pdf->Rect($pos_left,$rect_top,$invoice_width+45,$r_count*4+7);
			//$inv_subtotal=$currencies->format($inv_subtotal,true, $row['currency'], $row['currency_value']);
			$tax_value = $inv_tax;
			$shipping_value = $inv_shipping;
			//$inv_tax=$currencies->format($inv_tax,true, $row['currency'], $row['currency_value']);
			//$inv_shipping=$currencies->format($shipping_value,true, $row['currency'], $row['currency_value']);
			$pdf->SetLineWidth('');
			$r_width=$pdf->GetStringWidth($orders_det[$icnt]['payment_detail']);
			$r_width=($r_width>100)?60:45;
			$pdf->SetDrawColor(153,152,152);
			$pdf->Rect($pos_left,$pos_top+8,$invoice_width+45,$r_width+5,'D');
			$pos_top+=15;
			$temp_top=$pos_top;
			$inv_total=$inv_shipping+$inv_subtotal;			
			$order_total=array('ot_subtotal'=>array(INVOICE_SUBTOTAL,$inv_subtotal),'ot_tax'=>array(INVOICE_TAX,$tax_value),'ot_shipping'=>array(INVOICE_SHIPPING,$shipping_value),'ot_total'=>array(INVOICE_TOTAL,$inv_total));
			$order_status_query=tep_db_query("SELECT class from " . TABLE_ORDERS_TOTAL . " where orders_id='" . $o_id . "' order by sort_order");
			$inv_width=$pos_left+$invoice_width+43;
			$temp_width=0;
			while($order_status=tep_db_fetch_array($order_status_query)){
				$order_value=&$order_total[$order_status["class"]];
				if ($order_value[1]>0){
					if ($order_status["class"]=="ot_total"){
						$order_text=sprintf("%14s",$currencies->format(tep_get_rounded_amount($order_value[1]),true, $row['currency'], $row['currency_value']));
					} else {
						$order_text=sprintf("%14s",$currencies->format($order_value[1],true, $row['currency'], $row['currency_value']));
					}
					$pdf->Text($inv_width-$pdf->GetStringWidth($order_text),$pos_top,$order_text);
					$pdf->Text($inv_width-20-$pdf->GetStringWidth($order_value[0]),$pos_top,$order_value[0]);
					$pos_top+=4;
				}
			}
			$pos_top=$temp_top+23;
			/*$inv_subtotal=$currencies->format($inv_subtotal,true, $row['currency'], $row['currency_value']);
			$tax_value = $inv_tax;
			$shipping_value = $inv_shipping;
			$inv_tax=$currencies->format($inv_tax,true, $row['currency'], $row['currency_value']);
			$inv_shipping=$currencies->format($inv_shipping,true, $row['currency'], $row['currency_value']);
			$pdf->SetLineWidth('');
			$r_width=$pdf->GetStringWidth($orders_det[$icnt]['payment_detail']);
			$r_width=($r_width>100)?60:45;
			$pdf->SetDrawColor(153,152,152);
			$pdf->Rect($pos_left,$pos_top+8,$invoice_width+45,$r_width,'D');
			$pos_top+=8;
			$temp_text=sprintf(TEXT_AMOUNT,$inv_subtotal);
			$inv_width=$pos_left+$invoice_width+43-($pdf->GetStringWidth($temp_text));
			$pdf->Text($inv_width-($pdf->GetStringWidth(INVOICE_SUBTOTAL)),$pos_top+5,INVOICE_SUBTOTAL);
			if($shipping_value>0){
				$pdf->Text($inv_width-($pdf->GetStringWidth(INVOICE_SHIPPING)),$pos_top+9,INVOICE_SHIPPING);
				$pdf->Text($inv_width-($pdf->GetStringWidth(TEXT_INV_TOTAL)),$pos_top+13,TEXT_INV_TOTAL);
				if($tax_value>0)$pdf->Text($inv_width-($pdf->GetStringWidth(INVOICE_TAX)),$pos_top+17,INVOICE_TAX);
			}else{
				$pdf->Text($inv_width-($pdf->GetStringWidth(TEXT_INV_TOTAL)),$pos_top+9,TEXT_INV_TOTAL);
				if($tax_value>0)$pdf->Text($inv_width-($pdf->GetStringWidth(INVOICE_TAX)),$pos_top+13,INVOICE_TAX);
			}
			$pdf->Text($inv_width,$pos_top+5,sprintf(TEXT_AMOUNT,$inv_subtotal));
			if($shipping_value>0){
				$pdf->Text($inv_width,$pos_top+9,sprintf(TEXT_AMOUNT,$inv_shipping));
				$pdf->Text($inv_width,$pos_top+13,sprintf(TEXT_AMOUNT,$inv_subtotal));
				if($tax_value>0)$pdf->Text($inv_width+1,$pos_top+17,sprintf(TEXT_AMOUNT,$inv_tax ));
			}else{
				$pdf->Text($inv_width,$pos_top+9,sprintf(TEXT_AMOUNT,$inv_subtotal));
				if($tax_value>0)$pdf->Text($inv_width+1,$pos_top+13,sprintf(TEXT_AMOUNT,$inv_tax ));
			}
			$pos_top+=25;*/
			$pdf->SetFont('Arial','',8);
			//	Don't Need on OSCOMMRES2
			/*$arr_payment=split("<br>",$orders_det[$icnt]['payment_detail']);
			for($k=0,$max=sizeof($arr_payment);$k<$max;$k++)
			{
				if(strpos(strtolower($arr_payment[$k]),'<b>')!==false)
				{
					$pdf->SetFont('Arial','B',9);
					$arr_payment[$k]=str_replace('<b>','',strtolower($arr_payment[$k]));
					$arr_payment[$k]=str_replace('</b>','',strtolower($arr_payment[$k]));
				}
				//$pdf->Text($pos_left+3,$pos_top,$arr_payment[$k]);
				$pos_top+=5;
			}*/
			$pdf->Line($pos_left,$pos_top+3,$pos_left+$invoice_width+45,$pos_top+3);
			$pdf->SetFont('Arial','',9);
			$pdf->Text($content_width-$pdf->GetStringWidth(TEXT_FOOTER)/2,$pos_top+7,TEXT_FOOTER);
			$pdf->Line($pos_left,$pos_top+9,$pos_left+$invoice_width+45,$pos_top+9);
			$pdf->Line($pos_left,$pos_top+11,$pos_left+$invoice_width+45,$pos_top+11);
		} 
		$orders_printed.="," . $orders_det[$icnt]['id'];
	  }
  		$pdf->output(DIR_FS_CATALOG . $page_name);
	} else {
		$pdf->AddPage();
		$pdf->SetFont('Arial','B',10);
		$pdf->Text(85,20,TEXT_NO_RECORDS_FOUND);
		$pdf->output(DIR_FS_CATALOG . $page_name);
	}
	if($orders_products_id!="")
	{
		//$orders_products_id=substr($order_products_id,0,-1);
		//echo "update  " . TABLE_ORDERS_PRODUCTS . "  set invoice_printed=1,invoice_printed_time='" . date("y-m-d H:i:s") . "' where orders_products_id in(" .$orders_products_id. ")";
		//exit;
		tep_db_query("update  " . TABLE_ORDERS_PRODUCTS . "  set invoice_printed=1,invoice_printed_time='" . date("y-m-d H:i:s") . "' where orders_products_id in(" .$orders_products_id. ")");
	}
	tep_redirect(DIR_WS_CATALOG . $page_name);

function get_payment_detail($status)
{
	switch($status)
	{
	case 3:
		$payment_detail=PAYMT_PRODUCT_C;
		break;
	case 2:
		$payment_detail=PAYMT_PRODUCT_R;
		break;
	case 1:
		$payment_detail=sprintf(PAYMT_PRODUCT_P,STORE_NAME,STORE_NAME_ADDRESS);
		break;
	}
	return $payment_detail;
}
?>