<?php session_name('CAKEPHP'); session_start();
$host = "localhost"; //database location
$user = "root"; //database username
$pass = ""; //database password
$db_name = "compaint_crm"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
$r=mysql_select_db($db_name);
if(!$r)
{
  die('Error'.DB_error_msg($db));
}
//sets encoding to utf8
mysqli_query($link, "SET NAMES utf8");
                                        
 	    $SQL_client=mysqli_query($link, "SELECT A.account_no,A.accountname , A.email1  ,B.bill_city , B.bill_code , B.bill_country,B.bill_state,B.bill_street from xpert_account A ,xpert_accountbillads B   Where  A.CompanyId='".$_SESSION['ACGCompanyId']."' and A.accountid=B.accountaddressid " );
	 //	$sql="Select * from clientmaster Where id ='".$q."'";
// echo "SELECT A.account_no,A.accountname , A.email1  ,B.bill_city , B.bill_code , B.bill_country,B.bill_state,B.bill_street from xpert_account A ,xpert_accountbillads B   Where  A.CompanyId='".$_SESSION['ACGCompanyId']."' and A.accountid=B.accountaddressid ";
 		//$result = DB_query($sql,$db);
		if(!$SQL_client)
		{
		   die("Error".DB_error_msg($db));
		}
		
	 
		while($myrow=mysqli_fetch_array($SQL_client))
		{
		
		//  echo $myrow['account_no'].'<br>';
		$link = mysql_connect('localhost', 'root', $pass);
		$r=mysql_select_db('db_mxerp');
		
        
		echo "<br>-------------------------------------------------------------<br>";
 	 	    echo 	$sql= "insert into clientmaster (client_code,c_name,pre_address1,pre_address2,pre_address3,per_email,CompanyId) values('".$myrow['account_no']."','".$myrow['accountname']."','".$myrow['bill_city']."','".$myrow['bill_code']."','".$myrow['bill_country']."','".$myrow['email1']."','".$_SESSION['ACGCompanyId']."') ";
			  $r2=mysqli_query($link, $sql );
			 if(!$r2)
			 die('error'.DB_error_msg($db));
			 $lastid=mysqli_insert_id($link);
		echo "<br>**************************<br>";
			 
			echo 	$sql1= "insert into accountledger (vender_client,AccountName,UnderGroup,ledgertype,CompanyId) values('".$lastid."','".$myrow['accountname']."','110', 'client','".$_SESSION['ACGCompanyId']."') ";
				
				 $r1=mysqli_query($link, $sql1);
				 	 $lastid_lid=mysqli_insert_id($link);
				 if(!$r1)
			 die('error'.DB_error_msg($db)); 
			 
			 
			 	echo 	$sql2= "update   clientmaster set ledger_id='".$lastid_lid."' where id='".$lastid."'";
 				 $r2=mysqli_query($link, $sql2);
			  
 		echo "<br>-------------------------------------------------------------<br>";
		 
		}
		
?>
