<?php

session_start();
if($_SESSION['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($_SESSION['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());


$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];} 
$message 	= $_POST["message"]; if ($message == ""){$message = $_GET["message"];} 


$txtChgPurId	  	  	= $_POST["txtChgPurId"]; if ($txtChgPurId == ""){$txtChgPurId = $_GET["txtChgPurId"];}  
$txtChgAutoId	  		= $_POST["txtChgAutoId"]; if ($txtChgAutoId == ""){$txtChgAutoId = $_GET["txtChgAutoId"];}  
$txtLedgerNoDebit	  	= $_POST["txtLedgerNoDebit"]; if ($txtLedgerNoDebit == ""){$txtLedgerNoDebit = $_GET["txtLedgerNoDebit"];}  
$txtLedgerDescDebit	  	= $_POST["txtLedgerDescDebit"]; if ($txtLedgerDescDebit == ""){$txtLedgerDescDebit = $_GET["txtLedgerDescDebit"];}  
$txtLedgerNoCredit	  	= $_POST["txtLedgerNoCredit"]; if ($txtLedgerNoCredit == ""){$txtLedgerNoCredit = $_GET["txtLedgerNoCredit"];}  
$txtLedgerDescCredit	= $_POST["txtLedgerDescCredit"]; if ($txtLedgerDescCredit == ""){$txtLedgerDescCredit = $_GET["txtLedgerDescCredit"];}  
$txtChgCurrCode	  	 	= $_POST["txtChgCurrCode"]; if ($txtChgCurrCode == ""){$txtChgCurrCode = $_GET["txtChgCurrCode"];}  
$txtChgAmount	  		= $_POST["txtChgAmount"]; if ($txtChgAmount == ""){$txtChgAmount = $_GET["txtChgAmount"];}  
$txtChgDistOption	  	= $_POST["txtChgDistOption"]; if ($txtChgDistOption == ""){$txtChgDistOption = $_GET["txtChgDistOption"];}  
$txtChgIsVat	  		= $_POST["txtChgIsVat"]; if ($txtChgIsVat == ""){$txtChgIsVat = $_GET["txtChgIsVat"];}  
$txtChgGenDebit	  		= $_POST["txtChgGenDebit"]; if ($txtChgGenDebit == ""){$txtChgGenDebit = $_GET["txtChgGenDebit"];}  
$txtChgSubToCharge	  	= $_POST["txtChgSubToCharge"]; if ($txtChgSubToCharge == ""){$txtChgSubToCharge = $_GET["txtChgSubToCharge"];}  
$txtpurchasecharges_remark	  	= $_POST["txtpurchasecharges_remark"]; if ($txtpurchasecharges_remark == ""){$txtpurchasecharges_remark = $_GET["txtpurchasecharges_remark"];}  


  
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>Transfer Charges</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  x=document.transfercharges;
  var ChgPurId = x.txtChgPurId.value;
  
  x.actionmode.value='';
  x.txtChgAutoId.value='';
  x.txtLedgerNoDebit.value='';
  x.txtLedgerDescDebit.value='';
  x.txtLedgerNoCredit.value='';
  x.txtLedgerDescCredit.value='';
  x.txtChgCurrCode.value='';
  x.txtChgAmount.value='';
  x.txtChgDistOption.value='';
  x.txtChgIsVat.value='';
  x.txtChgGenDebit.value='';
  x.txtChgSubToCharge.value='';
  x.txtpurchasecharges_remark.value='';
  
  x.action='Form_transferCharges.php?txtChgPurId='+ChgPurId;
  x.submit();

}

function CheckFields(){
  
    x = document.transfercharges;
	
	var CurrCode   = x.txtChgCurrCode.value;
	var DistOption = x.txtChgDistOption.value;
	var ChgAmount  = x.txtChgAmount.value;
	var txtLedgerNoDebit  = x.txtLedgerNoDebit.value;
	var txtLedgerNoCredit  = x.txtLedgerNoCredit.value;
	
	
	
	if (txtLedgerNoDebit=='')
	{
		alert("Ledger Debit Column is mandatory!");
		x.txtLedgerNoDebit.focus();
		return 0;
	}
	else if (txtLedgerNoCredit=='')
	{
		alert("Ledger Credit Column is mandatory!");
		x.txtLedgerNoCredit.focus();
		return 0;
	}
	else if (CurrCode=='')
	{
		alert("Currency Column is mandatory!");
		x.txtChgCurrCode.focus();
		return 0;
	}
	else if (ChgAmount == '' || ChgAmount*1 != ChgAmount/1 || ChgAmount <= 0){
		alert('Amount should be a NUMERIC value and MORE THAN Zero');
		x.txtChgAmount.focus();
		return 0;
	}
	else if (DistOption == '' || DistOption*1 != DistOption/1 || (DistOption < 1 || DistOption > 5)) {
		alert('Distribution option should be a NUMERIC value and BETWEEN 1 and 5');
		x.txtChgDistOption.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}

function SubmitForm() {
	x=document.transfercharges;
	var ChgPurId = x.txtChgPurId.value;
	var vMode = x.actionmode.value;
	var ChargesId = x.txtChgAutoId.value;
	var result = CheckFields();
	if (result == 1) {
		if (ChgPurId != '' && vMode == 'edit')
		{
			x.action='Form_TransferCharges_Actions.php?Mode='+vMode+'&ChgPurId='+ChgPurId+'&ChgAutoId='+ChargesId;
			x.submit();
		}
		else if (ChgPurId != '' && vMode == '')
		{
			x.action='Form_TransferCharges_Actions.php?Mode=insert&ChgPurId='+ChgPurId;
			x.submit();
		}
	}	
	
}

function LoadFromAccount() {
	x=document.transfercharges;
	var ChgPurId = x.txtChgPurId.value;
	var vMode = x.actionmode.value;
	
	if (ChgPurId != '') {
		x.action='Form_transferCharges_Actions.php?Mode=loadfromaccount&ChgPurId='+ChgPurId;
		x.submit();
	}

}

function DeleteCharges(ChargesId) {
	x=document.transfercharges;
	var ChgPurId = x.txtChgPurId.value;
	
	if (confirm ( "Are you sure you want to Delete this record ?") )
	{
		x.action='Form_transferCharges_Actions.php?Mode=delete&ChgPurId='+ChgPurId+'&ChgAutoId='+ChargesId;
		x.submit();
	}

}

function EditCharges(ChargesId) {
	x=document.transfercharges;
	var ChgPurId = x.txtChgPurId.value;
	
	x.actionmode.value = 'edit';
	x.txtChgAutoId.value = ChargesId;
	x.action='Form_transferCharges.php?txtChgPurId='+ChgPurId+'&txtChgAutoId='+ChargesId;
	x.submit();
	
}

function CheckIsVat() {
	x=document.transfercharges;
	
	if (x.chkChgIsVat.checked) {
		x.txtChgIsVat.value = 1;
	} 
	else
	{
		x.txtChgIsVat.value = 0;
	}

}


function showlistledgers(str){

  window.open('List_Ledgers.php?item='+str,'mywindow','width=750,height=350,top=120,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function ChangeLocation() {
	x = document.transfercharges;
	var vMode = x.actionmode.value;
	
	opener.document.transfer.submit();

}

function showlistCostcenter(){
	window.open('List_TrsCostcenter.php?item=11&TransType=Tr','mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}
</script>

<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: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='ChangeLocation();' class='bodybackground'>
<form name='transfercharges' method='post' action=''>
		    
HERE;


// Load Data From SetupCharges To PurchaseCharges
if ($actionmode == '' && $txtChgPurId != '') {
	$query = " SELECT Count(*) ".
			 " FROM temp_transfercharges ".
			 " WHERE Transfer_id = ".$txtChgPurId;
			 //" AND Logged_Code = '".$_SESSION['useraccount']."' ";
			 
	$result = selectData($connection, $query);
	$exists = $result[0];		 
	
	if ($exists == 0) {
		$query = " INSERT INTO temp_transfercharges (Transfer_id, Ledger_NumberDebit, Ledger_NumberCredit, Currency_code, Charges_distoption, Charges_isVat, Logged_Code ) ".
				 " SELECT ".$txtChgPurId.", Ledger_NumberDebit, Ledger_NumberCredit, Currency_code, 1, Charges_isVat, '".$_SESSION['useraccount']."' ".
				 " FROM transferchargessetup ";
				 		
		if (!updateData($connection, $query, true))
		  $message  = mysql_error(); 
	
	}


}

if ($actionmode == 'edit' && $txtChgPurId != '' && $txtChgAutoId != '') {
	$query = " SELECT Auto As Charges_Id, Transfer_id, Ledger_NumberDebit, LedgerDebit.Ledger_Name As LedgerNameDebit, Ledger_NumberCredit, LedgerCredit.Ledger_Name As LedgerNameCredit, ".
			 " Temp_transfercharges.Currency_code, Round(Charges_Amount, $DecimalBase), Charges_AmountBase1, Charges_AmountBase2, Charges_distoption, ".
			 " ifNull(Charges_isVat,0) As Charges_isVat, ifNull(Charges_GenerateDebit,0) As Charges_GenerateDebit, ifNull(Charges_isSubjToCharge,0) As Charges_isSubjToCharge, ".
			 " transfercharges_remark, ChargesCostcent_code ".
			 " FROM Temp_transfercharges ".
			 " Left Join Ledger As LedgerDebit on Temp_transfercharges.Ledger_NumberDebit = LedgerDebit.Ledger_Number ".
			 " Left Join Ledger As LedgerCredit on Temp_transfercharges.Ledger_NumberCredit = LedgerCredit.Ledger_Number ".
			 " WHERE Transfer_id = ".$txtChgPurId.
			 " AND Auto = ".$txtChgAutoId;
			 //" AND Logged_Code = '".$_SESSION['useraccount']."' ";			   
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtChgAutoId         = $rows[0];
	$txtLedgerNoDebit     = $rows[2];
	$txtLedgerDescDebit   = $rows[3];
	$txtLedgerNoCredit    = $rows[4];
	$txtLedgerDescCredit  = $rows[5];
	$txtChgCurrCode       = $rows[6];
	$txtChgAmount         = $rows[7];
	$txtChgDistOption     = $rows[10];
	$txtChgIsVat 	      = $rows[11];
	$txtChgGenDebit       = $rows[12];
	$txtChgSubToCharge    = $rows[13];
	$txtpurchasecharges_remark  = $rows[14];
	$txtChgCostcent			    = $rows[15];

}


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:690px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Transfer Charges
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"<table border='0' style='table-layout:fixed'>
	  <col width=120>
	  <col width=120>
	  <col width=100>
	  <col width=100>
	  <col width=100>
	  <col width=130>
		     

	  <tr class='Search-header-bg'>
		<td align='left' colspan='6'><span class='style4'>
			<input type='hidden' name='txtChgPurId' value='$txtChgPurId' >
			<input type='hidden' name='actionmode' value='$actionmode' >
		</span></td>	
      </tr>
	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='6'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#F7F7F7' height='25'>
         <td valign='center' align='left' ><span class='style4'>Ledger No. Debit</span></td>
		 <td colspan='5' ><input type='hidden' name='txtChgAutoId' value='$txtChgAutoId' size='8'>
			    <input type='text' name='txtLedgerNoDebit' value='$txtLedgerNoDebit' maxlength='20' style='font-size:12px; width:80px; height:20px;'>
				<input type='text' name='txtLedgerDescDebit' id='txtLedgerDescDebit' value='$txtLedgerDescDebit' style='font-size:12px; width:300px; height:20px;' />
			    <label><span class='style4'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger No. ' 
                   onClick='showlistledgers(5); '><img src='img/btnopen.jpg' width='15' height='17' border='0' /></a> </span></label></td>
	  </tr>	 
	  
	  <tr bgcolor='#F7F7F7' height='25'>
         <td valign='center' align='left' ><span class='style4'>Ledger No. Credit</span></td>
		 <td colspan='5' >
			    <input type='text' name='txtLedgerNoCredit' value='$txtLedgerNoCredit' maxlength='20' style='font-size:12px; width:80px; height:20px;' >
				<input type='text' name='txtLedgerDescCredit' id='txtLedgerDescCredit' value='$txtLedgerDescCredit' style='font-size:12px; width:300px; height:20px;' />
			    <label><span class='style4'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger No. ' 
                   onClick='showlistledgers(6); '><img src='img/btnopen.jpg' width='15' height='17' border='0' /></a> </span></label></td>
	  </tr>	 
		  
	  <tr bgcolor='#F7F7F7' height='25'>
	     <td  valign='center' align='center' ><span class='style4'>Currency Code&nbsp;<span class='style26'>*</span></span></td> 
		 <td  valign='center' align='center' ><span class='style4'>Amount&nbsp;<span class='style26'>*</span></span></td> 
		 <td  valign='center' align='center' colspan='2'><span class='style4'>Costcenter</span></td> 
		 <td  valign='center' align='center' ><span class='style4'>is VAT</span></td> 
		 <td></td>
	  </tr>
	  <tr bgcolor='#F7F7F7' height='25'>
	     <td align='center'><span class='style4'><select name='txtChgCurrCode' id='txtChgCurrCode' style='font-size:12px; width:80px; height:20px;'>";
		  echo"<option></option>";
		  while($curf=mysql_fetch_row($resultcurrency)){
		  echo"<option ";
			if ($txtChgCurrCode==$curf[0])
				echo " selected ";
		    echo" value='$curf[0]'>$curf[0] </option>";
		  }
		  echo"</select></span></td>";		
		  
echo "	 <td align='center'><input type='text' name='txtChgAmount' value='$txtChgAmount' maxlength='20' style='font-size:12px; width:90px; height:20px;'></td>
		 <td align='center' colspan='2'><input type='text' name='txtChgCostcent' value='$txtChgCostcent' maxlength='20' style='font-size:12px; width:150px; height:20px;'>
			<img src='img/btnopen.jpg' width=15 height=15 onclick=\"showlistCostcenter();\" >
		 </td>
		 <input type='hidden' name='txtChgIsVat' value='$txtChgIsVat' >
		 <input type='hidden' name='txtChgGenDebit' value='$txtChgGenDebit' >
		 <input type='hidden' name='txtChgSubToCharge' value='$txtChgSubToCharge' >
		 <input type='hidden' name='txtChgDistOption' value='1' maxlength='1' >
		 <input name='chkChgGenDebit' type='hidden' id='chkChgGenDebit' value='1'>
		 <input name='chkChgSubToCharge' type='hidden' id='chkChgSubToCharge' value='1'>
		 <td align='center'><input name='chkChgIsVat' type='checkbox' id='chkChgIsVat' value='1' onClick='CheckIsVat();' "; 
          if($txtChgIsVat==1) echo "checked /></td>"; else echo " /></td>";
echo "<td></td>";	  
		  
echo"</tr>";

echo" <tr bgcolor='#F7F7F7'>";
echo"<td><span class='style4'>Remark</span></td>";
echo"<td colspan=10><span class='style4'><input type='text' name='txtpurchasecharges_remark' value='$txtpurchasecharges_remark' maxlength='500' style='font-size:12px; width:550px; height:20px;'></span></td>";
echo"</tr>";
echo" <tr bgcolor='#F7F7F7'>	
	    <td height='20' align='center' colspan='6'>&nbsp;</td> 
	  </tr>
	  <tr class='Search-header-bg'>
		<td align='center' colspan='6'><span class='style4'>
			 <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Submit'><span><span>Submit</span></span></button>
			 <button type='button' id='btnCancel' name='btnCancel' onclick='ClearForm(); return false;' title='Cancel'><span><span>Cancel</span></span></button>
			 <button type='button' id='btnLoad' name='btnLoad' onclick='LoadFromAccount(); return false;' title='Load From Account'><span><span>Load From Account</span></span></button>
		
		
		</span></td>
      </tr>
	  <tr ><td colspan='6'><table border='0' style='table-layout:fixed'>
		<col width=20>
		<col width=20>
		<col width=100>
		<col width=100>
		<col width=60>
		<col width=100>
		<col width=80>
		<col width=60>
		<col width=60>
		<col width=60>
		<tr class='Search-Label-bg'>
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>Ledger Debit</span></td>
			<td  height='25' align='center'><span class='style4'>Ledger Credit</span></td>
			<td  height='25' align='center'><span class='style4'>Currency</span></td>
			<td  height='25' align='center'><span class='style4'>Amount</span></td>
			<td  height='25' align='center' colspan=3><span class='style4'>Costcenter</span></td>
			<td  height='25' align='center'><span class='style4'>is VAT</span></td>
		</tr>";
		

$query = " SELECT Auto As Charges_Id, Transfer_id, Ledger_NumberDebit, LedgerDebit.Ledger_Name As LedgerNameDebit, Ledger_NumberCredit, LedgerCredit.Ledger_Name As LedgerNameCredit, ".
         " Temp_transfercharges.Currency_code, Charges_Amount, Charges_AmountBase1, Charges_AmountBase2, Charges_distoption, ".
		 " if(ifNull(Charges_isVat,0)=0, 'False', 'True') As Charges_isVat, if(ifNull(Charges_GenerateDebit,0)=0, 'False', 'True') As Charges_GenerateDebit, ".
		 " if(ifNull(Charges_isSubjToCharge,0)=0, 'False', 'True') As Charges_isSubjToCharge, ChargesCostcent_code ".
		 " FROM Temp_transfercharges ".
		 " Left Join Ledger As LedgerDebit on Temp_transfercharges.Ledger_NumberDebit = LedgerDebit.Ledger_Number ".
		 " Left Join Ledger As LedgerCredit on Temp_transfercharges.Ledger_NumberCredit = LedgerCredit.Ledger_Number ".
		 " WHERE Transfer_id = ".$txtChgPurId;
		 
//echo $query;
$result=mysql_query($query,$connection) or die(mysql_error());		
while($rows=mysql_fetch_array($result)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			

	echo'<tr '.$Color.'>
			<td height="20" align="center"><span class="style15"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick=EditCharges("'.$Charges_Id.'") title="Edit Record" ></span></td>
			<td height="20" align="center"><span class="style15"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick=DeleteCharges("'.$Charges_Id.'") title="Delete Record" ></span></td>
			<td height="20" align="center"><span class="style15">'.$Ledger_NumberDebit.'</span></td>
			<td height="20" align="center"><span class="style15">'.$Ledger_NumberCredit.'</span></td>
			<td height="20" align="center"><span class="style15">'.$Currency_code.'</span></td>
			<td height="20" align="right"><span class="style15">'.number_format($Charges_Amount, $DecimalBase).'</span></td>
			<td height="20" align="center" colspan=3><span class="style15">'.$ChargesCostcent_code.'</span></td>
			<td height="20" align="center"><span class="style15">'.$Charges_isVat.'</span></td>
		</tr>';
		
	echo'<tr '.$Color.'>	
			<td height="20" align="center" colspan="2">&nbsp;</td>
			<td height="20" align="left" colspan="3"><span class="style15">'.$LedgerNameDebit.'</span></td>
			<td height="20" align="left" colspan="5"><span class="style15">'.$LedgerNameCredit.'</span></td>
		 </tr>';
}
echo"   
 	  </table></td></tr></table><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>
	  <tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; ' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr></table>";

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

HERE;

?>