<?
set_time_limit(3000);
define('RELATIVE_PATH','include/html2fpdf/');
define('FPDF_FONTPATH','include/html2fpdf/font/');
require_once("include/html2fpdf/html2fpdf.php");
include_once("modules/EcmProductReports/vtigerConnector.php");

$vc=new vtigerConnector();
$vc->start=1;
$wc[]="1=1";
$wc[]="purchaseordercf.cf_528='0'";
$wc[]="purchaseorder.tracking_no!=''";
$vc1->where=@implode(" and ",$wc);
$vc->getData();
$cnt=$vc->getCount();
$data=$vc->data;

if(count($data)>0){
	foreach($data as $d){
		if(trim($d['tracking_no'])==trim($_GET['tracking_no'])){
			$dd[]=$d;
		}
	}
}
$data=$dd;
$ile=ceil((count($data)+1)/17);
$pdf = new HTML2FPDF("L");

$j=0;
$k=0;
$m=0;
for($i=0;$i<count($data)+1;$i++){
	if($j==0){
		$html.='<table border="1" width="1050"><tr height="80" width="1050">';
		$html.='<td height="80" align="center" width="210"><img src="modules/EcmPriceBooks/e5.png" border="0" width="200" height="70"></td>';
		$html.='<td height="80" align="center" width="840"><font color="#808080">'.iconv("UTF-8","ISO-8859-2",$GLOBALS['timedate']->to_display_date($data[$i]['deliverydate'])." ".$_GET['tracking_no']).'</font></td>';
		$html.='</tr></table>';
		$html.='<table border="1" width="1050"><tr align="center" height="30" width="1220">';
		$html.='<td bgcolor="#FF6600" align="center" height="30" width="150"><font color="#ffffff">Index</font></td>';
		$html.='<td bgcolor="#FF6600" align="center" height="30" width="630"><font color="#ffffff">Name</font></td>';
		$html.='<td bgcolor="#FF6600" align="center" height="30" width="90"><font color="#ffffff">Quantity</font></td>';
		$html.='<td bgcolor="#FF6600" align="center" height="30" width="90"><font color="#ffffff">Cartons</font></td>';
		$html.='<td bgcolor="#FF6600" align="center" height="30" width="90"><font color="#ffffff">CBM</font></td>';
		$html.='</tr>';
	}
	
	if($i==count($data)){
	
		$html.='<tr height="30" width="300">';		
		$html.='<td valign="top" align="center" height="30" width="40"></td>';
		$html.='<td valign="top" align="center" height="30" width="80"></td>';
		$html.='<td valign="top" align="center" height="30" width="40"><b>'.iconv("UTF-8","ISO-8859-2",$qty_sum).'</b></td>';
		$html.='<td valign="top" align="center" height="30" width="60"><b>'.iconv("UTF-8","ISO-8859-2",round($cartons_sum,0)).'</b></td>';
		$html.='<td valign="top" align="center" height="30" width="60"><b>'.iconv("UTF-8","ISO-8859-2",number_format($cbm_sum,2,",",".")).'</b></td>';
		$html.='</tr>';
	}
	else{
		$id=$vc->getIdByCode($data[$i]['productcode']);
		$r=mysql_fetch_array(mysql_query("select pieces_per_carton as ppc,carton_volume_meter as cvm from ecmproducts where id='".$id."'"));
		@$cartons=$data[$i]['quantity']/$r['ppc'];
		@$cbm=$data[$i]['quantity']*$r['cvm']/$r['ppc'];
		$qty_sum+=$data[$i]['quantity'];
		$cartons_sum+=$cartons;
		$cbm_sum+=$cbm;
	
		$html.='<tr height="30" width="300">';		
		$html.='<td valign="top" align="center" height="30" width="40">'.iconv("UTF-8","ISO-8859-2",$data[$i]['productcode']).'</td>';
		$html.='<td valign="top" align="center" height="30" width="80">'.iconv("UTF-8","ISO-8859-2",$data[$i]['productname']).'</td>';
		$html.='<td valign="top" align="center" height="30" width="40">'.iconv("UTF-8","ISO-8859-2",$data[$i]['quantity']).'</td>';
		$html.='<td valign="top" align="center" height="30" width="60">'.iconv("UTF-8","ISO-8859-2",round($cartons,0)).'</td>';
		$html.='<td valign="top" align="center" height="30" width="60">'.iconv("UTF-8","ISO-8859-2",number_format($cbm,2,",",".")).'</td>';
		$html.='</tr>';
	}
	
	$j++;
	$k++;
	$m++;

	if($j==17 || count($data)+1==$k){
		$st++;
		$html.='</table>';
		$html.='<table border="1" width="1050"><tr height="30" width="1050">';
		$html.='<td bgcolor="#FF6600" height="30" align="center" width="200"><font color="#ffffff">www.e5.pl</font></td>';
		$html.='<td bgcolor="#FF6600" height="30" align="right" width="850">'.$st.'/'.$ile.'&nbsp;&nbsp;&nbsp;&nbsp;</td>';
		$html.='</tr></table>';
		$ohtml=$html;
		$html='';
		$pdf->AddPage();
		$pdf->WriteHTML($ohtml); 
		$j=0;
	}
}
//echo $html;
$pdf->Output("Containers.pdf", "D");
?>