<?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');
	
	$currencies = new currencies();
	$product_ids=isset($HTTP_GET_VARS["product_ids"])?$HTTP_GET_VARS["product_ids"]:0;
	$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;
	$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";


	//echo $order_sql;
	$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_packslip_" .$login_id . "_" .time(). ".pdf";
	//$oscomm_logo_image=DIR_FS_ADMIN . DIR_WS_IMAGES . 'invoice_logo1.png';
	//$oscomm_logo_image= DIR_FS_ADMIN . DIR_WS_IMAGES . 'freeway.png';
	$oscomm_logo_image= DIR_FS_TEMPLATES.DEFAULT_TEMPLATE.'/'.DIR_WS_IMAGES . COMPANY_LOGO;
	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,
							"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,
							"telephone"=>$order->customer['telephone'],
							"email"=>$order->customer['email_address']
							);
			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'],
											"orders_products_id"=>$order->products[$i]['orders_products_id']
											);
				  	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 ;

			if (isset($info_logo))
				$pdf->Image($oscomm_logo_image,($pdf->w-$logo_width-13),$pos_top-5);
			$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_ORDER . $orders_det[$icnt]['id'] );
			$pos_top+=5;
			$pdf->SetTextColor(0,0,0);
			$pdf->SetFont('Arial','',10);
			$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>');
			$pdf->SetDrawColor(114,114,114);
			$pdf->SetFillColor(240,241,241);
			$pdf->Rect($pos_left,$pos_top,$invoice_width-28,sizeof($add_arr)*4+5+2*10+3,'FD');
			if($address_label!="")
			{
				$lable_arr=split("<br>",$address_label);
				$pdf->Rect($pos_left+$invoice_width-27,$pos_top,$invoice_width-53,sizeof($lable_arr)*4+5,'FD');
			}
			$pdf->SetLineWidth(5);
			$pdf->SetDrawColor(201,201,201);
			$pdf->Line($pos_left+3,$pos_top+3,$pos_left+$invoice_width-28-3,$pos_top+3);
			$pdf->Line($pos_left+$invoice_width-27 + 3,$pos_top+3,$pos_left+ $invoice_width +50-8,$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($pos_left+$invoice_width-27+1,$pos_top+4,TEXT_SHIP_TO);
			$pdf->SetFont('Arial','',8);
			$pos_top+=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($pos_left+$invoice_width-27 +1,$pos_top,$lable_arr[$it]);
						$pos_top+=4;
					}
			}
			$pdf->SetFont('Arial','B',8);
			$pdf->Text($pos_left+3,$pos_top,TEXT_TELEPHONE);
			$pdf->Text($pos_left+3,$pos_top+10,TEXT_EMAIL_ADDRESS);
			$pdf->Text($pos_left,$pos_top+20+4,TEXT_PAYMENT_METHOD);
			$pdf->SetFont('Arial','',8);
			$pdf->Text($pos_left+3,$pos_top+3,$orders_det[$icnt]['telephone']);
			$pdf->Text($pos_left+3,$pos_top+13,$orders_det[$icnt]['email']);
			$pos_top+=20+4;
			//========modified===========
			//$pdf->Text($pos_left+$pdf->GetStringWidth(TEXT_PAYMENT_METHOD)+4,$pos_top,$order->info['payment_method']);
			$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,$orders_det[$icnt]['payment_method']);
				$pos_top+=5;
			}
			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,$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-3,$image_width,$image_ht);
					$pos_top+=$image_ht-5;
				}
				$pos_top+=5;
			}
			//==============================
			//$pos_top+=5;
			$pdf->SetDrawColor(114,114,114);
			$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_PRODUCTS);
			$pdf->Text($pos_left+$invoice_width+5,$pos_top+4,TEXT_SKU);
			$pos_top+=4;
			for ($i = 0 ; $i < $n; $i++) {
				  $row=$details[$orders_det[$icnt]['id']][$i];
				   $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,$row['item']);
				  $pdf->Text($pos_left+$invoice_width+5,$pos_top+5,$row['sku']);
				  $pos_top+=4;
				  $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;
					}
				  }
			}
		}
		$pdf->SetLineWidth('');
		$pdf->SetDrawColor(153,152,152);
		$pdf->Rect($pos_left,$rect_top,$invoice_width+45,$r_count*4+7);
		$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);
	//echo $orders_products_id;
	//exit;
	if($orders_products_id!="")
	{
		//$orders_products_id=substr($order_products_id,0,-1);
		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_db_query("update  " . TABLE_ORDERS_PRODUCTS . "  set packing_slip_printed=1,packslip_printed_time='" . date("y-m-d H:i:s") . "' where orders_products_id in(" .$orders_products_id. ")");
	}
	tep_redirect(DIR_WS_CATALOG . $page_name);

?>