<?php

include_once('../hdr_general.php');


function right($str, $length) {
     return substr($str, -$length);
}


$varTahun = right($_REQUEST['tarikhMula'], 4); 
$var0101 = '01/01/'.$varTahun;  //jadi kan 1 Jan
$var3105 = '31/05/'.$varTahun;  //jadi kan 31 May
$var0106 = '01/06/'.$varTahun;  //jadi kan 1 jun
$var3112 = '31/12/'.$varTahun;  //jadi kan 31 Dec

//$unixTarikhMula=converttounix($_REQUEST['tarikhMula']);
//$unixTarikhAkhir=converttounix($_REQUEST['tarikhAkhir']);
if ($_REQUEST['tarikhMula']!=''){
$unixTarikhMula=converttounix($var0101);
$unixTarikhAkhir=converttounix($var3105);

$unixTarikhMula2=converttounix($var0106);
$unixTarikhAkhir2=converttounix($var3112);	
}
?>


<?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
?>

<?php		
				
			
?>
<style type="text/css">
<!--
.style1 {
	color: #0066CC;
	font-weight: bold;
}
.style2 {font-size: 24px}
-->
</style>

<!--<form method="post" name="frmMain">-->
<?php
		
		echo $pubdatefrom;
		
//=============================		START UTK DARI 01 JAN - 31 MAY================================				
		//get project primary key
		$selProKey = mysql_query("select * from project_details where projectname = '$pubprojectname'");		
		while($row = mysql_fetch_array($selProKey))
		{			
			$varProKey = $row['projectpkey'];				
		}
				
		//get total PO	
		if ($_REQUEST['tarikhMula'] !='')
		{		
		$selTotalPO = mysql_query("select count(*) as totalPO from po_details where po_projectpkey = '$varProKey' and po_cancel = 0 and (po_date between  '$unixTarikhMula' and '$unixTarikhAkhir')");		
		}
		else
		{$selTotalPO = mysql_query("select count(*) as totalPO from po_details where po_projectpkey = '$varProKey' and po_cancel = 0");}
		
		
		while($row = mysql_fetch_array($selTotalPO))
			{			
				$varTotalPO = $row['totalPO'];				
			}
		
		
		if ($varTotalPO == 0)
			{$varPerTotalPO = 0;}
		else
			{$varPerTotalPO = ($varTotalPO/$varTotalPO)*100;}
		
		//get PO Complete
		if ($_REQUEST['tarikhMula'] !='')
		{$selComPO = mysql_query("select count(*) as comPO from po_details where po_projectpkey = '$varProKey' and po_status = '3' and (po_date between  '$unixTarikhMula' and '$unixTarikhAkhir')");}
		else
		{$selComPO = mysql_query("select count(*) as comPO from po_details where po_projectpkey = '$varProKey' and po_status = '3'");}
		while($row = mysql_fetch_array($selComPO))
		{			
			$varComPO = $row['comPO'];				
		}
		
		if ($varTotalPO == 0)
			{$varPerTotalPO = 0;}
		else
			{$varPerComPO = ($varComPO/$varTotalPO)*100;}
		
		
		//get PO OutStanding
		if ($_REQUEST['tarikhMula'] !='')
		
		{$selOutPO = mysql_query("select count(*) as outPO from po_details where po_projectpkey = '$varProKey' and po_status = '1' and po_cancel = '0' and (po_date between  '$unixTarikhMula' and '$unixTarikhAkhir')");}
		else
		{	
			$selOutPO = mysql_query("select count(*) as outPO from po_details where po_projectpkey = '$varProKey' and po_status = '1' and po_cancel = '0'");}		
		
		while($row = mysql_fetch_array($selOutPO))
		{			
			$varOutPO = $row['outPO'];				
		}
		
		if ($varTotalPO == 0)
			{$varPerTotalPO = 0;}
		else
			{$varPerOutPO = ($varOutPO/$varTotalPO)*100;}
		
		//on time delivery
		if ($varComPO == 0)
			{
				$varPerOntimeDel = 0;
				$varDisplayOntimeDel =0;
			}
		else
		{
			$varDisplayOntimeDel = ($varComPO - $varOutPO);
			$varOntimeDel = ($varComPO - $varOutPO)/$varComPO;				
			$varPerOntimeDel = ($varDisplayOntimeDel/$varComPO)*100;
		}	
//=============================		END UTK DARI 01 JAN - 31 MAY================================		
		
		
//=============================		START UTK DARI 01 JUN - 31 DEC =================================
		//get total PO	
		if ($_REQUEST['tarikhMula'] !='')
		{		
		$selTotalPO2 = mysql_query("select count(*) as totalPO from po_details where po_projectpkey = '$varProKey'  and po_cancel = 0 and (po_date between  '$unixTarikhMula2' and '$unixTarikhAkhir2')");		
		}
		else
		{$selTotalPO2 = mysql_query("select count(*) as totalPO from po_details where po_projectpkey = '$varProKey'  and po_cancel = 0" );}
		
		
		while($row = mysql_fetch_array($selTotalPO2))
			{			
				$varTotalPO2 = $row['totalPO'];				
			}
		
		
		if ($varTotalPO2 == 0)
			{$varPerTotalPO2 = 0;}
		else
			{$varPerTotalPO2 = ($varTotalPO2/$varTotalPO2)*100;}
		
		//get PO Complete
		if ($_REQUEST['tarikhMula'] !='')
		{$selComPO2 = mysql_query("select count(*) as comPO from po_details where po_projectpkey = '$varProKey' and po_status = '3' and (po_date between  '$unixTarikhMula2' and '$unixTarikhAkhir2')");}
		else
		{$selComPO2 = mysql_query("select count(*) as comPO from po_details where po_projectpkey = '$varProKey' and po_status = '3'");}
		while($row = mysql_fetch_array($selComPO2))
		{			
			$varComPO2 = $row['comPO'];				
		}
		
		if ($varTotalPO2 == 0)
			{$varPerTotalPO2 = 0;}
		else
			{$varPerComPO2 = ($varComPO2/$varTotalPO2)*100;}
		
		
		//get PO OutStanding
		if ($_REQUEST['tarikhMula'] !='')
		
		{$selOutPO2 = mysql_query("select count(*) as outPO from po_details where po_projectpkey = '$varProKey' and po_status = '1' and po_cancel = '0' and (po_date between  '$unixTarikhMula2' and '$unixTarikhAkhir2')");}
		else
		{	
			$selOutPO2 = mysql_query("select count(*) as outPO from po_details where po_projectpkey = '$varProKey' and po_status = '1' and po_cancel = '0'");}		
		
		while($row = mysql_fetch_array($selOutPO2))
		{			
			$varOutPO2 = $row['outPO'];				
		}
		
		if ($varTotalPO2 == 0)
			{$varPerTotalPO2 = 0;}
		else
			{$varPerOutPO2 = ($varOutPO2/$varTotalPO2)*100;}
		
		if ($varComPO2 == 0)
			{
				$varPerOntimeDel2 = 0;
				$varDisplayOntimeDel2 = 0;
			}
		else
		{
			//on time delivery
			$varDisplayOntimeDel2 = ($varComPO2 - $varOutPO2);
			$varOntimeDel2 = ($varComPO2 - $varOutPO2)/$varComPO2;				
			$varPerOntimeDel2 = ($varDisplayOntimeDel2/$varComPO2)*100;	
		}
//=============================		END UTK DARI 01 JUN - 31 DEC =================================
		
// klu tak ada tahun
if ($varTahun == '')
{
	$varTotalPO2 = 0;
	$varPerTotalPO2 =0;
	$varComPO2=0;
	$varPerComPO2=0;
	$varOutPO2=0;
	$varPerOutPO2=0;
	$varDisplayOntimeDel2=0;
	$varComPO2=0;
	$varPerOntimeDel2=0;	
	
}


			
		
?>
<table width="859" border="0">
  <tr>
    <td height="23">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3"><div align="center"><img src="../gfx/zikayheader.png" width="225" height="82" /></div></td>
  </tr>
  <tr>
    <td height="23">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3"><div align="center" class="style1 style2" style="border-bottom:#000000 medium">ON TIME DELIVERY REPORT</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 width="145" height="23">DATE</td>
    <td width="12">:</td>
    <td width="83"><?php echo $var0101; //echo  $_REQUEST['tarikhMula'];  ?></td>
    <td width="45">To</td>
    <td width="552"><?php echo  $var3112; // echo  $_REQUEST['tarikhAkhir'] ?></td>
  </tr>
  <tr>
    <td height="23">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>

<table width="860" border cellspacing="0.5">
  <tr>
    <td>&nbsp;</td>
    <td colspan="4"><div align="center">ON TIME DELIVERY REPORT</div></td>
  </tr>
  
  
    <tr><td width="162">&nbsp;</td>
    <td colspan="2"><div align="center"><?php if ($varTahun == ''){echo 'All';}else{echo '1/'.right($_REQUEST['tarikhMula'], 2);} ?> </div></td>
    <td colspan="2"><div align="center"><?php if ($varTahun == ''){echo 'NONE';}else{echo '2/'.right($_REQUEST['tarikhMula'], 2);} ?></div></td>
    </tr><tr>
      <td>Type of PO</td>
      <td width="188"><div align="center">Quantity</div></td>
      <td width="159"><div align="center">(%)</div></td>
      <td width="181"><div align="center">Quantity</div></td>
      <td width="148"><div align="center">(%)</div></td>
    </tr>
  
  <tr>
    <td>PO Issued</td>
    <td><div align="center"><?php echo $varTotalPO;?></div></td>
    <td><div align="center"><?php echo number_format($varPerTotalPO, 2, '.', ''); ?>%</div></td>
    <td><div align="center"><?php echo $varTotalPO2;?></div></td>
    <td><div align="center"><?php echo number_format($varPerTotalPO2, 2, '.', ''); ?>%</div></td>
  </tr>
  <tr>
    <td>PO Delivered</td>
    <td><div align="center"><?php echo $varComPO; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerComPO, 2, '.', '');?>%</div></td>
    <td><div align="center"><?php echo $varComPO2; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerComPO2, 2, '.', '');?>%</div></td>
  </tr>
  <tr>
    <td>PO Outstanding</td>
    <td><div align="center"><?php echo $varOutPO; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerOutPO, 2, '.', ''); ?>%</div></td>
    <td><div align="center"><?php echo $varOutPO2; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerOutPO2, 2, '.', ''); ?>%</div></td>
  </tr>
  <tr>
    <td>On Time Delivery</td>
    <td><div align="center"><?php echo $varDisplayOntimeDel; ?>/<?php echo $varComPO; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerOntimeDel, 2, '.', '');?>%</div></td>
    <td><div align="center"><?php echo $varDisplayOntimeDel2; ?>/<?php echo $varComPO2; ?></div></td>
    <td><div align="center"><?php echo number_format($varPerOntimeDel2, 2, '.', '');?>%</div></td>
  </tr>
</table>
		
<!--</form>-->
