<?php

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


include("config.inc.php");
include("Functions_Purchase.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 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());


print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


function SubmitRecord() {
	x = document.chargesitems;
	
	var result = CheckFields();
	var vChgId      = x.PChgId.value;
	var vChgItemId  = x.txtChgItemId.value;
	var vMainMode   = x.mainaction.value;
	var vMode       = x.actionmode.value;
	
	if (vChgItemId == '')
		vMode = 'additem';	
	else
		vMode = 'edititem';	
	   
	if (result == 1) {
		x.action="Form_Charges_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SChgId="+vChgId+"&SChgItemId="+vChgItemId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.chargesitems;
	var vMode = x.actionmode.value;
	
	if (vMode == 'additem' || vMode == 'edititem') {
		opener.document.charges.actionmode.value = x.actionmode.value;
		opener.document.charges.mainaction.value = x.mainaction.value;
		opener.document.charges.txtChgId.value   = x.PChgId.value;
		opener.document.charges.submit();
			
		self.close();
	}
}

function CheckFields(){
  
    x = document.chargesitems;
	
	var vmode 	   = x.actionmode.value;
	var vVolume    = x.txtVolume.value;
	var vAmount    = x.txtAmount.value;
	var vPercent   = x.txtPercent.value;
	
	
	if (vAmount == '') vAmount = 0;
	vAmount = parseFloat(vAmount);
		
	if (vVolume == '') vVolume = 0;
	vVolume = parseFloat(vVolume);
	
	if (vPercent == '') vPercent = 0;
	vPercent = parseFloat(vPercent);
	
		
	if ((vAmount*1 != vAmount/1 || vAmount < 0)) {
		alert('Amount should be a NUMERIC and POSITIVE value ');
		x.txtAmount.focus();
		return 0;
	}
	else if ((vVolume*1 != vVolume/1 || vVolume < 0)) {
		alert('Volume should be a NUMERIC and POSITIVE value ');
		x.txtVolume.focus();
		return 0;
	}
	else if (((vPercent*1 != vPercent/1) || (vPercent < 0) || (vPercent > 100))) {
		alert('Percentage should be a NUMERIC value and BETWEEN 0 and 100 !');
		x.txtPercent.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}


function kd(e)
{
	x=document.chargesitems;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        //x.btnSubmitRecord.click();
		return false;
    }
    return true;
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Charges Details</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style12 {
	font-size: 10px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>


</head>
<body onLoad='ChangeLocation();' onkeydown='return kd(event)'>
<form name='chargesitems' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PChgId' value='$PChgId'>
<input type='hidden' name='PChgItemId' value='$PChgItemId'>

HERE;

if($PChgItemId != ''){
	$sub_query = " Select PurchasesDt.Item_CodePacked, PurchasesDt.Item_Code, Items.Item_Description, PurchasesDt.Warehouse_Code ".
				 " From Temp_ChargesItems As ChargesItems ".
				 " Inner Join PurchasesDt on ChargesItems.PurchaseDt_Id = PurchasesDt.PurchaseDt_Id ".
				 " Inner Join Items on PurchasesDt.Item_Code = Items.Item_code ".
				 " Where ChargesItems.ChargesItems_ID = '".$PChgItemId."' ";
	$sub_result = selectData($connection, $sub_query);
	$txtItemCode   = $sub_result[1];
	$txtItemDesc   = $sub_result[2];
}

$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

	  
echo"   <table border='0' style='table-layout:fixed' align='center' valign='top'>
          <col width=90>
		  <col width=550>";

		  
echo"	   <tr bgcolor='#F7F7F7' height='25'>
		     <th colspan='2'><span class='style3'>Item Charges Detail</span></th></tr>";
			 
		     
echo"   <tr bgcolor='#F7F7F7' height='25'>
		  <td align='left' ><span class='style12'>Item</span></td><td><b>".$txtItemCode." - ".$txtItemDesc."</b></td>";
echo"   </tr>";	

if ($PChgItemId != '' ) {
	$sub_query = " 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 Temp_ChargesItemsDt As ChargesItemsDt ".
				 " Inner Join Temp_ChargesItems As 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 = '".$PChgItemId."'";
				 
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
	$sub_totalrows = mysql_num_rows($sub_result);

	if ($sub_totalrows > 0) {
		
		echo"<tr bgcolor='#F7F7F7'>";
		echo"	<td align='left' colspan='2'>";
		echo"	<table border='0' style='table-layout:fixed'>
				  <col width=90>
				  <col width=150>
				  <col width=90>
				  <col width=150>
				  <col width=50>
				  <col width=100>";
		echo"		<tr>";
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>Debit</span></td>";
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>&nbsp;</span></td>";
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>Credit</span></td>";		  
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>&nbsp;</span></td>";
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>Currency</span></td>";
		echo"  			<td bgcolor='#F7F7F7' align='center'><span class='style27'>Amount</span></td>";
		echo"		</tr>";
		
		$TotalChargesAmt = 0;
		
		while($sub_rows=mysql_fetch_array($sub_result)){
			$ct+=1;

			if($ct%2==0)
				$Color="style='background-color:".$grid_color1."'";
			else			
				$Color="style='background-color:".$grid_color2."'";	
						
					
			echo "<tr ".$Color." >";
			echo "	<td align='left'><span class='style15'>".$sub_rows[1]."</span></td>";
			echo "	<td align='left'><span class='style15'>".$sub_rows[2]."</span></td>";
			echo "	<td align='left'><span class='style15'>".$sub_rows[3]."</span></td>";
			echo "	<td align='left'><span class='style15'>".$sub_rows[4]."</span></td>";
			echo "	<td align='center'><span class='style15'>".$sub_rows[5]."</span></td>";
			echo "	<td align='right'><span class='style15'>".number_format($sub_rows[6], $DecimalBase)."</span></td>";
			echo "</tr>";
			
			$TotalChargesAmt += $sub_rows[6];
									
		}
		
		echo "<tr ".$Color." style='height:25px;'>";
		echo "	<td align='right' colspan='5'><span class='style15'>Total Amount</span></td>";	
		echo "	<td align='right'><span class='style15'>".number_format($TotalChargesAmt, $DecimalBase)."</span></td>";
		echo "</tr>";
		
		echo"	</table></td>";
		echo"</tr>";

	}	

}
echo"</table>";

	

print <<<HERE
</form>
</body>

HERE;

?>