<?php session_name('CAKEPHP'); session_start();

 
//mysql_connect("localhost", "root", "");
mysql_connect("localhost", "compaint_paint", "Nlqg*%QatI!!");
$r =mysql_select_db("compaint_paint"); //your database

if(!$r)
{
  die('Error'.DB_error_msg($db));
}

 function GetFieldValue($table,$condition ,$field)
{
    	 $sql=mysqli_query($link, "select ".$field." from ".$table." where  $condition  " );
	    $r=mysqli_fetch_array($sql);
     	return ($r[$field]);
}
function RecordInsert($table, $dataArray)
{ 
 	 
  	  $sql="Insert into ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
			$sql.=" ".$key." = '".$val."'";
			else
			$sql.=", ".$key." = '".$val."'";			
			$i++;
			}
 	   echo "<br>*************INSERT***************************************************</br>"			;
	    echo $sql;
 	 $r=mysqli_query($link, $sql);
	return ($r); 
} 

//sets encoding to utf8
mysqli_query($link, "SET NAMES utf8");
                                        
 	    $SQL_client=mysqli_query($link, "SELECT * from csv_client_master_new   Where  id!=1" );
  		//$result = DB_query($sql,$db);
		if(!$SQL_client)
		{
		   die("Error".DB_error_msg($db));
		}
		
	 $i=2;
		while($myrow=mysqli_fetch_array($SQL_client))
		{
		
		$dataArray['client_code']='ACCOUNT-'.$i++;
		$dataArray['c_name']=$myrow['c_name'];
//		$dataArray['short_name']=$myrow['short_name'];
		$dataArray['Zone']=GetFieldValue('zone_master',"ZoneTitle='".$myrow['ledger_id']."'" ,'id')	;		 
		$dataArray['Site']=GetFieldValue('zone_master',"ZoneTitle='".$myrow['ledger_id']."'" ,'Site')	;		
			$dataArray['pre_address1']=$myrow['pre_address'];
	 
		$dataArray['tin_no']=$myrow['tinno'];
 		$dataArray['excies_no']=$myrow['eccno'];		
		     RecordInsert('clientmaster', $dataArray); 	 
			 $lastid=mysqli_insert_id($link);	 
				echo "<br>-------------------------------------------------------------<br>";		 
 			 $chkaccount=mysqli_query($link, "select * from accountledger where  strtolower(AccountName) ='".strtolower($myrow['c_name'])."' ");
//			 if(mysqli_num_rows($chkaccount)>0)
			 {			 
				echo "<br>**************************<br>";			 
			 
	 			echo $sql1= "insert into accountledger (vender_client,AccountName,UnderGroup,ledgertype,CompanyId) values('".$lastid."','".$myrow['c_name']."','111', 'client','1') ";
				 $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>";		 
		}
		
?>
