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

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


print <<< HERE
<HTML>
<HEAD>
<TITLE> List of PreProduction Report </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: 13px; 
                  font-family:Times-New-Roman;
                 }
				 
table.master1 th { font-size: 14px; 
                   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 
table.master1 td { font-size: 13px; 
				   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 

table.detail th { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 			   
table.detail td { font-size: 11px; 
				  font-weight: normal;
		          font-family:Times-New-Roman;
                 }			   
				 
.totals { font-size: 12px; 
          font-weight: bold;
          font-family:Times-New-Roman;
        }					  
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }   
   
</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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

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

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

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


$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse, ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice  ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

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

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
		 "                                          where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);

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

print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='100%' border='0' align='left' style='table-layout:fixed'>
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>List of PreProductions</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];

//if (empty($curr))  $curr = $Base1;


$dispfromdate = $fromdate;
$disptodate   = $todate;

  

if (empty($fromitem))  $fromitem = 'null';
else  $fromitem = "'".$fromitem."'";   
if (empty($toitem))  $toitem = 'null';
else  $toitem = "'".$toitem."'";      


if (empty($fromproject))  $fromproject = 'null';
else  $fromproject = "'".$fromproject."'";    
if (empty($toproject))  $toproject = 'null';
else  $toproject = "'".$toproject."'";       

if (empty($fromcostcent))  $fromcostcent = 'null';
else  $fromcostcent = "'".$fromcostcent."'";    
if (empty($tocostcent))  $tocostcent = 'null';
else  $tocostcent = "'".$tocostcent."'";       


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

$query=" select  warehouse_name ".
         " from warehouse where warehouse_code = $fromwarehouse" ;
		 
$result = mysql_query($query,$connection) or die(mysql_error());
$rows=mysql_fetch_row($result);
$warehousename = $rows[0];

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 height='5'><td colspan=4><h5></td></tr>
</table>
</td></tr>

<tr><td>
<table class='params' width='100%' border='0' align='left' style='table-layout:fixed' >
<tr>
  <td width="12%"><b>Date From/To:</b></td>
  <td width="18%">$dispfromdate - $disptodate </td>
  <td width="8%" valign='top'></td>
  <td width="12%"></td>
HERE;



  
  if($fromwarehouse1=='')
  echo"<td width='50%' valign='top'></td>";
  else
  echo"<td width='50%' valign='top'><b>Warehouse:</b> $fromwarehouse1 $warehousename</td>";
  
print <<<HERE

</tr>
<tr height='5'><td colspan=5><h4></td></tr>
</table>
</td></tr>

<tr><td>
HERE;

if ($showdetail == 1) $masterclass = 'master1';
else $masterclass = 'master';

if($status!=3)
echo "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0' class=$masterclass style='table-layout:fixed'>
	  <col width=70>
	  <col width=80>
	  <col width=100>
	  <col width=250>
	  <col width=60>
	  <col width=80>
	  <col width=80>
	  <col width=80>
	  <col width=80>
	  <col width=60>";

else
echo "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0' class=$masterclass style='table-layout:fixed'>
	  <col width=70>
	  <col width=80>
	  <col width=100>
	  <col width=250>
	  <col width=60>
	  <col width=80>
	  <col width=80>
	  <col width=80>
	  <col width=80>
	  <col width=80>
	  <col width=60>";


if ($status == 2) {
	echo"  <tr>
			<th >Date</th>
			<th >Reference</th>
			<th align='left'>Item Code</th>
			<th align='left'>Item Description</th>
			<th colspan='2'>Delivery Date</th>
			<th >Qty</th>
			<th >Status</th>
			<th >Age</th>
		  </tr>";
}else if ($status == 3) {
	echo"  <tr>
			<th >Date</th>
			<th >Reference</th>
			<th align='left'>Item Code</th>
			<th align='left'>Item Description</th>
			<th >Delivery Date</th>
			<th >Qty</th>
			<th >Status</th>
			<th >Age</th>
			<th >Production Reference</th>
			<th >Production Date</th>
			<th >Production Quantity</th>
		  </tr>";
} else {
	echo"  <tr>
			<th >Date</th>
			<th >Reference</th>
			<th align='left'>Item Code</th>
			<th align='left'>Item Description</th>
			<th colspan='2'>Delivery Date</th>
			<th >Qty</th>
			<th >Status</th>
			<th >Age</th>
		  </tr>";

}		  
	
if($status!=3)	
echo"<tr height='5'><td colspan=10><h4></td></tr>";
else
echo"<tr height='5'><td colspan=11><h4></td></tr>";

		 


$query = '';

$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					" 			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (project_code is not null and project_code != '') ".
					"			 ) Q4  ";
					
$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
					"			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					"              and (costcent_code is not null and costcent_code != '') ".
					"			 ) Q5  ";
	



$query = " Select PreProduction.Production_ID as ProductionID, PreProduction.Production_Reference as ProductionReference, Date_Format(PreProduction.Production_Date,'%d/%m/%Y') As ProductionDate, ".
		 " PreProduction.Item_code As ItemCode, Item_Description as ItemDescription, PreProduction.Warehouse_Code as WarehouseCode, Warehouse_Name as WarehouseName, ".
		 " ifNull(PreProduction.Production_Quantity,1) as ProductionQuantity,  PreProduction.Production_IdealQty as ProductionIdealQty, ".
		 " PreProduction.project_code, Date_Format(Preproduction.Production_Delivery_Date,'%d/%m/%Y') As ProductionDeliveryDate,".
		 " ifNull(Production.Preproduction_ID,0), Production.Production_Reference, Date_Format(Production.Production_Date,'%d/%m/%Y'),".
		 " ifNull(Production.Production_Quantity,1), DATEDIFF(ifNull(Production.Production_Date,Now()),Preproduction.Production_Delivery_Date) ".
		 " From PreProduction".
		 " Inner Join Items on PreProduction.Item_code = Items.Item_code ".
		 " Left Join Production on Production.Preproduction_ID = Preproduction.Production_Id".
		 " Left Join Warehouse on PreProduction.Warehouse_Code = Warehouse.Warehouse_Code ";
		 
if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on PreProduction.project_code like concat(Q4.project_code,'%') ";
							 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on PreProduction.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
		 

$query = $query.  " Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
				  " And ifNull(PreProduction.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(PreProduction.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(PreProduction.Project_Code,'-1')) ".
				  " And ifNull(PreProduction.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(PreProduction.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(PreProduction.Costcenter_Code,'-1')) ".
				  " And ifNull(PreProduction.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(PreProduction.Warehouse_Code,'-1')) ";
                  " And Date_format(PreProduction.Production_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(PreProduction.Production_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(PreProduction.Production_Date,'%Y-%m-%d'))  ";
				  
				  
					  
if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
	$query = $query." And Production.warehouse_code In (select distinct warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  
if ($groupexist > 0)
	$query = $query." And Items.group_code In (select distinct group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  


//Pending preproductions	
if ($status == 2) 
$query= $query." AND Production.Preproduction_ID IS NULL";

//Produced preproductions	
if ($status == 3) 
$query= $query." AND Production.Preproduction_ID IS NOT NULL"; 

//Order By Date
if ($orderBy == 1) 
$query= $query." ORDER BY PreProduction.Production_Date";
//Order By Delivery Date
if ($orderBy == 2) 
$query= $query." ORDER BY Preproduction.Production_Delivery_Date";
//Order By Reference
if ($orderBy == 3) 
$query= $query." ORDER BY PreProduction.Production_Reference";
//OrderBby item code
if ($orderBy == 4) 
$query= $query." ORDER BY PreProduction.Item_code";
//Order By item Desc
if ($orderBy == 5) 
$query= $query." ORDER BY Item_Description ";
		 
//echo $query;
	
if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
	$query = $query." And ProductionFG.warehouse_code In (select distinct warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  
if ($groupexist > 0)
	$query = $query." And Items.group_code In (select distinct group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";  
 
 
//$query = $query.  " Order By OrderProductionDate, ProductionReference ";
		 
		// echo $query;exit;

$TotalAmount  = 0;

$col=0;
$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_row($result)){
	
		if($col%2==0){
			$color = "style='background-color:".$report_color1."'";
		}else{
			$color = "style='background-color:".$report_color2."'";
		}
	
		

		 
		echo "<tr $color>"; 
		echo "	<td align='left' valign='top' style='border-right:1px solid black; '>". $rows[2] ."</td>";
		echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[1] ."</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[3] ."</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[4] ."</td>";
		if($status!=3)
			echo "	<td align='center' colspan='2' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[10] ."</td>";
		else	
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[10] ."</td>";			
		echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[7] ."</td>";

		if($status ==2){
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>Pending</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[15] ."</td>";
		}
		
		else if($status ==3){
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>Produced</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[15] . "</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[12] ."</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[13] ."</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[14] ."</td>";
		}
			
		else if($status ==1){
			if($rows[11]==0){
				echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>Pending</td>";	
				echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[15] . "</td>";
				}
			else{
				echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>Produced</td>";
				echo "	<td align='center' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rows[15] . "</td>";
				}
		}
		
	echo "</tr>";

	
	$col+=1;
	
}


// Report Totals

	

echo "</table></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;
}

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;

?>