<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


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

<style type="text/css" >
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }
table.group td { font-size: 12px; 
                 font-weight: bold;
                 font-family:Times-New-Roman;
               }				 
				 
table.detail td { font-size: 10px; 
                  font-family:Times-New-Roman;
                 }				 
table.headerdetail td { font-size: 10px; 
                        font-weight: bold;
                        font-family:Times-New-Roman;
                      }				 
table.totals td { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }					  
				 

</style>


</HEAD>

HERE;


include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

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


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//maroun 30.03.2011
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';

$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, report_color1, report_color2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}


$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist   = mysql_num_rows($projectresult);

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in (select warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
//echo $querywh;

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

print <<< HERE
<table width="100%" border="0" align="center">
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>List of Transfer</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

$dispfromref  = $fromref;
$disptoref = $toref;
$dispfromdate = $fromdate;
$disptodate = $todate;
$dispfromproject = $fromproject;

if (empty($fromref))
  $fromref = 'null'; 
  
if (empty($toref))
  $toref = 'null';   
  
if ($cur == 'currency1'){
   $cur='null';  
   $displaycur = '';
}   
else {
   $displaycur = $cur;
   $cur="'".$cur."'";
}   

$fromproject = "".$fromproject;
if ($fromproject == '')   
   $fromproject = '';
else
   $fromproject = "'".$fromproject."%'";    

if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  

if (empty($fromdate))   
   $fromdate = 'null';
else
   $fromdate = "'".$fromdate."'"; 
   
if (empty($todate))   
   $todate = 'null';
else
   $todate = "'".$todate."'";

print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>


<table class="params" width="100%" border="0" align="center" cellpadding='0' cellspacing='0' >
<tr>
  <td width="10%" align='left'><b>Date From/To</b></td>
  <td width="20%">$dispfromdate - $disptodate </td>
  <td width="10%" align='center'><b>Currency:</b></td>
  <td width="10%" align='left'>$displaycur </td>
  <td width="10%" align='center'><b>Project:</b></td>
  <td width="40%" align='left'>$dispfromproject </td>
</tr>
</table>


<table width="100%" border="0" align="center" class='master' cellpadding='0' cellspacing='0' style='table-layout:fixed;' >
<col width=70>
<col width=80>
<col width=80>
<col width=90>
<col width=70>
<col width=90>
<col width=90>
<col width=410>
<tr>
    <th align='left'>Date</th>
    <th>Reference</th>
    <th>Costcenter </th>
    <th>Project From</th>
	<th>To</th>
	<th>Wh. From</th>
    <th>To</th>
	<th>Remark</th>
</tr>
HERE;

    if ($showdetail == 1) {
		echo "<tr>";
		echo "  <td colspan='9'><table class='headerdetail' width='100%' border='0' align='left' cellpadding='0' cellspacing='0' >
		<col width=90>
		<col width=160>
		<col width=50>
		<col width=50>
		<col width=30>
		<col width=200>";
        echo "<tr> ";
		echo "  <td align='center'> Item Code/Description </td>";
		echo "  <td >&nbsp;</td>";
		echo "  <td align='center'> Wh.From </td>";
		echo "  <td align='center'> Wh.To </td>";
		echo "  <td align='center'> Qty. </td>";
		echo "  <td align='center'> Remark </td>";
		echo " </tr></table></td>";
		echo " </tr>";
		
	}	
print <<<HERE
<tr>
    <td colspan='9'><hr width='100%' color='black' size='2'></td>
</tr>
 
HERE;
 
    

   
$querytransfer= " select Transfer_Id, date_format(Transfer_Date,'%d/%m/%Y') Transfer_Date, Transfer_Reference, Transfer_Remark,  ".
                " Costcenter_Code, Project_code1 , Project_code2, Transfer_WarehouseFrom, Transfer_WarehouseTo, ".
				" if('".$date."' = 'date', date_format(Transfer_Date,'%Y-%m-%d'), Transfer_Reference) ord ".
 				" from Transfer ";
				
if ($projectexist >	0)
  //$querytransfer= $querytransfer." left join ".$_SESSION['dbLabel'].".loginstk on Transfer.Project_code1 like concat(loginstk.project_code,'%') ";
  $querytransfer= $querytransfer." left join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
							   	 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
								 "             and (project_code is not null and project_code != '') ".
								 "			 ) Q1 on Transfer.Project_code1 like concat(Q1.project_code,'%') ";

$querytransfer= $querytransfer.  				
				" where Transfer_Reference between ifNull(". $fromref .",Transfer_Reference) and ifNull(". $toref.",Transfer_Reference) ".
				" and date_format(Transfer_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Transfer_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Transfer_Date,'%Y-%m-%d'))  ";

if ($fromproject != '')				
	$querytransfer= $querytransfer." and (upper(Transfer.Project_code1) like upper(".$fromproject.") Or upper(Transfer.Project_code2) like upper(".$fromproject.") ) ";
								

//if ($projectexist >	0)					  
  //$querytransfer= $querytransfer." and loginstk.user_account='".$_SESSION['useraccount']."' ";
  
if ($whexist >	0)
  $querytransfer= $querytransfer." and ( Transfer_WarehouseFrom In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') or  ".
								 "       Transfer_WarehouseTo In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  or ".
								 "       Transfer_WarehouseFrom Is Null or Transfer_WarehouseTo Is Null )  ";
  
$querytransfer= $querytransfer.  
				" order by if($grpproject=1, Transfer.Project_code1, null ), ord, Transfer_Reference ";
				

	
//echo $querytransfer;

$resulttransfer=mysql_query($querytransfer,$connection) or die(mysql_error());
$col=0;
while($rowtransfer=mysql_fetch_row($resulttransfer)){
	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}
    
	$querytransferdt= " select transferdt.Item_code, items.item_description, TransferDt_WarehouseFrom, TransferDt_WarehouseTo, ".
	                  " TransferDt_Quantity, Transferdt_remark ".
					  " from transferdt ".
					  " left join items on transferdt.Item_code = items.item_code ".
					  " where transferdt.Transfer_Id = ". $rowtransfer[0];
    if ($whexist >	0)
      $querytransferdt= $querytransferdt." and ( TransferDt_WarehouseFrom In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') or ".
                                         "       TransferDt_WarehouseTo In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') or  ".
										 "       TransferDt_WarehouseFrom Is Null or  TransferDt_WarehouseTo Is Null ) ";
					

	//echo $querytransferdt;
	
	if ($showdetail == 1) {	
		echo "<tr>"; 
		echo "<td align='left'  >". $rowtransfer[1] ."</td>";
		echo "<td align='left'  >". $rowtransfer[2] ."</td>";
		echo "<td align='center'>". $rowtransfer[4] ."</td>";
		echo "<td align='left'  >". $rowtransfer[5] ."</td>";
		echo "<td align='center'>". $rowtransfer[6] ."</td>";
		echo "<td align='center'>". $rowtransfer[7] ."</td>";
		echo "<td align='center'>". $rowtransfer[8] ."</td>";
		echo "<td align='left'  >". $rowtransfer[3] ."</td>";
		echo "</tr>";
	}else{
		echo "<tr $color>"; 
		echo "<td align='left'  style='border-right:1px solid black; '>". $rowtransfer[1] ."</td>";
		echo "<td align='left'  style='border-right:1px solid black; padding-left:2px;'>". $rowtransfer[2] ."</td>";
		echo "<td align='center' style='border-right:1px solid black; '>". $rowtransfer[4] ."</td>";
		if( $rowtransfer[5]!='')
			echo "<td align='left'  style='border-right:1px solid black; padding-right:2px;'>". $rowtransfer[5] ."</td>";
		else
			echo "  <td style='border-right:1px solid black; padding-left:2px;'>&nbsp;</td>";
		if( $rowtransfer[6]!='')
			echo "<td align='center' style='border-right:1px solid black; padding-right:2px;'>". $rowtransfer[6] ."</td>";
		else
			echo "  <td style='border-right:1px solid black; padding-left:2px;'>&nbsp;</td>";
		echo "<td align='center' style='border-right:1px solid black; '>". $rowtransfer[7] ."</td>";
		echo "<td align='center' style='border-right:1px solid black; '>". $rowtransfer[8] ."</td>";
		if( $rowtransfer[3]!='')
			echo "<td align='left'  style='border-right:1px solid black; padding-left:2px;'>". $rowtransfer[3] ."</td>";
		else 
			echo "  <td style='border-right:1px solid black; padding-left:2px;'>&nbsp;</td>";
		echo "</tr>";
	}
	$col+=1;
	

	if ($showdetail == 1) {
   	  $resulttransferdt=mysql_query($querytransferdt,$connection) or die(mysql_error());
	  $col1=0;
	  while($rowtransferdt=mysql_fetch_row($resulttransferdt)){
		if($col1%2==0){
			$color1 = "style='background-color:".$report_color1."'";
		}else{
			$color1 = "style='background-color:".$report_color2."'";
		}
		echo "<tr>";
		echo "  <td colspan='9'><table class='detail' width='100%' border='0' align='center' cellpadding='0' cellspacing='0' >
		<col width=10>
		<col width=80>
		<col width=160>
		<col width=50>
		<col width=50>
		<col width=30>
		<col width=200>";
		echo "<tr $color1> ";
		echo "  <td >&nbsp;</td>";
		echo "  <td style='border-right:1px solid black; '>". $rowtransferdt[0] ."</td>";
		echo "  <td style='border-right:1px solid black; padding-left:2px;'>". $rowtransferdt[1] ."</td>";
		echo "  <td style='border-right:1px solid black; padding-left:2px;'>". $rowtransferdt[2] ."</td>";
		echo "  <td style='border-right:1px solid black; padding-left:2px;'>". $rowtransferdt[3] ."</td>";
		echo "  <td align='right' style='border-right:1px solid black; padding-right:2px;'> ". $rowtransferdt[4] ."</td>";
		if($rowtransferdt[5]!='')
			echo "  <td style='border-right:1px solid black; padding-left:2px;'>". $rowtransferdt[5] ."</td>";
		else
			echo "  <td style='border-right:1px solid black; padding-left:2px;'>&nbsp;</td>";
		echo " </tr></table></td></tr>";
		$col1+=1;
	  }
	}
	

}

	

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;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}

if ($printaccess==1)
print <<< HERE

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

?>