<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Charges Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<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.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
				  font-weight: bold;
                  font-family:Times-New-Roman;
                 }	
				 
table.detail1 th { font-size: 12px; 
                   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 
				 
table.detail1 td { font-size: 11px; 
				   font-weight: normal;
                   font-family:Times-New-Roman;
                 }				 

				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}						
				 
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h3 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 150px;
   }   
   
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}   
   
   
</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($ChargesId != '')
{
	$query = " Select Charges_ID, Charges_Reference, Date_Format(Charges_Date,'%d/%m/%Y') As Charges_Date, Charges_Remark ".
			 " From ChargesHD ".
	         " Where Charges_ID = ".$ChargesId;
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	
	//Read From Project Table
	$queryProject="Select Project_Memo As ProjectMemo From Project Where Project_Code = '".$ProjectCode."'";
	$resultProject=mysql_query($queryProject,$connection) or die(mysql_error());
	while($rowProject=mysql_fetch_array($resultProject)){
		extract($rowProject);
	}
	
	//Read From ChargesDT Table
	$sub_query = " Select ChargesDt_Id, ChargesDt_DebitAccount, LedgerDb.Ledger_Name As LedgerDbName, ChargesDt_CreditAccount, ".
				 " LedgerCr.Ledger_Name As LedgerCrName, ChargesDT.Currency_Code, ifNull(ChargesDt_Total,0), ".
				 " if(ChargesDt_DistOption=1, 'Amount', if(ChargesDt_DistOption=2, 'Quantity', if(ChargesDt_DistOption=3, 'Percentage',  ".
				 " if(ChargesDt_DistOption=4, 'Volume', if(ChargesDt_DistOption=5, 'Detail Amount', ''))))) As DistOption  ".
				 " From ChargesDT ".
				 " Left Join Ledger as LedgerDb on ChargesDT.ChargesDt_DebitAccount = LedgerDb.Ledger_Number ".
				 " Left Join Ledger as LedgerCr on ChargesDT.ChargesDt_CreditAccount = LedgerCr.Ledger_Number ".
				 " Where ChargesDT.Charges_Id = '".$ChargesId."'";
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
	$sub_totalrows = mysql_num_rows($sub_result);
	
	
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=165>
		  <col width=270>
		  <col width=95>
		  <col width=110>";
		  //<img src='img/imgheader1.jpg' width='115' height='50' />
	echo "<tr><td height='60'><img src='LogoFile.php' width='115' height='50' /></td>
	          <td align='center'><b><font size=5>Charges</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>";
	
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=110>
		  <col width=340>
		  <col width=90>
		  <col width=100>";

    //echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Reference</th>";
	echo "  <td align='left'>$Charges_Reference</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$Charges_Date</td>";
	echo "</tr>";
	
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$Charges_Remark</td>";
	echo "</tr>";
	
	
	if ($sub_totalrows > 0) {
		echo "<tr height='5'><td colspan=4><h4></td></tr>";
		echo"<tr>";
		echo"	<td align='left' colspan='4'>";
		echo"	<table border='0' width='635' class='detail1' tyle='table-layout:fixed'>
				  <col width=90>
				  <col width=120>
				  <col width=90>
				  <col width=120>
				  <col width=50>
				  <col width=90>
				  <col width=80>";
		echo"		<tr>";
		echo"  			<th align='left'>Debit</th>";
		echo"  			<th align='center'>&nbsp;</th>";
		echo"  			<th align='left'>Credit</th>";		  
		echo"  			<th align='center'>&nbsp;</th>";
		echo"  			<th align='center'>Currency</th>";
		echo"  			<th align='center'>Amount</th>";
		echo"  			<th align='center'>Method</th>";
		echo"		</tr>";
		
		while($sub_rows=mysql_fetch_array($sub_result)){
					
			echo "<tr>";
			echo "	<td align='left' valign='top'>".$sub_rows[1]."</td>";
			echo "	<td align='left' valign='top'>".$sub_rows[2]."</td>";
			echo "	<td align='left' valign='top'>".$sub_rows[3]."</td>";
			echo "	<td align='left' valign='top'>".$sub_rows[4]."</td>";
			echo "	<td align='center' valign='top'>".$sub_rows[5]."</td>";
			echo "	<td align='right' valign='top'>".number_format($sub_rows[6], $DecimalBase)."</td>";
			echo "	<td align='left' valign='top'>".$sub_rows[7]."</td>";
			echo "</tr>";
									
		}
		
		echo"	</table></td>";
		echo"</tr>";
		

	}
	
	
	
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";

	$sub_query1 = " Select ChargesPurchases.ChargesPurchases_Id, Purchases.Purchase_Reference, ChargesItems.ChargesItems_ID, ".
				  " Purchases.Supplier_Code, PurchasesDt.Project_Code, PurchasesDt.CostCent_Code,  ".
				  " PurchasesDt.Item_CodePacked, PurchasesDt.Item_Code, Items.Item_Description, PurchasesDt.Warehouse_Code, ".
				  " ifNull(PurchaseDt_Quantity,0), ChargesItems.ChargesItems_Volume, Round(ChargesItems_Amount, ".$DecimalBase."), ".
				  " ChargesItems_AmountB1, ChargesItems_AmountB2, ifNull(ChargesItems_Perc, 0) ".
				  " From ChargesPurchases ".
				  " Inner Join Purchases on ChargesPurchases.Purchase_Id = Purchases.Purchase_Id ".
				  " Inner Join ChargesItems on ChargesItems.ChargesPurchases_Id = ChargesPurchases.ChargesPurchases_Id ".
				  " Inner Join PurchasesDt on ChargesItems.PurchaseDt_Id = PurchasesDt.PurchaseDt_Id ".
				  " Inner Join Items on PurchasesDt.Item_Code = Items.Item_code ".
				  " Where ChargesPurchases.Charges_Id = '".$ChargesId."' ";
				   
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=90>
		  <col width=130>
		  <col width=90>
		  <col width=85>
		  <col width=70>
		  <col width=100>
		  <col width=70>";
		  
	  
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Pur Ref.</th>";
	echo "   <th align='left'>Item Code</th>";
	echo "	 <th align='center'>Warehouse</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "	 <th align='center'>Volume</th>";
	echo "	 <th align='center'>Chg Amt.</th>";
	echo "	 <th align='center'>Chg Per.</th>";
	echo "</tr>";			
		  
	echo "<tr height='5'><td colspan=7><h4></td></tr>";
	
	$TotalQty = 0;
	
	$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
	while($sub_rows1=mysql_fetch_array($sub_result1)){
		extract($sub_rows1);
		
		$TotalQty = $TotalQty + $PurchaseDt_Quantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$Purchase_Reference</td>";
		echo "  <td align='left' valign='top'>$Item_Code</td>";
		echo "  <td align='left' valign='top'>$Warehouse_Code</td>";
		echo "	<td align='right' valign='top'>$PurchaseDt_Quantity</td>";
		echo "	<td align='right' valign='top'>$ChargesItems_Volume</td>";
		echo "	<td align='right' valign='top'>".number_format($ChargesItems_Amount, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($ChargesItems_Perc, $DecimalBase)."</td>";
		echo "</tr>";
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>&nbsp;</td>";
		echo "  <td align='left' valign='top' colspan='6'>$Item_Description	</td>";
		echo "</tr>";
		
		
		$sub_query2 = " Select ChargesItemsDt_Id, ChargesItemsDt_DebitAccount, LedgerDb.Ledger_Name As LedgerDbName, ChargesItemsDt_CreditAccount, ".
					  " LedgerCr.Ledger_Name As LedgerCrName, ChargesItemsDt.Currency_Code, ifNull(ChargesItemsDt_Amount,0) ".
					  " From ChargesItemsDt ".
					  " Inner Join ChargesItems on ChargesItems.ChargesItems_Id = ChargesItemsDt.ChargesItems_Id ".
					  " Left Join Ledger as LedgerDb on ChargesItemsDt.ChargesItemsDt_DebitAccount = LedgerDb.Ledger_Number ".
					  " Left Join Ledger as LedgerCr on ChargesItemsDt.ChargesItemsDt_CreditAccount = LedgerCr.Ledger_Number ".
					  " Where ChargesItems.ChargesItems_ID = '".$ChargesItems_ID."'";
				 
		$sub_result2=mysql_query($sub_query2,$connection) or die(mysql_error());	
		$sub_totalrows2 = mysql_num_rows($sub_result2);
	
		if ($sub_totalrows2 > 0) {
			echo "<tr><td align='left' colspan='7'>";
			echo "<table border=0 width='635' cellspacing=2 class='detail1' style='table-layout:fixed;' >
				  <col width=30>
				  <col width=90>
				  <col width=120>
				  <col width=90>
				  <col width=120>
				  <col width=70>
				  <col width=100>";
				  
			echo"<tr>";
			echo"	<th align='center'>&nbsp;</th>";
			echo"	<th align='left'>Debit</th>";
			echo"	<th align='center'>&nbsp;</th>";
			echo"	<th align='left'>Credit</th>";		  
			echo"	<th align='center'>&nbsp;</th>";
			echo"	<th align='center'>Currency</th>";
			echo"	<th align='center'>Amount</th>";
			echo"</tr>";	  
				  
			while($sub_rows2=mysql_fetch_array($sub_result2)){
			
				echo "<tr>";
				echo "	<td align='left' valign='top'>&nbsp;</td>";
				echo "	<td align='left' valign='top'>".$sub_rows2[1]."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows2[2]."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows2[3]."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows2[4]."</td>";
				echo "	<td align='center' valign='top'>".$sub_rows2[5]."</td>";
				echo "	<td align='right' valign='top'>".number_format($sub_rows2[6], $DecimalBase)."</td>";
				echo "</tr>";
				
										
			}
			echo"</table>";
			echo"</td></tr>";	  
		}
	}
	

	echo "<tr height='5'><td colspan=7><h4></td></tr>";
	
	/*echo "<tr>";
	echo "   <td align='left' colspan='3'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>$TotalQty</th>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "</tr>";*/
	
	echo "</table></td></tr>";

	

}


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

