<?php

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


include("config.inc.php");
include("Functions_Trans.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 Login Table
$querylog=" select ifNull(Login_ModifyDiscount,0) As Login_ModifyDiscount, ifNull(Login_ModifySellingPrice,0) As Login_ModifySellingPrice ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}
//echo $querylog;
	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


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.salesjvdt;
	
	var result = CheckFields();
	var vId       = x.PId.value;
	var vDtId     = x.txtDtId.value;
	var vMainMode = x.mainaction.value;
	var vMode     = x.actionmode.value;
	
	if (vDtId == '')	vMode = 'submitrecord';	
	else         		vMode = 'editdt';	
	   
	if (result == 1) {
		x.action="Form_SalesJV_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SId="+vId+"&SDtId="+vDtId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.salesjvdt;
	var vMode = x.actionmode.value;
	
	if (vMode == 'submitrecord') {
		
		opener.document.salesjv.actionmode.value   = x.actionmode.value;
		opener.document.salesjv.mainaction.value   = x.mainaction.value;
		//opener.document.salesjv.message.value   = x.message.value;
		opener.document.salesjv.txtSalesJVId.value = x.PId.value;
		opener.document.salesjv.submit();
		
	} 
	else if (vMode == 'submitrecord' || vMode == 'editdt') {
		opener.document.salesjv.actionmode.value   = x.actionmode.value;
		opener.document.salesjv.mainaction.value   = x.mainaction.value;
		opener.document.salesjv.txtSalesJVId.value = x.PId.value;
		opener.document.salesjv.submit();
		
		self.close();
	
	}

}

function CheckFields(){
  
    x = document.salesjvdt;
	
	var vLedgerNo	 = x.txtLedgerNo.value;
	var vDtQty		 = x.txtDtQty.value;
	var vDtPrice 	 = x.txtDtPrice.value;
	var vDtDiscPer 	 = x.txtDtDiscPer.value;

	var vmode = x.actionmode.value;
	
	if (vDtPrice == '')   vDtPrice = 0;
	if (vDtQty == '')     vDtQty = 0;
	if (vDtDiscPer == '') vDtDiscPer = 0;
	
	vDtQty     = parseFloat(vDtQty);	
	vDtPrice   = parseFloat(vDtPrice);	
	vDtDiscPer = parseFloat(vDtDiscPer);	
	
	if (vLedgerNo == '')
	{
		alert("Ledger is mandatory!");
		x.txtLedgerNo.focus();
		return 0;
	}
	else if ( vDtQty*1 != vDtQty/1 || vDtQty <= 0 ) {
		alert('Quantity should be a NUMERIC and POSITIVE value.');
		x.txtDtQty.focus();
		return 0;
	}
	else if ((vDtPrice*1 != vDtPrice/1 || vDtPrice <= 0) ) {
		alert('Price should be a NUMERIC and POSITIVE value.');
		x.txtDtPrice.focus();
		return 0;
	}
	else if (vDtDiscPer != 0 && (vDtDiscPer*1 != vDtDiscPer/1 || vDtDiscPer < 0) ) {
		alert('Discount should be a NUMERIC and POSITIVE value.');
		x.txtDtDiscPer.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}



function CalcDtTotal(option, dec) {
	x = document.salesjvdt;
	
	if (!e) { var e = window.event;}
    var intKey = (window.Event) ? e.which : e.keyCode;
	
	var vmode = x.actionmode.value;
	var vDtQty     = x.txtDtQty.value;
	var vDtPrice   = x.txtDtPrice.value;
	var vDtDiscPer = x.txtDtDiscPer.value;
	var vDtTotal   = x.txtDtTotal.value;
	
	if (vDtPrice == '')   vDtPrice = 0;
	if (vDtQty == '')     vDtQty = 0;
	if (vDtDiscPer == '') vDtDiscPer = 0;
	if (vDtTotal == '')   vDtTotal = 0;
	
	vDtQty     = parseFloat(vDtQty);	
	vDtPrice   = parseFloat(vDtPrice);	
	vDtDiscPer = parseFloat(vDtDiscPer);	
	vDtTotal   = parseFloat(vDtTotal);	
	
	if (option == 1 && vDtQty > 0 && vDtPrice >= 0)
		x.txtDtTotal.value = Math.round(((vDtQty*vDtPrice) - (vDtQty*vDtPrice*vDtDiscPer/100))*Math.pow(10,dec))/Math.pow(10,dec);
	else if (option == 2 && intKey != 9 && vDtQty > 0 && vDtTotal >= 0)
		x.txtDtPrice.value = Math.round((vDtTotal/vDtQty)*Math.pow(10,dec))/Math.pow(10,dec);

}


function SetVat() {
	x = document.salesjvdt;
	var vmode = x.actionmode.value;
	
	x.txtDtVat.value = 0;
	if (x.chkDtVat.checked) x.txtDtVat.value = 1;
	   
}


function showlistLedgers(str){
	x = document.salesjvdt;
	var vTransType = 'JS';
	
	window.open('Form_Trans_ListLedgers.php?str=2&PTransType='+vTransType,'mywindow','width=650,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');

}

function showlistManualNo(str) {
	x = document.salesjvdt;

	var vTransType = 'JS';
	var vLedgerNo  = x.txtLedgerNo.value;
	
	window.open('Form_Trans_ListManualNo.php?str='+str+'&PTransType='+vTransType+'&PLedgerNo='+vLedgerNo,'Form_Trans_ListManualNo','width=740,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function RefreshPage()
{
  document.salesjvdt.submit();

}


function kd(e)
{
	x=document.salesjvdt;
    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>SalesJV Detail</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;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style13 {
	font-size: 11px;
	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>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body onLoad='ChangeLocation();' >
<form name='salesjvdt' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PDate' value='$PDate'>
<input type='hidden' name='PId' value='$PId'>
<input type='hidden' name='PDtId' value='$PDtId'>
<input type='hidden' name='PMaturityDate' value='$PMaturityDate'>
<input type='hidden' name='PClientCode' value='$PClientCode'>
<input type='hidden' name='PSalesmanCode' value='$PSalesmanCode'>
<input type='hidden' name='PCurr' value='$PCurr'>
<input type='hidden' name='PProjectCode' value='$PProjectCode'>
<input type='hidden' name='PCostCentCode' value='$PCostCentCode'>
<input type='hidden' name='PDiscountPer' value='$PDiscountPer'>
<input type='hidden' name='PDiscountAmt' value='$PDiscountAmt'>
<input type='hidden' name='PVatChkbx' value='$PVatChkbx'>



HERE;
echo "<input type='hidden' name='PRemark' value='".htmlspecialchars($PRemark, ENT_QUOTES)."' > ";
echo "<input type='hidden' name='PManualNo' value='".htmlspecialchars($PManualNo, ENT_QUOTES)."' > ";
	


// User Has No Access To Modify The Price / Discount	
$ModeModifySellPrice = '';
$DiscountMode = '';
if ($Login_ModifySellingPrice == 0) $ModeModifySellPrice = ' readonly ';
if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';



$sub_query = " Select Currency_Code ".
			 " From Temp_SalesJv ".
			 " Where SalesJv_ID = '".$PId."' ";
$sub_result = selectData($connection, $sub_query);				 

if (empty($txtDtManualNo)) $txtDtManualNo = $PManualNo;

if (empty($txtDtCurr))
{
	if ($sub_result[0] != '')	$txtDtCurr = $sub_result[0];
	else		$txtDtCurr = $base1;
}	

if ($PClientCode != '') {
	$sub_query1 = " Select ifNull(Client_VAT,0) From Clients ".
				  " Where Ledger_Number = '".$PClientCode."' ";
	$sub_result1 = selectData($connection, $sub_query1);	
	$txtDtVat = $sub_result1[0];
	$chkDtVat = $sub_result1[0];
}	

//Edit SalesJV Detail Mode
if($PDtId != ''){
					   
	$querydt = " Select SalesJVDt.SalesJVDt_ID, SalesJVDt.Ledger_Number, Ledger.Ledger_Name, SalesJVDt_Description, SalesJvDt_Description2, SalesJVDt.Currency_Code, ".
	           " ifNull(SalesJVDt_Quantity,0), Round(ifNull(SalesJVDt_Price,0), ".$DecimalBase."), SalesJVDt_PriceBase1, SalesJVDt_PriceBase2, ".
			   " Round(ifNull(SalesJVDt_DiscountPer,0), ".$DecimalBase."), Round(ifNull(SalesJVDTCalcTotal,0), ".$DecimalBase.") As Total, ".
			   " ifNull(SalesJVDt.SalesJVDt_VAT, 0) As SalesJVDtVAT, SalesJvDt_ManualNo ".
			   " From Temp_SalesJVDt As SalesJVDt Left Join Ledger on SalesJVDt.Ledger_Number = Ledger.Ledger_Number ".
			   " Where SalesJVDt.SalesJVDt_ID = ".$PDtId;
			   //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";			   
	//echo $querydt;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
    $rowsdt=mysql_fetch_array($resultdt);
	
	$txtDtId           = $rowsdt[0];
	$txtLedgerNo       = $rowsdt[1];
	$txtLedgerName	   = $rowsdt[2];
	$txtDescription1   = $rowsdt[3];
	$txtDescription2   = $rowsdt[4];
	$txtDtCurr		   = $rowsdt[5];
	$txtDtQty          = $rowsdt[6];
	$txtDtPrice 	   = $rowsdt[7];
	$txtDtDiscPer  	   = $rowsdt[10];
	$txtDtTotal        = $rowsdt[11];
	$chkDtVat          = $rowsdt[12];
	$txtDtVat          = $rowsdt[12];
	$txtDtManualNo     = $rowsdt[13];
	
}

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

	  
echo"   <tr><td valign='top'>
        <table border='0' style='table-layout:fixed' align='center' valign='top' >
          <col width=75>
		  <col width=120>
		  <col width=300>
		  <col width=70> ";
  
	echo"  <tr bgcolor='#F7F7F7' height='25'>
		     <th colspan='4'><span class='style3'>Add Detail </span></th></tr>
		   <tr bgcolor='#F7F7F7' >	
			 <td colspan='4' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>			 
			 
		   <tr bgcolor='#F7F7F7' height='25'>
            <td valign='center' align='left' >
				<input type='hidden' name='txtDtId' value='$txtDtId' >
				<span class='style12'>Ledger No.&nbsp;<span class='style26'>*</span></span></td>";
	if ($PDtId == '') {		 
		echo"  	<td ><span class='style11'><input type='text' name='txtLedgerNo' id='txtLedgerNo' value='".htmlspecialchars($txtLedgerNo, ENT_QUOTES)."' maxlength='10' style='font-size:12px; width:90px; height=20px;' /></span>
					<label><span class='style11'><img src='img/btnopen.jpg' title=' Select Ledger ' width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistLedgers(1); '  /></span></label></td>";
		echo"   <td colspan='2'><span class='style11'><input type='text' name='txtLedgerName' id='txtLedgerName' value='".htmlspecialchars($txtLedgerName, ENT_QUOTES)."' maxlength='100' style='font-size:12px; width:300px; height=20px;' /></span></td>";
	}
	 else {
		echo " <td colspan='3'><input type='hidden' name='txtLedgerNo' value='$txtLedgerNo' ><input type='hidden' name='txtLedgerName' id='txtLedgerName' value='$txtLedgerName'  />
					<span class='style13'>$txtLedgerNo &nbsp;&nbsp; $txtLedgerName</span></td>";
	}
	echo" </tr>";	 
	
	echo" <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Description<span class='style26'>*</span></span></td>
		     <td colspan='3'><input type='text' name='txtDescription1' id='txtDescription1' value='".htmlspecialchars($txtDescription1, ENT_QUOTES)."' maxlength='100' style='font-size:12px; width:420px; height:18px;' /></td>";
	echo" </tr>";
	
	echo" <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Description 2</span></td>
			 <td colspan='3'><input type='text' name='txtDescription2' id='txtDescription2' value='".htmlspecialchars($txtDescription2, ENT_QUOTES)."'  maxlength='100' style='font-size:12px; width:420px; height:18px;'></td>";
	echo" </tr>";
	
	echo"<tr bgcolor='#F7F7F7' height='23'>";
	echo"  <td align='left' ><span class='style12'>Manual No</span></td>";
	echo"  <td colspan='3'><span class='style11'><input type='text' name='txtDtManualNo' value='$txtDtManualNo' style='text-align:left; font-size:12px; width:100px; height=18px;' maxlength='45' >
			<label><img src='img/btnopen.jpg' title='Select Manual No' width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistManualNo(2); '  /></label></td>";
	echo"</tr>";
	
	echo" <tr bgcolor='#F7F7F7' height='23'>";
    echo" 	 <td align='left' ><span class='style12'>Currency</span></td>";
  	echo"  	 <td colspan='3'><input type='hidden' name='txtDtCurr' id='txtDtCurr' value='$txtDtCurr'><span class='style13'>$txtDtCurr</span></td>";
	echo" </tr>";
			  
	echo" <tr bgcolor='#F7F7F7' height='23'>";
	echo" 	 <td align='left' ><span class='style12'>Quantity</span></td>";
	echo"  	 <td colspan='3'><span class='style11'><input type='text' name='txtDtQty' value='$txtDtQty' style='text-align:right; font-size:12px; width:100px; height=18px;' onKeyup='CalcDtTotal(1,$DecimalBase);' ></td>";	
	echo" </tr>";
	
	echo" <tr bgcolor='#F7F7F7' height='23'>";
	echo" 	 <td align='left' ><span class='style12'>Price</span></td>";
	echo"  	 <td colspan='3'><span class='style11'><input type='text' name='txtDtPrice' value='$txtDtPrice' style='text-align:right; font-size:12px; width:100px; height=18px;' onKeyup='CalcDtTotal(1,$DecimalBase);' $ModeModifySellPrice></td>";	
	echo" </tr>";
	
	echo" <tr bgcolor='#F7F7F7' height='23'>";
	echo" 	 <td align='left' ><span class='style12'>Discount(%)</span></td>";
	echo"  	 <td colspan='3'><span class='style11'><input type='text' name='txtDtDiscPer' value='$txtDtDiscPer' style='text-align:right; font-size:12px; width:100px; height=18px;' onKeyup='CalcDtTotal(1,$DecimalBase);' $DiscountMode></td>";	
	echo" </tr>";
	 
	echo" <tr bgcolor='#F7F7F7' height='23'>";
	echo" 	 <td align='left' ><span class='style12'>Total</span></td>";
	echo"  	 <td><span class='style11'><input type='text' name='txtDtTotal' value='$txtDtTotal' style='text-align:right; font-size:12px; width:100px; height=18px;' onKeyup='CalcDtTotal(2,$DecimalBase);' $ModeModifySellPrice></span></td>";	
	echo"  	 <td colspan='2'><span class='style11'><input type='hidden' name='txtDtVat' value='$txtDtVat'><input name='chkDtVat' type='checkbox' id='chkDtVat' value='1' onclick='SetVat();' ";	
			if($chkDtVat==1 || $txtDtVat == 1) echo "checked"; echo " /> Vat</span></td>"; 
	echo" </tr>";		
	

	echo" <tr bgcolor='#F7F7F7' height='30'>
		    <td colspan='4' align='center' bgcolor=#D8D8D8>
		    <input type='button' id='btnSubmitRecord' name='btnSubmitRecord'  value='Submit Record'  onclick='SubmitRecord()' $style title='Submit Record'></td>
			 
		  </tr>
        </table>";



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

HERE;

?>