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

//Report Version For GDK

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


print <<< HERE
<HTML>
<HEAD>
<TITLE> Porder/Purchase Request 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: 12px; 
                  font-family:Times-New-Roman;
                 }
table.group td { font-size: 12px; 
                 font-weight: bold;
				 font-style: italic;
                 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($_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);
}
$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);
}

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) 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 From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$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']."' ".
			  " 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 ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$_SESSION['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 ".$_SESSION['dbLabel'].".loginstk ".
		 "                                          where user_account='".$_SESSION['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 ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['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 width='25%'><b>$CompanyName</b></td>
  <td width='55%' align='center'><font size=4><b>List of Purchase Order & Request</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];

$dispfromproject  = $fromproject;
$disptoproject = $toproject;
$dispfromitem = $fromitem;
$disptoitem = $toitem;
$dispfromgroup = $fromgroup;
$disptogroup = $togroup;
$dispfromsubgroup = $fromsubgroup;
$disptosubgroup = $tosubgroup;
$dispfromwarehouse = $fromwarehouse;
$disptowarehouse = $towarehouse;
$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($fromgroup))  $fromgroup = 'null';
else   $fromgroup = "'".$fromgroup."'";    
if (empty($togroup))  $togroup = 'null';
else  $togroup = "'".$togroup."'";   

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


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

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>

<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="10%" valign='top'><b>Item From/To</b></td>
  <td width="40%">$dispfromitem - $disptoitem </td>
  <td width="15%" valign='top'><b>Group From/To</b></td>
  <td width="35%">$dispfromgroup - $disptogroup </td>
</tr>
<tr>
  <td width="10%"><b>Date From/To</b></td>
  <td width="40%">$dispfromdate - $disptodate </td>
  <td width="15%" valign='top'><b>SubGroup From/To</b></td>
  <td width="35%">$dispfromsubgroup - $disptosubgroup </td>
</tr>
<tr height='5'><td colspan=4><h4></td></tr>
</table>
HERE;


echo "<table  border='0' width='100%' align='center' class='master' cellpadding='0' cellspacing='0' style='table-layout:fixed'>
      <col width=110>
	  <col width=200>
	  <col width=80>
	  <col width=90>
	  <col width=100>
	  <col width=60>
	  <col width=70>
	  <col width=70>";



echo "<tr>
		<th >Item Code</th>
		<th >Description</th>
		<th >Stock Qty</th>
	    <th >Incoming </th>
		<th >Re-Order</th>
		<th >&nbsp;</th>
		<th >&nbsp;</th>
		<th >&nbsp;</th>
	  </tr>";
		 



	
print <<<HERE

<tr height='5'><td colspan=8><h4></td></tr>
 
HERE;

$query = '';

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

$query = " Select ItemCode, ItemDescription, MasterCode, MasterDescription, Round(QuantityExists) As QuantityExists, ".
         " ifNull(QuantityPR,0) As QuantityPR, ifNull(QuantityPR,0) - ifNull(QuantityOrdered,0) As QuantityPending, ".
		 " ifNull(QuantityPO,0) As QuantityPO, ifNull(QuantityUsed,0) As QuantityUsed, ".
		 " ifNull(QuantityPO,0) - ifNull(QuantityUsed,0) As QuantityRemain  ".
		 " From ( ";
		 
$query = $query. " Select Items.Item_Code as ItemCode, Item_Description as ItemDescription, ".
				 " if($groupby=2, ifNull(Items.Group_Code,''), if($groupby=3, ifNull(Items.SubGroup_Code,''), '')) As MasterCode, ".
				 " if($groupby=2, Grouping.Group_Name, if($groupby=3, SubGroup.SubGroup_Name, '')) As MasterDescription, ".
				 " Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered) As QuantityExists, ".  
				 " ifNull(Q2.QuantityPR,0) As QuantityPR, ifNull(Q2.QuantityOrdered,0) as QuantityOrdered, ".
				 " ifNull(Q3.QuantityPO,0) As QuantityPO, ifNull(Q3.QuantityUsed,0) As QuantityUsed ".
                 " From Items ".
  				 " Inner Join StkTransaction on Items.Item_Code = StkTransaction.Item_Code ".
				 " Left Join Grouping on Items.Group_Code = Grouping.Group_Code ".
				 " Left Join SubGroup on Items.SubGroup_Code = SubGroup.SubGroup_Code ".
				 " Left Join Warehouse on StkTransaction.Warehouse_Code = Warehouse.Warehouse_Code ".
				 " Left Join ( Select QPReqPend.Item_Code, Sum(ifNull(QPReqPend.QuantityPR,0)) as QuantityPR, ".
				 " 			   Sum(ifNull(QPReqPend.QuantityOrdered,0)) As QuantityOrdered ".
				 " 			   From ( ".
				 " 			   Select PRequestDt.PRqstDt_ID, PRequestDt.Item_Code, ifNull(PRequestDt.PRqstDt_Quantity,0) as QuantityPR, ".
				 " 			   Sum(ifNull(PorderDt.PorderDt_Quantity,0)) As QuantityOrdered ".
				 " 			   From PRequestDt ".
				 " 			   Left Join PorderDt on PorderDt.RelatedDt_ID = PRequestDt.PRqstDt_ID ";
				 
				if ($projectexist > 0 && $FI_RepRestrictProject == 1)
					$query= $query.$QueryRestrictPrj." on PRequestDt.project_code like concat(Q4.project_code,'%') ";
							 
				if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
					$query= $query.$QueryRestrictCst." on PRequestDt.Costcent_code like concat(Q5.costcent_code,'%') ";
								   
$query= $query.	 " 			   Where PRequestDt.Item_Code Between ifNull(".$fromitem.", PRequestDt.Item_Code) and ifNull(". $toitem.", PRequestDt.Item_Code) ".
				 " 			   And ifNull(PRequestDt.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(PRequestDt.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(PRequestDt.Project_Code,'-1')) ".
				 " 			   And ifNull(PRequestDt.Costcent_Code,'-1') between ifNull(".$fromcostcent.",ifNull(PRequestDt.Costcent_Code,'-1')) and ifNull(". $tocostcent.",ifNull(PRequestDt.Costcent_Code,'-1')) ".
				 " 			   And ifNull(PRequestDt.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(PRequestDt.Warehouse_Code,'-1')) ".
				 " 			   And Date_format(PRequestDt.PRqst_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(PRequestDt.PRqst_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(PRequestDt.PRqst_Date,'%Y-%m-%d'))  ";
				 
				if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
					$query = $query." And PRequestDt.Warehouse_Code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
	
$query= $query.	 "			   Group By PRequestDt.PRqstDt_ID, PRequestDt.Item_Code ".
				 "        ) As QPReqPend Group By QPReqPend.Item_Code   ) Q2 on Q2.Item_Code = Items.Item_Code ".
				 " Left Join ( Select QPurPend.Item_Code, Sum(ifNull(QPurPend.QuantityPO,0)) as QuantityPO, ".
				 " 			   Sum(ifNull(QPurPend.QuantityUsed,0)) As QuantityUsed ".
				 " 			   From ( ".
				 "			   Select PorderDt.PorderDt_ID, PorderDt.Item_Code, ifNull(PorderDt.PorderDt_Quantity,0) as QuantityPO, ".
				 " 			   Sum(ifNull(PurchasesDt.PurchaseDt_Quantity,0)) As QuantityUsed ".
				 " 			   From PorderDt ".
				 " 			   Left Join PurchasesDt on (Purchasesdt.RelatedDt_ID = PorderDt.PorderDt_ID And PurchasesDt.Journal_Type='PI') ";
				 
				if ($projectexist > 0 && $FI_RepRestrictProject == 1)
					$query= $query.$QueryRestrictPrj." on PorderDt.project_code like concat(Q4.project_code,'%') ";
							 
				if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
					$query= $query.$QueryRestrictCst." on PorderDt.Costcent_code like concat(Q5.costcent_code,'%') ";
	
$query= $query.  " 			   Where PorderDt.Item_Code Between ifNull(".$fromitem.", PorderDt.Item_Code) and ifNull(". $toitem.", PorderDt.Item_Code) ".
				 " 			   And ifNull(PorderDt.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(PorderDt.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(PorderDt.Project_Code,'-1')) ".
				 " 			   And ifNull(PorderDt.Costcent_Code,'-1') between ifNull(".$fromcostcent.",ifNull(PorderDt.Costcent_Code,'-1')) and ifNull(". $tocostcent.",ifNull(PorderDt.Costcent_Code,'-1')) ".
				 " 			   And ifNull(PorderDt.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(PorderDt.Warehouse_Code,'-1')) ".
				 " 			   And Date_format(PorderDt.Porder_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(PorderDt.Porder_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(PorderDt.Porder_Date,'%Y-%m-%d'))  ".
				 "             And ifNull(Porderdt_OldData,0) = 0  ";
				 
				if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
					$query = $query." And PorderDt.Warehouse_Code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
					
$query= $query.	 "			   Group By PorderDt.PorderDt_ID, PorderDt.Item_Code ".
				 "          ) As QPurPend Group By QPurPend.Item_Code ) Q3 on Q3.Item_Code = Items.Item_Code ";

if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on StkTransaction.project_code like concat(Q4.project_code,'%') ";
							 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on StkTransaction.Costcent_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(Items.Group_Code,-1) between ifNull(". $fromgroup .", ifNull(Items.Group_Code,-1)) and ifNull(". $togroup.", ifNull(Items.Group_Code,-1)) ".
				  " And ifNull(Items.SubGroup_Code,-1) between ifNull(". $fromsubgroup .", ifNull(Items.SubGroup_Code,-1)) and ifNull(". $tosubgroup.", ifNull(Items.SubGroup_Code,-1)) ".
				  " And ifNull(StkTransaction.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(StkTransaction.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(StkTransaction.Project_Code,'-1')) ".
				  " And ifNull(StkTransaction.Costcent_Code,'-1') between ifNull(".$fromcostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) and ifNull(". $tocostcent.",ifNull(StkTransaction.Costcent_Code,'-1')) ".
				  " And ifNull(StkTransaction.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(StkTransaction.Warehouse_Code,'-1')) ";
				  
				  
					  
if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
	$query = $query." And StkTransaction.warehouse_code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
if ($groupexist > 0)
	$query = $query." And Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  

//Tarek 2011-10-26	
if ($Module_Grn == 1)
	$query = $query." And StkTransaction.Transaction_Type <> 'PI' ";
  
$query = $query.  " Group By MasterCode, ItemCode, ItemDescription ".
				  " Order By MasterCode, ItemCode, ItemDescription ";
				
$query = $query. " ) As Q1 Order By MasterCode, ItemCode, ItemDescription ";
				
	//echo $whexist; exit;
//echo $query;
//exit;

$GroupByCode  = '';
$GroupByLabel = '';

$MasterCode  = '';

$SumQtyExist  = 0;
$SumQtyPO     = 0;
$SumQtyPR     = 0;
$SumQtyPend   = 0;
$SumQtyUsed   = 0;
$SumQtyRem    = 0;

$TotalQtyExist  = 0;
$TotalQtyPO     = 0;
$TotalQtyPR     = 0;
$TotalQtyPend   = 0;
$TotalQtyUsed   = 0;
$TotalQtyRem    = 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."'";
	}
	
	$MasterCode  = $rows[2];
	if ($MasterCode == '') $MasterCode = 'None';
	
    if (($groupby == 2 || $groupby == 3) && ($GroupByCode != ''  && $GroupByCode != $MasterCode)) 
    {
	    $GroupByLabel = '';
		if ($groupby == 2) $GroupByLabel = 'Total by Group';
		else if ($groupby == 3) $GroupByLabel = 'Total by SubGroup';
		
		echo "<tr class='totals'>"; 
		echo "  <td align='right' colspan='2'>$GroupByLabel</td>";
		echo "	<td align='center' valign='top' >". number_format($SumQtyExist, 0) ."</td>";
		//echo "	<td align='center' valign='top' >". number_format($SumQtyPO, 0) ."</td>";
		//echo "	<td align='center' valign='top' >". number_format($SumQtyPR, 0) ."</td>";
		echo "	<td align='center' valign='top' >". number_format($SumQtyRem, 0) ."</td>";
		//echo "	<td align='center' valign='top' >". $SumQtyUsed ."</td>";
		echo "	<td align='center' valign='top' >". number_format($SumQtyPend, 0) ."</td>";
		echo "<td align='center' valign='top' colspan='3'>&nbsp;</td>";
		echo "</tr>";
		
		$SumQtyExist  = 0;
		$SumQtyPO     = 0;
		$SumQtyPR     = 0;
		$SumQtyPend   = 0;
		$SumQtyUsed   = 0;
		$SumQtyRem    = 0;
		
	}
    
    if (($groupby == 2 || $groupby == 3 ) && ($GroupByCode == '' || $GroupByCode != $MasterCode)) {
	  
		$GroupByLabel = '';
		if ($groupby == 2) $GroupByLabel = 'Group by Group';
		else if ($groupby == 3) $GroupByLabel = 'Group by SubGroup';
		
		echo "<tr>"; 
		echo "<td colspan='8'><table class='group' width='100%' border='0' align='center' style='table-layout:fixed' >";
		echo "<tr>";
		echo "   <td width='7%'> $GroupByLabel</td>";
		echo "   <td width='7%' >". $rows[2] ."</td>";
		echo "   <td width='40%'>". $rows[3] ."</td>";
		echo "</tr>"; 
		echo "<tr height='8'><td colspan='3'>&nbsp;</td></tr></table></td></tr>";

		$GroupByCode = $MasterCode;
	
	}
	

	
	
	echo "<tr $color>"; 
	echo "<td align='left' valign='top' style='border-right:1px solid black;'>". $rows[0] ."</td>";
	echo "<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[1] ."</td>";
	echo "<td align='center' valign='top' style='border-right:1px solid black; '>". number_format($rows[4], 0) ."</td>";
	//echo "<td align='center' valign='top' >". number_format($rows[7], 0) ."</td>";
	//echo "<td align='center' valign='top' >". number_format($rows[5], 0) ."</td>";
	echo "<td align='center' valign='top' style='border-right:1px solid black;'>". number_format($rows[9], 0) ."</td>";
	//echo "<td align='center' valign='top' >". $rows[8] ."</td>";
	echo "<td align='center' valign='top' style='border-right:1px solid black;'>". number_format($rows[6], 0) ."</td>";
	echo "<td align='center' valign='top' colspan='3'>&nbsp;</td>";
	echo "</tr>";
	
	
	$SumQtyExist = $SumQtyExist +  $rows[4];
	$SumQtyPO    = $SumQtyPO    +  $rows[7];
	$SumQtyPR    = $SumQtyPR    +  $rows[5];
	$SumQtyPend  = $SumQtyPend  +  $rows[6];
	$SumQtyUsed  = $SumQtyUsed  +  $rows[8];
	$SumQtyRem   = $SumQtyRem   +  $rows[9];
	
	$TotalQtyExist = $TotalQtyExist +  $rows[4];
	$TotalQtyPO    = $TotalQtyPO    +  $rows[7];
	$TotalQtyPR    = $TotalQtyPR    +  $rows[5];
	$TotalQtyPend  = $TotalQtyPend  +  $rows[6];
	$TotalQtyUsed  = $TotalQtyUsed  +  $rows[8];
	$TotalQtyRem   = $TotalQtyRem   +  $rows[9];
	

	$col+=1;
}


if (($groupby == 2 || $groupby == 3)) 
{
    $GroupByLabel = '';
	if ($groupby == 2) $GroupByLabel = 'Total by Group';
	else if ($groupby == 3) $GroupByLabel = 'Total by SubGroup';
		
	echo "<tr class='totals'>"; 
	echo "  <td align='right' colspan='2'>$GroupByLabel</td>";
	echo "	<td align='center' valign='top' >". number_format($SumQtyExist, 0) ."</td>";
	//echo "	<td align='center' valign='top' >". number_format($SumQtyPO, 0) ."</td>";
	//echo "	<td align='center' valign='top' >". number_format($SumQtyPR, 0) ."</td>";
	echo "	<td align='center' valign='top' >". number_format($SumQtyRem, 0) ."</td>";
	//echo "	<td align='center' valign='top' >". $SumQtyUsed ."</td>";
	echo "	<td align='center' valign='top' >". number_format($SumQtyPend, 0) ."</td>";
	echo "<td align='center' valign='top' colspan='3'>&nbsp;</td>";
	echo "</tr>";
	
}


// Report Totals
echo "<tr height='5'><td colspan=8><h4></td></tr>"; 
echo "<tr height='10'><td colspan='8'>&nbsp;</td></tr>"; 
echo "<tr class='totals'>"; 
echo "  <td align='right' colspan='2'>Total </td>";
echo "	<td align='center' valign='top' >". number_format($TotalQtyExist, 0) ."</td>";
//echo "	<td align='center' valign='top' >". number_format($TotalQtyPO, 0) ."</td>";
//echo "	<td align='center' valign='top' >". number_format($TotalQtyPR, 0) ."</td>";
echo "	<td align='center' valign='top' >". number_format($TotalQtyRem, 0) ."</td>";
//echo "	<td align='center' valign='top' >". $TotalQtyUsed ."</td>";
echo "	<td align='center' valign='top' >". number_format($TotalQtyPend, 0) ."</td>";
echo "<td align='center' valign='top' colspan='3'>&nbsp;</td>";
echo "</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;

?>