<?php


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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Charging Report </TITLE>

<style type="text/css">

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

				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 150px;
   }   
   
   
   
</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


$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");



//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Company_memo As CompanyMemo, ".
             " Company_Name As CompanyName from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}



//Read From Module Table
$querymodule=" Select ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
	
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);



//if($PrePayMId != '')
//{
	$sub_query = " Select PrePaymentM.PrePaymentM_Id As PrePaymentMId, PrePaymentM.Ledger_Number As LedgerNumber, Ledger_Name As LedgerName, PrePaymentM.PrePayment_dbcd As PrePaymentdbcd, ".
				  " ifNull(PrePaymentM.PrePayment_TotalB1,0) As PrePaymentTotalB1, ifNull(PrePaymentM.PrePayment_TotalB2,0) As PrePaymentTotalB2, ".
				  " ifNull(PrePaymentM.PrePayment_RemainB1,0) As PrePaymentRemainB1, ifNull(PrePaymentM.PrePayment_RemainB2,0) As PrePaymentRemainB2  ".
				  " From PrePaymentM ".
				  " Inner Join Ledger on PrePaymentM.Ledger_Number = Ledger.Ledger_Number ";
				  //" Where PrePaymentM_Id = ".$PrePayMId;
	
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='1200' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=165>
		  <col width=270>
		  <col width=95>
		  <col width=110>";
		
	echo" <tr><td colspan=4><img src='LogoFile.php' width='115' height='50' style='float:left;'/>";
	echo"	    <div style='display:inline-block; vertical-align:top;'><span style='font-size:16px; font-weight:bold;'>$CompanyName</span> <br>";
	echo"		<span style='font-size:9px; font-weight:normal;'>".nl2br($CompanyMemo)."</span></div>";
	echo" </td></tr>";
	
	echo" <tr><td align='center'>&nbsp;</td>";
	echo"	  <td align='center'><font size=5><b>Monthly Charging</b></font></td><td align='right' colspan=2 ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2];
	echo "</font></td></tr></table></td></tr>";
	
	echo "<tr><td align='left'>";
	echo "<table border=0 width='1200' cellspacing=1 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=110>
		  <col width=330>
		  <col width=80>
		  <col width=70>
		  <col width=60>
		  <col width=100>
		  <col width=100>
		  <col width=100>
		  <col width=100>
		  <col width=100>";

	echo "<tr height='5'><td colspan=10><h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left'>Ledger No.</th>";
	echo "  <th align='left'>Ledger Name/Description</th>";
	echo "  <th align='center'>Date</th>";
	echo "  <th align='center'>Division</th>";
	echo "  <th align='center'>Cur.</th>";
	echo "  <th align='center'>Amount</th>";
	echo "  <th align='center'>Amount Gen.</th>";
	echo "  <th align='center'>Balance</th>";
	echo "  <th align='center'>Project</th>";
	echo "  <th align='center'>CostCenter</th>";
	echo "</tr>";
	echo "<tr height='5'><td colspan=10><h4></td></tr>";
	
    $sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
	while($sub_rows=mysql_fetch_array($sub_result)){
		extract($sub_rows);
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$LedgerNumber</td>";
		echo "  <td align='left' valign='top'>$LedgerName</td>";
		echo "	<td align='left' valign='top' colspan='8'>&nbsp;</td>";
		echo "</tr>";
		
		
		$sub_query1= " Select PrePaymentD.Ledger_Number As LedgerNumberD, Ledger.Ledger_Name As LedgerNameD, PrePaymentD.Description As DescriptionD, ".
					 " Date_Format(PrePaymentD.Date1,'%d/%m/%Y') As PrePayDDate, concat(ifNull(PrePaymentD.DivisionGenerate,''), '/', ifNull(PrePaymentD.Division,'')) as RemainDivision,  ".
					 " PrePaymentD.Project_Code As ProjectCode, PrePaymentD.Costcent_Code As CostcentCode, PrePaymentD.Project_CodeD, PrePaymentD.Costcent_CodeD,   ".
					 " PrePaymentD.Currency_Code As CurrencyCode, Round(PrePaymentD.Amount, ".$DecimalBase.") As Amount, ".
					 " Round(PrePaymentD.AmountGenerate, ".$DecimalBase.") As AmountGenerate, ".
					 " Round((PrePaymentD.Amount - PrePaymentD.AmountGenerate), ".$DecimalBase.") As AmountBalance ".
					 " From PrePaymentD Left Join Ledger on PrePaymentD.Ledger_Number = Ledger.Ledger_Number ".
					 " Where PrePaymentD.PrePaymentM_Id = '".$PrePaymentMId."' ".
					 " Order By PrePaymentD.Ledger_Number ";	
					 
		$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
		while($sub_rows1=mysql_fetch_array($sub_result1)){
			extract($sub_rows1);			 
			
			
			echo "<tr>";
			echo "  <td class='detail' align='left' valign='top' style='padding-left:15px;'>$LedgerNumberD</td>";
			echo "  <td class='detail' align='left' valign='top'>$LedgerNameD</td>";
			echo "  <td class='detail' align='center' valign='top'>$PrePayDDate</td>";
			echo "  <td class='detail' align='left' valign='top'>$RemainDivision</td>";
			echo "  <td class='detail' align='center' valign='top'>$CurrencyCode</td>";
			echo "	<td class='detail' align='right' valign='top'>".number_format($Amount, $DecimalBase)."</td>";
			echo "	<td class='detail' align='right' valign='top'>".number_format($AmountGenerate, $DecimalBase)."</td>";
			echo "	<td class='detail' align='right' valign='top'>".number_format($AmountBalance, $DecimalBase)."</td>";
			echo "	<td class='detail' align='left' valign='top' style='padding-left:15px;'>$ProjectCode</td>";
			echo "	<td class='detail' align='left' valign='top'>$CostcentCode</td>";
			echo "</tr>";
			
			if ($DescriptionD != '') {
				echo "<tr>";
				echo "  <td class='detail' align='left' valign='top'>&nbsp;</td>";
				echo "  <td class='detail' align='left' valign='top'>$DescriptionD</td>";
				echo "  <td class='detail' align='left' valign='top' colspan='8'>&nbsp;</td>";
				echo "</tr>";
			}
		}	
		
		echo "<tr height='5'><td colspan=10><h4></td></tr>";
	}	
			
			
	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 format_currency($amount, $DecimalBase) { 

    if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
 
    else return number_format($amount, $DecimalBase); 
} 

print <<< HERE

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

