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

 

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   Where  1=1" );
  		//$result = DB_query($sql,$db);
		if(!$SQL_client)
		{
		   die("Error".DB_error_msg($db));
		}
		
	 
		while($myrow=mysqli_fetch_array($SQL_client))
		{
		
		$dataArray['client_code']='ACCOUNT-'.$myrow['id'];
		$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['salesman_id']=$myrow['salesman_id'];
		
		$dataArray['ccategory']=$myrow['ccategory'];
		$dataArray['pre_address1']=$myrow['pre_address1'];
	//	$dataArray['pre_address2']=$myrow['pre_address2'];
	//	$dataArray['pre_address3']=$myrow['pre_address3'];
		//$dataArray['pre_area']=$myrow['pre_area'];
	//	$dataArray['fax']=$myrow['fax'];
		$dataArray['pre_mobile']=$myrow['pre_mobile'];
		$dataArray['pre_state']=GetFieldValue('state_master',"lower(state)='".strtolower($myrow['pre_state'])."'" ,'id')	 ;
		$dataArray['pre_city']=GetFieldValue('city_master',"lower(city)='".strtolower($myrow['pre_city'])."'" ,'id')	;;;
		$dataArray['pre_country']=GetFieldValue('country_master',"lower(country)='".strtolower($myrow['pre_country'])."'" ,'id')	;;
		$dataArray['pre_pincode']=$myrow['pre_pincode'];
/*		$dataArray['ccategory']=$myrow['con_person_1'];
		$dataArray['ccategory']=$myrow['con_per_des_1'];
		$dataArray['ccategory']=$myrow['con_person_2'];
		$dataArray['ccategory']=$myrow['con_per_des_2'];
		$dataArray['ccategory']=$myrow['gov_no'];
		$dataArray['ccategory']=$myrow['gov_date'];*/
 		$dataArray['per_email']=$myrow['per_email'];
		//$dataArray['lst_no']=$myrow['lst_no'];
 		$dataArray['lst_date']=$myrow['lst_date'];
		//$dataArray['ccategory']=$myrow['pf_no'];
		$dataArray['pf_date']=$myrow['pf_date'];
	//	$dataArray['ccategory']=$myrow['cst_date'];
		$dataArray['excies_no']=$myrow['esi_no'];
		/*
$dataArray['ccategory']=$myrow['esi_date'];
$dataArray['ccategory']=$myrow['excies_no'];
$dataArray['ccategory']=$myrow['excies_date'];
$dataArray['ccategory']=$myrow['reference'];
$dataArray['ccategory']=$myrow['company_req_date'];
$dataArray['ccategory']=$myrow['termination_date'];
$dataArray['ccategory']=$myrow['pc_tds'];
$dataArray['ccategory']=$myrow['remarks'];
*/
$dataArray['pan_no']=$myrow['pan_no'];	
$dataArray['Extension']=$myrow['Extension'];	
$dataArray['pre_phone1']=$myrow['pre_phone1'];
$dataArray['pre_phone2']=$myrow['pre_phone2'];		
$dataArray['company_req_date']=$myrow['company_req_date'];
$dataArray['termination_date']=$myrow['termination_date'];
		
		
		  RecordInsert('clientmaster', $dataArray);
 	 
			 $lastid=mysqli_insert_id($link);
		 
		  $dataArray_contact['master_code']= $lastid;
		 $dataArray_contact['person']=$myrow['con_person_1'];
		$dataArray_contact['designation']=$myrow['con_per_des_1'];
		RecordInsert('contact_information_master', $dataArray_contact);

		
		  $dataArray_contact2['master_code']= $lastid;
		$dataArray_contact2['person']=$myrow['con_person_2'];
		$dataArray_contact2['designation']=$myrow['con_per_des_2'];
		
        RecordInsert('contact_information_master', $dataArray_contact2);
			
			 
			 
		echo "<br>**************************<br>";
			 
			echo 	$sql1= "insert into accountledger (vender_client,AccountName,UnderGroup,ledgertype,CompanyId) values('".$lastid."','".$myrow['c_name']."','110', '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>";
		 
		}
		
?>
