<?php
session_start();

include("config.inc.php");
include("function.inc.php");
include("Functions_Sales.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");


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

   
print <<<HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Client Detail</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value1,value2){

	vFrm  = document.form1.frm.value;
	vItem = document.form1.item.value;
	
	var vIndex	= document.form1.index.value;

	if (vFrm=='1'){
		opener.document.salesorder.txtClientDtCode.value = value1;
		opener.document.salesorder.txtClientDtName.value = value2;
		opener.document.salesorder.txtClientDtCode.focus();
		//opener.document.salesorder.submit();
	}
	else if (vFrm=='2'){
		opener.document.transdt.txtClientDtCode.value = value1;
		opener.document.transdt.txtClientDtName.value = value2;
		opener.document.transdt.txtClientDtCode.focus();
	}
	else if (vFrm=='3'){
		if (vItem=='1'){
			opener.document.getElementById('clientdtcode['+vIndex+']').value=value1;
			opener.document.getElementById('clientdtname['+vIndex+']').value=value2;
			opener.document.getElementById('clientdtcode['+vIndex+']').focus();
		} else {
			opener.document.getElementById('clientdtcode2['+vIndex+']').value=value1;
			opener.document.getElementById('clientdtname2['+vIndex+']').value=value2;
			opener.document.getElementById('clientdtcode2['+vIndex+']').focus();
		}
	}
	

	self.close();
}

function ClearForm()
{
	document.form1.PDetailCode.value='';
	document.form1.submit();
}

function AddRecord() {
	var x = document.form1;
	
	x.PDetailCode.value='';
	x.PActionMode.value='Add';
	x.submit();
}

function CancelRecord() {
	var x = document.form1;
	
	x.PActionMode.value='';
	
	x.txtCode.value='';
	x.txtDetailID.value='';
	x.txtName.value='';
	x.txtRemark.value='';
	x.txtAddress.value='';
	
	x.submit();
}

function SubmitRecord() {
	var x = document.form1;
	var result = CheckFields();
	
	if (result == 1) {
		x.PActionMode.value='Submit';
		x.submit();
	}
}

function CheckFields(){
  
    var x = document.form1;
	
	var vID   = x.txtDetailID.value;
	var vName = x.txtName.value;
	
	if (vID=='' )
	{
		alert("ID is mandatory!");
		x.txtDetailID.focus();
		return 0;
	}
	else if (vName=='')
	{
		alert("Name is mandatory!");
		x.txtName.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}


</script>
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 13px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {
	color: #999999; 
    font-weight: bold;

}
.style4 {
	color: #3333FF; 
    
}
.style5 {
	color: #999999;
	font-weight: bold;
	font-size: 12px;
}
</style>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input type='hidden' name='PClientCode' id='PClientCode'  value='$PClientCode' >
<input type='hidden' name='PActionMode' id='PActionMode'  value='$PActionMode' >
<input type='hidden' name='item' id='item' value='$item' />
<input type='hidden' name='frm' id='frm' value='$frm' />
<input type='hidden' name='index' value='$index'>
<input type='hidden' name='message' value='$message' size='200'>
			
  <table width="650" border="0" cellspacing="1" align="center">
    <tr>
      <th colspan="2" bgcolor="#DDDDDD"><span class="style1"> CLIENT DETAIL </span></th>
    </tr>
	
HERE;


if ($PActionMode == 'Submit') {
	$ErrorExists = 0;
	
	if ($txtDetailID != '') {
		$sub_query = " Select Count(Detail_ID) From ClientsDetail ".
					 " Where Detail_ID = '".$txtDetailID."'";
		$sub_result = selectData($connection, $sub_query);
		if ($sub_result[0] != 0) $ErrorExists = 1;
	}
	
	if ($ErrorExists == 1) {
		$PActionMode = 'Add';
		$message = 'Insert not allowed. Duplicate ID: '.$txtDetailID;
	} else {
		$sub_query = " Select Client_SeqCode From Clients ".
					 " Where Ledger_Number = '".$PClientCode."'";
		$sub_result = selectData($connection, $sub_query);
		if ($sub_result[0] == '' || empty($sub_result[0])) {
			$ErrorExists = 1;
			$PActionMode = 'Add';
			$message = 'Insert not allowed. Please check Clients data, Client Sequential Code is empty. ';
		}	
	}
	
		
	if ($PClientCode == ''){
		$PActionMode = 'Add';
		$ErrorExists = 1;
		$message = 'Insert not allowed. Invalid client code. ';
	}	
	
	if ($ErrorExists == 0){
		$sub_query = " Select Client_SeqCode From Clients ".
					 " Where Ledger_Number = '".$PClientCode."'";
		$sub_result = selectData($connection, $sub_query);
		$vCltCode   = $sub_result[0];
		
		$sub_query = " Select Lpad(ifNull(Max(Substr(Detail_Code,length(Client_SeqCode) + 1)),0) + 1, 5, '0') ".
					 " From Clients, ClientsDetail ".
					 " Where ClientsDetail.Ledger_Number = Clients.Ledger_Number ".
					 " And Clients.Ledger_Number = '".$PClientCode."' ";
		$sub_result = selectData($connection, $sub_query);
		$txtCode    = $sub_result[0];
		
		$txtCode = $vCltCode.$txtCode;
		
		fbegin();
		$query = " INSERT INTO clientsdetail (Detail_Code, Detail_ID, Detail_Name, Ledger_Number, Detail_Remark, Detail_Address ) ".
				 " VALUES ( '".$txtCode."', '".$txtDetailID."', '".ereg_replace("'", "\'", trim($txtName))."', '".$PClientCode."', ".
				 "          '".ereg_replace("'", "\'", trim($txtRemark))."', '".ereg_replace("'", "\'", trim($txtAddress))."'  )";
					 
			 
		$result=mysql_query($query,$connection);
		//echo $query."<br>";exit;	
		
		if ($result){
			$PActionMode = '';
			$PDetailCode = $txtCode;
			fcommit();
			
			header ("Location: ./List_ClientDetail.php?frm=1&item=".$item."&PDetailCode=".$PDetailCode."&PClientCode=".$PClientCode."&message=".$message);	
		}else{
			$PActionMode = 'Add';
			$message  = mysql_error(); 
			frollback();
		}
	
	}
	
}

if ($PActionMode == 'Add') {
	echo"<tr><td colspan='2'>";
	echo"  <table width='650' border='0' cellspacing='1' align='left'>";
	echo"	<tr bgcolor='#F7F7F7'>
			  <td height='20' colspan='2' align='center'><font size='1' color='#FF0000' face='Verdana'><b>$message</b></font></td>
			</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>ID</span></td>";
	echo"	  <td width='500'><input type='hidden' name='txtCode' id='txtCode' value='$txtCode' />
	            <input type='text' name='txtDetailID' id='txtDetailID' value='$txtDetailID' style='width:126px;' maxlength='30' />
	          </td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>Name</span></td>";
	echo"	  <td width='500'><input type='text' name='txtName' id='txtName' value='".htmlspecialchars($txtName, ENT_QUOTES)."'  style='width:350px;' maxlength='100' /></td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>Remark</span></td>";
	echo"	  <td width='500'><input type='text' name='txtRemark' id='txtRemark' value='".htmlspecialchars($txtRemark, ENT_QUOTES)."' style='width:350px;' maxlength='100' /></td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>Address</span></td>";
	echo"	  <td width='500'><input type='text' name='txtAddress' id='txtAddress' value='".htmlspecialchars($txtAddress, ENT_QUOTES)."' style='width:350px;' maxlength='100' /></td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#DDDDDD'>";
	echo"	  <td colspan='2' align='center'>
				<input type='button' name='btnSubmit' value='Submit' onclick='SubmitRecord();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
				<input type='button' name='btnClear' value='Cancel' onclick='CancelRecord();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	echo"	  </td>";
	echo"	</tr>";
	echo"  </table>";
	echo"</td></tr>";

}

if ($PActionMode == '') {
	echo"<tr>
		  <td bgcolor='#DDDDDD'><span class='style3'>Search by Code / Name</span></td>
		  <td bgcolor='#DDDDDD'><input type='text' name='PDetailCode' value='$PDetailCode'  size='30' />
			<input type='submit' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='ClearForm();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnAdd' value='Add' onclick='AddRecord();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		  </td>
		</tr>";
}


echo"<tr bgcolor='#E5E5E5'>
      <td width='150'><span class='style2'>Code </span></td>
      <td width='380'><span class='style2'>Name </span></td>
    </tr>";



$sub_query= " select Detail_Code, Detail_Name ".
			" from clientsdetail  ".
			" where ((Detail_Code like ".GetSQLValueString($PDetailCode.'%','text').") or (Detail_Name like ".GetSQLValueString('%'.$PDetailCode.'%','text')."))  ".
			" and Ledger_Number = '".$PClientCode."' ".
			" order by Detail_Code ";
		

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_rows=mysql_fetch_array($sub_result)){
	
	extract($sub_rows);

	$ct+=1;

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

    $desc=ereg_replace("'","%27",$sub_rows[1]);

	echo"<tr $Color>";
	echo"  <td align='left'><a onclick=\"SelectRecord('$sub_rows[0]','$desc');\" href=''><span class='style4'>$sub_rows[0]</span></a></td>";
	echo"  <td align='left'><span class='style4'>$sub_rows[1]</span></td>";
    echo"</tr>";
	
}	

function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

print <<<HERE
  </table>
</form>
</body>
</html>

HERE;
?>