<?php
include_once('../../hdr_general.php');
include_once('../../lib/lib_general.php');
include_once('../../dbincludes.php');
include_once('../../libincludes.php');
include_once('../../classincludes.php');
// start include db
include_once("../../db/db_config.php");

$con = mysql_connect($db_config_pms['server'],$db_config_pms['username'],$db_config_pms['password']);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_config_pms['db'], $con);
// end include db
?>

<style type="text/css">
<!--
.style1 {
	color: #0066CC;
	font-weight: bold;
}
.style2 {font-size: 24px}
.style10 {font-size: 14px; color: #FFFFFF; font-weight: bold; }
.style11 {
	color: #666666;
	font-weight: bold;
	font-size: 12px;
}
-->
</style>

<!--<form method="post" name="frmMain">-->
<?php
		
		//declare GrandTotal
		$varGrandTotPO = 0;
		$varGrandTotalCancelPO = 0;
		$varGrandTotalCompletePO = 0;
		$varGrandTotalOSPO = 0;
		$varGrandOnTime = 0;
		$varGrandNotOnTime = 0;
		
		
		$varSupplier = $_REQUEST[varsupplier];
		//convert date
		$varTarikhMula = $_REQUEST['tarikhMula'];
		$varTarikhAkhir = $_REQUEST['tarikhAkhir'];
		
		$unixTarikhMula=converttounix($varTarikhMula);
		$unixTarikhAkhir=converttounix($varTarikhAkhir);	
		//end convert date		
		
		//get project primary key
		$selProKey = mysql_query("select * from project_details where projectname = '$pubprojectname'");		
		while($row = mysql_fetch_array($selProKey))
		{			
			$varProKey = $row['projectpkey'];				
		}
				
			// start include cms db			
			$con2 = mysql_connect($db_config_cms['server'],$db_config_cms['username'],$db_config_cms['password']);			
			if (!$con2)
			  {die('Could not connect: ' . mysql_error());}			
			mysql_select_db($db_config_cms['db'], $con);
			// end include cms  db
			
			 $selSubCon = mysql_query("select * from settings_suppliers where supplier_pkey ='$varSupplier'");		
			while($row = mysql_fetch_array($selSubCon))
			{	
				$varSupplierName =  $row['supplier_name'];				
			}
		
		
?>

<style type="text/css" media="print,screen" >
	th {
	    font-family:Arial;
	    color:black;
	    background-color:lightgrey;
	}
	thead {
	    display:table-header-group;
	}
	tbody {
	    display:table-row-group;
	}
</style>

<table width="870" border="0">
  <tr>
    <td width="128" height="23">&nbsp;</td>
    <td width="10">&nbsp;</td>
    <td colspan="3"><div align="center"><img src="../../gfx/zikayheader.png" width="225" height="82" /></div></td>
  </tr>
  <tr>
    <td height="23" class="style11">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3"><div align="left" class="style1 style2" style="border-bottom:#000000 medium">
      <div align="center">SUPPLIER PERFORMANCE  REPORT</div>
    </div></td>
  </tr>
  <tr>
    <td height="23">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td height="23">PROJECT TITLE</td>
    <td>:</td>
    <td colspan="3"><?php echo  $pubprojectname; ?></td>
  </tr>
  <tr>
    <td height="23">SUPPLIER</td>
    <td>:</td>
    <td colspan="3"><?php echo $varSupplierName;?></td>
  </tr>
  
  <tr>
    <td height="23">DATE</td>
    <td>:</td>
    <td width="100"><?php echo $_REQUEST[tarikhMula]?></td>
    <td width="41">To</td>
    <td width="569"><?php echo $_REQUEST[tarikhAkhir]?></td>
  </tr>
</table>

<br>
<table width="1079" border cellspacing="0.5">
<thead>
	<tr>
		<th width="32"><span class="style16">No</span></th>
		<th width="332"><span class="style16">Supplier</span></th>
		<th width="89"><span class="style16">PO Issued </span></th>
		<th width="109"><span class="style16">PO Cancel </span></th>
		<th width="122"><span class="style16">PO Delivered </span></th>
		<th width="71"><span class="style16">PO O/S </span></th>
		<th width="101"><span class="style16">PO On Time </span></th>
		<th width="121"><span class="style16">PO Not On Time</span></th>
		<th width="64"><span class="style16">View</span></th>
	</tr>
  </thead>
 <tbody>
 
<?php  		
$i = 0;
// start include pms db			
$con2 = mysql_connect($db_config_pms['server'],$db_config_pms['username'],$db_config_pms['password']);			
if (!$con2)
  {die('Could not connect: ' . mysql_error());}			
mysql_select_db($db_config_pms['db'], $con);
// end include pms  db

//select distinct supplier key from po detail

if ($_REQUEST[varsupplier] != ''  and $_REQUEST['tarikhMula'] != '' and $_REQUEST['tarikhAkhir'] != '')
	{$sqlSupKey = "select distinct(po_supplierpkey) from po_details where po_supplierpkey = '$_REQUEST[varsupplier]' and po_date BETWEEN $unixTarikhMula and $unixTarikhAkhir" ;
	//print $sqlSupKey;exit;
	$selSupKey = mysql_query($sqlSupKey );}
	
elseif ($_REQUEST[varsupplier] != '')
	{$selSupKey = mysql_query("select distinct(po_supplierpkey) from po_details where po_supplierpkey = '$_REQUEST[varsupplier]' order by po_date");}
	
else
	{$selSupKey = mysql_query("select distinct(po_supplierpkey) from po_details");}
	
while($row = mysql_fetch_array($selSupKey))
{			
	$varTotPO = 0;
	$i = $i + 1;
	$varSupKey = $row['po_supplierpkey'];	
	
	//select count PO Cancel =1
	if ($_REQUEST['tarikhMula'] != '' and $_REQUEST['tarikhAkhir'] != '')
		{$sqlselCountCancelPO = "select count(*) as totCanPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 1 and po_cancel = 1 and po_date BETWEEN $unixTarikhMula and $unixTarikhAkhir";}
	else
		{$sqlselCountCancelPO = "select count(*) as totCanPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 1 and po_cancel = 1";}
	
	$selCountCancelPO = mysql_query($sqlselCountCancelPO);		
	while($row = mysql_fetch_array($selCountCancelPO))
	{			
		$varTotalCancelPO = $row['totCanPO'];
		$varGrandTotalCancelPO = $varGrandTotalCancelPO + $varTotalCancelPO;
		
	}
	
	//select count PO delived =3
	if ($_REQUEST['tarikhMula'] != '' and $_REQUEST['tarikhAkhir'] != '')
		{$sqlselCountCompletePO = "select count(*) as totComPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 3 and po_cancel = 0 and po_date BETWEEN $unixTarikhMula and $unixTarikhAkhir";}
	else
		{$sqlselCountCompletePO = "select count(*) as totComPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 3 and po_cancel = 0";}			
		
	$selCountCompletePO = mysql_query($sqlselCountCompletePO);		
	while($row = mysql_fetch_array($selCountCompletePO))
	{			
		$varTotalCompletePO = $row['totComPO'];
		$varGrandTotalCompletePO = $varGrandTotalCompletePO + $varTotalCompletePO;
		
	}
	
	//select count PO Outstanding
	if ($_REQUEST['tarikhMula'] != '' and $_REQUEST['tarikhAkhir'] != '')
		{$sqlselCountOSPO = "select count(*) as totOSPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 1 and po_cancel = 0 and po_date BETWEEN $unixTarikhMula and $unixTarikhAkhir";}
	else
		{$sqlselCountOSPO = "select count(*) as totOSPO from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_status = 1 and po_cancel = 0";}		

	$selCountOSPO = mysql_query($sqlselCountOSPO);		
	while($row = mysql_fetch_array($selCountOSPO))
	{			
		$varTotalOSPO = $row['totOSPO'];
		$varGrandTotalOSPO = $varGrandTotalOSPO + $varTotalOSPO;
		
	}
	//====== check total po on time and not on time  
	$varNotOnTime = 0;
	$varOnTime = 0;
	
	//select po no
	if ($varTarikhMula != '' and $varTarikhAkhir != '')
		{$sqlSelPO = "select *  from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey'  and po_cancel <> 1 and po_date BETWEEN $unixTarikhMula and $unixTarikhAkhir   order by po_date";}
	else
		{$sqlSelPO = "select *  from po_details where po_projectpkey = '$varProKey' and po_supplierpkey = '$varSupKey' and po_cancel <> 1 order by po_date";}
	
	$selPO = mysql_query($sqlSelPO);	
		
	while($row = mysql_fetch_array($selPO))
	{			
		//$i = $i + 1;
		$varCheckPOOnTime = "";
		$varPKey = $row['po_pkey'];	
		$varPONO = $row['po_no'];
		$varPODateOri = $row['po_date'];	
		$varPODate = unconverttounixslash($row['po_date']);		
		$varMROKey = $row['po_mropkey'];	
		
		//select Expected Delivery
		$sqlExDe = "select *  from mro_details where mro_pkey = '$varMROKey'";
		$selExDe = mysql_query($sqlExDe);
		while($row = mysql_fetch_array($selExDe))
		{
			$varExDeOri = $row['mro_deliverydate'];
			$varExDe = unconverttounixslash($row['mro_deliverydate']);				
		}		
		// try
		//select DO details
		$selDO = mysql_query("select *  from do_items where item_popkey = '$varPKey' order by item_dodate");		
		while($row = mysql_fetch_array($selDO))
		{		
			$varDONo = $row['item_dono'];
			$varDODateOri = $row['item_dodate'];					
			$varDODate = unconverttounixslash($row['item_dodate']);				
			
			$offset = $varExDeOri - $varDODateOri;
			$varDePe = floor($offset/60/60/24); 
			
			$varCheckNeg = substr($varDePe, 0, 1);
			if ($varCheckNeg  == "-")
				{$varCheckPOOnTime = "No";}
		}
		
		
		if ($varCheckPOOnTime  == "No")
			{
				$varNotOnTime = $varNotOnTime + 1;
				$varGrandNotOnTime = $varGrandNotOnTime + 1;
			}	
		else
			{
				$varOnTime = $varOnTime + 1;
				$varGrandOnTime = $varGrandOnTime + 1;
			}	
		// end try
		
		
		
		
		//$offset = $varExDeOri - $varPODateOri;
//		$varDePe = floor($offset/60/60/24) . " Days"; 
//		//echo $varDePe;
//		$varCheck = substr($varDePe, 0, 1);
//		if ($varCheck  == "-")
//			{
//				$varNotOnTime = $varNotOnTime + 1;
//				$varGrandNotOnTime = $varGrandNotOnTime + 1;
//			}	
//		else
//			{
//				$varOnTime = $varOnTime + 1;
//				$varGrandOnTime = $varGrandOnTime + 1;
//			}	
	}	
	
	//====== end check total po on time and not on time  
	
  //select supplier name from CMS			
	
		// start include cms db			
		$con2 = mysql_connect($db_config_cms['server'],$db_config_cms['username'],$db_config_cms['password']);			
		if (!$con2)
		  {die('Could not connect: ' . mysql_error());}			
		mysql_select_db($db_config_cms['db'], $con);
		// end include cms  db
			
			 $selSubCon = mysql_query("select * from settings_suppliers where supplier_pkey ='$varSupKey'");	
			 //print	$selSubCon;end;
			while($row = mysql_fetch_array($selSubCon))
			{	
				$varSupplierName =  $row['supplier_name'];				
			}
	
	
	// start include pms db			
$con2 = mysql_connect($db_config_pms['server'],$db_config_pms['username'],$db_config_pms['password']);			
if (!$con2)
  {die('Could not connect: ' . mysql_error());}			
mysql_select_db($db_config_pms['db'], $con);
// end include pms  db

	$varTotPO = $varTotalCancelPO + $varTotalCompletePO + $varTotalOSPO;
	$varGrandTotPO = $varGrandTotPO + $varTotPO;
	//klu tak ada data tak payah display
	if ($varTotPO != 0){
	echo "<tr>";			
		echo "<td><div align='left' class='style11'>$i</td>";
		echo "<td><div align='left' class='style11'>$varSupplierName</td>";
		echo "<td><div align='right' class='style11'>$varTotPO</td>";
		echo "<td><div align='right' class='style11'>$varTotalCancelPO</td>";
		echo "<td><div align='right' class='style11'>$varTotalCompletePO </td>";
		echo "<td><div align='right' class='style11'>$varTotalOSPO</td>";
		echo "<td><div align='right' class='style11'>$varOnTime</td>";
		echo "<td><div align='right' class='style11'>$varNotOnTime</td>";
		echo "<td><div align='right' class='style11'><a href='rpt_supplier_details.php?varSupKey=$varSupKey&varSupplierName=$varSupplierName&varTarikhMula=$varTarikhMula&varTarikAkhir=$varTarikhAkhir'>Details</a></td>";
	echo "</tr>";
	}	
}
?>
<tr>
	<td><div align='right' class='style11'>&nbsp;</td>
	<td><div align='right' class='style11'>Grand Total</td>
	<td><div align='right' class='style11'><?php echo $varGrandTotPO;?></td>
	<td><div align='right' class='style11'><?php echo $varGrandTotalCancelPO?></td>
	<td><div align='right' class='style11'><?php echo $varGrandTotalCompletePO;?></td>
	<td><div align='right' class='style11'><?php echo $varGrandTotalOSPO;?></td>
	<td><div align='right' class='style11'><?php echo $varGrandOnTime;?></td>
	<td><div align='right' class='style11'><?php echo $varGrandNotOnTime;?></td>
	<td><div align='right' class='style11'>&nbsp;</td>	
</tr>
 </tbody>
</table>
		

<!--</form>-->