<?php

//Report Version For Winco
session_start();

if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}
		
		
include 'function.inc.php';
include("config.inc.php");

if($exp ==1){
	$filename = "Transfer_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

print <<< HERE
<HTML>
<HEAD>
<TITLE> Transfer Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 13px;
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}						
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 3px;
   }   

h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 180px;
   }
</style>


</HEAD>

<BODY>
HERE;


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//Read From Module Table
$querymodule="Select ifNull(Module_TransferWithOneWh,0) As Module_TransferWithOneWh From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_VatNo as SetupVatNo, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail  ".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);


//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter , Module_ShowLayoutProj , Module_ShowLayoutCost From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$ModuleProject=$Module_Project;
$ModuleCostCenter=$Module_CostCenter;




if($transfer_id!='')
{
	$query = "SELECT TRANSFER_REFERENCE , date_format(transfer.TRANSFER_DATE,'%d/%m/%Y') as TRANSFER_DATE, Transfer_Remark , transfer.Project_code1 , transfer.Project_code2 , 
				Costcenter_Code 
				, Driver_Name , Vehicle_No ,ToCostcenter_Code ,Document_RefNo
				from transfer 
				left join transferdt on transferdt.transfer_id = transfer.transfer_id
				where transfer.transfer_id = ".$transfer_id."
				group by transfer.TRANSFER_REFERENCE , transfer.TRANSFER_DATE , transfer.Transfer_Remark , transfer.Project_code1 ,
				transfer.Project_code2 , transfer.Costcenter_Code , Transfer_WarehouseFrom ,
				Transfer_WarehouseTo , Driver_Name , Vehicle_No
				";
				


	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}


	echo "<table border=0 width=635 cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >";
	echo"   <col width=115>	  
			<col width=340>	  
			<col width=100>
			<col width=80>";
				  
	echo"	<tr>";
	echo"		<td ><img src='LogoFile.php' width='115' height='50' /></td>";
	echo"		<td align='left' colspan='3'><span style='font-size:22px; font-weight:bold; padding-left:7px;'>$CompanyName</span> <br>";
	echo"			<span style='font-size:11px; font-weight:bold; padding-left:7px;'>".nl2br($CompanyMemo)." </span><br><br></td>";
	echo"	</tr>";
	
	echo"	<tr><td align='center' rowspan='2'>&nbsp;</td>";

	if ($TransType=='Rm')	$ReportTitle = "Transfer RM Waybill ".$TRANSFER_REFERENCE;
	else	$ReportTitle = "Transfer Waybill ".$TRANSFER_REFERENCE;
		
	echo"	<td align='center' valign='middle' rowspan='2' style='font-size:22px; font-weight:bold;'>$ReportTitle</td>";	
	echo"	<td align='left'><font size='2'><b>Printed&nbsp;Date</b></font></td>";
	echo"	<td align='left'><font size='2'>".$printdate[2]."/".$printdate[1]."/".$printdate[0]."</font></td>";
	echo "</tr>";
	
	echo"	<tr>";
	echo"	<td align='left'><font size='2'><b>Time</b></font></td>";
	echo"	<td align='left'><font size='2'>".$dt[1]." ".$dt[2]."</font></td>";
	echo "</tr>";
	
	echo"</table>";	
	
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >";
	echo"   <col width=100>	  
			<col width=355>	  
			<col width=80>	  
			<col width=100>";

		
	echo "<tr height='5'><td colspan=4><h5></td></tr>";

	echo"<tr><td bgcolor=#D8D8D8 valign='top'><b>Remark</b></td>";
	echo"	<td valign='top'>$Transfer_Remark</td>";
	echo"	<td valign='top' bgcolor=#D8D8D8><b>Date</b></td>";
	echo"	<td valign='top'>$TRANSFER_DATE</td>";
	echo"</tr>";

	echo"<tr height='5'><td colspan=4><h4></td></tr>";
	echo"</table></td></tr>";

	



	if ($detail==0) {
		$query = "select transferdt.Item_CodeFinished as Item_code, transferdt.Item_Barcode Item_Barcode, ifnull(Transferdt.Item_qtyPacked,0) as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id,  count(*) as itemscount
					 ,TransferDt_WarehouseTo from transferdt , items
					where items.item_code = transferdt.Item_CodeFinished
					And transferdt.Item_CodeFinished Is Not Null
					and transfer_id = ".$transfer_id."
					group by Transferdt.Item_CodeFinished, Transferdt.TransferDt_WarehouseTo 
					Union
					select transferdt.Item_CodeSemiFinished Item_code, transferdt.Item_Barcode Item_Barcode, ifnull(Transferdt.Item_qtyPacked,0) as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id , count(*) as itemscount
					 ,TransferDt_WarehouseTo
					 from transferdt , items
					where items.item_code = transferdt.Item_CodeSemiFinished
					And transferdt.Item_CodeSemiFinished Is Not Null
					and transfer_id = ".$transfer_id."
					group by Transferdt.Item_CodeSemiFinished, Transferdt.TransferDt_WarehouseTo 
					union
					select transferdt.Item_code Item_code, transferdt.Item_Barcode Item_Barcode, TransferDt_Quantity as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id , 1 as itemscount
					,TransferDt_WarehouseTo
					from transferdt , items
					where items.item_code = transferdt.item_code
					And transferdt.Item_CodeSemiFinished Is Null
					And transferdt.Item_CodeFinished Is Null
					and transfer_id = ".$transfer_id;
	}
	else {
		$query = "select transferdt.Item_code Item_code, transferdt.Item_Barcode Item_Barcode, TransferDt_Quantity as TransferDt_Quantity,
            		TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id
					,TransferDt_WarehouseTo,transferdt_warehousefrom from transferdt , items
					where items.item_code = transferdt.item_code
					and transfer_id = ".$transfer_id;
	
	}
	//echo $query;
	$pquery = "select serialno_code from purchasesserialno ";
	$result=mysql_query($query,$connection) or die(mysql_error());
	
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing='2' class='detail' style='table-layout:fixed;' >";	
	echo"   <col width=90>	  
			<col width=275>	  
			<col width=95>	  
			<col width=95>
			<col width=80>";
	
	echo"<tr bgcolor=#D8D8D8><th align=left>Code</th>";
	if ($Module_TransferWithOneWh==0)
	{
		echo"<th align=left>Description</th>";
		echo"<th align=center>From</th>";					
		echo"<th align=center>To</th>";				
		echo"<th align=center>Quantity</th>";			
	}
	else{	
		echo"<th align=left colspan='3'>Description</th>";
		echo"<th align=center>Quantity</th>";			
	}
	echo"</tr>";
	echo"<tr height='5'><td colspan='5'><h4></td></tr>";
	
	$TotalQuantity = 0;
	while($rows=mysql_fetch_array($result)){
		extract($rows);
		
		$TotalQuantity = $TotalQuantity + $TransferDt_Quantity;
		
		$pquery = "select serialno_code from purchasesserialno where transferdt_id = ".$transferdt_id." and item_code = '".$Item_code."'";
		//echo $pquery;
		$presult=mysql_query($pquery,$connection) or die(mysql_error());
		echo"<tr><td align=left>$Item_code</td>";
		
		if ($Module_TransferWithOneWh==0)
		{
			echo"<td align=left>$Item_description</td>";
			echo"<td align=left>$transferdt_warehousefrom</td>";			
			echo"<td align=left>$TransferDt_WarehouseTo</td>";			
			echo"<td align=right>$TransferDt_Quantity</td>";			
		}
		else {
			echo"<td align=left colspan='3'>$Item_description</td>";
			echo"<td align=right>$TransferDt_Quantity</td>";			
		}	
		
		echo"</tr>";
		
		$num=mysql_num_rows($presult);
		if ($num!=0)
		{
			echo"<tr><td align=center valign=top><b><font size='2'><u>SerialNo</u></b></td><td colspan=4 ><font size='1'>";
			while($prows=mysql_fetch_array($presult)){
				echo"$prows[0]"." - ";
			}
			echo"</td></tr>";
		}
		
	}
	
	echo"<tr height='5'><td colspan='5'><h4></td></tr>";
	echo"<tr><th align=right colspan='4'>Total Quantity</th>";
	echo"	<th align=right>$TotalQuantity</th>";
	echo"</tr>";
	
	echo"</table></td></tr>";
	echo"</table>";

	
	echo"</br>";
	echo"</br>";
	echo"</br>";
	echo"</br>";

	echo"</br>";
	echo"</br>";
	
	
	echo"<table border=0 width='635'>";
	//echo"<tr height='5'><td colspan='3'><h4></td></tr>";
	echo"<tr height='35'><td colspan='3'>&nbsp;</td></tr>";
	echo"<tr><td align=center><h3></td><td align=center><h3></td><td align=center><h3></td></tr>";
	echo"<tr><td align=center>Sales Rep</td><td align=center>Driver Signature</td><td align=center>Received By</td></tr>";
	echo"</table>";
}

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}


print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
?>

