<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Functions_Synchro.php");

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

$deactivatesalesmen 	= $_POST["deactivatesalesmen"]; if ($deactivatesalesmen == ""){$deactivatesalesmen = $_GET["deactivatesalesmen"];} 
$deactivatedate 	= $_POST["deactivatedate"]; if ($deactivatedate == ""){$deactivatedate = $_GET["deactivatedate"];} 


$SalesComm_Id			= $_POST["SalesComm_Id"]; if ($SalesComm_Id == ""){$SalesComm_Id = $_GET["SalesComm_Id"];} 
$telephone_increment	= $_POST["telephone_increment"]; if ($telephone_increment == ""){$telephone_increment = $_GET["telephone_increment"];} 
$SalesCommByClient_Id	= $_POST["SalesCommByClient_Id"]; if ($SalesCommByClient_Id == ""){$SalesCommByClient_Id = $_GET["SalesCommByClient_Id"];} 

$Salesmandt_id			= $_POST["Salesmandt_id"]; if ($Salesmandt_id == ""){$Salesmandt_id = $_GET["Salesmandt_id"];} 
$ReceiptBook_ID			= $_POST["ReceiptBook_ID"]; if ($ReceiptBook_ID == ""){$ReceiptBook_ID = $_GET["ReceiptBook_ID"];} 
$SalesmenGroup_id		= $_POST["SalesmenGroup_id"]; if ($SalesmenGroup_id == ""){$SalesmenGroup_id = $_GET["SalesmenGroup_id"];} 


$ledger_number 		= $_POST["ledger_number"]; if ($ledger_number == ""){$ledger_number = $_GET["ledger_number"];} 
$ledger_name 		= $_POST["ledger_name"]; if ($ledger_name == ""){$ledger_name = $_GET["ledger_name"];} 
$ledger_description = $_POST["ledger_description"]; if ($ledger_description == ""){$ledger_description = $_GET["ledger_description"];} 
$currency 			= $_POST["currency"]; if ($currency == ""){$currency= $_GET["currency"];} 
$currency_code 		= $_POST["currency_code"]; if ($currency_code == ""){$currency_code = $_GET["currency_code"];} 
$credit_limit 		= $_POST["credit_limit"]; if ($credit_limit == ""){$credit_limit = $_GET["credit_limit"];} 
$salesman_bonus 	= $_POST["salesman_bonus"]; if ($salesman_bonus == ""){$salesman_bonus = $_GET["salesman_bonus"];} 
$salesman_commission= $_POST["salesman_commission"]; if ($salesman_commission == ""){$salesman_commission = $_GET["salesman_commission"];} 
$area_code 			= $_POST["area_code"]; if ($area_code == ""){$area_code = $_GET["area_code"];} 
$areaname 			= $_POST["areaname"]; if ($areaname == ""){$areaname = $_GET["areaname"];} 
$manager_code 		= $_POST["manager_code"]; if ($manager_code == ""){$manager_code = $_GET["manager_code"];} 
$manager_name 		= $_POST["manager_name"]; if ($manager_name == ""){$manager_name = $_GET["manager_name"];} 
$salesman_mref 		= $_POST["salesman_mref"]; if ($salesman_mref == ""){$salesman_mref = $_GET["salesman_mref"];} 
$salesman_ismanager	= $_POST["salesman_ismanager"]; if ($salesman_ismanager == ""){$salesman_ismanager = $_GET["salesman_ismanager"];} 
$warehouse_code		= $_POST["warehouse_code"]; if ($warehouse_code == ""){$warehouse_code = $_GET["warehouse_code"];} 
$Address 			= $_POST["Address"]; if ($Address == ""){$Address = $_GET["Address"];} 
$salesman_cashaccount= $_POST["salesman_cashaccount"]; if ($salesman_cashaccount == ""){$salesman_cashaccount = $_GET["salesman_cashaccount"];} 
$salesman_invoicesequence= $_POST["salesman_invoicesequence"]; if ($salesman_invoicesequence == ""){$salesman_invoicesequence = $_GET["salesman_invoicesequence"];} 

$fax 		 = $_POST["fax"]; if ($fax == ""){$fax = $_GET["fax"];} 
$email 		 = $_POST["email"]; if ($email == ""){$email = $_GET["email"];} 
$phone1	  	 = $_POST["phone1"]; if ($phone1 == ""){$phone1 = $_GET["phone1"];} 
$phone2 	 = $_POST["phone2"]; if ($phone2 == ""){$phone2 = $_GET["phone2"];} 

$telephoneno_title 	= $_POST["telephoneno_title"]; if ($telephoneno_title == ""){$telephoneno_title = $_GET["telephoneno_title"];} 
$telephone_name		= $_POST["telephone_name"]; if ($telephone_name == ""){$telephone_name = $_GET["telephone_name"];} 
$telephone_fname 	= $_POST["telephone_fname"]; if ($telephone_fname == ""){$telephone_fname = $_GET["telephone_fname"];} 
$telephone_lname 	= $_POST["telephone_lname"]; if ($telephone_lname == ""){$telephone_lname = $_GET["telephone_lname"];} 
$telephone_no 		= $_POST["telephone_no"]; if ($telephone_no == ""){$telephone_no = $_GET["telephone_no"];} 
$telephone_no2 		= $_POST["telephone_no2"]; if ($telephone_no2 == ""){$telephone_no2 = $_GET["telephone_no2"];} 
$telephone_no3 		= $_POST["telephone_no3"]; if ($telephone_no3 == ""){$telephone_no3 = $_GET["telephone_no3"];} 
$telephone_email	= $_POST["telephone_email"]; if ($telephone_email == ""){$telephone_email = $_GET["telephone_email"];} 
$telephone_reference= $_POST["telephone_reference"]; if ($telephone_reference == ""){$telephone_reference = $_GET["telephone_reference"];} 
$telephone_city1 	= $_POST["telephone_city1"]; if ($telephone_city1 == ""){$telephone_city1 = $_GET["telephone_city1"];} 
$telephone_city2 	= $_POST["telephone_city2"]; if ($telephone_city2 == ""){$telephone_city2 = $_GET["telephone_city2"];} 
$telephone_city3 	= $_POST["telephone_city3"]; if ($telephone_city3 == ""){$telephone_city3 = $_GET["telephone_city3"];} 

$start_date 	 = $_POST["start_date"]; if ($start_date == ""){$start_date = $_GET["start_date"];} 
$end_date 		 = $_POST["end_date"]; if ($end_date == ""){$end_date = $_GET["end_date"];} 
$target_amount	 = $_POST["target_amount"]; if ($target_amount == ""){$target_amount = $_GET["target_amount"];} 
$currencycom	 = $_POST["currencycom"]; if ($currencycom == ""){$currencycom = $_GET["currencycom"];} 
$target_amountcom	 = $_POST["target_amountcom"]; if ($target_amountcom == ""){$target_amountcom = $_GET["target_amountcom"];} 
$commission  	 = $_POST["commission"]; if ($commission == ""){$commission = $_GET["commission"];} 

$client_code 		= $_POST["client_code"]; if ($client_code == ""){$client_code = $_GET["client_code"];} 
$client_name		= $_POST["client_name"]; if ($client_name == ""){$client_name = $_GET["client_name"];} 
$commissionbyclient = $_POST["commissionbyclient"]; if ($commissionbyclient == ""){$commissionbyclient = $_GET["commissionbyclient"];} 

$area_codedt	= $_POST["area_codedt"]; if ($area_codedt == ""){$area_codedt = $_GET["area_codedt"];} 
$area_namedt	= $_POST["area_namedt"]; if ($area_namedt == ""){$area_namedt = $_GET["area_namedt"];} 

$start_daterec 		= $_POST["start_daterec"]; if ($start_daterec == ""){$start_daterec = $_GET["start_daterec"];} 
$end_daterec		= $_POST["end_daterec"]; if ($end_daterec == ""){$end_daterec = $_GET["end_daterec"];} 
$from_reference		= $_POST["from_reference"]; if ($from_reference == ""){$from_reference = $_GET["from_reference"];} 
$to_reference		= $_POST["to_reference"]; if ($to_reference == ""){$to_reference = $_GET["to_reference"];} 
$receipt_serialno	= $_POST["receipt_serialno"]; if ($receipt_serialno == ""){$receipt_serialno = $_GET["receipt_serialno"];} 

$group_codedt 	= $_POST["group_codedt"]; if ($group_codedt == ""){$group_codedt = $_GET["group_codedt"];} 
$group_namedt 	= $_POST["group_namedt"]; if ($group_namedt == ""){$group_namedt = $_GET["group_namedt"];} 	
$creation_date 	= $_POST["creation_date"]; if ($creation_date == ""){$creation_date = $_GET["creation_date"];} 	
$project_code 	= $_POST["project_code"]; if ($project_code == ""){$project_code = $_GET["project_code"];} 	
$costcenter_code 	= $_POST["costcenter_code"]; if ($costcenter_code == ""){$costcenter_code = $_GET["costcenter_code"];} 	
$deactivateclient 	= $_POST["deactivateclient"]; if ($deactivateclient == ""){$deactivateclient = $_GET["deactivateclient"];} 	



	
if (!empty($deactivatedate))	{
	$deactivatedate  = explodeword($deactivatedate);
	$deactivatedate  = "'$deactivatedate'";

}	
else {$deactivatedate= 'null';}


if(empty($warehouse_code))  $warehouse_code='null';
else  $warehouse_code="'$warehouse_code'";

if (!empty($creation_date))	{
	$creation_date  = explodeword($creation_date);
	$creation_date  = "'$creation_date'";

}	
else {$creation_date= 'null';}

if(empty($area_code))
  $area_code= 'null' ;
else
  $area_code="'$area_code'";
  
  
  
  
if(empty($deactivatesalesmen))
	$deactivatesalesmen=0;

	
	
  
  if(empty($currency_code))
 $currency_code= 'null' ;
else
  $currency_code="'$currency_code'";

  
if(empty($salesman_commission))
$salesman_commission= 'null' ;
else
  $salesman_commission="'$salesman_commission'";
  
  
if(empty($salesman_bonus))
	$salesman_bonus= 'null' ;
else
	$salesman_bonus="'$salesman_bonus'";

	
if(empty($deactivateclient))
	$deactivateclient=0;

	

if(empty($credit_limit))
	$credit_limit= 'null' ;
else
  $credit_limit="'$credit_limit'";

		
 if (!empty($start_date))	{
	$start_date  = explodeword($start_date);
	$start_date  = "'$start_date'";

}	
else {$start_date= 'null';}

 if (!empty($end_date))	{
	$end_date  = explodeword($end_date);
	$end_date  = "'$end_date'";

}	
else {$end_date= 'null';}

 if (!empty($start_daterec))	{
	$start_daterec  = explodeword($start_daterec);
	$start_daterec  = "'$start_daterec'";

}	
else {$start_daterec= 'null';}

 if (!empty($end_daterec))	{
	$end_daterec  = explodeword($end_daterec);
	$end_daterec  = "'$end_daterec'";

}	
else {$end_daterec= 'null';}
	
						
$ledger_number = ereg_replace("'", "\'",$ledger_number);
$ledger_name = ereg_replace("'", "\'",$ledger_name);
$ledger_description = ereg_replace("'", "\'",$ledger_description);
$Address = ereg_replace("'", "\'",$Address);
$fax= ereg_replace("'", "\'",$fax);
$email = ereg_replace("'", "\'",$email);
$homepage = ereg_replace("'", "\'",$homepage);
$phone1 = ereg_replace("'", "\'",$phone1);
$phone2 = ereg_replace("'", "\'",$phone2);
$salesman_mref = ereg_replace("'", "\'",$salesman_mref);




$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}




$query_login="select login_belongstogrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

$row_login=mysql_fetch_row($resultlogin);
$belongstogroup=$row_login[0];

$query_user = "select User_Name from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
$result_user = mysql_query($query_user, $connection_history);
$rows_user = mysql_fetch_array($result_user);
extract($rows_user);

//Read From Module Table
$querymodule=" Select ifNull(Module_Synchro,0) As Module_Synchro From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$query_setup=" Select Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

$ledger_numberPrv=trim($ledger_numberPrv);
$ledger_number=trim($ledger_number);


//echo $actionmode; exit;
//my code
//$actionmode=$_GET['actionmode'];
 //my code
	if($actionmode=='getledgergroup'){	
		$ledger_number=$_GET['ledger_number'];
		//echo "here";
	//	exit;
		$query_getlastnum="select max(ledger_number) AS gratest_ledger_number from salesmen where ledger_number like '$ledger_number%' and Salesman_BelongsToGroup=0";
		$result2=mysql_query($query_getlastnum,$connection);
			$row_num=mysql_num_rows($result2);
			//echo $query_getlastnum;
		//	echo $row_num;
			
			while($rowledgerdt2=mysql_fetch_array($result2)){
				extract($rowledgerdt2);
			}
			//echo $row_num;
		//	echo "<br>".$gratest_ledger_number;
			if($gratest_ledger_number==0 || $gratest_ledger_number==''){
				
				$ledgetdtserial.=$ledger_number;
				$ledgetdtserial.="0001";
			//	echo $ledgetdtserial; exit;
			
			header ("Location: ./Form_Salesmen.php?msg=notfound&ledgetdtserial=$ledgetdtserial");
			}
			else{ 
			$ledger_number_plus=$gratest_ledger_number+1; 
			//echo $ledger_number_plus."2ss";exit;
			header ("Location: ./Form_Salesmen.php?msg=hasledgerdt&ledger_number_plus=$ledger_number_plus");
			}
	}
	
	
	
	
if($mainmode=='insert' && $actionmode!='getledgergroup'){
	
		
	$query="Insert into salesmen (Ledger_number, Ledger_name, Ledger_Desc, Salesman_address, Salesman_Fax, Salesman_Email, Salesman_commission, Salesman_CreditLimit, Salesman_Bonus, Area_Code, 
							Currency_Code,Manager_Code, Salesman_IsManager, Salesman_MRef, salesman_CreationDate,Salesman_BelongsToGroup,
							Salesman_Telephone1,Salesman_Telephone2, Salesman_WarehouseCode, Salesman_CashAccount, Salesman_InvoiceSequence, Owner_Branch
							,Salesmen_deactivatedate,Salesmen_deactivate
							)

				values ('".$ledger_number."','".$ledger_name."','".$ledger_description."','".$Address."','".$fax."','".$email."',$salesman_commission,
						$credit_limit,$salesman_bonus,$area_code,$currency_code,'".$manager_code."',$salesman_ismanager,
						'".$salesman_mref."',now(),$belongstogroup,'".$phone1."','".$phone2."', $warehouse_code, '".$salesman_cashaccount."', 
						'".$salesman_invoicesequence."', '".$Setup_Branch."',$creation_date,'".$deactivatesalesmen."')";
						
	//echo$query;exit;
	$result = mysql_query($query, $connection);
	if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
	   $query_history  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$ledger_number', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'salesmen', '$currCompany')";
	   $result_history = mysql_query($query_history, $connection); 
	}
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','SALESMEN','',$ledger_number,$Setup_Branch);
		}
	}

//// Default Currency for Client and Journal

	$query_setup="select Salesman_DefaultCur,Journal_DefaultCur from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_defaultcur=mysql_fetch_row($result_setup);
	
	$salesman_cur=$row_defaultcur[0];
	$journal_cur=$row_defaultcur[1];

		////// Insert into Ledger 	
		$query_ledger="insert into ledger (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Ledger_desc,Ledger_Deactivate, Owner_Branch)
			VALUES ('".$ledger_number."','".$ledger_name."','0',".$currency_code.",$belongstogroup,'3','".$ledger_description."','0', '".$Setup_Branch."')";
											
	
		$result_ledger=mysql_query($query_ledger,$connection);
		
		if($config['log_file'] == 1){
		
		}
		if ($result_ledger) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','LEDGER','',$ledger_number,$Setup_Branch);
			}
		}
		
		$ledger_firstsequence=substr($ledger_number,0,4); 
		$ledger_sequence=substr($ledger_number,4);
			
		$query_acctypesetup="select ledger_number,ledger_activity from acctypesetup ,acctype
						where acctypesetup.type_code=acctype.acctype_code
						and acctype.acctype_ledger=".$ledger_firstsequence;
		
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
	
	
	while($row_ledgers=mysql_fetch_row($result_accsetup)){
		$ledgerpart=$row_ledgers[0];
		$ledger_acc=$ledgerpart.''.$ledger_sequence;
		$ledger_activity=$row_ledgers[1];
	
		///insert into ledger all details 									
		$query_ledgerdetail="insert into ledger (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Ledger_Deactivate,Ledger_desc,ledger_activity, Owner_Branch)
							VALUES ('".$ledger_acc."','".$ledger_name."','0',".$currency_code.",$belongstogroup,'0','0','".$ledger_description."',$ledger_activity, '".$Setup_Branch."')";			

		$result_ledgerdetail=mysql_query($query_ledgerdetail,$connection);
		
			if($config['log_file'] == 1){
		
		$query_ledgerdetail_history = "insert into ledger(ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Ledger_Deactivate,Ledger_desc,ledger_activity, Owner_Branch, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
							VALUES ('".$ledger_acc."','".$ledger_name."','0',".$currency_code.",$belongstogroup,'0','0','".$ledger_description."',$ledger_activity, '".$Setup_Branch."',
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_ledgerdetail_history = mysql_query($query_ledgerdetail_history, $connection_history);
		$message    = mysql_error();
			}
			
		if ($result_ledgerdetail) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','LEDGER','',$ledger_acc,$Setup_Branch);
			}
		}
	}
}

if($mainmode=='edit' && $actionmode!='getledgergroup'){

	$query_update="update  salesmen set Ledger_number='".$ledger_number."',
							Ledger_name='".$ledger_name."',
							Ledger_Desc='".$ledger_description."',
							Salesman_address='".$Address."',
							Salesman_Fax='".$fax."',
							Salesman_Email='".$email."', 
							Salesman_commission=$salesman_commission, 
							Salesman_CreditLimit=$credit_limit, 
							Salesman_Bonus=$salesman_bonus,
							Area_Code=$area_code,
							Currency_Code=$currency_code,
							Manager_Code='".$manager_code."',
							Salesman_IsManager=$salesman_ismanager, 
							Salesman_MRef='".$salesman_mref."',
							salesman_CreationDate=$creation_date,
							Salesman_Telephone1='".$phone1."',
							Salesman_Telephone2='".$phone2."',
							Salesman_WarehouseCode = $warehouse_code,
							Salesman_CashAccount = '".$salesman_cashaccount."',
							Salesman_InvoiceSequence = '".$salesman_invoicesequence."'
							,Salesmen_deactivatedate = $deactivatedate
							,Salesmen_deactivate = $deactivatesalesmen
							
							where ledger_number='".$ledger_numberPrv."'";			
		//echo$query_update;exit;
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	
		if($config['log_file'] == 1){
	
	$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
	   $query_history  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$ledger_number', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'salesmen', '$currCompany')";
	   $result_history = mysql_query($query_history, $connection); 
		}
		
	if ($resultedit) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SALESMEN','',$ledger_numberPrv,$Setup_Branch);
		}
	}


	$query_update="update ledger set Ledger_number='".$ledger_number."',ledger_name='".$ledger_name."',	
				Ledger_desc='".$ledger_description."'
				where ledger_number='".$ledger_numberPrv."'";
				//echo $query_update; exit;
	$result_ledgerupdate=mysql_query($query_update,$connection);
	
		if($config['log_file'] == 1){
	
	$query_update_history = "insert into ledger (Ledger_number, ledger_name, Ledger_desc, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status) values ('".$ledger_number."',
	                         '".$ledger_name."', '".$ledger_description."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
	$result_ledgerupdate_history = mysql_query($query_update_history, $connection_history);
	$message    = mysql_error();
	
	if ($result_ledgerupdate) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','LEDGER','',$ledger_numberPrv,$Setup_Branch);
		}
	}
	
	if($ledger_number==$ledger_numberPrv){
		$ledger_firstsequence=substr($ledger_number,0,4); 
		$ledger_sequence=substr($ledger_number,4);

		$query_acctypesetup="select ledger_number,ledger_activity from acctypesetup ,acctype
							where acctypesetup.type_code=acctype.acctype_code
							and acctype.acctype_ledger=".$ledger_firstsequence;
		$result_accsetup=mysql_query($query_acctypesetup,$connection);

		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
			$ledger_activity=$row_ledgers[1];
			//echo$ledger_acc."<br>";
			$update_ledgerdetail="update ledger set 
								ledger_name='".$ledger_name."',	
								Project_Code=$project_code,
								CostCenter_code=$costcenter_code,
								Ledger_desc='".$ledger_description."',
								ledger_activity=$ledger_activity,
								Currency_Code=$currency_code
								where ledger_number='".$ledger_acc."'";
				//echo$update_ledgerdetail;exit;
			$resultupdate=mysql_query($update_ledgerdetail,$connection);
			
				if($config['log_file'] == 1){
			
			$update_ledgerdetail_history = "insert into ledger(ledger_name, Project_Code, CostCenter_code, Ledger_desc, ledger_activity, Ledger_Date, Ledger_operator, Ledger_schema, 
			                                Ledger_status) values ('".$ledger_name."', $project_code, $costcenter_code, '".$ledger_description."', $ledger_activity, now(), '".$User_Name."', 
											'".$_SESSION['dbName']."', 'U')";
			$resultupdate_history = mysql_query($update_ledgerdetail_history, $connection_history);
			
			$message    = mysql_error();
				}
				
			if ($resultupdate) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'U','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
		}
	}
	/////
		}
//echo "ss";
 
	if($ledger_numberPrv!=$ledger_number && $resultedit && actionmode!='getledgergroup'){
		$query="update telephoneno set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		//echo "ss";
			if($config['log_file'] == 1){
		$query_history = "insert into telephoneno (ledger_number, Telephone_date, Telephone_operator_id, Telephone_schema, Telephone_status) values ('".$ledger_number."',
		                  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
			
		$query="update salescomm set Salesman_Code='".$ledger_number."' where Salesman_Code='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		
		
			if($config['log_file'] == 1){
		$query_history = "insert into salescomm (Salesman_Code, SalesComm_date, SalesComm_operator_id, SalesComm_schema, SalesComm_status) values ('".$ledger_number."', now(), 
		                 '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
			}
			/*$query="insert into salescommbyclient (Salesman_Code, Client_Code,SalesmanByClient_Comm) 
		                  values ('".$ledger_number."', '$client_code',$commissionbyclient )";
						  echo $query;
		$result_query = mysql_query($query, $connection);*/
			
		$query="update salescommbyclient set Salesman_Code='".$ledger_number."' where Salesman_Code='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		
			if($config['log_file'] == 1){
		
		$query_history = "insert into salescommbyclient (Salesman_Code, SalesCommByClient_date, SalesCommByClient_operator_id, SalesCommByClient_schema, SalesCommByClient_status) 
		                  values ('".$ledger_number."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		
			}
			
		$query="update salesmandt set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		
			if($config['log_file'] == 1){
		
		$query_history = "insert into salesmandt (ledger_number,Salesmandt_date, Salesmandt_operator_id,  Salesmandt_schema, Salesmandt_status) 
		values ('".$ledger_number."',
		                  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
			
		$query="update receiptbooks set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		
			if($config['log_file'] == 1){
		
		$query_history = "insert into receiptbooks (ledger_number,ReceiptBook_date, ReceiptBook_operator_id, ReceiptBook_schema, ReceiptBook_status) values ('".$ledger_number."',
		                  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
			
		$ledger_sequence=substr($ledger_number,4);
		$ledger_firstsequence=substr($ledger_number,0,4);
	
		//// Getting ledgers from acctypesetup table
		$query_acctypesetup="select ledger_number,ledger_activity from acctypesetup ,acctype
							where acctypesetup.type_code=acctype.acctype_code
							and acctype.acctype_ledger=".$ledger_firstsequence;
							
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
		$message    = mysql_error();
		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
			$ledger_activity=$row_ledgers[1];
		
			///insert into ledger all details 									
											
			$query_ledgerdetail="insert into ledger (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Project_Code,CostCenter_code,Ledger_Deactivate,Ledger_inactivedate,
							Ledger_desc,ledger_activity)
							VALUES ('".$ledger_acc."','".$ledger_name."',	'0',".$currency_code.",$belongstogroup,'0',$project_code,$costcenter_code,'".$deactivateclient."',
									$deactivatedate,'".$ledger_description."',$ledger_activity)";			
			//	echo $query_ledgerdetail;								
			$result_ledgerdetail=mysql_query($query_ledgerdetail,$connection);
			
				if($config['log_file'] == 1){
			
			$query_ledgerdetail_history = "insert into ledger (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Project_Code,CostCenter_code,Ledger_Deactivate,Ledger_inactivedate,
							Ledger_desc,ledger_activity, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
							VALUES ('".$ledger_acc."','".$ledger_name."',	'0',".$currency_code.",$belongstogroup,'0',$project_code,$costcenter_code,'".$deactivateclient."',
									$deactivatedate,'".$ledger_description."',$ledger_activity, now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
				$result_ledgerdetail=mysql_query($query_ledgerdetail_history,$connection_history);
				$message    = mysql_error();
				}
				
				
			if ($query_ledgerdetail) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'I','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
		}
	}
	//echo "jhk";
}


if($mainmode=='delete' ){
    $query_ledger = "select Ledger_number, Ledger_name, Ledger_desc from ledger where ledger_number='$ledgernumber'";
	$result_ledger = mysql_query($query_ledger, $connection);
	$rows_ledger = mysql_fetch_row($result_ledger);
	$Ledger_number = $rows_ledger[0];
	$Ledger_name = $rows_ledger[1];
	$Ledger_desc = $rows_ledger[2];
	if($config['log_file'] == 1){
			$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
	   $query_history  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$ledger_number', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'salesmen', '$currCompany')";
	  // echo $query_history;exit;
	   $result_history = mysql_query($query_history, $connection); 			
		}
	$deleteledger = "delete from ledger where ledger_number='$ledgernumber'";
	$result=mysql_query($deleteledger);
	$errornum= mysql_errno();
	
		
		
	if($errornum=='1451') {
		$message="Delete Not Allowed . Ledger Number is used .";
				
	}
	else {
		if ($deleteledger) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','LEDGER','',$ledgernumber,$Setup_Branch);
			}
		}
		
		$ledger_firstsequence=substr($ledger_number,0,4); 
		$ledger_sequence=substr($ledger_number,4);

		$query_acctypesetup="select ledger_number from acctypesetup ,acctype
							where acctypesetup.type_code=acctype.acctype_code
							and acctype.acctype_ledger=".$ledger_firstsequence;
							
							
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
		$message    = mysql_error();
		
		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
			 $query_ledger = "select Ledger_number, Ledger_name, Ledger_desc from ledger where ledger_number='".$ledger_acc."'";
	         $result_ledger = mysql_query($query_ledger, $connection);
	         $rows_ledger = mysql_fetch_row($result_ledger);
	         $Ledger_number = $rows_ledger[0];
	         $Ledger_name = $rows_ledger[1];
	         $Ledger_desc = $rows_ledger[2];
			$delete_ledger="delete from ledger where ledger_number='".$ledger_acc."'";				
			//	echo$update_ledgerdetail;
			$resultupdate=mysql_query($delete_ledger,$connection);
			$message    = mysql_error();
			
				if($config['log_file'] == 1){
			$delete_ledger_history = "insert into ledger (Ledger_number, Ledger_name, Ledger_desc, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status) 
			                          values ('".$Ledger_number."', '".$Ledger_name."', '".$Ledger_desc."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
			$resultdelete_history = mysql_query($delete_ledger_history, $connection_history);
			$message    = mysql_error();
				}
				
			if ($delete_ledger) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'D','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
		}

		$query_supplier = "select Ledger_number, Ledger_name, Ledger_Desc, Salesman_address from salesmen where ledger_number='$ledgernumber'";
		$result_supplier = mysql_query($query_supplier, $connection);
		$rows_supplier = mysql_fetch_row($result_supplier);
		$Ledger_number = $rows_supplier[0];
		$Ledger_name = $rows_supplier[1];
		$Ledger_desc = $rows_supplier[2];
		$Salesman_address = $rows_supplier[3];
		$deletesupplier = "delete from salesmen where ledger_number='$ledgernumber'";
		//echo $deletesupplier; exit;
		$result_delete=mysql_query($deletesupplier, $connection);
		$message    = mysql_error();
		
			if($config['log_file'] == 1){
		$deletesupplier_history = "insert into salesmen (Ledger_number, Ledger_name, Ledger_Desc, Salesman_address, 
		salesman_date, salesman_operator_id, salesman_schema, salesman_status) 
		values ('".$Ledger_number."', '".$Ledger_name."', '".$Ledger_desc."', '".$Salesman_address."',
								   now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$result_delete_history = mysql_query($deletesupplier_history, $connection_history);
		$message    = mysql_error();
			}
		if ($deletesupplier) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','SALESMEN','',$ledgernumber,$Setup_Branch);
			}
		}
	}
}

//echo $actionmode; exit;

if($actionmode=='submitcontact' && $actionmode!='getledgergroup' ){
//echo "here"; exit;
	if($telephone_increment==''){
		$query="insert into telephoneno(Ledger_number,TelephoneNo_ContactTitle,Telephone_name,telephoneno_fname,telephoneno_lname,TelephoneNo,TelephoneNo2,TelephoneNo3,telephoneno_email,telephoneno_reference,
										telephoneno_city1,telephoneno_city2,telephoneno_city3)
				values ('".$ledger_number."','".$telephoneno_title."','".$telephone_name."','".$telephone_fname."','".$telephone_lname."','".$telephone_no."','".$telephone_no2."','".$telephone_no3."',
						'".$telephone_email."','".$telephone_reference."','".$telephone_city1."','".$telephone_city2."','".$telephone_city3."')";
		//echo$query;exit;
		$result=mysql_query($query,$connection);
		$message    = mysql_error();
		
		$message    = mysql_error();
			if($config['log_file'] == 1){
		
		$query_history = "insert into telephoneno(Ledger_number,TelephoneNo_ContactTitle,Telephone_name,telephoneno_fname,telephoneno_lname,TelephoneNo,TelephoneNo2,TelephoneNo3,telephoneno_email,telephoneno_reference,
										telephoneno_city1,telephoneno_city2,telephoneno_city3, Telephone_date, Telephone_operator_id, Telephone_schema, Telephone_status)
				values ('".$ledger_number."','".$telephoneno_title."','".$telephone_name."','".$telephone_fname."','".$telephone_lname."','".$telephone_no."','".$telephone_no2."','".$telephone_no3."',
						'".$telephone_email."','".$telephone_reference."','".$telephone_city1."','".$telephone_city2."','".$telephone_city3."',
						now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history)or die(mysql_error($query_history));
		$message    = mysql_error();
		}
	}
	else if($telephone_increment!=''){
		$select ="select Ledger_number from telephoneno where Telephone_increment='".$telephone_increment."'";
		$result=mysql_query($select,$connection);
		$row=mysql_fetch_array($result);
		$Ledger_number=$row[0];
		$message    = mysql_error();
		
		$query="update  telephoneno set TelephoneNo_ContactTitle='".$telephoneno_title."',
										Telephone_name='".$telephone_name."',
										telephoneno_fname='".$telephone_fname."',
										telephoneno_lname='".$telephone_lname."',
										TelephoneNo='".$telephone_no."',
										TelephoneNo2='".$telephone_no2."',
										TelephoneNo3='".$telephone_no3."',
										telephoneno_email='".$telephone_email."',
										telephoneno_reference='".$telephone_reference."',
										telephoneno_city1='".$telephone_city1."',
										telephoneno_city2='".$telephone_city2."',
										telephoneno_city3='".$telephone_city3."'
				where Telephone_increment='".$telephone_increment."'";
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		$message    = mysql_error();
		
		
			if($config['log_file'] == 1){
				
				
				
		$query_history = "insert into telephoneno ( Ledger_number,TelephoneNo_ContactTitle, Telephone_name, telephoneno_fname, telephoneno_lname, TelephoneNo, TelephoneNo2, TelephoneNo3,
		                  telephoneno_email, telephoneno_reference, telephoneno_city1, telephoneno_city2, telephoneno_city3, Telephone_date, Telephone_operator_id, 
						  Telephone_schema, Telephone_status) values ('".$Ledger_number."','".$telephoneno_title."', '".$telephone_name."', '".$telephone_fname."', '".$telephone_lname."', '".$telephone_no."', 
						  '".$telephone_no2."', '".$telephone_no3."', '".$telephone_email."', '".$telephone_reference."', '".$telephone_city1."', '".$telephone_city2."', '".$telephone_city3."', 
						  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
		
			}
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SALESMEN','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}

else if($actionmode=='deletecontact'){
	$query = "select ledger_number, TelephoneNo, Telephone_name, Telephone_posted,Telephone_increment from telephoneno where Telephone_increment='".$telephone_increment."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	
	$ledger_number=$rows[0];
	$TelephoneNo=$rows[1];
	$Telephone_name=$rows[2];
	$Telephone_posted=$rows[3];
	$Telephone_increment=$rows[4];
	
	$query="delete from telephoneno where Telephone_increment='".$telephone_increment."'";
	$result=mysql_query($query,$connection);
	$message    = mysql_error();
	
	$query_history = "insert into telephoneno (ledger_number, TelephoneNo, Telephone_name, Telephone_posted, Telephone_date, Telephone_operator_id, Telephone_schema, Telephone_status) 
	                  values ('".$ledger_number."', '".$TelephoneNo."', '".$Telephone_name."', '".$Telephone_posted."', 
					  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result_history = mysql_query($query_history, $connection_history)or die(mysql_error($query_history));
	$message    = mysql_error();
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SALESMEN','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}

else if($actionmode=='submitcom' && $actionmode!='getledgergroup'){


	if($SalesComm_Id==''){
		$query="insert into salescomm(Salesman_Code,SalesComm_StartDate,SalesComm_EndDate,SalesComm_TargetAmnt,SalesComm_TargetComm,Currency_Code,SalesComm_Commission)
				values ('".$ledger_number."',$start_date,$end_date,'".$target_amount."','".$target_amountcom."','".$currencycom."',
				'".$commission."')";
		//echo $query;exit;
		$result=mysql_query($query,$connection);
		$message    = mysql_error();
		
			if($config['log_file'] == 1){
		$query_history = "insert into salescomm(Salesman_Code,SalesComm_StartDate,SalesComm_EndDate,SalesComm_TargetAmnt,SalesComm_TargetComm,Currency_Code,SalesComm_Commission, SalesComm_date, SalesComm_operator_id, SalesComm_schema, SalesComm_status)
				values ('".$ledger_number."',$start_date,$end_date,'".$target_amount."','".$target_amountcom."','".$currencycom."','".$commission."', 
				now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history)or die(mysql_error($query_history));
		$message    = mysql_error();
			}
	}
	else if($SalesComm_Id!=''){
		$query="update  salescomm set SalesComm_StartDate=$start_date,
										SalesComm_EndDate=$end_date,
										SalesComm_TargetAmnt='".$target_amount."',
										SalesComm_TargetComm='".$target_amountcom."',
										Currency_Code='".$currencycom."',
										SalesComm_Commission='".$commission."'
										
				where SalesComm_Id='".$SalesComm_Id."'";
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		$message    = mysql_error();
		
		
			if($config['log_file'] == 1){
				
				$select_code_history="select Salesman_Code from salescomm where SalesComm_Id='".$SalesComm_Id."'";
				$resultcode_history = mysql_query($select_code_history, $connection);
				
				if($rowcode=mysql_fetch_row($resultcode_history)){$Salesman_Code=$rowcode[0];}
		$query_history = "insert into salescomm ( Salesman_Code,SalesComm_StartDate, SalesComm_EndDate, SalesComm_TargetAmnt, SalesComm_TargetComm, Currency_Code, SalesComm_Commission, SalesComm_date, SalesComm_operator_id, SalesComm_schema, SalesComm_status) 
		                  values ('".$Salesman_Code."',$start_date, $end_date, '".$target_amount."', '".$target_amountcom."', '".$currencycom."', '".$commission."',
						  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		//echo $query_history;exit;
		$message    = mysql_error();
			}
	}
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SALESMEN','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Sales Commission';
}
else if($actionmode=='deletesalescom'){
	
	
	$query_hi = "select Salesman_Code, SalesComm_StartDate, SalesComm_EndDate from salescomm where SalesComm_Id='".$SalesComm_Id."'";
	
	
	$result=mysql_query($query_hi,$connection);
	$rows=mysql_fetch_row($result);
	$Salesman_Code=$rows[0];
	$SalesComm_StartDate=$rows[1];
	$SalesComm_EndDate=$rows[2];
	
	
	$query="delete from salescomm where SalesComm_Id='".$SalesComm_Id."'";
	//echo $query; exit;

		if($config['log_file'] == 1){
	$query_history = "insert into salescomm (Salesman_Code, SalesComm_StartDate, SalesComm_EndDate, SalesComm_date, SalesComm_operator_id, SalesComm_schema, SalesComm_status) 
	                  values ('".$Salesman_Code."', '".$SalesComm_StartDate."', '".$SalesComm_EndDate."', 
					  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result_history = mysql_query($query_history, $connection_history);
	$message    = mysql_error();
		}
		
			$result=mysql_query($query,$connection);
	$message    = mysql_error();
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SALESMEN','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Sales Commission';
}

else if($actionmode=='submitcombyclient' && $actionmode!='getledgergroup'){


	if($SalesCommByClient_Id==''){
		//echo "dfsfs";
		$query="insert into salescommbyclient(Salesman_Code,Client_Code,SalesmanByClient_Comm)
				values ('".$ledger_number."','".$client_code."','".$commissionbyclient."')";
		//echo $query;exit;
		$result=mysql_query($query,$connection) or die(mysql_error($query));
		$message    = mysql_error();
		
			if($config['log_file'] == 1){
				
		$query_history = "insert into salescommbyclient(Salesman_Code,Client_Code,SalesmanByClient_Comm, SalesCommByClient_date, SalesCommByClient_operator_id, SalesCommByClient_schema, SalesCommByClient_status)
				values ('".$ledger_number."','".$client_code."','".$commissionbyclient."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history) or die(mysql_error());
		$message    = mysql_error();
	}
	}
	else if($SalesCommByClient_Id!=''){
		$query_hi = "select Salesman_Code from salescommbyclient where SalesCommByClient_Id='".$SalesCommByClient_Id."'";
	
	
	$result=mysql_query($query_hi,$connection);
	$rows=mysql_fetch_row($result);
	$Salesman_Code=$rows[0];
	
		
		$query="update  salescommbyclient set Client_Code='".$client_code."',
										SalesmanByClient_Comm='".$commissionbyclient."'
				where SalesCommByClient_Id='".$SalesCommByClient_Id."'";
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		
			if($config['log_file'] == 1){
				
				
	
		$query_history = "insert into salescommbyclient( Salesman_Code,Client_Code, SalesmanByClient_Comm,
		SalesCommByClient_date, SalesCommByClient_operator_id, SalesCommByClient_schema, SalesCommByClient_status
		                  ) values ( '".$Salesman_Code."','".$client_code."', '".$commissionbyclient."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
	}
	
	}
	$vTabFocus='Sales Commission By Client';
	
	

}
else if($actionmode=='deletesalescombyclient'){

$query_hi = "select Salesman_Code,Client_Code from salescommbyclientwhere SalesCommByClient_Id='".$SalesCommByClient_Id."'";
	
	
	$result=mysql_query($query_hi,$connection);
	$rows=mysql_fetch_row($result);
	$Salesman_Code=$rows[0];
	$Client_Code=$rows[1];
	
	$query="delete from salescommbyclient where SalesCommByClient_Id='".$SalesCommByClient_Id."'";
	$result=mysql_query($query,$connection)or die (mysql_error($query));
	$message    = mysql_error();
	echo $query;
	//exit;
	
	
	 if($config['log_file'] == 1){
				
				
	
		$query_history = "insert into salescommbyclient( Salesman_Code,Client_Code,
		SalesCommByClient_date, SalesCommByClient_operator_id, SalesCommByClient_schema, SalesCommByClient_status
		                  ) values ( '".$Salesman_Code."','".$Client_Code."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
	
	}
	$vTabFocus='Sales Commission By Client';
}

else if($actionmode=='submitarea'){


	if($Salesmandt_id==''){
		$query="insert into salesmandt(Area_code,Ledger_number)
				values ('".$area_codedt."','".$ledger_number."')";
		//echo$query;exit;
		$result=mysql_query($query,$connection);
		
			if($config['log_file'] == 1){
				
		$query_history = "insert into salesmandt (Area_code,Ledger_number,Salesmandt_date, Salesmandt_operator_id,  Salesmandt_schema, Salesmandt_status) 
		                  values ('".$area_codedt."', '".$ledger_number."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
			
	}
	else if($Salesmandt_id!=''){
		$query="update  salesmandt set Area_code='".$area_codedt."'
				where Salesmandt_id='".$Salesmandt_id."'";
				
		//echo$query;exit;
		$result=mysql_query($query,$connection);
		$message    = mysql_error();
	}
	$vTabFocus='Area';
}

else if($actionmode=='deletesalesarea'){

	$query="delete from salesmandt where Salesmandt_id='".$Salesmandt_id."'";
	$result=mysql_query($query,$connection);
	$message    = mysql_error();
	$vTabFocus='Area';
}

else if($actionmode=='submitreceipt' && $actionmode!='getledgergroup'){


	if($ReceiptBook_ID==''){
		$query="insert into receiptbooks(ReceiptBook_FromDate,ReceiptBook_ToDate,ReceiptBook_FromSequence,ReceiptBook_ToSequence,ReceiptBook_SerialNo,Ledger_number)
				values ($start_daterec,$end_daterec,'".$from_reference."','".$to_reference."','".$receipt_serialno."','".$ledger_number."')";
		//echo$query;exit;
		$result=mysql_query($query,$connection);
		
			if($config['log_file'] == 1){
				
		$query_history = "insert into receiptbooks(ReceiptBook_FromDate,ReceiptBook_ToDate,ReceiptBook_FromSequence,ReceiptBook_ToSequence,ReceiptBook_SerialNo,Ledger_number, ReceiptBook_date, ReceiptBook_operator_id, ReceiptBook_schema, ReceiptBook_status)
		values ($start_daterec,$end_daterec,'".$from_reference."','".$to_reference."','".$receipt_serialno."',
						  '".$ledger_number."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
			
	}
	else if($ReceiptBook_ID!=''){
		$query="update  receiptbooks set ReceiptBook_FromDate=$start_daterec,
										ReceiptBook_ToDate=$end_daterec,
										ReceiptBook_FromSequence='".$from_reference."',
										ReceiptBook_ToSequence='".$to_reference."',
										ReceiptBook_SerialNo='".$receipt_serialno."'
				where ReceiptBook_ID='".$ReceiptBook_ID."'";
				
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		$message    = mysql_error();
		
			if($config['log_file'] == 1){
		$query_history = "insert into receiptbooks(ReceiptBook_ID, ReceiptBook_FromDate, ReceiptBook_ToDate, ReceiptBook_FromSequence, ReceiptBook_ToSequence, ReceiptBook_SerialNo,
		                  ReceiptBook_date, ReceiptBook_operator_id, ReceiptBook_schema, ReceiptBook_status) values ('".$ReceiptBook_ID."', $start_daterec, $end_daterec, '".$from_reference."',
						  '".$to_reference."', '".$receipt_serialno."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
			}
	}
	$vTabFocus='Receipt Books';
}

else if($actionmode=='deletesalesrec'){
$select_query="select ReceiptBook_ID, ReceiptBook_SerialNo, Ledger_Number from receiptbooks";
$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$ReceiptBook_ID=$rows[0];
	$ReceiptBook_SerialNo=$rows[1];
	$Ledger_Number=$rows[2];
	
	$query="delete from receiptbooks where ReceiptBook_ID='".$ReceiptBook_ID."'";
	$result=mysql_query($query,$connection);
	$message    = mysql_error();
	$vTabFocus='Receipt Books';
	if($config['log_file'] == 1){
	$query_history="insert into receiptbooks (ReceiptBook_ID,ReceiptBook_SerialNo,Ledger_Number,
	ReceiptBook_date, ReceiptBook_operator_id, ReceiptBook_schema, ReceiptBook_status) 
	values('".$ReceiptBook_ID."','".$ReceiptBook_SerialNo."','".$Ledger_Number."',
	 now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	 $result=mysql_query($query_history,$connection_history);
	 $message    = mysql_error();
	}
	 
}

else if($actionmode=='submitgroup' && $actionmode!='getledgergroup'){

	if($SalesmenGroup_id==''){
		$query="insert into salesmengroup(group_code,salesman_code)
				values ('".$group_codedt."','".$ledger_number."')";
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		
			if($config['log_file'] == 1){
		$query_history = "insert into salesmengroup(group_code,salesman_code, SalesmenGroup_date, SalesmenGroup_operator_id, SalesmenGroup_schema, SalesmenGroup_status)
				values ('".$group_codedt."','".$ledger_number."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
	}}
	else if($SalesmenGroup_id!=''){
		$query="update  salesmengroup set group_code='".$group_codedt."'
									
				where SalesmenGroup_id='".$SalesmenGroup_id."'";
				
		//echo$query;exit;
		$result=mysql_query($query, $connection);
		$message    = mysql_error();
		
			if($config['log_file'] == 1){
		$query_history = "insert into salesmengroup (SalesmenGroup_id, group_code, SalesmenGroup_date, SalesmenGroup_operator_id, SalesmenGroup_schema, SalesmenGroup_status) 
		                  values ('".$SalesmenGroup_id."', '".$group_codedt."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
	}}
	$vTabFocus='Groups';
}

else if($actionmode=='deletesalesgroup'){
	$select_query="select SalesmenGroup_id,salesman_code  from salesmengroup";
$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$SalesmenGroup_id=$rows[0];
	$salesman_code=$rows[1];
	
	
if($config['log_file'] == 1){
	$query="delete from salesmengroup where SalesmenGroup_id='".$SalesmenGroup_id."'";
	$result=mysql_query($query,$connection);
	$message    = mysql_error();
	
	$query_history="insert into salesmengroup (SalesmenGroup_id,salesman_code,SalesmenGroup_date, SalesmenGroup_operator_id, SalesmenGroup_schema, SalesmenGroup_status)
	values('".$SalesmenGroup_id."','".$salesman_code."',
	now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result=mysql_query($query,$connection_history);
	$message    = mysql_error();
}
	$vTabFocus='Groups';
}


$message = ereg_replace(" ", "%20", $message);


/*
if($actionmode=='deletesalesgroup' || $actionmode=='submitgroup' || $actionmode=='deletesalesrec' || $actionmode=='submitreceipt' || $actionmode=='deletesalesarea' || $actionmode=='submitarea' || $actionmode=='deletesalescombyclient'
|| $actionmode=='submitcombyclient' || $actionmode=='deletesalescom' || $actionmode=='submitcom' || $actionmode=='deletecontact' || $actionmode=='submitcontact')
header ("Location: ./Form_Salesmen.php?ledger_number=$ledger_number&vTabFocus=$vTabFocus&mainmode=editmode");

else
header ("Location: ./Form_Salesmen.php?mainmode=");
*/

if ($mainmode != 'delete' && $actionmode!='getledgergroup') {
	header ("Location: ./Form_Salesmen.php?mainmode=editmode&ledger_number=$ledger_number&vTabFocus=$vTabFocus&message=".$message);
} 
else if($actionmode!='getledgergroup'){
	header ("Location: ./Form_Salesmen.php?mainmode=$mainmode&vTabFocus=$vTabFocus&ledger_number=$ledger_number&message=".$message);
}
	   


mysql_close($connection);



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

HERE;
?>