
<?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");

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

if($config['log_file'] == 1){
$connection_history = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($config['dbName_history'],$connection_history) or die("Could not find DB");
}

$query_login="select login_belongstogrp,ifnull(FL_Logistic,0) 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());

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

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

$query_path="select setup_GCIExp,ClientFirstDigit,NbrOfDigitInLedger, Setup_Branch from setup";
$resultpath=mysql_query($query_path,$connection) or die(mysql_error());
$row_path=mysql_fetch_array($resultpath);

$ourFileName=$row_path[0];
$ourFileName=$ourFileName."dgcidata";
$ClientFirstDigit=$row_path[1];
$NbrOfDigitInLedger=$row_path[2];

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


$CompInfo = explode('_', $_SESSION['dbName']);
$default_company = $CompInfo[0];
$default_year = $CompInfo[1];
	
$HideMenu   = $_POST["HideMenu"]; if ($HideMenu == ""){$HideMenu = $_GET["HideMenu"];}   
$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"];} 


$clientact_id 		= $_POST["clientact_id"]; if ($clientact_id == ""){$clientact_id = $_GET["clientact_id"];} 
$edittelephone    	= $_POST["edittelephone"]; if ($edittelephone == ""){$edittelephone = $_GET["edittelephone"];} 
$editcompetitor 	= $_POST["editcompetitor"]; if ($editcompetitor == ""){$editcompetitor = $_GET["editcompetitor"];} 
$telephone_increment = $_POST["telephone_increment"]; if ($telephone_increment == ""){$telephone_increment = $_GET["telephone_increment"];} 
$competitor_id 	   	= $_POST["competitor_id"]; if ($competitor_id == ""){$competitor_id = $_GET["competitor_id"];} 
$checkcode    		= $_POST["checkcode"]; if ($checkcode == ""){$checkcode = $_GET["checkcode"];} 
$fromledger 	   	= $_POST["fromledger"]; if ($fromledger == ""){$fromledger = $_GET["fromledger"];} 
$fill_ledger 		= $_POST["fill_ledger"]; if ($fill_ledger == ""){$fill_ledger = $_GET["fill_ledger"];} 
$title_name 	   	= $_POST["title_name"]; if ($title_name == ""){$title_name = $_GET["title_name"];} 
$title_owner		= $_POST["title_owner"]; if ($title_owner == ""){$title_owner = $_GET["title_owner"];} 
$title_refered 	   	= $_POST["title_refered"]; if ($title_refered == ""){$title_refered = $_GET["title_refered"];} 
$title_assistant   	= $_POST["title_assistant"]; if ($title_assistant == ""){$title_assistant = $_GET["title_assistant"];} 
$addresstab 	   	= $_POST["addresstab"]; if ($addresstab == ""){$addresstab = $_GET["addresstab"];} 
$check_ledgergrp   	= $_POST["check_ledgergrp"]; if ($check_ledgergrp == ""){$check_ledgergrp = $_GET["check_ledgergrp"];} 
$ledger_numberPrv  	= $_POST["ledger_numberPrv"]; if ($ledger_numberPrv == ""){$ledger_numberPrv = $_GET["ledger_numberPrv"];} 
$Clientip_id 	   	= $_POST["Clientip_id"]; if ($Clientip_id == ""){$Clientip_id = $_GET["Clientip_id"];} 


$creation_date 		= $_POST["creation_date"]; if ($creation_date == ""){$creation_date = $_GET["creation_date"];} 
$deactivateclient 	= $_POST["deactivateclient"]; if ($deactivateclient == ""){$deactivateclient = $_GET["deactivateclient"];} 
$deactivatedate 	= $_POST["deactivatedate"]; if ($deactivatedate == ""){$deactivatedate = $_GET["deactivatedate"];} 
$client_tc 			= $_POST["client_tc"]; if ($client_tc == ""){$client_tc = $_GET["client_tc"];} 
$client_groupcode	= $_POST["client_groupcode"]; if ($client_groupcode == ""){$client_groupcode = $_GET["client_groupcode"];} 
$client_groupname	= $_POST["client_groupname"]; if ($client_groupname == ""){$client_groupname = $_GET["client_groupname"];} 
$client_tax0 		= $_POST["client_tax0"]; if ($client_tax0 == ""){$client_tax0 = $_GET["client_tax0"];} 
$client_tax1 		= $_POST["client_tax1"]; if ($client_tax1 == ""){$client_tax1 = $_GET["client_tax1"];} 
$client_tax2 		= $_POST["client_tax2"]; if ($client_tax2 == ""){$client_tax2 = $_GET["client_tax2"];} 
$client_tax3 		= $_POST["client_tax3"]; if ($client_tax3 == ""){$client_tax3 = $_GET["client_tax3"];} 
$client_tax4 		= $_POST["client_tax4"]; if ($client_tax4 == ""){$client_tax4 = $_GET["client_tax4"];} 
$client_tax5 		= $_POST["client_tax5"]; if ($client_tax5 == ""){$client_tax5 = $_GET["client_tax5"];} 
$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_namef		= $_POST["ledger_namef"]; if ($ledger_namef == ""){$ledger_namef = $_GET["ledger_namef"];} 
$ledger_description = $_POST["ledger_description"]; if ($ledger_description == ""){$ledger_description = $_GET["ledger_description"];} 
$salesman_code 		= $_POST["salesman_code"]; if ($salesman_code == ""){$salesman_code = $_GET["salesman_code"];} 
$salesman_name 		= $_POST["salesman_name"]; if ($salesman_name == ""){$salesman_name = $_GET["salesman_name"];} 
$client_export 		= $_POST["client_export"]; if ($client_export == ""){$client_export = $_GET["client_export"];} 
$client_local 		= $_POST["client_local"]; if ($client_local == ""){$client_local = $_GET["client_local"];} 
$client_frequencyofvisit = $_POST["client_frequencyofvisit"]; if ($client_frequencyofvisit == ""){$client_frequencyofvisit = $_GET["client_frequencyofvisit"];} 
$salesman_com 		= $_POST["salesman_com"]; if ($salesman_com == ""){$salesman_com = $_GET["salesman_com"];} 
$sent_email 		= $_POST["sent_email"]; if ($sent_email == ""){$sent_email = $_GET["sent_email"];} 
$client_cashmode 	= $_POST["client_cashmode"]; if ($client_cashmode == ""){$client_cashmode = $_GET["client_cashmode"];} 
$selling_price 		= $_POST["selling_price"]; if ($selling_price == ""){$selling_price = $_GET["selling_price"];} 
$client_sellingprice= $_POST["client_sellingprice"]; if ($client_sellingprice == ""){$client_sellingprice = $_GET["client_sellingprice"];} 
$discount 			= $_POST["discount"]; if ($discount == ""){$discount = $_GET["discount"];} 
$credit_control 	= $_POST["credit_control"]; if ($credit_control == ""){$credit_control = $_GET["credit_control"];} 
$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"];} 
$budget_limit 		= $_POST["budget_limit"]; if ($budget_limit == ""){$budget_limit = $_GET["budget_limit"];} 
$client_group 		= $_POST["client_group"]; if ($client_group == ""){$client_group = $_GET["client_group"];} 
$client_rating 		= $_POST["client_rating"]; if ($client_rating == ""){$client_rating = $_GET["client_rating"];} 
$client_vat 		= $_POST["client_vat"]; if ($client_vat == ""){$client_vat = $_GET["client_vat"];} 
$client_vatno 		= $_POST["client_vatno"]; if ($client_vatno == ""){$client_vatno = $_GET["client_vatno"];} 
$vat_date 			= $_POST["vat_date"]; if ($vat_date == ""){$vat_date = $_GET["vat_date"];} 
$legaltype 			= $_POST["legaltype"]; if ($legaltype == ""){$legaltype = $_GET["legaltype"];} 
$client_legaltype	= $_POST["client_legaltype"]; if ($client_legaltype == ""){$client_legaltype = $_GET["client_legaltype"];} 
$client_checkaccount= $_POST["client_checkaccount"]; if ($client_checkaccount == ""){$client_checkaccount = $_GET["client_checkaccount"];} 
$client_chterms 	= $_POST["client_chterms"]; if ($client_chterms == ""){$client_chterms = $_GET["client_chterms"];} 
$project_code 		= $_POST["project_code"]; if ($project_code == ""){$project_code = $_GET["project_code"];} 
$project_name 		= $_POST["project_name"]; if ($project_name == ""){$project_name = $_GET["project_name"];} 
$paymenttype		= $_POST["paymenttype"]; if ($paymenttype == ""){$paymenttype = $_GET["paymenttype"];} 
$client_paytermtype	= $_POST["client_paytermtype"]; if ($client_paytermtype == ""){$client_paytermtype = $_GET["client_paytermtype"];} 
$client_paymentterm = $_POST["client_paymentterm"]; if ($client_paymentterm == ""){$client_paymentterm = $_GET["client_paymentterm"];} 
$client_paymentterm2 = $_POST["client_paymentterm2"]; if ($client_paymentterm2 == ""){$client_paymentterm2 = $_GET["client_paymentterm2"];} 
$client_paymentmode = $_POST["client_paymentmode"]; if ($client_paymentmode == ""){$client_paymentmode = $_GET["client_paymentmode"];} 
$costcenter_code 	= $_POST["costcenter_code"]; if ($costcenter_code == ""){$costcenter_code = $_GET["costcenter_code"];} 
$costcenter_name	= $_POST["costcenter_name"]; if ($costcenter_name == ""){$costcenter_name = $_GET["costcenter_name"];} 
$profit_code		= $_POST["profit_code"]; if ($profit_code == ""){$profit_code = $_GET["profit_code"];} 
$client_financialno	= $_POST["client_financialno"]; if ($client_financialno == ""){$client_financialno = $_GET["client_financialno"];} 


$country 	 = $_POST["country"]; if ($country == ""){$country = $_GET["country"];} 
$state 		 = $_POST["state"]; if ($state == ""){$state = $_GET["state"];} 
$city_client = $_POST["city_client"]; if ($city_client == ""){$city_client = $_GET["city_client"];} 
$area_code	 = $_POST["area_code"]; if ($area_code == ""){$area_code = $_GET["area_code"];} 
$areaname 	 = $_POST["areaname"]; if ($areaname == ""){$areaname = $_GET["areaname"];} 
$Address 	 = $_POST["Address"]; if ($Address == ""){$Address = $_GET["Address"];} 
$Address2  	 = $_POST["Address2"]; if ($Address2 == ""){$Address2 = $_GET["Address2"];} 
$Address3 	 = $_POST["Address3"]; if ($Address3 == ""){$Address3 = $_GET["Address3"];} 
$email 		 = $_POST["email"]; if ($email == ""){$email = $_GET["email"];} 
$homepage 	 = $_POST["homepage"]; if ($homepage == ""){$homepage = $_GET["homepage"];} 
$phone1	  	 = $_POST["phone1"]; if ($phone1 == ""){$phone1 = $_GET["phone1"];} 
$phone2 	 = $_POST["phone2"]; if ($phone2 == ""){$phone2 = $_GET["phone2"];} 
$phone3 	 = $_POST["phone3"]; if ($phone3 == ""){$phone3 = $_GET["phone3"];} 
$pobox 		 = $_POST["pobox"]; if ($pobox == ""){$pobox = $_GET["pobox"];} 
$fax 		 = $_POST["fax"]; if ($fax == ""){$fax = $_GET["fax"];} 
$technical_person 	= $_POST["technical_person"]; if ($technical_person == ""){$technical_person = $_GET["technical_person"];} 
$technical_email 	= $_POST["technical_email"]; if ($technical_email == ""){$technical_email = $_GET["technical_email"];} 

$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"];} 

$clientcontact_amount 	= $_POST["clientcontact_amount"]; if ($clientcontact_amount == ""){$clientcontact_amount = $_GET["clientcontact_amount"];} 
$txtProjectCode 	= $_POST["txtProjectCode"]; if ($txtProjectCode == ""){$txtProjectCode = $_GET["txtProjectCode"];} 
$txtcontractdate 	= $_POST["txtcontractdate"]; if ($txtcontractdate == ""){$txtcontractdate = $_GET["txtcontractdate"];} 
$contractterm	 	= $_POST["contractterm"]; if ($contractterm == ""){$contractterm = $_GET["contractterm"];} 



$agent_code 	= $_POST["agent_code"]; if ($agent_code == ""){$agent_code = $_GET["agent_code"];} 
$agent_name 	= $_POST["agent_name"]; if ($agent_name == ""){$agent_name = $_GET["agent_name"];} 
$client_mothercompany 		 = $_POST["client_mothercompany"]; if ($client_mothercompany == ""){$client_mothercompany = $_GET["client_mothercompany"];} 
$client_companyname 		 = $_POST["client_companyname"]; if ($client_companyname == ""){$client_companyname = $_GET["client_companyname"];} 
$bussiness_type 			 = $_POST["bussiness_type"]; if ($bussiness_type == ""){$bussiness_type = $_GET["bussiness_type"];} 
$titleowner_description		 = $_POST["titleowner_description"]; if ($titleowner_description == ""){$titleowner_description = $_GET["titleowner_description"];} 
$client_companyowner 		 = $_POST["client_companyowner"]; if ($client_companyowner == ""){$client_companyowner = $_GET["client_companyowner"];} 
$titlerefered_description	 = $_POST["titlerefered_description"]; if ($titlerefered_description == ""){$titlerefered_description = $_GET["titlerefered_description"];} 
$client_referedby  			 = $_POST["client_referedby"]; if ($client_referedby == ""){$client_referedby = $_GET["client_referedby"];} 


$client_sdino = $_POST["client_sdino"]; if ($client_sdino == ""){$client_sdino = $_GET["client_sdino"];} 
$titleassistant_description	 = $_POST["titleassistant_description"]; if ($titleassistant_description == ""){$titleassistant_description = $_GET["titleassistant_description"];} 
$client_deliveryassistant	 = $_POST["client_deliveryassistant"]; if ($client_deliveryassistant == ""){$client_deliveryassistant = $_GET["client_deliveryassistant"];} 
$client_dlvworktitle 		 = $_POST["client_dlvworktitle"]; if ($client_dlvworktitle == ""){$client_dlvworktitle = $_GET["client_dlvworktitle"];} 
$delivery_phone 		 	 = $_POST["delivery_phone"]; if ($delivery_phone == ""){$delivery_phone = $_GET["delivery_phone"];} 
$delivery_areacode			 = $_POST["delivery_areacode"]; if ($delivery_areacode == ""){$delivery_areacode = $_GET["delivery_areacode"];} 
$delivery_areaname 			 = $_POST["delivery_areaname"]; if ($delivery_areaname == ""){$delivery_areaname = $_GET["delivery_areaname"];} 
$warehouse 					 = $_POST["warehouse"]; if ($warehouse == ""){$warehouse = $_GET["warehouse"];} 
$client_deliveryaddress 	 = $_POST["client_deliveryaddress"]; if ($client_deliveryaddress == ""){$client_deliveryaddress = $_GET["client_deliveryaddress"];} 
$client_deliveryaddress2 	 = $_POST["client_deliveryaddress2"]; if ($client_deliveryaddress2 == ""){$client_deliveryaddress2 = $_GET["client_deliveryaddress2"];} 
$client_deliveryaddress3 	 = $_POST["client_deliveryaddress3"]; if ($client_deliveryaddress3 == ""){$client_deliveryaddress3 = $_GET["client_deliveryaddress3"];} 

$client_gcino 	 	= $_POST["client_gcino"]; if ($client_gcino == ""){$client_gcino = $_GET["client_gcino"];} 
$client_archivefileno 		= $_POST["client_archivefileno"]; if ($client_archivefileno == ""){$client_archivefileno = $_GET["client_archivefileno"];} 
$client_evaluationpoints 	= $_POST["client_evaluationpoints"]; if ($client_evaluationpoints == ""){$client_evaluationpoints = $_GET["client_evaluationpoints"];} 
$client_weakness 	= $_POST["client_weakness"]; if ($client_weakness == ""){$client_weakness = $_GET["client_weakness"];} 
$client_tips 	 	= $_POST["client_tips"]; if ($client_tips == ""){$client_tips = $_GET["client_tips"];} 
$client_remark 	 	= $_POST["client_remark"]; if ($client_remark == ""){$client_remark = $_GET["client_remark"];} 
$client_invrounded  = $_POST["client_invrounded"]; if ($client_invrounded == ""){$client_invrounded = $_GET["client_invrounded"];} 
$invoice_language 	= $_POST["invoice_language"]; if ($invoice_language == ""){$invoice_language = $_GET["invoice_language"];} 
$delivsof 	 		= $_POST["delivsof"]; if ($delivsof == ""){$delivsof = $_GET["delivsof"];} 

$competitor_name 	 = $_POST["competitor_name"]; if ($competitor_name == ""){$competitor_name = $_GET["competitor_name"];} 
$competitor_product  = $_POST["competitor_product"]; if ($competitor_product == ""){$competitor_product = $_GET["competitor_product"];} 
$competitor_ratio 	 = $_POST["competitor_ratio"]; if ($competitor_ratio == ""){$competitor_ratio = $_GET["competitor_ratio"];} 
$competitor_salesman = $_POST["competitor_salesman"]; if ($competitor_salesman == ""){$competitor_salesman = $_GET["competitor_salesman"];} 

$clientact_startdate = $_POST["clientact_startdate"]; if ($clientact_startdate == ""){$clientact_startdate = $_GET["clientact_startdate"];} 
$clientact_enddate 	 = $_POST["clientact_enddate"]; if ($clientact_enddate == ""){$clientact_enddate = $_GET["clientact_enddate"];} 
$clientact_remark 	 = $_POST["clientact_remark"]; if ($clientact_remark == ""){$clientact_remark = $_GET["clientact_remark"];} 

$client_businesssector		 = $_POST["client_businesssector"]; if ($client_businesssector == ""){$client_businesssector = $_GET["client_businesssector"];} 
$client_businesssectordesc 	 = $_POST["client_businesssectordesc"]; if ($client_businesssectordesc == ""){$client_businesssectordesc = $_GET["client_businesssectordesc"];} 
$client_tobcode 	  = $_POST["client_tobcode"]; if ($client_tobcode == ""){$client_tobcode = $_GET["client_tobcode"];} 
$client_multiplesales = $_POST["client_multiplesales"]; if ($client_multiplesales == ""){$client_multiplesales = $_GET["client_multiplesales"];} 
$client_btype2 	 	  = $_POST["client_btype2"]; if ($client_btype2 == ""){$client_btype2 = $_GET["client_btype2"];} 
$client_btype3 	 	  = $_POST["client_btype3"]; if ($client_btype3 == ""){$client_btype3 = $_GET["client_btype3"];} 
$Client_BusinessType  = $_POST["Client_BusinessType"]; if ($Client_BusinessType == ""){$Client_BusinessType = $_GET["Client_BusinessType"];} 

$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"];} 
$ClientIP_LastTransDate	 = $_POST["ClientIP_LastTransDate"]; if ($ClientIP_LastTransDate == ""){$ClientIP_LastTransDate = $_GET["ClientIP_LastTransDate"];} 
$ClientIP_PeriodByMonth	 = $_POST["ClientIP_PeriodByMonth"]; if ($ClientIP_PeriodByMonth == ""){$ClientIP_PeriodByMonth = $_GET["ClientIP_PeriodByMonth"];} 
$Client_Inst_Month 	= $_POST["Client_Inst_Month"]; if ($Client_Inst_Month == ""){$Client_Inst_Month = $_GET["Client_Inst_Month"];} 
$technician_name 	= $_POST["technician_name"]; if ($technician_name == ""){$technician_name = $_GET["technician_name"];} 
$subscriber_name 	= $_POST["subscriber_name"]; if ($subscriber_name == ""){$subscriber_name = $_GET["subscriber_name"];} 
$Clientip_IP 		= $_POST["Clientip_IP"]; if ($Clientip_IP == ""){$Clientip_IP = $_GET["Clientip_IP"];} 
$clientip_tc 	 	= $_POST["clientip_tc"]; if ($clientip_tc == ""){$clientip_tc = $_GET["clientip_tc"];} 
$category_name 	 	= $_POST["category_name"]; if ($category_name == ""){$category_name = $_GET["category_name"];} 
$service_name 	 	= $_POST["service_name"]; if ($service_name == ""){$service_name = $_GET["service_name"];} 
$clientiparea_code 	= $_POST["clientiparea_code"]; if ($clientiparea_code == ""){$clientiparea_code = $_GET["clientiparea_code"];} 
$clientip_amount 	= $_POST["clientip_amount"]; if ($clientip_amount == ""){$clientip_amount = $_GET["clientip_amount"];} 
$currencyip 	 	= $_POST["currencyip"]; if ($currencyip == ""){$currencyip = $_GET["currencyip"];} 
$upload_ip 	 		= $_POST["upload_ip"]; if ($upload_ip == ""){$upload_ip = $_GET["upload_ip"];} 
$download_ip 	 	= $_POST["download_ip"]; if ($download_ip == ""){$download_ip = $_GET["download_ip"];} 
$client_macaddress 	= $_POST["client_macaddress"]; if ($client_macaddress == ""){$client_macaddress = $_GET["client_macaddress"];} 
$client_port 	 	= $_POST["client_port"]; if ($client_port == ""){$client_port = $_GET["client_port"];} 
$client_modemip 	= $_POST["client_modemip"]; if ($client_modemip == ""){$client_modemip = $_GET["client_modemip"];} 
$clientipproject_code 	 = $_POST["clientipproject_code"]; if ($clientipproject_code == ""){$clientipproject_code = $_GET["clientipproject_code"];} 
$clientipcostcent_code	 = $_POST["clientipcostcent_code"]; if ($clientipcostcent_code == ""){$clientipcostcent_code = $_GET["clientipcostcent_code"];} 
$clientip_reason 	= $_POST["clientip_reason"]; if ($clientip_reason == ""){$clientip_reason = $_GET["clientip_reason"];} 
$collection_name 	= $_POST["collection_name"]; if ($collection_name == ""){$collection_name = $_GET["collection_name"];} 
$Subscriber_Address = $_POST["Subscriber_Address"]; if ($Subscriber_Address == ""){$Subscriber_Address = $_GET["Subscriber_Address"];} 

$client_showcreditlimit = $_POST["client_showcreditlimit"]; if ($client_showcreditlimit == ""){$client_showcreditlimit = $_GET["client_showcreditlimit"];} 
$client_showmaturitydate = $_POST["client_showmaturitydate"]; if ($client_showmaturitydate == ""){$client_showmaturitydate = $_GET["client_showmaturitydate"];} 
$client_discountonInvoice=$_POST["client_discountonInvoice"]; if ($client_discountonInvoice == ""){$client_discountonInvoice = $_GET["client_discountonInvoice"];} 

$whtpercentage	= $_POST["whtpercentage"]; if ($whtpercentage == ""){$whtpercentage = $_GET["whtpercentage"];} 
$client_wht		= $_POST["client_wht"]; if ($client_wht == ""){$client_wht = $_GET["client_wht"];} 

if($whtpercentage == '') $whtpercentage = "NULL";
if($client_wht == '') $client_wht = 0;
//echo $mainmode; exit;

if($actionmode=='getledgergroup'){	
		$ledger_number=$_GET['ledger_number'];
		$check_ledgergrp=$_GET['check_ledgergrp'];
		//echo "here";exit;
		$query_getlastnum="select max(ledger_number) AS gratest_ledger_number from clients where ledger_number like '$ledger_number%' and Client_BelongsToGroup=0";
		$result2=mysql_query($query_getlastnum);
			//$row_num=mysql_num_rows($result2);
		//echo $query_getlastnum;exit;
			//echo $row_num;
			while($rowledgerdt2=mysql_fetch_array($result2)){
				extract($rowledgerdt2);
			}
			if($gratest_ledger_number==0 || $gratest_ledger_number==''){
				
				$ledgetdtserial.=$ledger_number;
				$ledgetdtserial.="0001";
				//echo $ledgetdtserial;
			
			header ("Location: ./Form_Clients.php?msg=notfound&ledgetdtserial=$ledgetdtserial&check_ledgergrp=$check_ledgergrp&check_ledgergrp=$check_ledgergrp&mainmode=$mainmode");
			}
			else{
			$ledger_number_plus=$gratest_ledger_number+1; 
			//echo $ledger_number_plus;exit;
			header ("Location: ./Form_Clients.php?msg=hasledger&ledger_number_plus=$ledger_number_plus&check_ledgergrp=$check_ledgergrp&mainmode=$mainmode");exit;
			}
	}

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

}	
else {$creation_date= "NULL";}




if (!empty($clientact_startdate))	{
	$clientact_startdate  = explodeword($clientact_startdate);
	$clientact_startdate  = "$clientact_startdate";

}	
else {$clientact_startdate= "NULL";}


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

}	
else {$deactivatedate= "NULL";}


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

}	
else {$vat_date= "NULL";}


if (!empty($clientact_enddate))	{
	$clientact_enddate  = explodeword($clientact_enddate);
	$clientact_enddate  = "$clientact_enddate";

}	
else {$clientact_enddate= "NULL";}

 if(empty($delivery_areacode))
	$delivery_areacode= "NULL" ;
else
	$delivery_areacode="'$delivery_areacode'";
			
if(empty($competitor_salesman))
	$competitor_salesman= "NULL" ;
else
	$competitor_salesman="'$competitor_salesman'";

if(empty($area_code))
	$area_code= "NULL" ;
else
	$area_code="'$area_code'";
  
$costcenter_code = "".$costcenter_code;  
if($costcenter_code == '')
  $costcenter_code= "NULL" ;
else
  $costcenter_code="'$costcenter_code'";

$project_code = "".$project_code;
 if($project_code == '')
  $project_code="NULL";
else
  $project_code="'$project_code'"; 
  
  
if(empty($client_groupcode))
  $client_groupcode="NULL";
else
  $client_groupcode="$client_groupcode"; 

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


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


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

if(empty($client_paytermtype))
	$client_paytermtype= "NULL";
else
  $client_paytermtype="'$client_paytermtype'"; 

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

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


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

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


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

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

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

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

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

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

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

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


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


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


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

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

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


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($ClientIP_LastTransDate))	{
	$ClientIP_LastTransDate  = explodeword($ClientIP_LastTransDate);
	$ClientIP_LastTransDate  = "'$ClientIP_LastTransDate'";
}	
else {$ClientIP_LastTransDate= 'null';}



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


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

 if (!empty($Client_Inst_Month))	{
	$Client_Inst_Month  = explodeword($Client_Inst_Month);
	$Client_Inst_Month  = "'$Client_Inst_Month'";
}	
else {$Client_Inst_Month= 'null';}

 if(empty($clientip_amount))
		 $clientip_amount=0;	
		 
if(empty($upload_ip))
	$upload_ip=0;			 
		 
if(empty($download_ip))
	$download_ip=0;	
	
	
if(empty($client_export))
	$client_export=0;	
if(empty($client_local))
	$client_local=0;
	
$clientipproject_code = "".$clientipproject_code; 
 if($clientipproject_code == '')
  $clientipproject_code='null';
else
  $clientipproject_code="'$clientipproject_code'"; 
  
 $clientipcostcent_code = "".$clientipcostcent_code; 
 if($clientipcostcent_code == '')
  $clientipcostcent_code='null';
else
  $clientipcostcent_code="'$clientipcostcent_code'";   
  
  if(empty($clientiparea_code))
  $clientiparea_code='null';
else
  $clientiparea_code="'$clientiparea_code'";   
   
  
  
$ledger_firstsequence=substr($ledger_number,0,4);
$ledger_name = ereg_replace("'", "\'", $ledger_name);
$ledger_namef = ereg_replace("'", "\'", $ledger_namef);
$client_tc = ereg_replace("'", "\'", $client_tc);
$ledger_description = ereg_replace("'", "\'",$ledger_description);
$client_vatno = ereg_replace("'", "\'",$client_vatno);
$credit_control = ereg_replace("'", "\'",$credit_control);
$client_group = ereg_replace("'", "\'",$client_group);
$client_checkaccount = ereg_replace("'", "\'",$client_checkaccount);
$country = ereg_replace("'", "\'",$country);
$state = ereg_replace("'", "\'",$state);
$city_client = ereg_replace("'", "\'",$city_client);
$Address = ereg_replace("'", "\'",$Address);
$Address2 = ereg_replace("'", "\'",$Address2);	
$email = ereg_replace("'", "\'",$email);
$homepage = ereg_replace("'", "\'",$homepage);
$phone1 = ereg_replace("'", "\'",$phone1);
$phone2 = ereg_replace("'", "\'",$phone2);
$fax = ereg_replace("'", "\'",$fax);
$pobox = ereg_replace("'", "\'",$pobox);
$bussiness_type = ereg_replace("'", "\'",$bussiness_type);
$client_companyowner = ereg_replace("'", "\'",$client_companyowner);
$client_referedby = ereg_replace("'", "\'",$client_referedby);
$client_deliveryassistant = ereg_replace("'", "\'",$client_deliveryassistant);
$client_dlvworktitle = ereg_replace("'", "\'",$client_dlvworktitle);
$delivery_phone = ereg_replace("'", "\'",$delivery_phone);
$delivery_fax = ereg_replace("'", "\'",$delivery_fax);
$client_deliveryaddress = ereg_replace("'", "\'",$client_deliveryaddress);
$client_deliveryaddress2 = ereg_replace("'", "\'",$client_deliveryaddress2);
$client_archivefileno = ereg_replace("'", "\'",$client_archivefileno);
$client_evaluationpoints = ereg_replace("'", "\'",$client_evaluationpoints);
$client_weakness = ereg_replace("'", "\'",$client_weakness);
$client_tips = ereg_replace("'", "\'",$client_tips);
$client_businesssector = ereg_replace("'", "\'",$client_businesssector);
$client_tobcode= ereg_replace("'", "\'",$client_tobcode);
$client_btype2= ereg_replace("'", "\'",$client_btype2);
$client_btype3= ereg_replace("'", "\'",$client_btype3);
$Client_BusinessType= ereg_replace("'", "\'",$Client_BusinessType);
$client_tips= ereg_replace("'", "\'",$client_tips);
$Address3= ereg_replace("'", "\'",$Address3);
$client_deliveryaddress3= ereg_replace("'", "\'",$client_deliveryaddress3);
$technical_person= ereg_replace("'", "\'",$technical_person);
$technical_email= ereg_replace("'", "\'",$technical_email);
$ledger_numberPrv=trim($ledger_numberPrv);
$ledger_number=trim($ledger_number);
 //echo $client_groupcode; exit;
if($mainmode=='insert'){

	if($ledger_numberPrv==$ledger_number){
		fbegin();
		$ledger_number=GenerateLedgerCode($connection,$client_groupcode,$ClientFirstDigit,$NbrOfDigitInLedger,$module_ledgergrp);
		fcommit();
	}
	//echo$ledger_number;exit;
//$client_groupcode = '44';
	$query="insert into clients (Client_CreationDate,Client_deactivate,Client_deactivatedate,Client_TC,ClientGrp_Code,Ledger_number,Ledger_name,Ledger_Desc,Salesmen_Code,Client_FrequencyOfVisit,
			Salesmen_Commission,Client_Rating,Client_VAT,Client_VatNo,Client_VatDate,Client_SellingPrice,Client_DiscountPer,Client_CreditControl,Client_budgetlimit,Currency_Code,Client_CreditLimit,
			Client_Group,Project_Code,Client_PayTermType,Client_PaymentTerms,Client_paymentTerms2,Client_PaymentMode,CostCenter_code,Client_checkAccount,Client_ChkTerms,Client_CashMode,
			Client_sentEmail,Client_tax,Client_Tax1,Client_Tax2,Client_Tax3,Client_Tax4,Client_Tax5,client_export,client_local,Client_CompLegalType,Client_Country,Client_State,Client_City,
			Area_Code,Client_Address,Client_Address2,Client_Email,Client_Homepage,Client_Telephone1,Client_Telephone2,Client_Fax,Client_Pobox,Client_AgentAccount,Client_MotherCompany,Client_Work,
			Client_CompOwnerTitle,Client_CompOwner,Client_ReferedByTitle,Client_ReferedBy,Client_SDINo,Client_DlvAssistantTitle,Client_DeliveryAssistant,Client_DlvWorkTitle,Client_DeliveryPhone,
			Client_DlvFax,Client_DlvAreaCode,Client_DeliveryAddress,Client_DeliveryAddress2,warehouse_code,Client_GCIno,Client_ArchiveFileNo,Client_EvaluationPoints,client_Weakness,Client_Remark,Client_InvRounded,
			Client_InvLanguage,Client_BusinessSector,Client_BusinessSectorDesc,Client_TOBcode,Client_MultipleSales,Client_BType2,Client_BType3,Client_BusinessType,Client_BelongsToGroup,client_tips,
			Client_Address3,Client_DeliveryAddress3,client_technicalRepresentative,client_technicalemail,Client_INVDLV,ProfitCenter_CODE,Ledger_namef, client_financialno, Owner_Branch, client_showcreditlimit, 
			client_showmaturitydate,client_discountonInvoice,WHTchecked,WHTPercentage,Client_priceatcost)

	values ('$creation_date','".$deactivateclient."',$deactivatedate,'".$client_tc."',$client_groupcode,'".$ledger_number."','".$ledger_name."','".$ledger_description."','".$salesman_code."',
			$client_frequencyofvisit,'".$salesman_com."','".$client_rating."',$client_vat,'".$client_vatno."',$vat_date,'".$client_sellingprice."','".$discount."',
			'".$credit_control."','".$budget_limit."','".$currency_code."','".$credit_limit."','".$client_group."',$project_code,$client_paytermtype,$client_paymentterm,
			$client_paymentterm2,'".$client_paymentmode."',$costcenter_code,'".$client_checkaccount."','".$client_chterms."',$client_cashmode,$sent_email,
			$client_tax0,$client_tax1,$client_tax2,$client_tax3,$client_tax4,$client_tax5,'".$client_export."','".$client_local."','".$client_legaltype."',
			'".$country."','".$state."','".$city_client."',$area_code,'".$Address."','".$Address2."','".$email."','".$homepage."','".$phone1."','".$phone2."','".$fax."','".$pobox."',
			'".$agent_code."','".$client_mothercompany."','".$bussiness_type."','".$title_owner."','".$client_companyowner."','".$title_refered."','".$client_referedby."','".$client_sdino."',
			'".$title_assistant."','".$client_deliveryassistant."','".$client_dlvworktitle."','".$delivery_phone."','".$phone3."',$delivery_areacode,'".$client_deliveryaddress."','".$client_deliveryaddress2."',
			'".$warehouse."','$client_gcino','".$client_archivefileno."','".$client_evaluationpoints."','".$client_weakness."','".$client_remark."',$client_invrounded,'".$invoice_language."',
			'".$client_businesssector."','".$client_businesssectordesc."','".$client_tobcode."',$client_multiplesales,'".$client_btype2."','".$client_btype3."','".$Client_BusinessType."',$belongstogroup,
			'".$client_tips."','".$Address3."','".$client_deliveryaddress3."','".$technical_person."','".$technical_email."','".$delivsof."','".$profit_code."', '".$ledger_namef."', '".$client_financialno."', '".$Setup_Branch."'
			, ".GetSQLValueString($client_showcreditlimit,'double').
			", ".GetSQLValueString($client_showmaturitydate,'double').
			", ".GetSQLValueString($client_discountonInvoice,'double').
			", $client_wht, $whtpercentage , $Client_priceatcost )";

    //echo $query; exit;
	$result=mysql_query($query,$connection);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	   $company = explode("_", $_SESSION['dbName']);
	   $currCompany = strtoupper($company[0]);	
	   $query_history = "call ".$config['dbName_history'].".InsertToHistory('I', '$ledger_number', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'clients', '$currCompany')";
	   $result_history = mysql_query($query_history, $connection); 
	}
	
	//echo$query;exit;

	if ($result) {
		
		
				$queryledger="select client_id from clients where ledger_number = '$ledger_number'";
				$resultledger=mysql_query($queryledger,$connection) or die(mysql_error());
				while($rowledger=mysql_fetch_array($resultledger)){
					extract($rowledger);
				}
				
				 


		
// send notification
		$queryacc = "SELECT user_code, ifnull(nbrofdays,0), ifnull(view_access,0) FROM ".$_SESSION['dbLabel'].".notification_access
												where notification_name='client_created' and ifnull(view_access,0) = 1 ";
		$resacc = mysql_query($queryacc, $connection);
		
		
									
		//echo $queryacc;
		while ($rwacc = mysql_fetch_array($resacc)){
		$querynot = "call sendNotification('client_created', '$rwacc[0]', $rwacc[1], $rwacc[2], $client_id, '" .$_SESSION['useraccount'] ."') ";
		 // echo $querynot;exit;
										
		$resnot = mysql_query($querynot, $connection);
		}		
		
			
// end send notification		
		
		
		
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','CLIENTS','',$ledger_number,$Setup_Branch);
		}
	}
	
	/*if($result && $module_ledgergrp=='1'){

		$query_update="update ledgergrp set LedgerGrp_reference=LedgerGrp_reference+1
						where LedgerGrp_Code='".$client_groupcode."'";
		$resultupdate=mysql_query($query_update,$connection);
		$message    = mysql_error();

	}*/


//// Default Currency for Client and Journal

	$query_setup="select Client_DefaultCur,Journal_DefaultCur from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_defaultcur=mysql_fetch_row($result_setup);
	
	$client_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,
									Project_Code,
									CostCenter_code,
									Ledger_Deactivate,
									Ledger_inactive,
									Ledger_inactivedate,
									Ledger_desc,
									Owner_Branch)
									
									
									VALUES ('".$ledger_number."',
											'".$ledger_name."',	
											'0',
											'".$currency_code."',
											$belongstogroup,
											'1',
											$project_code,
											$costcenter_code,
											'".$deactivateclient."',
											'".$deactivateclient."',
											$deactivatedate,
											'".$ledger_description."', '".$Setup_Branch."')";
											
	
	$result_ledger=mysql_query($query_ledger,$connection);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	$query_ledger_history = "insert into ledger_history (ledger_number,
									ledger_name,
									Ledger_grpdet,
									Currency_code,
									Ledger_BelongsToGroup,
									ledger_type,
									Project_Code,
									CostCenter_code,
									Ledger_Deactivate,
									Ledger_inactive,
									Ledger_inactivedate,
									Ledger_desc,
									Owner_Branch,
									Ledger_Date,
									Ledger_operator,
									Ledger_schema,
									Ledger_status)
									
									
									VALUES ('".$ledger_number."',
											'".$ledger_name."',	
											'0',
											'".$currency_code."'
											$belongstogroup,
											'1',
											$project_code,
											$costcenter_code,
											'".$deactivateclient."',
											'".$deactivateclient."',
											$deactivatedate,
											'".$ledger_description."', '".$Setup_Branch."',
											now(),
											'".$User_Name."',
											'".$_SESSION['dbName']."',
											'I')";
	//echo $query_ledger_history; exit;
	$result_ledger_history = mysql_query($query_ledger_history, $connection_history);
	$message    = mysql_error();
	}
		if ($result_ledger) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','LEDGER','',$ledger_number,$Setup_Branch);
		}
	}
		
		
	$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);
	
	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,
									Owner_Branch)

										VALUES ('".$ledger_acc."',
											'".$ledger_name."',	
											'0',
											'".$currency_code."'
											$belongstogroup,
											'0',
											$project_code,
											$costcenter_code,
											'".$deactivateclient."',
											$deactivatedate,
											'".$ledger_description."',
											$ledger_activity, '".$Setup_Branch."')";			

											
	//	echo $query_ledgerdetail;								
	$result_ledgerdetail=mysql_query($query_ledgerdetail,$connection);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	$query_ledgerdetail_history = "insert into ledger_history (ledger_number,
									ledger_name,
									Ledger_grpdet,
									Currency_code,
									Ledger_BelongsToGroup,
									ledger_type,
									Project_Code,
									CostCenter_code,
									Ledger_Deactivate,
									Ledger_inactivedate,
									Ledger_desc,
									ledger_activity,
									Owner_Branch,
									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, '".$Setup_Branch."',
											now(),
											'".$User_Name."',
											'".$_SESSION['dbName']."',
											'I')";
											
	$result_ledgerdetail_history = mysql_query($query_ledgerdetail_history, $connection_history);
	}
	if ($result_ledgerdetail) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','LEDGER','',$ledger_acc,$Setup_Branch);
		}
	}
}
	$message    = mysql_error();
	
/*
if(!empty($name)){ 

	$name= ereg_replace("'", "\'",$name);
	$work_title= ereg_replace("'", "\'",$work_title);
	$telephoneno= ereg_replace("'", "\'",$telephoneno);
	$telephoneno2= ereg_replace("'", "\'",$telephoneno2);
	$telephoneno3= ereg_replace("'", "\'",$telephoneno3);


	$insert="insert into telephoneno (ledger_number,TelephoneNo_ContactTitle,Telephone_name,Client_workTitle,Client_Type,TelephoneNo,TelephoneNo2,TelephoneNo3 )
			values ('".$ledger_number."',".$title_name."','".$name."','".$work_title."','".$client_type."','".$telephoneno."','".$telephoneno2."','".$telephoneno3."')";
	$resulttelephone=mysql_query($insert,$connection);
	$message    = mysql_error();
}*/

	/*if(!empty($competitor_ratio)  && !empty($competitor_salesman) && !empty($competitor_name)){ 	

		$competitor_ratio= ereg_replace("'", "\'",$competitor_ratio);
		$competitor_product= ereg_replace("'", "\'",$competitor_product);
		$competitor_name= ereg_replace("'", "\'",$competitor_name);
				
		$querycompetitor="insert into competitors(Ledger_Number,Competitor_Ratio,Competitor_Salesman,Competitor_Name)
						values ('".$ledger_number."','".$competitor_ratio."','".$competitor_product."',$competitor_salesman,'".$competitor_name."')";
				
		$resultcompetitor=mysql_query($querycompetitor,$connection) or die(mysql_error());
		$message    = mysql_error();
	}
			
	if(($clientact_startdate!='null') or ($clientact_enddate!='null')){ 

		$clientact_remark= ereg_replace("'", "\'",$clientact_remark);
		$queryactivation="insert into clientactivationhistoric(Ledger_Number,ClientAct_StartDate,ClientAct_EndDate,ClientAct_Remark)
						values ('".$ledger_number."','$clientact_startdate','$clientact_enddate','".$clientact_remark."')";
				
		//echo$queryactivation."<BR>";		
				
		$resultcompetitor=mysql_query($queryactivation,$connection);
		$message    = mysql_error();
	}*/
	
}

else if($mainmode=='edit'){

	//echo $ledger_name,"<br>";
	$update_client="Update clients set 
	ledger_number='".$ledger_number."',
	Client_CreationDate='$creation_date',
	Client_deactivate='".$deactivateclient."',
	Client_deactivatedate=$deactivatedate,
	Client_TC='".$client_tc."',
	ClientGrp_Code='".$client_groupcode."',
	Ledger_name='".$ledger_name."',
	Ledger_namef='".$ledger_namef."',
	Ledger_Desc='".$ledger_description."',
	Salesmen_Code='".$salesman_code."',
	Client_FrequencyOfVisit=$client_frequencyofvisit,
	Salesmen_Commission='".$salesman_com."',
	Client_Rating='".$client_rating."',
	Client_VAT=$client_vat,
	Client_VatNo='".$client_vatno."',
	Client_VatDate=$vat_date,
	Client_SellingPrice='".$client_sellingprice."',
	Client_DiscountPer='".$discount."',
	Client_CreditControl='".$credit_control."',
	Client_budgetlimit='".$budget_limit."',
	Currency_Code='".$currency_code."',
	Client_CreditLimit='".$credit_limit."',
	Client_Group='".$client_group."',
	Project_Code=$project_code,
	Client_PayTermType=$client_paytermtype,
	Client_PaymentTerms=$client_paymentterm,
	Client_paymentTerms2=$client_paymentterm2,
	Client_PaymentMode='".$client_paymentmode."',
	CostCenter_code=$costcenter_code,
	Client_checkAccount='".$client_checkaccount."',
	Client_ChkTerms='".$client_chterms."',
	Client_CashMode=$client_cashmode,
	Client_sentEmail=$sent_email,
	Client_tax=$client_tax0,
	Client_Tax1=$client_tax1,
	Client_Tax2=$client_tax2,
	Client_Tax3=$client_tax3,
	Client_Tax4=$client_tax4,
	Client_Tax5=$client_tax5,
	client_export='".$client_export."',
	client_local='".$client_local."',
	Client_CompLegalType='".$client_legaltype."',
	Client_Country='".$country."',
	Client_State='".$state."',
	Client_City='".$city_client."',
	Area_Code=$area_code,
	Client_Address='".$Address."',
	Client_Address2='".$Address2."',
	Client_Email='".$email."',
	Client_Homepage='".$homepage."',
	Client_Telephone1='".$phone1."',
	Client_Telephone2='".$phone2."',
	Client_Fax='".$fax."',
	Client_Pobox='".$pobox."',
	Client_AgentAccount='".$agent_code."',
	Client_MotherCompany='".$client_mothercompany."',
	Client_Work='".$bussiness_type."',
	Client_CompOwnerTitle='".$title_owner."',
	Client_CompOwner='".$client_companyowner."',
	Client_ReferedByTitle='".$title_refered."',
	Client_ReferedBy='".$client_referedby."',
	Client_SDINo='".$client_sdino."',
	Client_DlvAssistantTitle='".$title_assistant."',
	Client_DeliveryAssistant='".$client_deliveryassistant."',
	Client_DlvWorkTitle='".$client_dlvworktitle."',
	Client_DeliveryPhone='".$delivery_phone."',
	Client_DlvFax='".$phone3."',
	Client_DlvAreaCode=$delivery_areacode,
	Client_DeliveryAddress='".$client_deliveryaddress."',
	Client_DeliveryAddress2='".$client_deliveryaddress2."',
	warehouse_code='".$warehouse."',
	Client_GCIno='$client_gcino',
	Client_ArchiveFileNo='".$client_archivefileno."',
	Client_EvaluationPoints='".$client_evaluationpoints."',
	client_Weakness='".$client_weakness."',
	Client_Tips='".$client_tips."',
	Client_InvRounded=$client_invrounded,
	Client_InvLanguage='".$invoice_language."',
	Client_BusinessSector='".$client_businesssector."',
	Client_BusinessSectorDesc='".$client_businesssectordesc."',
	Client_TOBcode='".$client_tobcode."',
	Client_MultipleSales=$client_multiplesales,
	Client_BType2='".$client_btype2."',
	Client_BType3='".$client_btype3."',
	Client_BusinessType='".$Client_BusinessType."',
	client_remark='".$client_remark."',
	Client_Address3='".$Address3."',
	Client_DeliveryAddress3='".$client_deliveryaddress3."',
	client_technicalRepresentative='".$technical_person."',
	client_technicalemail='".$technical_email."',
	Client_INVDLV='".$delivsof."',
	ProfitCenter_CODE='".$profit_code."',
	client_financialno='".$client_financialno."' 
	,client_showcreditlimit = ".GetSQLValueString($client_showcreditlimit,'double')."
	,client_showmaturitydate = ".GetSQLValueString($client_showmaturitydate,'double')."
	,client_discountonInvoice = ".GetSQLValueString($client_discountonInvoice,'double')."
	, WHTchecked = $client_wht, WHTPercentage = $whtpercentage, Client_EndofMonthPay='$Client_EndofMonthPay',
	Client_priceatcost='$Client_priceatcost'
	where ledger_number='$ledger_numberPrv'";

	//echo$update_client;exit;

	$resultupdate=mysql_query($update_client,$connection);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	   $company = explode("_", $_SESSION['dbName']);
	   $currCompany = strtoupper($company[0]);	
	   $query_history  = "call ".$config['dbName_history'].".InsertToHistory('U', '$ledger_numberPrv', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'clients', '$currCompany')";
	   $result_history = mysql_query($query_history, $connection); 
	}
	

	if ($resultupdate) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','CLIENTS','',$ledger_numberPrv,$Setup_Branch);
		}
	}

	$query_update="update ledger set 
								ledger_name='".$ledger_name."',	
								Project_Code=$project_code,
								CostCenter_code=$costcenter_code,
								Ledger_Deactivate='".$deactivateclient."',
								Ledger_inactive='".$deactivateclient."',
								Ledger_inactivedate=$deactivatedate,
								Ledger_desc='".$ledger_description."',
								Currency_Code='".$currency_code."'
								where ledger_number='$ledger_numberPrv'";
	//echo$query_update;exit;							
	$result_ledgerupdate=mysql_query($query_update,$connection);
	$message    = mysql_error();
	/*
	if($config['log_file'] == 1){
	$query_update_history = "insert into ledger_history (ledger_name, Project_Code, CostCenter_code, Ledger_Deactivate, Ledger_inactive, Ledger_inactivedate,
	                         Ledger_desc, ledger_number, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status) values ('".$ledger_name."', $project_code, $costcenter_code,
							 '".$deactivateclient."', '".$deactivateclient."', $deactivatedate, '".$ledger_description."', '$ledger_numberPrv', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
	$result_update_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_sequence=substr($ledger_numberPrv,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];
			
			$update_ledgerdetail="update ledger set 
										
										ledger_name='".$ledger_name."',	
										Project_Code=$project_code,
										CostCenter_code=$costcenter_code,
										Ledger_Deactivate='".$deactivateclient."',
										Ledger_inactive='".$deactivateclient."',
										Ledger_inactivedate=$deactivatedate,
										Ledger_desc='".$ledger_description."',
										ledger_activity=$ledger_activity,
										Currency_Code='".$currency_code."'
										where ledger_number='$ledger_acc'";
										
										
			//	echo$update_ledgerdetail; exit;
			$resultupdate2=mysql_query($update_ledgerdetail,$connection);
				$message    = mysql_error();
				/*
			if($config['log_file'] == 1){
			$update_ledgerdetail_history = "insert into ledger_history (ledger_name, Project_Code, CostCenter_code, Ledger_Deactivate, Ledger_inactive,
			                                Ledger_inactivedate, Ledger_desc, ledger_activity, ledger_number, Ledger_Date, Ledger_operator, Ledger_schema,
											Ledger_status) values ('".$ledger_name."', $project_code, $costcenter_code, '".$deactivateclient."', '".$deactivateclient."', 
											$deactivatedate, '".$ledger_description."', $ledger_activity, '$ledger_acc', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
			
			$result_ledgerdetail_history = mysql_query($update_ledgerdetail_history, $connection_history);
				$message    = mysql_error();
			}
			*/
			
			if ($resultupdate2) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'U','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
		}
	}
	
	if($ledger_numberPrv!=$ledger_number && $resultupdate){
		
		$query="update telephoneno set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
			$message    = mysql_error();
		
		if($config['log_file'] == 1){
		$query_history = "insert into telephoneno_history (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 competitors set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
			$message    = mysql_error();
		
		if($config['log_file'] == 1){
		$query_history = "insert into competitors_history (ledger_number, Competitor_date, Competitor_operator_id, Competitor_schema, Competitor_status)
		                  values ('".$ledger_number."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_history = mysql_query($query_history, $connection_history);
			$message    = mysql_error();
		}
		
		$query="update clientactivationhistoric set ledger_number='".$ledger_number."' where ledger_number='".$ledger_numberPrv."'";
		$result=mysql_query($query,$connection);
		
		if($config['log_file'] == 1){
		$query_history = "insert into clientactivationhistoric_history (ledger_number, ClientAct_date, ClientAct_operator_id, ClientAct_schema, ClientAct_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,Ledger_inactive)
							VALUES ('".$ledger_acc."','".$ledger_name."',	'0','".$currency_code."',$belongstogroup,'0',$project_code,$costcenter_code,'".$deactivateclient."',
									$deactivatedate,'".$ledger_description."',$ledger_activity,'".$deactivateclient."')";			
			//	echo $query_ledgerdetail;								
			$result_ledgerdetail=mysql_query($query_ledgerdetail,$connection);
				$message    = mysql_error();
			if($config['log_file'] == 1){
			$query_ledgerdetail_history = "insert into ledger_history (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_inactive, 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,'".$deactivateclient."', 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($actionmode=='insertcontract'){
	$ndates = explode('/', trim($txtcontractdate));
	$txtcontractdate = $ndates[2]."-".$ndates[1]."-".$ndates[0];
	
	if($contractterm=="Annual"){
		$query="insert into ClientsContract(ledger_number,project_code,amount,contract_startdate,contract_terms) values('".$ledger_number."','".$txtProjectCode."','".$clientcontact_amount."','".$txtcontractdate."','".$contractterm."')";
		$result=mysql_query($query);
		$message    = mysql_error();
	}
	else if($contractterm=="Monthly"){
		$i=0;
		$clientcontact_amount=$clientcontact_amount/12;
		while($i<12){			
			$query="insert into ClientsContract(ledger_number,project_code,amount,contract_startdate,contract_terms) values('".$ledger_number."','".$txtProjectCode."','".$clientcontact_amount."','".$txtcontractdate."','".$contractterm."')";
			$result=mysql_query($query);
			$message    = mysql_error();
			$querydate="select  DATE_ADD( '".$txtcontractdate."', INTERVAL 30 DAY ) from dual";
			$resultdate=mysql_query($querydate,$connection);
			$rowdate=mysql_fetch_array($resultdate);
			$txtcontractdate=$rowdate[0];

			$i++;
		}
	}
	else if($contractterm=="Quarterly"){
		$i=0;
		$clientcontact_amount=$clientcontact_amount/4;
		while($i<4){			
			$query="insert into ClientsContract(ledger_number,project_code,amount,contract_startdate,contract_terms) values('".$ledger_number."','".$txtProjectCode."','".$clientcontact_amount."','".$txtcontractdate."','".$contractterm."')";
			$result=mysql_query($query);
			$message    = mysql_error();
			$querydate="select  DATE_ADD( '".$txtcontractdate."', INTERVAL 90 DAY ) from dual";
			$resultdate=mysql_query($querydate,$connection);
			$rowdate=mysql_fetch_array($resultdate);
			$txtcontractdate=$rowdate[0];

			$i++;
		}
	}
	else {
		$i=0;
		$clientcontact_amount=$clientcontact_amount/2;
		while($i<2){			
			$query="insert into ClientsContract(ledger_number,project_code,amount,contract_startdate,contract_terms) values('".$ledger_number."','".$txtProjectCode."','".$clientcontact_amount."','".$txtcontractdate."','".$contractterm."')";
			$result=mysql_query($query);
			$message    = mysql_error();
			$querydate="select  DATE_ADD( '".$txtcontractdate."', INTERVAL 182 DAY ) from dual";
			$resultdate=mysql_query($querydate,$connection);
			$rowdate=mysql_fetch_array($resultdate);
			$txtcontractdate=$rowdate[0];

			$i++;
		}
	}


		
	$vTabFocus='Contract';
}

if($actionmode=='submitcontact'){

	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);
			$message    = mysql_error();
		if($config['log_file'] == 1){
		$query_history = "insert into telephoneno_history(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);
				$message    = mysql_error();

		}
	
	}
	else if($telephone_increment!=''){
		$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);
				$message    = mysql_error();

		if($config['log_file'] == 1){
		$query_history = "insert into telephoneno_history (TelephoneNo_ContactTitle, Telephone_name, telephoneno_fname, telephoneno_lname, TelephoneNo,
		                  TelephoneNo2, TelephoneNo3, telephoneno_email, telephoneno_reference, telephoneno_city1, telephoneno_city2, telephoneno_city3, Telephone_increment,
						  Telephone_date, Telephone_operator_id, Telephone_schema, Telephone_status) values ('".$telephoneno_title."', '".$telephone_name."', '".$telephone_fname."',
						  '".$telephone_lname."', '".$telephone_no."', '".$telephone_no2."', '".$telephone_no3."', '".$telephone_email."', '".$telephone_reference."',
						  '".$telephone_city1."', '".$telephone_city2."', '".$telephone_city3."', '".$telephone_increment."', 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','CLIENTS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}
else if($actionmode=='deletecontact'){
	$query = "select ledger_number, TelephoneNo, Telephone_name 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];
	
	$query="delete from telephoneno where Telephone_increment='".$telephone_increment."'";
	$result=mysql_query($query);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	$query_history = "insert into telephoneno_history (ledger_number, TelephoneNo, Telephone_name, Telephone_increment, Telephone_date,
	                  Telephone_operator_id, Telephone_schema, Telephone_status) values ('".$ledger_number."', '".$TelephoneNo."', '".$Telephone_name."',
					  '".$telephone_increment."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	
	$result_history = mysql_query($query_history, $connection_history);
	$message    = mysql_error();
	}
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','CLIENTS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}	

else if($actionmode=='deletecontract'){
	$ContractId=$_GET['contractid'];
	$query="delete from clientscontract where id='".$ContractId."'";
	$result=mysql_query($query);
	$message    = mysql_error();
	$vTabFocus='Contract';
}

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

	$competitor_ratio= ereg_replace("'", "\'",$competitor_ratio);
	$competitor_product= ereg_replace("'", "\'",$competitor_product);
	$competitor_name= ereg_replace("'", "\'",$competitor_name);


	if($competitor_id==''){  

		$competitor_ratio= ereg_replace("'", "\'",$competitor_ratio);
		$competitor_product= ereg_replace("'", "\'",$competitor_product);
		$competitor_name= ereg_replace("'", "\'",$competitor_name);


		$querycompetitor="insert into competitors(Ledger_Number,Competitor_Ratio,Competitor_Product,Competitor_Salesman,Competitor_Name)
					values ('".$ledger_number."','".$competitor_ratio."','".$competitor_product."',$competitor_salesman,'".$competitor_name."')";
				//	echo$querycompetitor;exit;
		$resultcompetitor=mysql_query($querycompetitor,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$querycompetitor_history = "insert into competitors_history(Ledger_Number,Competitor_Ratio,Competitor_Product,Competitor_Salesman,Competitor_Name,
		                           Competitor_date, Competitor_operator_id, Competitor_schema, Competitor_status) 
								   values ('".$ledger_number."','".$competitor_ratio."','".$competitor_product."',$competitor_salesman,'".$competitor_name."',
								   now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_competitor_history = mysql_query($querycompetitor_history, $connection_history);
		$message    = mysql_error();
		}
		
	}
	else if($competitor_id!=''){
	
		$update_competitor="Update competitors set
						Competitor_Ratio='".$competitor_ratio."',
						Competitor_Product='".$competitor_product."',
						Competitor_Salesman=$competitor_salesman,
						Competitor_Name='".$competitor_name."'
						where competitor_id='$competitor_id'";
		//echo$update_competitor;exit;
		$resultcompetitorupdate=mysql_query($update_competitor,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$update_competitor_history = "insert into competitors_history(Competitor_Ratio, Competitor_Product, Competitor_Salesman, Competitor_Name,
		                              competitor_id, Competitor_date, Competitor_operator_id, Competitor_schema, Competitor_status) values ('".$competitor_ratio."',
									  '".$competitor_product."', $competitor_salesman, '".$competitor_name."', '$competitor_id', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$resultcompetitorupdate_history = mysql_query($update_competitor_history, $connection_history);
		$message    = mysql_error();
		}
		
	}
	$vTabFocus='Competitor';
}

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


    $query_competitor = "select Competitor_Id, Ledger_Number, Competitor_Ratio, Competitor_Product from competitors where competitor_id='".$competitor_id."'";
	$result_competitor = mysql_query($query_competitor, $connection);
	$row_competitor = mysql_fetch_row($result_competitor, $connection);
	$Competitor_Id = $row_competitor[0];
	$Ledger_Number = $row_competitor[1];
	$Competitor_Ratio = $row_competitor[2];
	$Competitor_Product = $row_competitor[3];
	
	$query="delete from competitors where competitor_id='".$competitor_id."'";
	$result=mysql_query($query);
	$message    = mysql_error();
	if($config['log_file'] == 1){
	$query_history = "insert into competitors_history (Competitor_Id, Ledger_Number, Competitor_Ratio, Competitor_Product, Competitor_date, Competitor_operator_id,
	                  Competitor_schema, Competitor_status) values ('".$Competitor_Id."', '".$Ledger_Number."', '".$Competitor_Ratio."', '".$Competitor_Product."', now(),
					  '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
	$result_history = mysql_query($query_history, $connection_history);
	$message    = mysql_error();
	}
	
	$vTabFocus='Competitor';
}	


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


	if($clientact_id==''){ 

		$clientact_remark= ereg_replace("'", "\'",$clientact_remark);

		$insertactivation="insert into  clientactivationhistoric
							(Ledger_Number,ClientAct_StartDate,ClientAct_EndDate,ClientAct_Remark)
							values ('".$ledger_number."','$clientact_startdate','$clientact_enddate','".$clientact_remark."')";
			
		$resultactivation=mysql_query($insertactivation,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$insertactivation_history = "insert into  clientactivationhistoric_history
							(Ledger_Number,ClientAct_StartDate,ClientAct_EndDate,ClientAct_Remark, ClientAct_date, ClientAct_operator_id, ClientAct_schema, ClientAct_status)
							values ('".$ledger_number."','$clientact_startdate','$clientact_enddate','".$clientact_remark."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$resultactivation_history = mysql_query($insertactivation_history, $connection_history);
		$message    = mysql_error();
		}
		
	}
	else if($clientact_id!=''){ 
		$update_activation="Update clientactivationhistoric set
							Ledger_Number='".$ledger_number."',
							ClientAct_StartDate='$clientact_startdate',
							ClientAct_EndDate='$clientact_enddate',
							ClientAct_Remark='".$clientact_remark."'
							where clientact_id='$clientact_id'";
		$resultactivationupdate=mysql_query($update_activation,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$update_activation_history = "insert into clientactivationhistoric_history (Ledger_Number, ClientAct_StartDate, ClientAct_EndDate, ClientAct_Remark,
		                              clientact_id, ClientAct_date, ClientAct_operator_id, ClientAct_schema, ClientAct_status) values ('".$ledger_number."', '$clientact_startdate',
									  '$clientact_enddate', '".$clientact_remark."', '$clientact_id', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$result_activation_history = mysql_query($update_activation_history, $connection_history);
		$message    = mysql_error();
		}
		
	}
	$vTabFocus='Activation';
}

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

	$category_name = ereg_replace("'", "\'", $category_name);
	$subscriber_name = ereg_replace("'", "\'", $subscriber_name);
	$clientip_reason = ereg_replace("'", "\'", $clientip_reason);
	$technician_name = ereg_replace("'", "\'", $technician_name);
	$collection_name = ereg_replace("'", "\'", $collection_name);
	$Subscriber_Address = ereg_replace("'", "\'", $Subscriber_Address);
	$client_macaddress = ereg_replace("'", "\'", $client_macaddress);
	$service_name = ereg_replace("'", "\'", $service_name);
	
	if($Clientip_id==''){ 
		fbegin();
		$Clientip_id = get_nextsequence('Clientip_id','',$connection);
		fcommit();
	
		
		
		$query="insert into  clientsip
							(Clientip_id,Ledger_Number,CLIENT_TC,CLIENT_CATEGORY,SUBSCRIBER_NAME,ClientIP_PeriodByMonth,Client_Inst_Month,Clientip_amount,Currency_code,Clientip_startDate,ClientIP_EndDate".
						",ClientIP_LastTransDate,Reason_disabled,Technician_Name,Collection_name,Subscriber_Address,Clientip_upload,Clientip_download,Project_Code,Costcenter_code,Area_Code,".
						"Client_MacAddress,Client_port,Modem_IP,service_name,Clientip_IP)".
						" values ('".$Clientip_id."','".$ledger_number."','".$clientip_tc."','".$category_name."','".$subscriber_name."','$ClientIP_PeriodByMonth',$Client_Inst_Month,'$clientip_amount',
						'".$currencyip."',$start_date,$end_date,$ClientIP_LastTransDate,'".$clientip_reason."','".$technician_name."','".$collection_name."','".$Subscriber_Address."',".
						"'$upload_ip','$download_ip',$clientipproject_code,$clientipcostcent_code,$clientiparea_code,'".$client_macaddress."','".$client_port."','".$client_modemip."','".$service_name."','".$Clientip_IP."')";
		//	echo$query;exit;
		$result=mysql_query($query,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$query_history = "insert into  clientsip_history
							(Clientip_id,Ledger_Number,CLIENT_TC,CLIENT_CATEGORY,SUBSCRIBER_NAME,ClientIP_PeriodByMonth,Client_Inst_Month,Clientip_amount,Currency_code,Clientip_startDate,ClientIP_EndDate".
						",ClientIP_LastTransDate,Reason_disabled,Technician_Name,Collection_name,Subscriber_Address,Clientip_upload,Clientip_download,Project_Code,Costcenter_code,Area_Code,".
						"Client_MacAddress,Client_port,Modem_IP,service_name,Clientip_IP, Clientip_date, Client_operator_id, Client_schema, Client_status)".
						" values ('".$Clientip_id."','".$ledger_number."','".$clientip_tc."','".$category_name."','".$subscriber_name."','$ClientIP_PeriodByMonth',$Client_Inst_Month,'$clientip_amount',
						'".$currencyip."',$start_date,$end_date,$ClientIP_LastTransDate,'".$clientip_reason."','".$technician_name."','".$collection_name."','".$Subscriber_Address."',".
						"'$upload_ip','$download_ip',$clientipproject_code,$clientipcostcent_code,$clientiparea_code,'".$client_macaddress."','".$client_port."','".$client_modemip."','".$service_name."','".$Clientip_IP."',
						now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error();
		}
		
	}
	else if($Clientip_id!=''){ 
		$query="Update clientsip set
							CLIENT_TC='".$clientip_tc."',
							CLIENT_CATEGORY='".$category_name."',
							SUBSCRIBER_NAME='".$subscriber_name."',
							ClientIP_PeriodByMonth='$ClientIP_PeriodByMonth',
							Client_Inst_Month=$Client_Inst_Month,
							Clientip_amount='$clientip_amount',
							Currency_code='".$currencyip."',
							Clientip_startDate=$start_date,
							ClientIP_EndDate=$end_date,
						ClientIP_LastTransDate=$ClientIP_LastTransDate,
						Reason_disabled='".$clientip_reason."',
						Technician_Name='".$technician_name."',
						Collection_name='".$collection_name."',
						Subscriber_Address='".$Subscriber_Address."',
						Clientip_upload='$upload_ip',
						Clientip_download='$download_ip',
						Project_Code=$clientipproject_code,
						Costcenter_code=$clientipcostcent_code,
						Area_Code=$clientiparea_code,
						Client_MacAddress='".$client_macaddress."',
						Client_port='".$client_port."',
						Modem_IP='".$client_modemip."',
						service_name='".$service_name."',
						Clientip_IP='".$Clientip_IP."'
						
				where Clientip_id='$Clientip_id'";
			//	echo$query;exit;
		$result=mysql_query($query,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$query_history = "insert into clientsip_history (CLIENT_TC, CLIENT_CATEGORY, SUBSCRIBER_NAME, ClientIP_PeriodByMonth, Client_Inst_Month, Clientip_amount,
		                  Currency_code, Clientip_startDate, ClientIP_EndDate, ClientIP_LastTransDate, Reason_disabled, Technician_Name, Collection_name, 
						  Subscriber_Address, Clientip_upload, Clientip_download, Project_Code, Costcenter_code, Area_Code, Client_MacAddress, Client_port, Modem_IP,
						  service_name, Clientip_IP, Clientip_id, Clientip_date, Client_operator_id, Client_schema, Client_status) values ('".$clientip_tc."', '".$category_name."', '".$subscriber_name."', '$ClientIP_PeriodByMonth', $Client_Inst_Month,
						  '$clientip_amount', '".$currencyip."', $start_date, $end_date, $ClientIP_LastTransDate, '".$clientip_reason."', '".$technician_name."', '".$collection_name."',
						  '".$Subscriber_Address."', '$upload_ip', '$download_ip', $clientipproject_code, $clientipcostcent_code, $clientiparea_code, '".$client_macaddress."',
						  '".$client_port."', '".$client_modemip."', '".$service_name."', '".$Clientip_IP."', '$Clientip_id', 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','CLIENTS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='ClientsIP';
}

else if($actionmode=='deleteip'){
	$query = "select ledger_number, Clientip_id from clientsip where Clientip_id='".$Clientip_id."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$ledger_number=$rows[0];
	$Clientip_id=$rows[1];
	
	$query="delete from clientsip where Clientip_id='".$Clientip_id."'";
	$result=mysql_query($query);
	if($config['log_file'] == 1){
	$query_history = "insert into clientsip_history (ledger_number, Clientip_id, Clientip_date, Client_operator_id, Client_schema, Client_status) 
	                  values ('".$ledger_number."', '".$Clientip_id."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result_history = mysql_query($query_history, $connection_history);
	$message    = mysql_error();
	}
	$message    = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','CLIENTS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='ClientsIP';
}	

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

    $query_clientactivationhistoric = "select Ledger_Number, ClientAct_ID from clientactivationhistoric where clientact_id='".$clientact_id."'";
    $result_clientactivationhistoric = mysql_query($query_clientactivationhistoric, $connection);
	$row_clientactivationhistoric = mysql_fetch_row($result_clientactivationhistoric);
	$Ledger_Number = $row_clientactivationhistoric[0];
	$ClientAct_ID = $row_clientactivationhistoric[1];
	$query="delete from clientactivationhistoric where clientact_id='".$clientact_id."'";
	$result=mysql_query($query);
	if($config['log_file'] == 1){
	$query_history = "insert into clientactivationhistoric_history (Ledger_Number, ClientAct_ID, ClientAct_date, ClientAct_operator_id, ClientAct_schema, ClientAct_status) 
	                  values ('".$Ledger_Number."', '".$ClientAct_ID."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result_history = mysql_query($query_history, $connection_history);
	$message    = mysql_error();
	}
	$message    = mysql_error();
	$vTabFocus='Activation';
}

else if($actionmode == 'sellingprice')	{
	$clientcode = $_GET['clientcode'];
	$pitemcode = $_GET['pitemcode'];
	$pcurrencycode = $_GET['pcurrencycode'];
	$psellingprice = $_GET['psellingprice'];
	$psellingpriceb1 = $_GET['psellingpriceb1'];
	$psellingpriceb2 = $_GET['psellingpriceb2'];
	$sellingprice_id = $_GET['sellingprice_id'];
	if($sellingprice_id == ''){
		$queryins = "insert into sellingpricebyclient (client_code, item_code, sellingprice, sellingpriceb1, sellingpriceb2, currency_code) ".
					"values ('$clientcode','$pitemcode','$psellingprice','$psellingpriceb1','$psellingpriceb2','$pcurrencycode')";
		$resins = mysql_query($queryins,$connection);
		$message = mysql_error();
		if($message == ''){
			$queryhistory = "insert into clientssellingprice (clientssellingprice_code, clientssellingprice_default, clientssellingprice_amount, clientssellingprice_currency,
				clientssellingprice_amountb1, clientssellingprice_amountb2,client_code) values ".
				"	('$pitemcode',1,'$psellingprice','$pcurrencycode','$psellingpriceb1','$psellingpriceb2','$clientcode')";
			$reshistory = mysql_query($queryhistory,$connection);		
			
		}
	} else if($sellingprice_id != ''){
		$queryupd = "update sellingpricebyclient set
					client_code = '$clientcode', 
					item_code = '$pitemcode', 
					sellingprice = '$psellingprice', 
					sellingpriceb1 = '$psellingpriceb1', 
					sellingpriceb2 = '$psellingpriceb2', 
					currency_code = '$pcurrencycode'
					where sellingprice_id=$sellingprice_id";
					
		$resupd = mysql_query($queryupd,$connection);
		$message = mysql_error();
		
			$querycheck = "select clientssellingprice_id from clientssellingprice
				where clientssellingprice_code = '$pitemcode' and client_code='$clientcode'
				and clientssellingprice_default=1 and clientssellingprice_amount != $psellingprice";
			$rescheck = mysql_query($querycheck,$connection);
			$rwcheck = mysql_fetch_row($rescheck);		
			$clientssellingprice_id = $rwcheck[0];
			if($clientssellingprice_id != ''){
				$queryup = "update clientssellingprice set clientssellingprice_default = 0 where clientssellingprice_id=$clientssellingprice_id";
				$resup = mysql_query($queryup,$connection);
				$queryhistory = "insert into clientssellingprice (clientssellingprice_code, clientssellingprice_default, clientssellingprice_amount, clientssellingprice_currency,
				clientssellingprice_amountb1, clientssellingprice_amountb2,client_code) values ".
				"	('$pitemcode',1,'$psellingprice','$pcurrencycode','$psellingpriceb1','$psellingpriceb2','$clientcode')";
				$reshistory = mysql_query($queryhistory,$connection);
				
				
			} 
		
	}
	if($message == '') echo 0;
	else echo $message;
	exit;
}
else if($actionmode == 'changeamount'){
	$pcurrencycode = $_GET['pcurrencycode'];
	$sellingprice = $_GET['sellingprice'];
	$curb1 = $_GET['curb1'];
	$curb2 = $_GET['curb2'];
	$pdate = $_GET['pdate'];
	$dec1 = $_GET['dec1'];
	$dec2 = $_GET['dec2'];
	
	$newdate = explode('-',$pdate);
	$pdate = $newdate[2]."-".$newdate[1]."-".$newdate[0];
	
	$query = "SELECT ifNull(convertcurfunc('".$pcurrencycode."', '".$curb1."', '$pdate', 0, 0, '', '', ifNull($sellingprice,0)),0) From Dual ";
	$result = selectData($connection, $query);
	$vamount1 =	$result[0];
	
	$query = "SELECT ifNull(convertcurfunc('".$pcurrencycode."', '".$curb2."', '$pdate', 0, 0, '', '', ifNull($sellingprice,0)),0) From Dual ";
	$result = selectData($connection, $query);
	$vamount2 =	$result[0];
	
	
	echo round($vamount1,$dec1);
	echo "[BRK]";
	echo round($vamount2,$dec2);
	exit;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else if($actionmode == 'deleteSellingPrice'){
	$sellingprice_id = $_GET['sellingprice_id'];
	$querydel = "delete from sellingpricebyclient where sellingprice_id=$sellingprice_id";
	$resdel = mysql_query($querydel,$connection);
	$message = mysql_error();
	if($message == '') echo 0;
	else echo $message;
	exit;
}


if (($mainmode=='insert' || $mainmode=='edit') && $message == ''){
	$querySearch = "select replace('$ledger_number', Acctype_ledger, Ledger_Number) as newLedger, concat('$ledger_name',' ', GeneratedType_Name) as newDesc, Ledger_activity, GeneratedType_Name, Ledger_Number, 
					Acctype_ledger
					from AcctypeSetup
					inner join Acctype on acctype_code = Type_Code
					where  '$ledger_number' like concat(Acctype_ledger,'%') ";	
	$resSearch = mysql_query($querySearch, $connection);	
	while($rwSearch = mysql_fetch_array($resSearch)){
		$newLedger = check_ledger($rwSearch[0], $connection);
		// ledger doesn't Exists
		
		if ($newLedger == 1){
			$insert = "insert into ledger (Ledger_number, Ledger_name, Ledger_desc, Ledger_grpdet, Ledger_activity, Acctype_increment, Document, Ledger_number2, Ledger_name2, Project_Code, Ledger_BelongsToGroup, CashType_Code, Ledger_RequiredDetail,
							CostCenter_code, Ledger_Deactivate,Ledger_inactivedate, Currency_code, Ledger_inactive, ledger_type, Owner_Branch, ledger_sequence,ledger_length, ledger_asset, ledger_assetPer, ledger_assetCode, ledger_assetDebit, 
							ledger_assetCredit, ledger_assetSequence, ledger_assetLength) 
					  select '$rwSearch[0]', '$rwSearch[1]', Ledger_desc, Ledger_grpdet, '$rwSearch[1]', Acctype_increment, NULL, Ledger_number2, Ledger_name2, Project_Code, Ledger_BelongsToGroup, CashType_Code, Ledger_RequiredDetail,
							CostCenter_code, Ledger_Deactivate,Ledger_inactivedate, Currency_code, Ledger_inactive, ledger_type, Owner_Branch, ledger_sequence,ledger_length, ledger_asset, ledger_assetPer, ledger_assetCode, ledger_assetDebit, 
							ledger_assetCredit, ledger_assetSequence, ledger_assetLength
							from ledger where ledger_number = '$ledger_number' ";
			$resins = mysql_query($insert, $connection);
			if($config['log_file'] == 1){
				$transType = "I";
				$company = explode("_", $_SESSION['dbName']);
				$currCompany = strtoupper($company[0]);	
				$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$rwSearch[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'ledger', '$currCompany')";
				//echo $query_insertToHistory;exit;
				$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
			}
		} else {
			$update = "update ledger set Ledger_name = '$rwSearch[1]' where ledger_number = '$rwSearch[0]' ";
			$resup = mysql_query($update, $connection);			
		}
	}
}


if (($mainmode=='insert' || $mainmode=='edit') && $FL_Logistic == 1 && $message == ''){
	$queryChk = "select 1 from ".$_SESSION['dbLabel'].".users where User_account = '".$ledger_number."' and upper(concat(defaultComp,'_',defaultyear)) = upper('".$_SESSION['dbName']."') ;"; //echo $queryChk;exit;
	$resChk = mysql_query($queryChk,$connection);
	$numrw = mysql_num_rows($resChk);
	if($user_password == '') $user_password = $ledger_number;
	
	if($numrw == 0){
		$query = "insert into ".$_SESSION['dbLabel'].".users (User_account,User_name,User_IsGroup,User_Group,DefaultComp,DefaultYear,User_Password, user_complex_password, user_first_login )
				values ('".$ledger_number."','".$ledger_name."','0', NULL,'".$default_company."','".$default_year."',";
		
		if ($config['EncryptPassMod'] == 1)
			$query .= "encryptpassword('".$user_password."') ";
		else
			$query .= "'".$user_password."' ";		
					
			$query .= " , '0' ";
		if($ledger_number == $user_password)
		   $query .= ", '1'"; 
		else  $query .= ", '0'"; 
		$query .= " )";
		$res = mysql_query($query,$connection);
	} else {
		$query = "update ".$_SESSION['dbLabel'].".users set User_Password = ";
		if ($config['EncryptPassMod'] == 1)
			$query .= "encryptpassword('".$user_password."') ";
		else
			$query .= "'".$user_password."' ";
		$query .= " where User_account = '".$ledger_number."';";
		$res = mysql_query($query,$connection);
	}
	//echo $query;exit;
	if($res){
			
		$queryChk = "select 1 from ".$_SESSION['dbLabel'].".login where User_account = '".$ledger_number."' and upper(Login_Schema) = upper('".$_SESSION['dbName']."';";
		$resChk = mysql_query($queryChk,$connection);
		$numrw = mysql_num_rows($resChk);
		if($numrw == 0){	
			$query_login = "insert into ".$_SESSION['dbLabel'].".login (User_Name, User_account, Login_Schema, User_password,  Print, Access, Login_Delete, Login_Append, Costcenter, Project, Currency, 
							Ledger, Budget, Keyboard_Layout, 
							Language, FullAccess, FI_SupplierItemAuction, Login_FLClientTracking, FC_Tracking)
							values ('".$ledger_name."', '".$ledger_number."', upper('".$_SESSION['dbName']."'), ";
			
				if ($config['EncryptPassMod'] == 1)
			$query_login .= "encryptpassword('".$user_password."') ";
				else
			$query_login .= "'".$user_password."' ";	
		
			$query_login .= " , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'English', 0, 0, 1, 0);";//echo $query_login;exit;
			$reslogin = mysql_query($query_login,$connection);
		}
		
						
		$queryChk = "select 1 from ".$_SESSION['dbLabel'].".listuserdir where Usercode = '".$ledger_number."' and upper(concat(CompanyName,'_',Diryear)) = upper('".$_SESSION['dbName']."');";
		$resChk = mysql_query($queryChk,$connection);
		$numrw = mysql_num_rows($resChk);
		if($numrw == 0){	
			$query_dir = "insert into ".$_SESSION['dbLabel'].".listuserdir (Usercode, Username, CompanyName, Diryear, AllowLockedPeriod, LockedPeriod, LockedPeriodDays, LockViewPeriod, ViewPeriodDate, 
							ViewLockDaily, ViewNumberOfDays)
							select '".$ledger_number."', '".$ledger_name."', '".$default_company."','".$default_year."', AllowLockedPeriod, LockedPeriod, LockedPeriodDays, LockViewPeriod, ViewPeriodDate, 
							ViewLockDaily, ViewNumberOfDays from ".$_SESSION['dbLabel'].".listuserdir where Usercode = '01' and upper(concat(CompanyName,'_',Diryear)) = upper('".$_SESSION['dbName']."'); ";//echo $query_dir;exit;
			$resdir = mysql_query($query_dir,$connection);
		}	
			
	}
}

/// Appending in file 
$select_query1="select Client_GCIno,7 as de,1 as fs,
date_format(client_creationdate,'%Y'),4 as dsfd,3 as dfdf,
date_format(client_creationdate,'%m'),2 as dsfd,3 as dfdf,
date_format(client_creationdate,'%d'),2 as dsfd,3 as dfdf,


ledger_name,70 as de4,0 as xx,
mid(Ledger_name,1,30),30 as de5,0 as we,
mid(Ledger_Desc,1,30),30 as de6,0 as gfgf,
Client_CompOwnerTitle,1 as de7,0 as wew,
Client_CompOwner,30 as de8,0 as wwq,
'C' as c,0 as de99,0 as de999,
mid(Ledger_number,LENGTH(ledger_number)-5+1,5) as M,5 as de9,1  as wwq1,
'1' as dfd,0 as dfd,0 as fdsf,
date_format(client_creationdate,'%Y'),4 as dssfd,3 as dsfdf,
date_format(client_creationdate,'%m'),2 as dssfd,3 as dfsdf,
date_format(client_creationdate,'%d'),2 as dssfd,3 as dfsdf,
null,18 as fdf,1 as fwwe
from clients				
where ledger_number='$ledger_number'";


//echo$select_query1;


$ourFileHandle = fopen($ourFileName, 'a') or die("can't open file");



if ( '' == file_get_contents($ourFileName) )

{
echo"";
}
else fwrite($ourFileHandle,"\r\n");


$empty1=export_data($connection,'72',$select_query1,$ourFileName,$ourFileHandle,'0');

if($empty1=='5'){

$d=1;
while($d<=179){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}

/////total = 208 characteres


//// 2nd set of variables

$select_query2="select 
TelephoneNo_ContactTitle,1 as len1,0 as i1,
Telephone_name,30 as len2,0 as i2,
Client_workTitle,25 as len3,0 as i3


from telephoneno
where client_type='C' and ledger_number='$ledger_number'";

//echo$select_query2;

$empty2=export_data($connection,'18',$select_query2,$ourFileName,$ourFileHandle,'0');

if($empty2=='5'){

$d=1;
while($d<=56){
fwrite($ourFileHandle,' ');
$d=$d+1;
}

while($d<=89){
fwrite($ourFileHandle,'0');
$d=$d+1;
}
}
//total 89

$select_query3="select 
Client_Telephone1,11 as dfd,1 as dsd,
Client_Telephone2,11 as dfdf,1 as dfdf,
null,11 as dffd,1 as dfdsf,
mid(client_pobox,1,9),9 as dfd,3 as dfd from clients where ledger_number='$ledger_number'";
$empty3=export_data($connection,'12',$select_query3,$ourFileName,$ourFileHandle,'0');

if($empty3=='5'){

$d=1;
while($d<=24){
fwrite($ourFileHandle,'0');
$d=$d+1;
}
}



///4 th set of variables 
$select_query4="select 
TelephoneNo_ContactTitle,'1' as len1,0 as t1,
Telephone_name,'30' as len2,0 as t2,
Client_workTitle,'25' as len3,0 as t3
from telephoneno
where client_type='A' and ledger_number='$ledger_number'";

//echo$select_query4;

$empty4=export_data($connection,'10',$select_query4,$ourFileName,$ourFileHandle,'0');

if($empty4=='5'){

$d=1;
while($d<=56){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}


//total 56
// Business sector value 
$business_query="select Client_BusinessSector from clients where ledger_number='$ledger_number'";

//echo$business_query;
$resultbusiness=mysql_query($business_query,$connection) or die(mysql_error());
$rowbusiness=mysql_fetch_array($resultbusiness);
$business_sector=$rowbusiness[0];

if($business_sector=='')
$business_sector='1';
//echo$business_sector;




$select_query5="select 
Client_Address,100 as fsdf,0 as sfsd,

Client_Address2,50 as fsdsf,0 as fgdf,

mid(Area_Code,length(Area_Code)-4+1,4),4 as fdsdsw,1 as fdss,
Client_ReferedByTitle,1 as fds,0 as sfsd,
Client_ReferedBy,30 as dfds,0 as dfds,
Client_SDINo,7 as ff,3 as dfsdf  ,
Client_DeliveryPhone,11 as fsd,1 asfs,
mid(Client_DeliveryAddress,1,50),50 as dfs ,0 as fsfsf, 
mid(Client_DeliveryAddress,51,50),50 as dfs ,0 as fsfsf, 
Client_DeliveryAddress2,50 as fsdfsww,0 as iiti,

mid(Client_DlvAreaCode,length(Client_DlvAreaCode)-4+1,4),4 as rtrd,1 as bxb,
Client_DlvAssistantTitle,1 as fssdvvf,0 as dsfsdf,
Client_DeliveryAssistant,30 as xxcvx,0 as dscsd,
Client_DlvWorkTitle,25 as fdsfsdc,0 as cbvbcb,
Client_DlvFax,11 as ytry,1 as uuy,
if(client_export=1,'E',''),1 as dfsd,0 as gfdgd,
if(client_local=1,'L',''),1 as dfsd,0 as gfdgd,
";





if($business_sector=='A')
$select_query5=$select_query5."'A' as a,0 as de99,0 as de999,
	 'A' as a1,0 as de919,0 as de1999,
	Client_BusinessSectorDesc,10 as ffwfw,0 as fwfwef,
	null,1 as fgdf,0 as rg,
	null,10 as fg,0 as rgdf,
	null,1 as fgsd,0 as fggfe,
	null,10 as ffwsfs,0 as fsfsf";
	
	
if($business_sector=='I')
$select_query5=$select_query5."'I' as iasd,0 as deai99,0 as de9ii99,
		null,1 as fgdf,0 as rg,
		null,10 as idsfsd,0 as fsdff,
		'I' as iaddassd,0 as deaiasd99,0 as de9iias99,
		Client_BusinessSectorDesc,10 as fsdfsd,0 as gfdsgf,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifsddsfsd,0 as fssdfdff";
		
if($business_sector=='O')
$select_query5=$select_query5."null,1 as fgdf,0 as rg,
		null,1 as idsfsd,0 as fsdff,
		null,10 as ifsddsfsd,0 as fssdfdff,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifds,0 as fssdfsdfdff,
		'o' as iasd,0 as deai99,0 as de9ii99,
	  Client_BusinessSectorDesc,10 as fsdfsd,0 as gfdsgf";
	  
if($business_sector=='1')
$select_query5=$select_query5."
		null,1 as idsfsd,0 as fsdff,
		null,10 as ifsddsfsd,0 as fssdfdff,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifds,0 as fssdfsdfdff,
		null,1 as ffasdsdfgdf,0 as rasdfg,
	  null,10 as fsdfsd,0 as gfdsgf";	


 

$select_query5=$select_query5." from clients where ledger_number='$ledger_number'";


//echo$select_query5;

$empty5=export_data($connection,'83',$select_query5,$ourFileName,$ourFileHandle,'0');
 


if($empty5=='5'){

$d=1;
while($d<=450){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}


$select_query6="select 

Client_BusinessType,50 as b11,0 as dsde,
Client_BType2,50 as bfd,0 as hgh,
Client_BType3,50 as begf,0 as dfd,
Client_TOBcode,4 as sdf,1 as bfd,
Client_EvaluationPoints,50 as bedd,0 as dfsd,
client_Weakness,50 as fdg,0 as gdfsg,
null,50 as dfsfsd,0 as sdf,																				
mid(Client_Tips,1,50),50 as fsd,0 as fsdf,		
mid(Client_Tips,51,50),50 as fsd,0 as fsdf,	
null,50 as bdf,0 as fsd,																		
null,4 as sd,0 as fsdf,                  													
mid(Salesmen_Code,length(Salesmen_Code)-4+1,4),4 as fsdf,0 as fsd,
null,3 as ds ,0 as sdf,																			
null,4 as sd,0 as dfsd, 																			
mid(Salesmen_Code,length(Salesmen_Code)-4+1,4),4 as fsdf,0 as fsd,

null,3 as fsdf,0 as sfd,																	
Client_CreditLimit,8 as dsfsd,1 as fdsf,
mid(Currency_Code,1,2),2 as fsdf,0 as fdsf,

Client_Rating,3 as fds,0 as fsd,
Client_PayTermType,1 as df,0 as dsfsd,
mid(Client_PaymentTerms,1,3),3 as dfds,1 as fsdf,
Client_FrequencyOfVisit,3 as dds,1 as fsf,
null,18 as fsdf,1 as fgdff,
'A',1 as fsdf,0 as fgdff,
null,2 as fsdf,1 as fgdff,
null,1 as fsdf,0 as fgdff,
Client_INVDLV,1 as ddsf,0 as dfsdf,
mid(Client_InvLanguage,1,1),1 as sdf ,0 as sdfsd,

if(client_invrounded=1,'R',''),1 as dedasd919,0 as drre1999,
null,13 as sfsd,1 as fsf,
null,13 as df,1 as dsfdsf,
null,10 as dfds,1 as sdfsdf,
null,10 as dfds,1 as fsdf,

Client_MotherCompany,5 as ds ,2 as fds,

null,30 as fsd,0 as ssdf,
User_Account,7 as fdsf,0 as fdsfds,
null,1 as fsd,0 as sfdd,
date_format(client_sysdate,'%Y'),4 as df,0 as dsfds,
date_format(client_sysdate,'%m'),2 as dffs,0 as dffds,
date_format(client_sysdate,'%d'),2 as sdfds,0 as fsd,

date_format(Client_SysTime,'%h%i%s%'),8 as fsf,0 as fsdf,
null,8 as fdsf,1 as fdsf,
null,4 as dfds,0 as ffdsfs 
  
from clients where ledger_number='$ledger_number'";




$empty6=export_data($connection,'130',$select_query6,$ourFileName,$ourFileHandle,'0');

if($empty6=='5'){

$d=1;
while($d<=638){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}
 



/// 7th set of variables 
$select_query7="select 
Competitor_Name,30 as fsdf,0 as dfsd,
Competitor_Ratio,5 as dfsd,2 as dsf,
Competitor_Salesman,30 as ds,0 as fsdf

from competitors where ledger_number='$ledger_number'";

//echo$select_query7;
$empty7=export_data($connection,'10',$select_query7,$ourFileName,$ourFileHandle,'3');

if($empty7=='5'){


for($p=1;$p<=3;$p++){
$d=1;
while($d<=30){
fwrite($ourFileHandle,' ');
$d=$d+1;
}

$d=1;
while($d<=5){
fwrite($ourFileHandle,'0');
$d=$d+1;
}

$d=1;
while($d<=30){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}
}


$select_query8="select 
12110,5 as fdfs,1 as sdfsd,
if(Client_MultipleSales='1','M',''),1 as fsf,0 as frsd
from clients where ledger_number='$ledger_number'";


$empty8=export_data($connection,'7',$select_query8,$ourFileName,$ourFileHandle,'0');

if($empty8=='5'){

$d=1;
while($d<=1){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}



////// Delete Mode 
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);
	$row_ledger = mysql_fetch_row($result_ledger);
	$Ledger_number = $row_ledger[0];
	$Ledger_name = $row_ledger[1];
	$Ledger_desc = $row_ledger[2];

	$deleteledger = "delete from ledger where ledger_number='$ledgernumber'";
	
	$result=mysql_query($deleteledger);
	if($config['log_file'] == 1){
	$deleteledger_history = "insert into ledger_history (Ledger_number, Ledger_name, Ledger_desc, Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status) 
	                         values ('".$Ledger_number."', '".$Ledger_name."', '".$Ledger_desc."', '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
	$result_history = mysql_query($deleteledger_history, $connection_history);	
	$message    = mysql_error();
	}
	$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);
			}
		}
		
		$querycontact = "select Telephone_increment, Ledger_number, TelephoneNo, Telephone_name from telephoneno where ledger_number='$ledgernumber'";
		$resultcontact = mysql_query($querycontact, $connection_history);
		$rowcontact = mysql_fetch_row($resultcontact);
		$Telephone_increment = $rowcontact[0];
		$Ledger_number = $rowcontact[1];
		$TelephoneNo = $rowcontact[2];
		$Telephone_name = $rowcontact[3];
		
		$deletecontact = "delete from telephoneno where ledger_number='$ledgernumber'";
		$result=mysql_query($deletecontact)	;	
		$message    = mysql_error();	
		if($config['log_file'] == 1){
		$deletecontact_history = "insert into telephoneno_history (Telephone_increment, Ledger_number, TelephoneNo, Telephone_name, Telephone_date, Telephone_operator_id,
		                          Telephone_schema, Telephone_status) values ('".$Telephone_increment."', '".$Ledger_number."', '".$TelephoneNo."', '".$Telephone_name."',
								  now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$resultcontact_history = mysql_query($deletecontact_history, $connection_history);
		$message    = mysql_error();
		}
		
		
		$querycompetitor = "select Competitor_Id, Ledger_Number, Competitor_Name from competitor where ledger_number='$ledgernumber'";
		$resultcompetitor = mysql_query($querycompetitor, $connection);
		
		    $Competitor_Id = $rowcompetitor[0];
			$Ledger_Number = $rowcompetitor[1];
			$Competitor_Name = $rowcompetitor[2];
			

		$deletecompetitor = "delete from competitors where ledger_number='$ledgernumber'";
		$result=mysql_query($deletecompetitor);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$deletecompetitor_history = "insert into competitor_history (Competitor_Id, Ledger_Number, Competitor_Name, Competitor_date, 
			                             Competitor_operator_id, Competitor_schema, Competitor_status) values ('".$Competitor_Id."', '".$Ledger_Number."',
										 '".$Competitor_Name."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$resultcompetitor_history = mysql_query($deletecompetitor_history, $connection_history);
		$message    = mysql_error();
		}
		
		$queryactivation = "select Ledger_Number, ClientAct_StartDate, ClientAct_EndDate, ClientAct_Remark from clientactivationhistoric where ledger_number='$ledgernumber'";
		$resultactivation = mysql_query($queryactivation, $connection);
		$rowactivation = mysql_fetch_row($resultactivation);
		$Ledger_Number = $rowactivation[0];
		$ClientAct_StartDate = $rowactivation[1];
		$ClientAct_EndDate = $rowactivation[2];
		$ClientAct_Remark = $rowactivation[3];
		
		$deleteactivation = "delete from clientactivationhistoric where ledger_number='$ledgernumber'";
		$result=mysql_query($deleteactivation);
		$message    = mysql_error();
		if($config['log_file'] == 1){
		$deleteactivation_history = "insert into clientactivationhistoric_history (Ledger_Number, ClientAct_StartDate, ClientAct_EndDate, ClientAct_Remark, ClientAct_date, ClientAct_operator_id, ClientAct_schema, ClientAct_status) 
		                             values ('".$Ledger_Number."', '".$ClientAct_StartDate."', '".$ClientAct_EndDate."', '".$ClientAct_Remark."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$resultactivation_history = mysql_query($deleteactivation_history, $connection_history);
		$message    = mysql_error();
		}
		$queryclient = "select Ledger_number, Ledger_name, Ledger_Desc, Client_Work from clients where ledger_number='$ledgernumber'";
		$resultclient = mysql_query($queryclient, $connection);
		$rowclient = mysql_fetch_row($resultclient);
		$Ledger_number = $rowclient[0];
		$Ledger_name = $rowclient[1];
		$Ledger_Desc = $rowclient[2];
		$Client_Work = $rowclient[3];
		
		if($config['log_file'] == 1){
	   $company = explode("_", $_SESSION['dbName']);
	   $currCompany = strtoupper($company[0]);	
	   $query_history  = "call ".$config['dbName_history'].".InsertToHistory('D', '$ledgernumber', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'clients', '$currCompany')";
	   $result_history = mysql_query($query_history, $connection); 
	}
		
		$deleteclient = "delete from clients where ledger_number='$ledgernumber'";
		$result=mysql_query($deleteclient, $connection);	
		$message    = mysql_error();
		
		if ($deleteclient) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','CLIENTS','',$ledgernumber,$Setup_Branch);
			}
		}
				
		$ledger_sequence=substr($ledgernumber,4);

		//// Getting ledgers from acctypesetup table
		$query_acctypesetup="select ledger_number from acctypesetup";
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
		
		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
			$query_ledger_detail = "select Ledger_number, Ledger_name, Ledger_desc from ledger where ledger_number = '$ledger_acc'";
			$result_ledger_detail = mysql_query($query_ledger_detail, $connection);
			$row_ledger_detail = mysql_fetch_row($result_ledger_detail);
			$Ledger_number = $row_ledger_detail[0];
			$Ledger_name = $row_ledger_detail[1];
			$Ledger_desc = $row_ledger_detail[2];
			
			$delete_ledger_detail= "delete from ledger where ledger_number='$ledger_acc'";
			//echo$delete_ledger_detail."<br>";
			$result_deletedetail=mysql_query($delete_ledger_detail);
					$message    = mysql_error();
			if($config['log_file'] == 1){
			$delete_ledger_detail_history = "insert into ledger_history (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')";
			$result_ledger_detail_history = mysql_query($delete_ledger_detail_history, $connection_history);
			$message    = mysql_error();
			}
			if ($delete_ledger_detail) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'D','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
		}
	}

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


/*if($actionmode=='submitcontact' || $actionmode=='deletecontact' || $actionmode=='submitcompetitor' || $actionmode=='deletecomp' || $actionmode=='submitactivation' || $actionmode=='deleteact' || $actionmode=='submitclientip' || $actionmode=='deleteip')
	header ("Location: ./Form_Clients.php?mainmode=editmode&ledger_number=$ledger_number&vTabFocus=$vTabFocus&message=".$message);
else

header ("Location: ./Form_Clients.php?mainmode=&message=".$message);*/

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

mysql_close($connection);

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

HERE;

function GenerateLedgerCode($connection,$PLedgerGroup,$ClientFirstDigit,$PLedgerDetailDigit,$PModuleGrp){
	
	
	$vErrorExist=1;
	
	while($vErrorExist==1){
		
		if($PModuleGrp==0){
			$length_client_firstdigit=strlen($ClientFirstDigit);
			$left_length=$PLedgerDetailDigit-$length_client_firstdigit;
			
			/// Client Increment
			$query_increment="select client_increment from sequence";
			
			$resultincrement=mysql_query($query_increment,$connection) or die(mysql_error());
			$row_increment=mysql_fetch_row($resultincrement);
			$client_increment=$row_increment[0];
		
			$client_increment=$client_increment+1;
			
			
			$query="update sequence set client_Increment='".$client_increment."'";
			$result=mysql_query($query,$connection) or die(mysql_error());
			if($config['log_file'] == 1){
			$query_history = "insert into sequence_history (client_Increment, Seq_date, Seq_operator_id, Seq_schema, seq_status) 
			                  values ('".$client_increment."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
			$result_history = mysql_query($query_history, $connection_history);
			$message    = mysql_error();
			}
			$client_increment=str_pad($client_increment, $left_length, '0', STR_PAD_LEFT);
			$ledger_number=$ClientFirstDigit.''.$client_increment;
			
			//echo$ledger_number;
		}
		else if($PModuleGrp==1){
			$length_supplier_groupcode=strlen($PLedgerGroup);
			$left_length=$PLedgerDetailDigit-$length_supplier_groupcode;
			
			$query_reference="select LedgerGrp_reference from ledgergrp where LedgerGrp_Code='".$PLedgerGroup."'";
			
			$resultreference=mysql_query($query_reference,$connection) or die(mysql_error());
			$row_reference=mysql_fetch_row($resultreference);
			$ledgergrp_reference=$row_reference[0];
			$ledgergrp_reference=$ledgergrp_reference+1;
			
			$query="update ledgergrp set LedgerGrp_reference='".$ledgergrp_reference."' where LedgerGrp_Code='".$PLedgerGroup."'";
			$result=mysql_query($query,$connection) or die(mysql_error());
			if($config['log_file'] == 1){
			$query_history = "insert ledgergrp_history into (LedgerGrp_reference, LedgerGrp_Code, LedgerGrp_Date, LedgerGrp_operator_id, LedgerGrp_schema, LedgerGrp_status) 
			                  values ('".$ledgergrp_reference."', '".$PLedgerGroup."', now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
			$result_history = mysql_query($query_history, $connection_history);
			$message    = mysql_error();
			}
			$ledgergrp_reference=str_pad($ledgergrp_reference, $left_length, '0', STR_PAD_LEFT);
			$ledger_number=$PLedgerGroup.$ledgergrp_reference;
			//echo$ledger_number;
		}
		
				
		$sub_query="select count(*) from clients where upper(Ledger_number)=upper('".$ledger_number."') ";
		//echo$sub_query."<BR>";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row=mysql_fetch_row($sub_result);
		$count=$sub_row[0];
		
		if($count>0) $vErrorExist=1;
		else {
			$vErrorExist=0;
		}	
		
	}

	return $ledger_number;
}

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

	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{	
		$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
		$result = selectData($connection, $query);
		//var_dump($query); 
		return	$result[0];

	}

function check_ledger($ledger_number, $connection){
	$query = "select 1 from ledger where ledger_number = '$ledger_number' and Ledger_grpdet=0 ";
	$result = mysql_query($query, $connection);
	$numr = mysql_num_rows($result);
	if ($numr > 0)
		return 0;
	return 1;
}	
?>