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

include("supplier_function.php");

 



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

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

//fetching the name of the current user
$query_user_name = "select User_Name from ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."'";
$result_user_name = mysql_query($query_user_name, $connection);
$rows_user_name = mysql_fetch_array($result_user_name);
extract($rows_user_name);

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

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

$querysetup="select SupplierFirstDigit,NbrOfDigitInLedger, Setup_Branch, ifnull(supplier_access,0) as supplier_access, ifnull(alwaysClearPage,0) as alwaysClearPage from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

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

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


$checkcode    		= $_POST["checkcode"]; if ($checkcode == ""){$checkcode = $_GET["checkcode"];} 
$fill_ledger 		= $_POST["fill_ledger"]; if ($fill_ledger == ""){$fill_ledger = $_GET["fill_ledger"];} 
$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"];} 
$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"];} 
$telephone_increment= $_POST["telephone_increment"]; if ($telephone_increment == ""){$telephone_increment = $_GET["telephone_increment"];} 


$supplier_groupcode	= $_POST["supplier_groupcode"]; if ($supplier_groupcode == ""){$supplier_groupcode = $_GET["supplier_groupcode"];} 
$supplier_groupname	= $_POST["supplier_groupname"]; if ($supplier_groupname == ""){$supplier_groupname = $_GET["supplier_groupname"];} 
$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"];} 
$representative 		= $_POST["representative"]; if ($representative == ""){$representative = $_GET["representative"];} 
$area_code 		= $_POST["area_code"]; if ($area_code == ""){$area_code = $_GET["area_code"];} 
$areaname 		= $_POST["areaname"]; if ($areaname == ""){$areaname = $_GET["areaname"];} 
$supplier_tax1 		= $_POST["supplier_tax1"]; if ($supplier_tax1 == ""){$supplier_tax1 = $_GET["supplier_tax1"];} 
$supplier_tax2 		= $_POST["supplier_tax2"]; if ($supplier_tax2 == ""){$supplier_tax2 = $_GET["supplier_tax2"];} 
$supplier_tax3 		= $_POST["supplier_tax3"]; if ($supplier_tax3 == ""){$supplier_tax3 = $_GET["supplier_tax3"];} 
$supplier_tax4 		= $_POST["supplier_tax4"]; if ($supplier_tax4 == ""){$supplier_tax4 = $_GET["supplier_tax4"];} 
$supplier_tax5 		= $_POST["supplier_tax5"]; if ($supplier_tax5 == ""){$supplier_tax5 = $_GET["supplier_tax5"];} 
$supplier_vat 		= $_POST["supplier_vat"]; if ($supplier_vat == ""){$supplier_vat = $_GET["supplier_vat"];} 
$supplier_vatno = $_POST["supplier_vatno"]; if ($supplier_vatno == ""){$supplier_vatno = $_GET["supplier_vatno"];} 
$vat_date 		= $_POST["vat_date"]; if ($vat_date == ""){$vat_date = $_GET["vat_date"];} 
$supplier_approved 		= $_POST["supplier_approved"]; if ($supplier_approved == ""){$supplier_approved = $_GET["supplier_approved"];} 
$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"];} 
$supplier_paymentterm 		= $_POST["supplier_paymentterm"]; if ($supplier_paymentterm == ""){$supplier_paymentterm = $_GET["supplier_paymentterm"];} 
$supplier_sub 		= $_POST["supplier_sub"]; if ($supplier_sub == ""){$supplier_sub = $_GET["supplier_sub"];} 
$localexport 		= $_POST["localexport"]; if ($localexport == ""){$localexport = $_GET["localexport"];} 
$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"];} 
$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"];} 

$fax 		 					= $_POST["fax"]; if ($fax == ""){$fax = $_GET["fax"];} 
$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"];} 

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


$country 	 					= $_POST["country"]; if ($country == ""){$country = $_GET["country"];} 
$state 		 					= $_POST["state"]; if ($state == ""){$state = $_GET["state"];} 
$city_supplier	 				= $_POST["city_supplier"]; if ($city_supplier == ""){$city_supplier = $_GET["city_supplier"];} 
$supplier_group	 				= $_POST["supplier_group"]; if ($supplier_group == ""){$supplier_group = $_GET["supplier_group"];} 
$Address 	 					= $_POST["Address"]; if ($Address == ""){$Address = $_GET["Address"];} 
$Address2  	 					= $_POST["Address2"]; if ($Address2 == ""){$Address2 = $_GET["Address2"];} 
$Address3  	 					= $_POST["Address3"]; if ($Address3 == ""){$Address3 = $_GET["Address3"];} 

$supplier_mothercompany 		= $_POST["supplier_mothercompany"]; if ($supplier_mothercompany == ""){$supplier_mothercompany = $_GET["supplier_mothercompany"];} 
$supplier_companyname			= $_POST["supplier_companyname"]; if ($supplier_companyname == ""){$supplier_companyname = $_GET["supplier_companyname"];} 
$titleowner_description 		= $_POST["titleowner_description"]; if ($titleowner_description == ""){$titleowner_description = $_GET["titleowner_description"];} 
$supplier_companyowner 			= $_POST["supplier_companyowner"]; if ($supplier_companyowner == ""){$supplier_companyowner = $_GET["supplier_companyowner"];} 
$titlerefered_description		= $_POST["titlerefered_description"]; if ($titlerefered_description == ""){$titlerefered_description = $_GET["titlerefered_description"];} 
$supplier_referedby 			= $_POST["supplier_referedby"]; if ($supplier_referedby == ""){$supplier_referedby = $_GET["supplier_referedby"];} 

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

$businesssector					= $_POST["businesssector"]; if ($businesssector == ""){$businesssector = $_GET["businesssector"];} 
$supplier_businesssector		= $_POST["supplier_businesssector"]; if ($supplier_businesssector == ""){$supplier_businesssector = $_GET["supplier_businesssector"];} 
$supplier_businesssectordesc	= $_POST["supplier_businesssectordesc"]; if ($supplier_businesssectordesc == ""){$supplier_businesssectordesc = $_GET["supplier_businesssectordesc"];} 
$supplier_btype2 				= $_POST["supplier_btype2"]; if ($supplier_btype2 == ""){$supplier_btype2 = $_GET["supplier_btype2"];} 
$supplier_btype3 				= $_POST["supplier_btype3"]; if ($supplier_btype3 == ""){$supplier_btype3 = $_GET["supplier_btype3"];} 	
$paymentcondition				= $_POST["paymentcondition"]; if ($paymentcondition == ""){$paymentcondition = $_GET["paymentcondition"];} 	

$supBankName					= $_POST["supBankName"]; if ($supBankName == ""){$supBankName = $_GET["supBankName"];} 	
$supBankAddr					= $_POST["supBankAddr"]; if ($supBankAddr == ""){$supBankAddr = $_GET["supBankAddr"];} 	
$supAccNbr						= $_POST["supAccNbr"]; if ($supAccNbr == ""){$supAccNbr = $_GET["supAccNbr"];} 	
$supSortCode					= $_POST["supSortCode"]; if ($supSortCode == ""){$supSortCode = $_GET["supSortCode"];} 	
$supIBAN						= $_POST["supIBAN"]; if ($supIBAN == ""){$supIBAN = $_GET["supIBAN"];} 	
$supSwift						= $_POST["supSwift"]; if ($supSwift == ""){$supSwift = $_GET["supSwift"];} 	


$CompInfo = explode('_', $_SESSION['dbName']);
$default_company = $CompInfo[0];
$default_year = $CompInfo[1];
//my code
$actionmode=$_GET['actionmode'];
 //my code
	if($actionmode=='getledgergroup'){	
		$ledger_number=$_GET['ledger_number'];
		//echo "here";
	//	exit;
		$query_getlastnum="select max(ledger_number) AS gratest_ledger_number from suppliers where ledger_number like '$ledger_number%' and Supplier_BelongsToGroup=0";
		$result2=mysql_query($query_getlastnum);
			//$row_num=mysql_num_rows($result2);
			//echo $query_getlastnum;
			//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_Suppliers.php?msg=notfound&ledgetdtserial=$ledgetdtserial");
			}
			else{
			$ledger_number_plus=$gratest_ledger_number+1; 
			//echo $ledger_number_plus;exit;
			header ("Location: ./Form_Suppliers.php?msg=hasledger&ledger_number_plus=$ledger_number_plus");
			}
	}
	

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

}	
else {$vat_date= 'null';}


		
if(empty($paymentterm))
$paymentterm="0";		

if(empty($creditlimit))
$creditlimit="0";

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

  if($project_code == '')
  $project_code= 'null' ;
else
  $project_code="'$project_code'";
  
    if($costcenter_code == '')
  $costcenter_code= 'null' ;
else
  $costcenter_code="'$costcenter_code'";
  
 if(empty($supplier_tax1))
$supplier_tax1=0;

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

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

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

 if(empty($supplier_tax5))
$supplier_tax5=0;
 
 if(empty($supplier_groupcode))
  $supplier_groupcode='null';
else
  $supplier_groupcode="$supplier_groupcode";
 
$ledger_name  					= ereg_replace("'", "\'", $ledger_name);
$ledger_namef 					= ereg_replace("'", "\'", $ledger_namef);
$ledger_description 			= ereg_replace("'", "\'", $ledger_description);
$representative 				= ereg_replace("'", "\'", $representative);
$Address 						= ereg_replace("'", "\'",$Address);
$Address2 						= ereg_replace("'", "\'",$Address2);	
$Address3 						= ereg_replace("'", "\'",$Address3);	
$email 							= ereg_replace("'", "\'",$email);
$homepage 						= ereg_replace("'", "\'",$homepage);
$phone1 						= ereg_replace("'", "\'",$phone1);
$phone2 						= ereg_replace("'", "\'",$phone2);
$fax 							= ereg_replace("'", "\'",$fax);
$city_supplier 					= ereg_replace("'", "\'",$city_supplier);
$state 							= ereg_replace("'", "\'",$state);
$country 						= ereg_replace("'", "\'",$country);
$supplier_companyowner 			= ereg_replace("'", "\'",$supplier_companyowner);
$supplier_businesssectordesc 	= ereg_replace("'", "\'",$supplier_businesssectordesc);
$supplier_btype2 				= ereg_replace("'", "\'",$supplier_btype2);
$supplier_btype3 				= ereg_replace("'", "\'",$supplier_btype3);
$supplier_referedby 			= ereg_replace("'", "\'",$supplier_referedby);
$paymentcondition 				= ereg_replace("'", "\'",$paymentcondition);

if($paymentcondition == '') $paymentcondition = "NULL";
else $paymentcondition = "'".$paymentcondition."'";

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

	if($credit_limit=='')
		$credit_limit=0;
		
	if($supplier_paymentterm=='')
		$supplier_paymentterm=0;	
	
if($supplier_gcino=='')
		$supplier_gcino=0;	
	
if($supplier_vat=='')
		$supplier_vat=0;

$ledger_numberPrv=trim($ledger_numberPrv);
$ledger_number=trim($ledger_number);
	
$itemSupplier_id = $_GET['itemSupplier_id']	;
$item_code = $_GET['item_code']	;
$group_code = $_GET['group_code']	;

if($mainmode=='' && $actionmode =='insertitem'){
	$check = "select 1 from item_supplier where item_code = '$item_code' and supplier_code = '$ledger_number'";
	$reschk =  mysql_query($check,$connection);
	$num = mysql_num_rows($reschk);
	if($num == 0){
		  
		$query = "insert into item_supplier (item_code,supplier_code) values ('$item_code','$ledger_number')";
		$res = mysql_query($query,$connection);
	}
	echo "[BRK]";
	draw_itemSupplier('',$ledger_number, $connection);
	exit;
}
else if($mainmode=='' && $actionmode =='updateitem' && $itemSupplier_id != ''){
	$check = "select 1 from item_supplier where item_code = '$item_code' and supplier_code = '$ledger_number'";
	$reschk =  mysql_query($check,$connection);
	$num = mysql_num_rows($reschk);
	if($num == 0){
		$query = "update item_supplier set item_code = '$item_code' where itemSupplier_id = $itemSupplier_id";
		$res = mysql_query($query,$connection);
	}
	echo "[BRK]";
	draw_itemSupplier('',$ledger_number, $connection);
	exit;
}	
else if($mainmode=='' && $actionmode =='getItem' && $itemSupplier_id != ''){
	echo "[BRK]";
	draw_itemSupplier($itemSupplier_id,$ledger_number, $connection);
	exit;
}
else if($mainmode=='' && $actionmode =='deleteItem' && $itemSupplier_id != ''){
	$query = "delete from item_supplier where itemSupplier_id = $itemSupplier_id";
	$res = mysql_query($query,$connection);
	echo "[BRK]";
	draw_itemSupplier('',$ledger_number, $connection);
	exit;
}
else if($mainmode=='' && $actionmode =='loadItems' && $ledger_number != ''){
	$querysel = "select item_code from items where if('$group_code' != '', Group_code = '$group_code', 1=1) ";
	$ressel =  mysql_query($querysel,$connection);
	while ($rwsel = mysql_fetch_row($ressel)){
		$item_code = $rwsel[0];
		$check = "select 1 from item_supplier where item_code = '$item_code' and supplier_code = '$ledger_number'";
		$reschk =  mysql_query($check,$connection);
		$num = mysql_num_rows($reschk);
		if($num == 0){
			$query = "insert into item_supplier (item_code,supplier_code) values ('$item_code','$ledger_number')";
			$res = mysql_query($query,$connection);
		}
	}
	echo "[BRK]";
	draw_itemSupplier('',$ledger_number, $connection);
	exit;
}
if($mainmode=='insert' && $actionmode!='getledgergroup'){
	
	if($ledger_numberPrv==$ledger_number && $usedCode == ''){
		fbegin();
		$ledger_number=GenerateLedgerCode($connection,$supplier_groupcode,$SupplierFirstDigit,$NbrOfDigitInLedger,$module_ledgergrp);
		fcommit();
	}

	
	
	
	$query="Insert into suppliers
			(Ledger_number, Ledger_name, Ledger_Desc, Supplier_Representative,Supplier_Address, Supplier_Telephone1, Supplier_Telephone2, Supplier_Fax, 
			Supplier_Email, Supplier_Homepage, Supplier_CreditLimit,Supplier_PaymentTerms,Area_Code, Currency_Code,Supplier_VatNo, Supplier_Approved,
			Supplier_Address2, Supplier_city, Supplier_State,Supplier_country,Project_Code, 
			Costcenter_Code,Supplier_Group,SupplierGrp_Code,Supplier_Tax1,Supplier_Tax2,
			Supplier_Tax3,Supplier_Tax4,Supplier_Tax5,Supplier_BelongsToGroup,Supplier_VAtDate,
			Supplier_VAT,supplier_MotherCompany,Supplier_CompOwnerTitle,Supplier_CompOwner,Supplier_ReferedByTitle,
			Supplier_ReferedBy,Supplier_GCINO,Supplier_BusinessSector,Supplier_BusinessSectorDesc,
			Supplier_BType2,Supplier_BType3,Supplier_SubContractor,Supplier_CreationDate,Supplier_Export, Ledger_nameF, Supplier_Address3, Owner_Branch, paymentcondition,
			Supplier_BankName, Supplier_BankAddress, Supplier_AccountNbr, Supplier_SortCode, Supplier_IBANNo, Supplier_SwiftCode)
	
	values ('".$ledger_number."','".$ledger_name."','".$ledger_description."','".$representative."','".$Address."','".$phone1."',
			'".$phone2."','".$fax."','".$email."','".$homepage."','".$credit_limit."',$supplier_paymentterm,$area_code,
			'".$currency_code."','".$supplier_vatno."',$supplier_approved,'".$Address2."','".$city_supplier."','".$state."',
			'".$country."',$project_code,$costcenter_code,'".$supplier_group."','".$supplier_groupcode."',
			'".$supplier_tax1."','".$supplier_tax2."','".$supplier_tax3."','".$supplier_tax4."','".$supplier_tax5."',
			$belongstogroup,$vat_date,$supplier_vat,'".$supplier_mothercompany."','".$title_owner."',
			'".$supplier_companyowner."','".$title_refered."','".$supplier_referedby."',$supplier_gcino,
			'".$supplier_businesssector."','".$supplier_businesssectordesc."','".$supplier_btype2."','".$supplier_btype3."',
			$supplier_sub,now(),'".$localexport."', '".$ledger_namef."', '".$Address3."', '".$Setup_Branch."', ".$paymentcondition.",".
			" ".GetSQLValueString($supBankName,'text').", ".GetSQLValueString($supBankAddr,'text').", ".GetSQLValueString($supAccNbr,'text').", ".GetSQLValueString($supSortCode,'text').",
			".GetSQLValueString($supIBAN,'text').", ".GetSQLValueString($supSwift,'text')."	)";

//echo $query;exit;

	$result = mysql_query($query, $connection);
	
	if ($result)
	{
	 
	 
		   
		 $queryledger="select supplier_id from suppliers where ledger_number = '$ledger_number'";
		 
		 
			 $resultledger=mysql_query($queryledger,$connection) or die(mysql_error());
			 
			 
				while($rowledger=mysql_fetch_array($resultledger)){
				extract($rowledger);
				}
					
		//by rod
				// send notification
		$queryacc = "SELECT user_code, ifnull(nbrofdays,0), ifnull(view_access,0) FROM ".$_SESSION['dbLabel'].".notification_access
												where notification_name='supplier_created' and ifnull(view_access,0) = 1 ";
		$resacc = mysql_query($queryacc, $connection);
		
	
									
		//echo $queryacc;
		while ($rwacc = mysql_fetch_array($resacc)){
		$querynot = "call sendNotification('supplier_created', '$rwacc[0]', $rwacc[1], $rwacc[2], $supplier_id, '" .$_SESSION['useraccount'] ."') ";
		 // echo $querynot;exit;
										
		$resnot = mysql_query($querynot, $connection);
		
			//echo $querynot;exit;
		}		
		
			
// end send notification	
				// end by rod
	}
	$message    = mysql_error();
	
	if($message == '' && $supplier_access == '1'){
		$queryChk = "select 1 from ".$_SESSION['dbLabel'].".users where User_account = '".$ledger_number."' and upper(concat(defaultComp,'_',defaultyear)) = upper('".$_SESSION['dbName']."') ;";
		$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);
			
			if($res){
				 
				
				  
				
				$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)
								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, 1 );";
				$reslogin = mysql_query($query_login,$connection);
				
				
				
			
			}
			
			
		}
		
		
	}
	if($config['log_file'] == 1){
		$query_history="Insert into suppliers_history
			(Ledger_number, Ledger_name, Ledger_Desc, Supplier_Representative,Supplier_Address, Supplier_Telephone1, Supplier_Telephone2, Supplier_Fax, 
			Supplier_Email, Supplier_Homepage, Supplier_CreditLimit,Supplier_PaymentTerms,Area_Code, Currency_Code,Supplier_VatNo, Supplier_Approved,
			Supplier_Address2, Supplier_city, Supplier_State,Supplier_country,Project_Code, 
			Costcenter_Code,Supplier_Group,SupplierGrp_Code,Supplier_Tax1,Supplier_Tax2,
			Supplier_Tax3,Supplier_Tax4,Supplier_Tax5,Supplier_BelongsToGroup,Supplier_VAtDate,
			Supplier_VAT,supplier_MotherCompany,Supplier_CompOwnerTitle,Supplier_CompOwner,Supplier_ReferedByTitle,
			Supplier_ReferedBy,Supplier_GCINO,Supplier_BusinessSector,Supplier_BusinessSectorDesc,
			Supplier_BType2,Supplier_BType3,Supplier_SubContractor,Supplier_CreationDate,Supplier_Export, Ledger_nameF, Supplier_Address3, Owner_Branch,
			Supplier_date, Supplier_operator_id, Supplier_schema, Supplier_status)
	
	values ('".$ledger_number."','".$ledger_name."','".$ledger_description."','".$representative."','".$Address."','".$phone1."',
			'".$phone2."','".$fax."','".$email."','".$homepage."','".$credit_limit."',$supplier_paymentterm,$area_code,
			'".$currency_code."','".$supplier_vatno."',$supplier_approved,'".$Address2."','".$city_supplier."','".$state."',
			'".$country."',$project_code,$costcenter_code,'".$supplier_group."','".$supplier_groupcode."',
			'".$supplier_tax1."','".$supplier_tax2."','".$supplier_tax3."','".$supplier_tax4."','".$supplier_tax5."',
			$belongstogroup,$vat_date,$supplier_vat,'".$supplier_mothercompany."','".$title_owner."',
			'".$supplier_companyowner."','".$title_refered."','".$supplier_referedby."',$supplier_gcino,
			'".$supplier_businesssector."','".$supplier_businesssectordesc."','".$supplier_btype2."','".$supplier_btype3."',
			$supplier_sub,now(),'".$localexport."', '".$ledger_namef."', '".$Address3."', '".$Setup_Branch."',
			now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history = mysql_query($query_history, $connection_history);
		$message    = mysql_error().$query_history;
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','SUPPLIERS','',$ledger_number,$Setup_Branch);
		}
	}

	//// Default Currency for Client and Journal

	$query_setup="select Supplier_DefaultCur,Journal_DefaultCur from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_defaultcur=mysql_fetch_row($result_setup);
		
	$supplier_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_desc,Ledger_Deactivate, Owner_Branch)
				VALUES ('".$ledger_number."','".$ledger_name."',	'0',
						'".$currency_code."',$belongstogroup,'2',
						$project_code,$costcenter_code,'".$ledger_description."','0', '".$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_desc,Ledger_Deactivate, Owner_Branch,Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
				VALUES ('".$ledger_number."','".$ledger_name."',	'0',
						'".$currency_code."',$belongstogroup,'2',
						$project_code,$costcenter_code,'".$ledger_description."','0', '".$Setup_Branch."',
			now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$query_result_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_firstsequence=substr($ledger_number,0,4); 
		$ledger_sequence=substr($ledger_number,4);
			
		$query_acctypesetup="select ledger_number,ledger_activity from acctypesetup ,acctype
							where acctypesetup.type_code=acctype.acctype_code
							and acctype.acctype_ledger=".$ledger_firstsequence;
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
	
	
	while($row_ledgers=mysql_fetch_row($result_accsetup)){
									
		$ledgerpart=$row_ledgers[0];
		$ledger_acc=$ledgerpart.''.$ledger_sequence;
		$ledger_activity=$row_ledgers[1];
		
		///insert into ledger all details 									
		$query_ledgerdetail="insert into ledger (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,
											ledger_type,Project_Code,CostCenter_code,Ledger_Deactivate,Ledger_desc,ledger_activity, Owner_Branch)

						VALUES ('".$ledger_acc."','".$ledger_name."','0','".$currency_code."',
								$belongstogroup,'0',$project_code,$costcenter_code,'0',
								'".$ledger_description."',$ledger_activity, '".$Setup_Branch."')";			
		$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_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,'0',
								'".$ledger_description."',$ledger_activity, '".$Setup_Branch."',
			now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$query_ledgerdetail_result_history = mysql_query($query_ledgerdetail_history, $connection_history);
		$message    = mysql_error();
	}
	
		if ($result_ledgerdetail) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','LEDGER','',$ledger_acc,$Setup_Branch);
			}
		}
	}
}

if($mainmode=='edit'){

$query_update="update suppliers set
			ledger_number='".$ledger_number."',
			Ledger_name='".$ledger_name."', 
			Ledger_nameF='".$ledger_namef."', 
			Ledger_Desc='".$ledger_description."',
			Supplier_Representative='".$representative."',
			 Supplier_Address='".$Address."',
			 Supplier_Telephone1='".$phone1."', 
			 Supplier_Telephone2='".$phone2."', 
			 Supplier_Fax='".$fax."',
			 Supplier_Email='".$email."',
			 Supplier_Homepage='".$homepage."', 
			 Supplier_CreditLimit='".$credit_limit."',
			 Supplier_PaymentTerms='".$supplier_paymentterm."',
			 Area_Code=$area_code,
			 Currency_Code='".$currency_code."',
			 Supplier_VatNo='".$supplier_vatno."', 
			 Supplier_Approved=$supplier_approved,
			 Supplier_Address2='".$Address2."', 
			 Supplier_Address3='".$Address3."', 
			 Supplier_city='".$city_supplier."', 
			 Supplier_State='".$state."',
			 Supplier_country='".$country."',
			 Project_Code=$project_code, 
			 Costcenter_Code=$costcenter_code,
			 Supplier_Group='".$supplier_group."',
			 SupplierGrp_Code='".$supplier_groupcode."',
			 Supplier_Tax1='".$supplier_tax1."',
			 Supplier_Tax2='".$supplier_tax2."',
			 Supplier_Tax3='".$supplier_tax3."',
			 Supplier_Tax4='".$supplier_tax4."',
			 Supplier_Tax5='".$supplier_tax5."',
			supplier_MotherCompany='".$supplier_mothercompany."',
			 Supplier_VAtDate=$vat_date,
			  Supplier_VAT=$supplier_vat,
			#supplier_MotherCompany='".$supplier_mothercompany."',
			 Supplier_CompOwnerTitle='".$title_owner."',
			 Supplier_CompOwner='".$supplier_companyowner."',
			 Supplier_ReferedByTitle='".$title_refered."',
			 Supplier_ReferedBy='".$supplier_referedby."',
			 Supplier_GCINO=$supplier_gcino,
			 Supplier_BusinessSector='".$supplier_businesssector."',
			 Supplier_BusinessSectorDesc='".$supplier_businesssectordesc."',
			 Supplier_BType2='".$supplier_btype2."',
			 Supplier_BType3='".$supplier_btype3."',
			 Supplier_SubContractor=$supplier_sub,
			 Supplier_Export='".$localexport."',
			paymentcondition = ".$paymentcondition.",
			Supplier_BankName =  ".GetSQLValueString($supBankName,'text').", 
			Supplier_BankAddress = ".GetSQLValueString($supBankAddr,'text').", 
			Supplier_AccountNbr = ".GetSQLValueString($supAccNbr,'text').", 
			Supplier_SortCode = ".GetSQLValueString($supSortCode,'text').",
			Supplier_IBANNo = ".GetSQLValueString($supIBAN,'text').", 
			Supplier_SwiftCode = ".GetSQLValueString($supSwift,'text')."	
		where ledger_number='$ledger_numberPrv'";  
	//echo$query_update;exit;
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	if($message == '' && $supplier_access == '1'){
		$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);
		}
		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)
								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, 1 );";//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);
			}	
				
		}
		
	}
	if($config['log_file'] == 1){
		$query_supplier_history="insert into suppliers_history
		(ledger_number,Ledger_name, 
			Ledger_nameF, Ledger_Desc,Supplier_Representative, Supplier_Address,
			 Supplier_Telephone1, Supplier_Telephone2, Supplier_Fax,Supplier_Email,Supplier_Homepage, 
			Supplier_CreditLimit, Supplier_PaymentTerms, Area_Code,Currency_Code,Supplier_VatNo, 
			Supplier_Approved,Supplier_Address2, Supplier_Address3, Supplier_city, Supplier_State,Supplier_country,
			 Project_Code, Costcenter_Code, Supplier_Group, SupplierGrp_Code,
			 Supplier_Tax1,Supplier_Tax2, Supplier_Tax3, Supplier_Tax4, Supplier_Tax5,
			supplier_MotherCompany, Supplier_VAtDate, Supplier_VAT,
			 Supplier_CompOwnerTitle,Supplier_CompOwner, Supplier_ReferedByTitle,
			 Supplier_ReferedBy,Supplier_GCINO, Supplier_BusinessSector,
			 Supplier_BusinessSectorDesc,Supplier_BType2, Supplier_BType3,Supplier_SubContractor,
			 Supplier_Export,Supplier_date, Supplier_operator_id, Supplier_schema, Supplier_status)
			 values('".$ledger_number."','".$ledger_name."','".$ledger_namef."','".$ledger_description."','".$representative."','".$Address."',
			 '".$phone1."', '".$phone2."', '".$fax."','".$email."','".$homepage."','".$credit_limit."',
			 '".$supplier_paymentterm."',$area_code,'".$currency_code."','".$supplier_vatno."', $supplier_approved ,'".$Address2."','".$Address3."',
			'".$city_supplier."', 
			'".$state."', '".$country."',$project_code, $costcenter_code,'".$supplier_group."','".$supplier_groupcode."','".$supplier_tax1."',
			'".$supplier_tax2."','".$supplier_tax3."','".$supplier_tax4."','".$supplier_tax5."','".$supplier_mothercompany."',$vat_date,
			$supplier_vat,'".$title_owner."','".$supplier_companyowner."','".$title_refered."',
			'".$supplier_referedby."',$supplier_gcino,
			'".$supplier_businesssector."',
			 '".$supplier_businesssectordesc."',
			 '".$supplier_btype2."',
			 '".$supplier_btype3."',
			 $supplier_sub,
			 '".$localexport."',
			
			now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$query_supplier_result_history = mysql_query($query_supplier_history, $connection_history) ;
		$message    = mysql_error().$query_supplier_history;
	}

	if ($resultedit) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SUPPLIERS','',$ledger_numberPrv,$Setup_Branch);
		}
	}
	
	$query_update="update ledger set 
								ledger_number='".$ledger_number."',
								ledger_name='".$ledger_name."',	
								Project_Code=$project_code,
								CostCenter_code=$costcenter_code,
								Ledger_desc='".$ledger_description."',
								Currency_Code='".$currency_code."'
								where ledger_number='$ledger_numberPrv'";
		$result_ledgerupdate=mysql_query($query_update,$connection);
		$message    = mysql_error();
		if($config['log_file'] == 1){
			
			$query_update_ledger="insert into ledger_history (ledger_number,
								ledger_name,	Project_Code,CostCenter_code,Ledger_desc,ledger_date,operator_id,ledger_schema,ledger_status)
				VALUES ('".$ledger_number."','".$ledger_name."',$project_code,
								 '$costcenter_code',
								'".$ledger_description."', '$ledger_numberPrv',
			now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$query_update_ledgerresult = mysql_query($query_update_ledger, $connection);
		$message    = mysql_error();
		}
		
//echo $query_update;
 //exit;
	if ($result_ledgerupdate) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','LEDGER','',$ledger_numberPrv,$Setup_Branch);
		}
	}	
 
	if($ledger_number==$ledger_numberPrv){
		$ledger_firstsequence=substr($ledger_number,0,4); 
		$ledger_sequence=substr($ledger_number,4);

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

		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
			$ledger_activity=$row_ledgers[1];
			//echo$ledger_acc."<br>";
			$update_ledgerdetail="update ledger set 
								ledger_name='".$ledger_name."',	
								Project_Code=$project_code,
								CostCenter_code=$costcenter_code,
								Ledger_desc='".$ledger_description."',
								ledger_activity=$ledger_activity,
								Currency_Code='".$currency_code."'
								where ledger_number=$ledger_acc";
			//	echo$update_ledgerdetail;
			$resultupdate=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_desc,ledger_activity,Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
							VALUES ('".$ledger_name."',$project_code, '$costcenter_code','".$ledger_description."', '$ledger_activity',
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
		$update_ledgerdetail_history_result = mysql_query($update_ledgerdetail_history, $connection_history);
		$message    = mysql_error();
		}
			
			if ($resultupdate) {
				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);
		
			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();
		}
		
		
		$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_desc,ledger_activity)
						VALUES ('".$ledger_acc."','".$ledger_name."',	'0','".$journal_cur."',$belongstogroup,'0',$project_code,$costcenter_code,'".$ledger_description."',$ledger_activity)";			
		//	echo $query_ledgerdetail;								
		$result_ledgerdetail=mysql_query($query_ledgerdetail,$connection);
		
		if($config['log_file'] == 1){
			
			$query_ledgerdetail_history="insert into ledger_history (ledger_number,ledger_name,Ledger_grpdet,Currency_code,Ledger_BelongsToGroup,ledger_type,Project_Code,CostCenter_code,
						Ledger_desc,ledger_activity,Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
						VALUES ('".$ledger_acc."','".$ledger_name."',	'0','".$journal_cur."',$belongstogroup,'0',$project_code,$costcenter_code,'".$ledger_description."',$ledger_activity,
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_ledgerdetail_history = mysql_query($query_ledgerdetail_history, $connection_history);
		$message    = mysql_error();
		}
			
		
		if ($query_ledgerdetail) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','LEDGER','',$ledger_acc,$Setup_Branch);
			}
		}
	}
	}
}	

if($mainmode=='delete'){
	
	
	$deleteledger = "delete from ledger where ledger_number='$ledgernumber'";
	
	
	
			$query="select ledger_number,ledger_name from ledger where ledger_number='$ledgernumber'";
			
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$ledger_number=$rows[0];
	$ledger_name=$rows[1];
			
		if($config['log_file'] == 1){	$deleteledger_history="insert into ledger_history (ledger_number,ledger_name,
			Ledger_Date, Ledger_operator, Ledger_schema, Ledger_status)
						VALUES ('".$ledger_number."','".$ledger_name."',
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$result_history = mysql_query($deleteledger_history, $connection_history);
		$message    = mysql_error();
		}
		
	$result=mysql_query($deleteledger);
	
	$errornum= mysql_errno();
						
	if($errornum=='1451') {
		$message="Delete Not Allowed . Ledger Number is used .";
		//echo $message;
	}
										
	else {
		if ($deleteledger) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','LEDGER','',$ledgernumber,$Setup_Branch);
			}
		}
			
		$ledger_firstsequence=substr($ledgernumber,0,4); 
		$ledger_sequence=substr($ledgernumber,4);

		$query_acctypesetup="select ledger_number from acctypesetup ,acctype
							where acctypesetup.type_code=acctype.acctype_code
							and acctype.acctype_ledger=".$ledger_firstsequence;
										
		$result_accsetup=mysql_query($query_acctypesetup,$connection);
		$message    = mysql_error();
		
		while($row_ledgers=mysql_fetch_row($result_accsetup)){
			$ledgerpart=$row_ledgers[0];
			$ledger_acc=$ledgerpart.''.$ledger_sequence;
		
			$delete_ledger="delete from ledger where ledger_number=".$ledger_acc;		
		
			$query="select ledger_number,ledger_name,Ledger_desc from ledger where ledger_number=".$ledger_acc;
							
					$result=mysql_query($query,$connection);
					$rows=mysql_fetch_row($result);
					$ledger_number=$rows[0];
					$ledger_name=$rows[1];
					$Ledger_desc=$rows[2];
							
						if($config['log_file'] == 1){	$delete_ledger_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')";
						$resultupdate_history = mysql_query($delete_ledger_history, $connection_history);
						$message    = mysql_error();
		}
			
			$resultupdate=mysql_query($delete_ledger,$connection);
			$message    = mysql_error();
			
			if ($delete_ledger) {
				if ($Module_Synchro == 1) {
					InsertSynchroLoc($connection,'D','LEDGER','',$ledger_acc,$Setup_Branch);
				}
			}
			
		}

		$deletesupplier = "delete from suppliers where ledger_number='$ledgernumber'";
		
			$query="select ledger_number,ledger_name,Ledger_Desc from suppliers where ledger_number='$ledgernumber'";
							
					$result=mysql_query($query,$connection);
					$rows=mysql_fetch_row($result);
					$ledger_number=$rows[0];
					$ledger_name=$rows[1];
					$Ledger_Desc=$rows[2];
							if($config['log_file'] == 1){
							
							$delete_ledger_history="insert into suppliers_history (ledger_number,ledger_name,Ledger_Desc,
							Supplier_date, Supplier_operator_id, Supplier_schema, Supplier_status)
										VALUES ('".$ledger_number."','".$ledger_name."','".$Ledger_Desc."',
											now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
						$resultupdate_history = mysql_query($delete_ledger_history, $connection_history);
						$message    = mysql_error();
						}
			
		$result_delete=mysql_query($deletesupplier,$connection);
		$message    = mysql_error();
		
		if ($deletesupplier) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','SUPPLIERS','',$ledgernumber,$Setup_Branch);
			}
		}
	}
}

if (($actionmode=='submitcontact') && ($telephone_name!='')){

	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 ( Telephone_increment,TelephoneNo_ContactTitle,
										Telephone_name,telephone_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 ('".$telephone_increment."','".$telephoneno_title."','".$telephone_name."','".$telephone_lname."','".$telephone_no."',
										'".$telephone_no2."','".$telephone_no3."','".$telephone_email."','".$telephone_reference."',
										'".$telephone_city1."','".$telephone_city2."','".$telephone_city3."',
											now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
						$result_history = mysql_query($query_history, $connection_history);
						$message    = mysql_error();
		}
		
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SUPPLIERS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}
else if($actionmode=='deletecontact'){
	$query = "select ledger_number from telephoneno where Telephone_increment='".$telephone_increment."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$ledger_number=$rows[0];
	
	$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,Telephone_increment,
										
										Telephone_date, Telephone_operator_id, Telephone_schema, Telephone_status)
										values ('".$ledger_number."','".$telephone_increment."'
											now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
						$result_history = mysql_query($query_history, $connection);
						$message    = mysql_error();
		}
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','SUPPLIERS','',$ledger_number,$Setup_Branch);
		}
	}
	
	$vTabFocus='Contact';
}


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


/*if($actionmode=='deletecontact' || $actionmode=='submitcontact')
header ("Location: ./Form_Suppliers.php?mainmode=editmode&ledger_number=$ledger_number&vTabFocus=$vTabFocus&txtKeyword=$txtKeyword&message=".$message);
 
 else 
header ("Location: ./Form_Suppliers.php?mainmode=&txtKeyword=$txtKeyword&message=".$message);
*/

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


mysql_close($connection);



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

HERE;


function GenerateLedgerCode($connection,$PLedgerGroup,$SupplierFirstDigit,$PLedgerDetailDigit,$PModuleGrp){
	

	$vErrorExist=1;
	
	while($vErrorExist==1){
		$now=date('Y-m-d H:i:s');
		
		if($PModuleGrp==0){
			$length_supplier_firstdigit=strlen($SupplierFirstDigit);
			$left_length=$PLedgerDetailDigit-$length_supplier_firstdigit;
			
			/// Supplier Increment
			$query_increment="select Supplier_Increment from sequence";
			$resultincrement=mysql_query($query_increment,$connection) or die(mysql_error());
			$row_increment=mysql_fetch_row($resultincrement);
			$supplier_increment=$row_increment[0];
				
			$supplier_increment=$supplier_increment+1;
			
			$query="update sequence set Supplier_Increment='".$supplier_increment."'";
			$result=mysql_query($query,$connection) or die(mysql_error());
			if($config['log_file'] == 1){

							
							$query_history="insert into sequence_history ( Supplier_Increment,
										
										Seq_date, Seq_operator_id, Seq_schema, seq_status)
										values ('".$supplier_increment."'
											now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
						$result_history = mysql_query($query_history, $connection_history);
		}
			
			
			$supplier_increment=str_pad($supplier_increment, $left_length, '0', STR_PAD_LEFT);
			$ledger_number=$SupplierFirstDigit.''.$supplier_increment;
		}
		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 into ledgergrp_history ( 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);
		}
			$ledgergrp_reference=str_pad($ledgergrp_reference, $left_length, '0', STR_PAD_LEFT);
			$ledger_number=$PLedgerGroup.$ledgergrp_reference;
			
		}
		
				
		$sub_query="select count(*) from suppliers 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;
			}	
			//
		//	echo$vErrorExist;
	}

	return $ledger_number;
}



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



