<?php 
include "db_config.php";

			 

	    class Lead {

	 
	    	public $table_name = "leads_crm";
	        public $db;

	 

	        public function __construct(){

	            $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
	 

	            if(mysqli_connect_errno()) {

	                echo "Error: Could not connect to database.";

	                    exit;

	            }

	        }

	        /*** for registration process ***/
	        public function dashboard_leadclient($enquiry,$consultant,$agent,$applicant_cnic,$spouse_cnic,$marital_status,$spouse_name,$address,$spouse_edu,$spouse_occu,$no_of_kids,$office_phone,$res_phone,$dob_spouse,$spouse_work_chk,$spouse_work_exp,$academic,$sponsor_name,$sponsor_relation,$applied_before,$before_country,$before_visa_status,$sponsorship,$sponsor_city,$sponsor_type,$languages,$social,$social_other,$business_nature,$business_year,$business_location,$annual_turn_over,$stock_rs,$cash_rs,$property_rs,$ielts_listening,$tofel_listening,$pte_listening,$ielts_reading,$tofel_reading,$pte_reading,$ielts_writing,$tofel_writing,$pte_writing,$ielts_speaking,$tofel_speaking,$pte_speaking,$ielts_date,$tofel_date,$pte_date,$master_edu_1 ,$master_year_1 ,$master_uni_1 ,$master_grade_1 ,$master_edu_2 ,$master_year_2 ,$master_uni_2 ,$master_grade_2 ,$double_master ,$bachelor_edu_1 ,$bachelor_year_1 ,$bachelor_uni_1 ,$bachelor_grade_1 ,$bachelor_edu_2 ,$bachelor_year_2 ,$bachelor_uni_2 ,$bachelor_grade_2 ,$double_bachelor ,$pgd_edu ,$pgd_year ,$pgd_uni ,$pgd_grade ,$diploma_edu_1 ,$diploma_year_1 ,$diploma_uni_1 ,$diploma_grade_1 ,$diploma_edu_2 ,$diploma_year_2 ,$diploma_uni_2 ,$diploma_grade_2 ,$double_diploma ,$hsc_edu ,$hsc_year ,$hsc_uni ,$hsc_grade ,$other_edu ,$other_year ,$other_uni ,$other_grade,$to_1,$from_1,$job_title_1,$employer_1,$more_exp,$to_2,$from_2,$job_title_2,$employer_2,$to_3,$from_3,$job_title_3,$employer_3,$to_4,$from_4,$job_title_4,$employer_4,$to_5,$from_5,$job_title_5,$employer_5,$country_type,$country_other,$Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww,  $Lead_Behaviour, $newfilename,$Lead_Source,$duplicate_check,$message,$birthdate)
		{

			$Date_Added = $noww;
			$Lead_Status='1';
			$CV=$newfilename;
			$brth="";
			if($birthdate!="")
			{
				$brth = $birthdate;
			}

			$sql="SELECT * FROM leads_crm WHERE Lead_Id='$enquiry'";
			$result_check = mysqli_query($this->db,$sql);
            $row_chk=mysqli_fetch_array($result_check);
	        $count_row = $result_check->num_rows;

			if ($count_row == 0){

/*				$query="INSERT INTO leads_crm(Lead_ID,Destination,Email_Address,Home_Country,Mobile_No,Your_Occupation,Near_Office,Full_Name,User_Assign,Date_Added,CV,Lead_Status,Lead_Source,Status_Remarks,message,date_of_birth)
                                                    VALUES('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$User_Assign','$Date_Added','$CV','$Lead_Status','$Lead_Source','$Status_Remarks','$message','$brth')";
	               
				$result = mysqli_query($this->db,$query) or die(mysqli_connect_error()."Data cannot inserted"); 
				$sql11="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
				$result_check11 = mysqli_query($this->db,$sql11);
		                              $row_chk11=mysqli_fetch_array($result_check11);
*/
				return "Given Enquiry No not found";

			}
			else
			{
			    $sql_d="SELECT * FROM leads_detail WHERE Lead_Id='$enquiry'";
			    $result_check_d = mysqli_query($this->db,$sql_d);
			    $row_chk_d=mysqli_fetch_array($result_check_d);
				$count_row_d = $result_check_d->num_rows;
	    		if ($count_row_d == 0){
			        $sql_up = "INSERT INTO leads_detail VALUES ('','".$enquiry."','$consultant','$agent','$visa_type','$applicant_cnic','$spouse_cnic','$marital_status','$spouse_name','$address','$spouse_edu','$spouse_occu','$no_of_kids','$office_phone','$res_phone','$dob_spouse','$spouse_work_chk','$spouse_work_exp','$academic','$sponsor_name','$sponsor_relation','$applied_before','$before_country','$before_visa_status','$sponsorship','$sponsor_city','$sponsor_type','$languages','$social','$social_other','$business_nature','$business_year','$business_location','$annual_turn_over','$stock_rs','$cash_rs','$property_rs','$ielts_listening','$tofel_listening','$pte_listening','$ielts_reading','$tofel_reading','$pte_reading','$ielts_writing','$tofel_writing','$pte_writing','$ielts_speaking','$tofel_speaking','$pte_speaking','$ielts_date','$tofel_date','$pte_date','$master_edu_1','$master_year_1','$master_uni_1','$master_grade_1','$master_edu_2','$master_year_2','$master_uni_2','$master_grade_2','$double_master','$bachelor_edu_1','$bachelor_year_1','$bachelor_uni_1','$bachelor_grade_1','$bachelor_edu_2','$bachelor_year_2','$bachelor_uni_2','$bachelor_grade_2','$double_bachelor','$pgd_edu','$pgd_year','$pgd_uni','$pgd_grade','$diploma_edu_1','$diploma_year_1','$diploma_uni_1','$diploma_grade_1','$diploma_edu_2','$diploma_year_2','$diploma_uni_2','$diploma_grade_2','$double_diploma','$hsc_edu','$hsc_year','$hsc_uni','$hsc_grade','$other_edu','$other_year','$other_uni','$other_grade','$to_1','$from_1','$job_title_1','$employer_1','$more_exp','$to_2','$from_2','$job_title_2','$employer_2','$to_3','$from_3','$job_title_3','$employer_3','$to_4','$from_4','$job_title_4','$employer_4','$to_5','$from_5','$job_title_5','$employer_5','$country_type','$country_other','".date(Y-m-d)."','".date(Y-m-d)."')";
			        $result_up = mysqli_query($this->db,$sql_up);
					$sql_up1 = "UPDATE leads_crm SET Destination='$Destination',date_of_birth='$birthdate' WHERE Lead_Id='$enquiry'";
					$result_up1 = mysqli_query($this->db,$sql_up1);
				    return "Client Data Created/Updated, Ref ID#: ".$row_chk['Lead_Id']; 

                }
                else
                {
			        $sql_up = "UPDATE leads_detail SET visa_type='$Visa_Type',applicant_cnic='$applicant_cnic',spouse_cnic='$spouse_cnic',marital_status='$marital_status',spouse_name='$spouse_name',address='$address',spouse_edu='$spouse_edu',spouse_occupation='$spouse_occu',
					                                   no_of_kids='$no_of_kids',office_phone='$office_phone',res_phone='$res_phone',spouse_dob='$dob_spouse',spouse_work_exp_chk='$spouse_work_chk',
													   spouse_work_exp='$spouse_work_exp',academic='$academic',sponsor_name='$sponsor_name',sponsor_relation='$sponsor_relation',applied_before='$applied_before',
													   before_country='$before_country',before_visa_status='$before_visa_status',sponsorship='$sponsorship',sponsor_city='$sponsor_city',sponsor_type='$sponsor_type',
													   languages='$languages',social='$social',social_other='$social_other',business_nature='$business_nature',business_year='$business_year',business_location='$business_location',annual_turn_over='$annual_turn_over',
													   stock_rs='$stock_rs',cash_rs='$cash_rs',property_rs='$property_rs',ielts_listening='$ielts_listening',tofel_listening='$tofel_listening',pte_listening='$pte_listening',ielts_reading='$ielts_reading',
													   tofel_reading='$tofel_reading',pte_reading='$pte_reading',ielts_writing='$ielts_writing',tofel_writing='$tofel_writing',pte_writing='$pte_writing',ielts_speak='$ielts_speaking',
													   tofel_speak='$tofel_speaking',pte_speak='$pte_speaking',ielts_date='$ielts_date',tofel_date='$tofel_date',pte_date='$pte_date',master_edu_1='$master_edu_1',master_year_1='$master_year_1',
													   master_uni_1='$master_uni_1',master_grade_1='$master_grade_1',master_edu_2='$master_edu_2',master_year_2='$master_year_2',master_uni_2='$master_uni_2',master_grade_2='$master_grade_2',
													   double_master='$double_master',bachelor_edu_1='$bachelor_edu_1',bachelor_year_1='$bachelor_year_1',bachelor_uni_1='$bachelor_uni_1',bachelor_grade_1='$bachelor_grade_1',
													   bachelor_edu_2='$bachelor_edu_2',bachelor_year_2='$bachelor_year_2',bachelor_uni_2='$bachelor_uni_2',bachelor_grade_2='$bachelor_grade_2',double_bachelor='$double_bachelor',
													   pgd_edu='$pgd_edu',pgd_year='$pgd_year',pgd_uni='$pgd_uni',pgd_grade='$pgd_grade',diploma_edu_1='$diploma_edu_1',diploma_year_1='$diploma_year_1',diploma_uni_1='$diploma_uni_1',
													   diploma_grade_1='$diploma_grade_1',diploma_edu_2='$diploma_edu_2',diploma_year_2='$diploma_year_2',diploma_uni_2='$diploma_uni_2',diploma_grade_2='$diploma_grade_2',
													   double_diploma='$double_diploma',hsc_edu='$hsc_edu',hsc_year='$hsc_year',hsc_uni='$hsc_uni',hsc_grade='$hsc_grade',other_edu='$other_edu',other_year='$other_year',
													   other_uni='$other_uni',other_grade='$other_grade',to_1='$to_1',from_1='$from_1',job_title_1='$job_title_1',employer_1='$employer_1',more_exp='$more_exp',to_2='$to_2',
													   from_2='$from_2',job_title_2='$job_title_2',employer_2='$employer_2',to_3='$to_3',from_3='$from_3',job_title_3='$job_title_3',employer_3='$employer_3',to_4='$to_4',
													   from_4='$from_4',job_title_4='$job_title_4',employer_4='$employer_4',to_5='$to_5',from_5='$from_5',job_title_5='$job_title_5',employer_5='$employer_5',country_type='$country_type',country_other='$country_other',date_modified=now() WHERE Lead_Id='$enquiry'";
			        $result_up = mysqli_query($this->db,$sql_up);
					$sql_up1 = "UPDATE leads_crm SET Destination='$Destination',date_of_birth='$birthdate' WHERE Lead_Id='$enquiry'";
					$result_up1 = mysqli_query($this->db,$sql_up1);

			        return "Client Data Updated, Ref ID#: ".$row_chk['Lead_Id']; 
    
                }    
			}	
		}
	 

	        /*** for registration process ***/
	        public function dashboard_lead($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww,  $Lead_Behaviour, $newfilename,$Lead_Source,$duplicate_check,$message,$birthdate,$consultant_id)
		{

			$Date_Added = $noww;
			$Lead_Status='1';
			$CV=$newfilename;
			$brth="";
			if($birthdate!="")
			{
				$brth = $birthdate;
			}

			$sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
			$result_check = mysqli_query($this->db,$sql);
                        $row_chk=mysqli_fetch_array($result_check);
	                $count_row = $result_check->num_rows;

			if ($count_row == 0){

				$query="INSERT INTO leads_crm(Lead_ID,Destination,Email_Address,Home_Country,Mobile_No,Your_Occupation,Near_Office,Full_Name,Visa_Type,User_Assign,Date_Added,CV,Lead_Status,Lead_Source,Status_Remarks,message,date_of_birth)
                                                    VALUES('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','$User_Assign','$Date_Added','$CV','$Lead_Status','$Lead_Source','$Status_Remarks','$message','$brth')";
	               
				$result = mysqli_query($this->db,$query) or die(mysqli_connect_error()."Data cannot inserted"); 
				$sql11="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
				$result_check11 = mysqli_query($this->db,$sql11);
		                              $row_chk11=mysqli_fetch_array($result_check11);

				$qry = "INSERT INTO leads_detail(leads_detail_id,Lead_Id,consultant,date_created,date_modified) VALUES ('','".$row_chk11['Lead_Id']."','$consultant_id',now(),now())";
				$res_qry= mysqli_query($this->db,$qry);

				return "Client Registered Sucessfuly, Ref ID#: ".$row_chk11['Lead_Id'];

			}
			else
			{
				$sql_up = "UPDATE leads_crm SET Status_Remarks='Re-Submitted the Lead on ".date('d-m-Y g:i:s')."',message='$message' WHERE Lead_Id='".$row_chk['Lead_Id']."'";
				$result_up = mysqli_query($this->db,$sql_up);
				return "Client Already Registered, Ref ID#: ".$row_chk['Lead_Id']; 
			}	
		}


	        public function web_lead($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww,  $Lead_Behaviour, $newfilename,$Lead_Source,$duplicate_check,$message,$birthdate)
		{

			$Date_Added = $noww;
			$Lead_Status='1';
			$CV=$newfilename;
			$brth="";
			if($birthdate!="")
			{
				$brth = $birthdate;
			}

			$sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
			$result_check = mysqli_query($this->db,$sql);
                        $row_chk=mysqli_fetch_array($result_check);
	                $count_row = $result_check->num_rows;

			if ($count_row == 0){

				$query="INSERT INTO leads_crm(Lead_ID,Destination,Email_Address,Home_Country,Mobile_No,Your_Occupation,Near_Office,Full_Name,Visa_Type,User_Assign,Date_Added,CV,Lead_Status,Lead_Source,Status_Remarks,message,date_of_birth)
                                                    VALUES('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','$User_Assign','$Date_Added','$CV','$Lead_Status','$Lead_Source','$Status_Remarks','$message','$brth')";
	               
				$result = mysqli_query($this->db,$query) or die(mysqli_connect_error()."Data cannot inserted"); 
				//return $query;
				return "Thanks for showing interest in AINiT, one of our expert consultants will get back to you soon!";

			}
			else
			{
				$sql_up = "UPDATE leads_crm SET Status_Remarks='Re-Submitted the Lead on ".date('d-m-Y g:i:s')."',message='$message' WHERE Lead_Id='".$row_chk['Lead_Id']."'";
				$result_up = mysqli_query($this->db,$sql_up);
				return "Oops! it seems that you have not been contacted yet. A reminder has been sent to one of our consultant and you will be contacted soon! We apologize for the delay"; 
			}	
		}

	        public function add_lead($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww,  $Lead_Behaviour, $newfilename ){
	 
		  $Assigned_to='';
		   if($Visa_Type==1)
			$Assigned_to="207";


	            $sql="SELECT * FROM leads_crm WHERE Email_Address='%s' OR Mobile_No='%s'";
	            $sql2 = sprintf($sql, mysqli_real_escape_string($this->db, $Email_Address), mysqli_real_escape_string($this->db, $Mobile_No));

	            //checking if the email and mobile no is available in db

	            $check =  $this->db->query($sql2) ;

	            $count_row = $check->num_rows;


	            //if the lead is not in db then insert to the table
	            if ($count_row == 0){

			if($Assigned_to=="")
		                $sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`) VALUES ('','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
		        else
		                $sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`, `Assigned_to`) VALUES ('','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','$Assigned_to')";



	                $query = sprintf($sql1, mysqli_real_escape_string($this->db, $Destination), mysqli_real_escape_string($this->db, $Email_Address), mysqli_real_escape_string($this->db, $Home_Country), mysqli_real_escape_string($this->db, $Mobile_No), mysqli_real_escape_string($this->db, $Your_Occupation), mysqli_real_escape_string($this->db, $Near_Office), mysqli_real_escape_string($this->db, $Full_Name), mysqli_real_escape_string($this->db, $Visa_Type), mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, $noww), mysqli_real_escape_string($this->db, $newfilename), mysqli_real_escape_string($this->db, $UserID), mysqli_real_escape_string($this->db, 1), mysqli_real_escape_string($this->db, $Lead_Behaviour), mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, $UserID));

	                $result = mysqli_query($this->db,$query) or die(mysqli_connect_error()."Data cannot inserted");

	                return $result;
	        	}else{

	        		return false;
	        		echo 'error occurred';
	        	}

	        }

	        public function add_leadGeneral($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww,  $Lead_Behaviour, $newfilename, $leavemessage ){

	 

	            $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";

	            //checking if the email and mobile no is available in db

	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;


	            //if the lead is not in db then insert to the table

	            if ($count_row == 0){
	            	$sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`) VALUES ('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','$leavemessage','$noww', '$newfilename', '$UserID', 1, '$Lead_Behaviour', '', '', '', '', '$UserID')";

	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
	                return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}

	        }

	       

	        public function addCityState($lid, $city, $state){
	        	$sql = "INSERT INTO `ainitcrm_clientregions`(`id`, `lead_Id`, `city_name`, `state_name`) VALUES ('','$lid','$city','$state')";
	        	$result = mysqli_query($this->db, $sql);
	        	return $result;
	        }

		public function add_leadStaffDOB($DOB,$Email_Address,$Mobile_No)
		{
			$sql="UPDATE leads_crm SET date_of_birth='$DOB' WHERE Email_Address='$Email_Address' AND Mobile_No='$Mobile_No' ";
	                $result = mysqli_query($this->db,$sql) or die(mysqli_connect_error()."Data cannot updated DOB--".$sql);

		}
	        public function add_leadStaff($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww, $Lead_Behaviour, $newfilename ){


	            $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";


	            //checking if the email and mobile no is available in db

	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;


	            //if the lead is not in db then insert to the table

	            if ($count_row == 0){

	                $sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`) VALUES ('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','','$noww', '$newfilename', '$UserID', 1, '$Lead_Behaviour', '', '', '', '', '$UserID')";

	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
	                return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}

	        }

	        public function AddLeadNew($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $noww, $Lead_Behaviour, $newfilename, $CID ){


	            $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";

	            //checking if the email and mobile no is available in db

	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;


	            //if the lead is not in db then insert to the table

	            if ($count_row == 0){

	                $sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`, `Assigned_to`, `Assigned_Date`) VALUES ('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','','$noww', '$newfilename', '$UserID', 1, '$Lead_Behaviour', '', '', '', '', '$UserID', '$CID', '$noww')";

	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
	                return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}

	        }

            public function add_leads_by_import_csv($Destination, $Visa_Type, $Full_Name, $Mobile_No, $Email_Address,  $Your_Occupation, $Near_Office, $newdob, $UserID, $noww, $Lead_Behaviour ){

               $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
                //checking if the email and mobile no is available in db
                $check =  $this->db->query($sql) ;
                $count_row = $check->num_rows;
                //if the lead is not in db then insert to the table

                if ($count_row == 0){
                    $sql1 = "INSERT INTO `leads_crm`(`Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `Date_Added`, `Referral_ID`, `Lead_Source` , `date_of_birth`) 
                       VALUES ('$Destination','$Email_Address','$Near_Office','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','2','$noww','102','Facebook','$newdob')";

                    $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
                    return $result;
                }else{
                    return false;
                    echo 'error occurred';
                }

            }



	        public function CreateRefLead($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $rfid, $noww, $CVFile, $UserID, $LeadSource, $AssID, $leadnotes){
	        	$sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`, `Lead_Status`, `Lead_Source`, `Status_Reason`, `Status_Remarks`, `Referrence_LeadID`, `Status_Dated`, `Updatedby`, `Assigned_to`, `Assigned_Date`, `Refference_Remarks`) VALUES ('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','','$noww', '$CVFile', '$UserID', 1, '$LeadSource', '', '', '$rfid', '', '$UserID', '$AssID', '$noww', '$leadnotes')";

	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
	                return $result;	
	        }



	        public function existClient(){
	        	$sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
	            $check =  $this->db->query($sql) ;
	        	$row=mysqli_fetch_array($check);
	 				$existID = $row['Lead_Id'];
	 				echo '<a href="#">'.$existID.'</a>';
	        }

	         public function add_leadagent($Destination, $Email_Address, $Home_Country, $Mobile_No, $Your_Occupation, $Near_Office, $Full_Name, $Visa_Type, $UserID, $file_name){

	 

	            $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";

	            //checking if the email and mobile no is available in db

	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;

	 
	            //if the lead is not in db then insert to the table

	            if ($count_row == 0){

	                $sql1="INSERT INTO `leads_crm`(`Lead_Id`, `Destination`, `Email_Address`, `Home_Country`, `Mobile_No`, `Your_Occupation`, `Near_Office`, `Full_Name`, `Visa_Type`, `User_Assign`, `Date_Added`, `CV`, `Referral_ID`) VALUES ('','$Destination','$Email_Address','$Home_Country','$Mobile_No','$Your_Occupation','$Near_Office','$Full_Name','$Visa_Type','',now(), '$file_name', '$UserID')";

	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");
//	                $pid = mysqli_insert_id($result);
//	        	$newname = "$pid.docx";
//	move_uploaded_file( $_FILES['file_name']['tmp_name'], "client/$newname");

	                return $result;

	               
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}

	        }

	        public function leadagent($pID, $intake_date, $selectedOption, $selectedInst, $UserID){
				$sql = "INSERT INTO `ainitCRM_agentLeads`(`AID`, `Lead_Id`, `Intake_Date`, `Client_Courses`, `Client_Institute`, `Addedby`, `Date_Added`) VALUES ('','$pID','$intake_date','$selectedOption','$selectedInst','$UserID',now())";
				$result = mysqli_query($this->db, $sql);
				return $result;
			}


	        public function CreateLeadCycle($Lead_Id){
	        	$sql="SELECT * FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";


	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;

	            if ($count_row == 0){

	        	$sql = "INSERT INTO `ainitcrm_leadstatus`(`StatusID`, `Lead_Id`, `Eligibility_Checked`, `Lead_Validated`, `Quotation_Given`, `Case_Validated`, `Agreement_Signed`, `Payment_Confirmed`, `Case_Handed_Over`) VALUES ('','$Lead_Id','','','','','','','')";
	        	$result = mysqli_query($this->db, $sql);

	        	return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}

	        }

	 		public function CreateLeadCycleStudy($Lead_Id, $Visa_Type){
	 			$sql="SELECT * FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";


	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;

	            if ($count_row == 0){

	        	$sql = "INSERT INTO `ainitcrm_leadstatus`(`StatusID`, `Lead_Id`, `Eligibility_Checked`, `Lead_Validated`, `Quotation_Given`, `Case_Validated`, `Agreement_Signed`, `Payment_Confirmed`, `Case_Handed_Over`) VALUES ('','$Lead_Id','1','','','1','','','')";
	        	$result = mysqli_query($this->db, $sql);
	        	return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}
	 		}

	 		public function CreateLeadCycleBusiness($Lead_Id, $Visa_Type){
	 			$sql="SELECT * FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";


	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;

	            if ($count_row == 0){

	        	$sql = "INSERT INTO `ainitcrm_leadstatus`(`StatusID`, `Lead_Id`, `Eligibility_Checked`, `Lead_Validated`, `Quotation_Given`, `Case_Validated`, `Agreement_Signed`, `Payment_Confirmed`, `Case_Handed_Over`) VALUES ('','$Lead_Id','1','','','','','','')";
	        	$result = mysqli_query($this->db, $sql);
	        	return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}
	 		}

	 		public function CreateLeadCycleGermany($Lead_Id, $Visa_Type){
	 			$sql="SELECT * FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";


	            $check =  $this->db->query($sql) ;

	            $count_row = $check->num_rows;

	            if ($count_row == 0){

	        	$sql = "INSERT INTO `ainitcrm_leadstatus`(`StatusID`, `Lead_Id`, `Eligibility_Checked`, `Lead_Validated`, `Quotation_Given`, `Case_Validated`, `Agreement_Signed`, `Payment_Confirmed`, `Case_Handed_Over`) VALUES ('','$Lead_Id','1','','','1','','','')";
	        	$result = mysqli_query($this->db, $sql);
	        	return $result;
	        	}else{
	        		return false;
	        		echo 'error occurred';
	        	}
	 		}

	        

			
	        /*** for showing the username or fullname ***/

	        public function getLeads(){

	            $sql3="SELECT * FROM leads_crm" ;

	            $result = mysqli_query($this->db,$sql3);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                            </tr>';

		    }

	        }

	        public function getLeadsAgent($UserID){

	            $sqlAgent="SELECT * FROM leads_crm WHERE Referral_ID='$UserID' AND Lead_Status=1" ;

	            $resultAgent = mysqli_query($this->db,$sqlAgent);
	           return $resultAgent;
	        }
	        

	        

	        public function getLeadsAgentP($UserID){

	            $sqlAgent="SELECT * FROM leads_crm WHERE Referral_ID='$UserID' AND Lead_Status=8" ;

	            $resultAgent = mysqli_query($this->db,$sqlAgent);
	            return $resultAgent;

	        }
	        public function getLeadsAgentPC($UserID){

	            $sqlAgent="SELECT * FROM leads_crm WHERE Referral_ID='$UserID' AND Lead_Status=11" ;

	            $resultAgent = mysqli_query($this->db,$sqlAgent);
	            return $resultAgent;

	        }

	        public function getLeadsSales($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=1 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
	            //SELECT * FROM leads_crm WHERE Lead_Status='New' and Assigned_to='$UserID' OR Assigned_to='$Reports_to' LIMIT 10" ;


	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>

                                            </tr>';

		    }

	        }

	        public function getLeadsSalesE($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=6 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesC($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=7 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesQ($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=8 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesCVM($UserID, $Reports_to){
	        	$sql="SELECT * FROM leads_crm WHERE Lead_Status=9 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesAG($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=10 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesPC($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=11 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesParked($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=13 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function getLeadsSalesRecycle($UserID, $Reports_to){
	        	
	            $sql="SELECT * FROM leads_crm WHERE Lead_Status=14 AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') LIMIT 10" ;

	            $result = mysqli_query($this->db,$sql);
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td><a href="lead_details.php?Lead_Id='.$Lead_Id.'">'.$Full_Name.'</a></td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }

	        public function AssignedSearchLead($Email_Address){
	        	$sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' " ;

	            $result = mysqli_query($this->db,$sql);
	            return result;
	            $i=0;
	            while($row = mysqli_fetch_array($result)){
	            	$Lead_Id=$row['Lead_Id'];
	            $Destination= $row['Destination'];
	            $Email_Address= $row['Email_Address'];	
	            $Home_Country= $row['Home_Country'];
	            $Mobile_No= $row['Mobile_No'];
	            $Your_Occupation= $row['Your_Occupation'];
	            $Near_Office= $row['Near_Office'];
		        $Full_Name= $row['Full_Name'];
		        $Visa_Type= $row['Visa_Type'];
		        $Date_Added= $row['Date_Added'];
		        $User_Assign= $row['User_Assign'];
		        $CV= $row['CV'];
		        $Referral_ID= $row['Referral_ID'];
		        $i++;
		        echo $extract_leads ='<tr class="info">
                                                <td>'.$Lead_Id.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }

	        }


	        public function getCountry(){

	            $sql="SELECT * FROM ainitcrm_countrydestination" ;

	            $result = mysqli_query($this->db,$sql);
	            while($country_data = mysqli_fetch_array($result)){
	            $CountryID= $country_data['CountryID'];
	            $CountryName= $country_data['CountryName'];	
		        echo $extract_lead ='<option value="'.$CountryID.'">'.$CountryName.'</option>';
			    }

	        }
	        public function getCountryName($Destination){

	            $sql="SELECT * FROM ainitcrm_countrydestination WHERE CountryID='$Destination'" ;

	            $result = mysqli_query($this->db,$sql);
	            $country_data = mysqli_fetch_array($result);
	            $CountryID= $country_data['CountryID'];
	            echo $CountryName= $country_data['CountryName'];	
	        }

	 
function uploadPhoto(){
 
    $result_message="";
 
    // now, if image is not empty, try to upload the image
    if($this->file_name){
 
        // sha1_file() function is used to make a unique file name
        $target_directory = "client/";
        $target_file = $target_directory . $this->file_name;
        $file_type = pathinfo($target_file, PATHINFO_EXTENSION);
 
        // error message is empty
        $file_upload_error_messages="";

        // make sure that file is a real image
//$check = getimagesize($_FILES["file_name"]["tmp_name"]);
//if($check!==false){
    // submitted file is an image
//}else{
  // $file_upload_error_messages.="<div>Submitted file is not an image.</div>";
//}
 
// make sure certain file types are allowed
$allowed_file_types=array("doc", "docx", "pdf", "jpg", "jpeg", "png", "gif");
if(!in_array($file_type, $allowed_file_types)){
    $file_upload_error_messages.="<div>Only Doc, Docx, PDF, JPG, JPEG, PNG, GIF files are allowed.</div>";
}
 
// make sure file does not exist
if(file_exists($target_file)){
    $file_upload_error_messages.="<div>File already exists. Try to change file name.</div>";
}
 
// make sure submitted file is not too large, can't be larger than 1 MB
if($_FILES['file_name']['size'] > (1024000)){
    $file_upload_error_messages.="<div>File must be less than 1 MB in size.</div>";
}
 
// make sure the 'uploads' folder exists
// if not, create it
if(!is_dir($target_directory)){
    mkdir($target_directory, 0777, true);
}

// if $file_upload_error_messages is still empty
if(empty($file_upload_error_messages)){
    // it means there are no errors, so try to upload the file
    if(move_uploaded_file($_FILES["file_name"]["tmp_name"], $target_file)){
        // it means photo was uploaded
    }else{
        $result_message.="<div class='alert alert-danger'>";
            $result_message.="<div>Unable to upload photo.</div>";
            $result_message.="<div>Update the record to upload photo.</div>";
        $result_message.="</div>";
    }
}
 
// if $file_upload_error_messages is NOT empty
else{
    // it means there are some errors, so show them to user
    $result_message.="<div class='alert alert-danger'>";
        $result_message.="{$file_upload_error_messages}";
        $result_message.="<div>Update the record to upload photo.</div>";
    $result_message.="</div>";
}
 
    }
 
    return $result_message;
}

	public function AssignedLead($assign_to, $current_date, $noww, $Lead_Id){
		$sql5="UPDATE leads_crm SET Assigned_to = '$assign_to', Assigned_Date='$noww' WHERE Lead_Id IN $Lead_Id" ;
		$result5 = mysqli_query($this->db, $sql5) or die(mysqli_connect_error()."Lead can not assigned");
		if($result5){
		return $result5;
		}else{
			return false;
		}
	}

	public function DeleteLead($Lead_Id){
		$sql="DELETE FROM leads_crm WHERE Lead_Id IN $Lead_Id" ;
		$result = mysqli_query($this->db, $sql) or die(mysqli_connect_error()."Lead can not DELETED");
		if($result){
		return $result;
		}else{
			return false;
		}
	}

	public function GetLeadDetails($Lead_Id){
		$sql = "SELECT * FROM leads_crm WHERE Lead_Id=$Lead_Id";
		$result = mysqli_query($this->db, $sql);
		//$row = mysqli_fetch_array($result);
	//	$result = array();
  //      while ($record = mysql_fetch_array($result)) {
//        echo  $result[] = $record;
    //}
		return $result;
			//	$Lead_Id=$row['Lead_Id'];
            //  $Destination= $row['Destination'];
          //    $Email_Address= $row['Email_Address'];  
        //      $Home_Country= $row['Home_Country'];
      //        $Mobile_No= $row['Mobile_No'];
    //          $Your_Occupation= $row['Your_Occupation'];
  //            $Near_Office= $row['Near_Office'];
//            $Full_Name= $row['Full_Name'];
            //$Visa_Type= $row['Visa_Type'];
            //$Date_Added= $row['Date_Added'];
          //  $User_Assign= $row['User_Assign'];
        //    $CV= $row['CV'];
      //      $Referral_ID= $row['Referral_ID'];
		 
	}

	public function GetLeadDetailsIntegrated($Lead_Id){
		$sql = "SELECT lc.Consultant_Name, ld2.leads_detail_id2, ld.leads_detail_id, l.* FROM leads_crm l 
                                      LEFT JOIN leads_detail ld ON (ld.Lead_Id = l.Lead_Id) 
                                      LEFT JOIN leads_detail2 ld2 ON (ld2.Lead_Id = l.Lead_Id)
                                      LEFT JOIN leads_consultant lc ON (lc.id = ld.consultant)
                                      WHERE l.Lead_Id=$Lead_Id";
		$result = mysqli_query($this->db, $sql);
		//$row = mysqli_fetch_array($result);
	//	$result = array();
  //      while ($record = mysql_fetch_array($result)) {
//        echo  $result[] = $record;
    //}
		return $result;
			//	$Lead_Id=$row['Lead_Id'];
            //  $Destination= $row['Destination'];
          //    $Email_Address= $row['Email_Address'];  
        //      $Home_Country= $row['Home_Country'];
      //        $Mobile_No= $row['Mobile_No'];
    //          $Your_Occupation= $row['Your_Occupation'];
  //            $Near_Office= $row['Near_Office'];
//            $Full_Name= $row['Full_Name'];
            //$Visa_Type= $row['Visa_Type'];
            //$Date_Added= $row['Date_Added'];
          //  $User_Assign= $row['User_Assign'];
        //    $CV= $row['CV'];
      //      $Referral_ID= $row['Referral_ID'];
		 
	}

	public function addAppointment($app_name, $app_type, $destination, $consultant, $time, $datepicker, $UserID, $noww, $Lead_Id){
		$sql1="INSERT INTO appointment (`id`, `app_name`, `app_type`, `destination`, `consultant`, `time`, `date`, `visit`, `added`, `iqno`, `status_app`) VALUES(NULL,'$app_name','$app_type','$destination','$consultant','$time','$datepicker','$UserID', '$noww', '$Lead_Id', 'Not Completed')";
	                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_error()."Data cannot inserted");

	                return $result;

	               if($result){
  $response = 'Appointment has been Added Successfully!'; 
	        	}else{
	        		return false;
	        		$response = 'error occurred';
	        	}

	}

	public function FetchAppointments($Lead_Id){
		$sql = "SELECT * FROM appointment WHERE iqno=$Lead_Id";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function addFollowUps($Lead_Id, $UserID, $noww, $Narrative, $nxtCallDate, $reminder){
		if($reminder=="")
			$reminder=0;
		if($nxtCallDate=="")
			$sql="INSERT INTO `tbl_client_narratives`(`ClientID`, `Narrative_DateANDTime`, `UserID`, `Narrative`, `nxtCallDate`, `Reminder`) VALUES ('$Lead_Id', '$noww','$UserID','$Narrative', NULL, '$reminder')";
		else
			$sql="INSERT INTO `tbl_client_narratives`(`ClientID`, `Narrative_DateANDTime`, `UserID`, `Narrative`, `nxtCallDate`, `Reminder`) VALUES ('$Lead_Id', '$noww','$UserID','$Narrative', '$nxtCallDate', '$reminder')";
	
		$result = mysqli_query($this->db, $sql) or die(mysqli_connect_error()."Data cannot inserted");
	    return $result;
	}

	public function addFeedNarr($Feed_Id, $Narrative, $UserID, $noww){
		$sql="INSERT INTO `ainitcrm_feedbacknarratives`(`NID`, `FId`, `Narratives`, `UsrID`, `DateAdded`) VALUES ('','$Feed_Id','$Narrative','$UserID','$noww')";
		$result = mysqli_query($this->db,$sql) or die(mysqli_connect_error()."Data cannot inserted");
	    return $result;
	}

	public function FetchFollowups($Lead_Id){
		$sql = "SELECT * FROM tbl_client_narratives WHERE ClientID=$Lead_Id";
		$result = mysqli_query($this->db, $sql);
		return $result;	
	}

	public function FetchFeedbackDetails($Feed_Id){
		$sql = "SELECT * FROM ainitcrm_feedbacks WHERE FId=$Feed_Id";
		$result = mysqli_query($this->db, $sql);
		return $result;	
	}

	public function FetchFeedbackNarratives($Feed_Id){
		$sql = "SELECT * FROM ainitcrm_feedbacknarratives WHERE FId=$Feed_Id";
		$result = mysqli_query($this->db, $sql);
		return $result;	
	}

	public function FetchFollowupsAll($UserID, $Reports_to){
		$sql = "SELECT * FROM tbl_client_narratives as t1 INNER JOIN tbl_users as t2 ON t2.UserID=t1.UserID WHERE (t2.UserID='$UserID' OR t2.Reports_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;	
	}

	public function addEligibilityCheck($Lead_Id, $reqpt, $age, $agepoint, $eng, $engpoint, $skm, $skmpoint, $qual, $qualpoint, $asr, $asrpt, $seq, $seqpt, $fac1, $facpt1, $fac2, $facpt2, $fac3, $facpt3, $fac4, $facpt4, $nom1, $nompt1, $nom2, $nompt2, $tpoint, $UserID, $noww, $skmsp, $skmpointsp, $qualsp, $qualpointsp){
		$sql11 = "INSERT INTO `ainitcrm_auseligibility`(`inqno`, `reqpt`, `age`, `agepoint`, `eng`, `engpoint`, `skm`, `skmpoint`, `qual`, `qualpoint`, `asr`, `asrpt`, `seq`, `seqpt`, `fac1`, `facpt1`, `fac2`, `facpt2`, `fac3`, `facpt3`, `fac4`, `facpt4`, `nom1`, `nompt1`, `nom2`, `nompt2`, `tpoint`, `Added_by`, `Date_Added`, `skmSp`, `skmPointSp`, `QualSp`, `QualPointSp`) VALUES ('$Lead_Id','$reqpt','$age','$agepoint','$eng','$engpoint','$skm','$skmpoint','$qual','$qualpoint','$asr','$asrpt','$seq','$seqpt','$fac1','$facpt1','$fac2','$facpt2','$fac3','$facpt3','$fac4','$facpt4','$nom1','$nompt1','$nom2','$nompt2','$tpoint','$UserID', '$noww', '$skmsp', '$skmpointsp', '$qualsp', '$qualpointsp')";
		$result1 = mysqli_query($this->db,$sql11, $query) or die(mysqli_connect_error()."Data cannot inserted");
	    return $result1;

	    $sqll = "UPDATE leads_crm SET Lead_Status='Eligibility Checked' WHERE Lead_Id='$Lead_Id'";
	    $resultt = mysqli_query($this->db, $sqll);
	    return $resultt;

	    
	}

		public function EditEligibilityCheck($Lead_Id, $reqpt, $age, $agepoint, $eng, $engpoint, $skm, $skmpoint, $qual, $qualpoint, $asr, $asrpt, $seq, $seqpt, $fac1, $facpt1, $fac2, $facpt2, $fac3, $facpt3, $fac4, $facpt4, $nom1, $nompt1, $nom2, $nompt2, $tpoint, $UserID, $noww, $skmsp, $skmpointsp, $qualsp, $qualpointsp){
		$sql11 = "UPDATE `ainitcrm_auseligibility` SET `reqpt`='$reqpt',`age`='$age',`agepoint`='$agepoint',`eng`='$eng',`engpoint`='$engpoint',`skm`='$skm',`skmpoint`='$skmpoint',`qual`='$qual',`qualpoint`='$qualpoint',`asr`='$asr',`asrpt`='$asrpt',`seq`='$seq',`seqpt`='$seqpt',`fac1`='$fac1',`facpt1`='$facpt1',`fac2`='$fac2',`facpt2`='$facpt2',`fac3`='$fac3',`facpt3`='$facpt3',`fac4`='$fac4',`facpt4`='$facpt4',`nom1`='$nom1',`nompt1`='$nompt1',`nom2`='$nom2',`nompt2`='$nompt2',`tpoint`='$tpoint',`Added_by`='$UserID',`Date_Added`='$noww', `skmSp`='$skmsp', `skmPointSp`='$skmpointsp', `QualSp`='$qualsp', `QualPointSp`='$qualpointsp' WHERE inqno='$Lead_Id'";
		$result1 = mysqli_query($this->db,$sql11, $query) or die(mysqli_connect_error()."Data cannot updated");
	    return $result1;
	}
	public function UpdateStatusLeadEC($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Eligibility_Checked=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function LeadStatusLead($Lead_Id){
	$sql = "UPDATE `ainitcrm_leadstatus` SET Eligibility_Checked=0 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;		
	}

	public function GetFactorDetails($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_auseligibility WHERE inqno='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function createLog($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',6,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function createLogReff($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',18,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function createLogEE($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',6,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function AssignLog($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',17,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function RefAssignLog($NewID, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$NewID',17,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function createLogPK($Lead_Id, $leadstatus, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id','$leadstatus','$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function createLogRC($Lead_Id, $UserID){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',14,'$UserID',now())";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function addLeadValidateCons($Lead_Id, $svl, $recommoccp, $anz, $visa, $rmcons, $sugg, $UserID, $noww, $Bank_Statement ){
		$sql11 = "INSERT INTO `ainitcrm_ausvalidate`(`id`, `inqno`, `svl`, `recommoccp`, `anz`, `visa`, `rmcons`, `sugg`, `Added_by`, `Date_Added`, `Bank_Statement`) VALUES ('','$Lead_Id','$svl','$recommoccp','$anz','$visa','$rmcons','$sugg','$UserID','$noww', '$Bank_Statement')";
		$result1 = mysqli_query($this->db,$sql11) or die(mysqli_connect_error()."Data cannot inserted");
	    return $result1;

	    $sqll = "UPDATE leads_crm SET Lead_Status='Lead Validated' WHERE Lead_Id='$Lead_Id'";
	    $resultt = mysqli_query($this->db, $sqll);
	    return $resultt;
	}

	public function GetValidateCDetails($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_ausvalidate WHERE inqno='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function createLogVC($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',7,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}
	public function UpdateStatusLeadVC($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Lead_Validated=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function ManagerLeadValidation($Lead_Id, $svl, $manrecomm, $recommoccp, $recommanz, $clscore, $sugg, $UserID, $noww){
		$sql11 = "INSERT INTO `ainitcrm_ausmanagervalidate`(`id`, `inqno`, `svl`, `manrecomm`, `recommoccp`, `recommanz`, `clscore`, `sugg`, `Addedby`, `Date_Added`) VALUES ('','$Lead_Id','$svl','$manrecomm','$recommoccp','$recommanz','$clscore','$sugg','$UserID', '$noww')";
		$result1 = mysqli_query($this->db,$sql11) or die(mysqli_connect_error()."Data cannot inserted");
	    return $result1;
	    $sqll = "UPDATE leads_crm SET Lead_Status='Case Validated' WHERE Lead_Id='$Lead_Id'";
	    $resultt = mysqli_query($this->db, $sqll);
	    return $resultt;

	    
	}

	public function GetValidateCMDetails($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_ausmanagervalidate WHERE inqno='$Lead_Id'";
		$result=mysqli_query($this->db, $sql);
		return $result;
	}
	public function GetCaseMDetails($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_ausmanagervalidate WHERE inqno='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function createLogCM($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',9,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function UpdateStatusLeadCM($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Case_Validated=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function createLogRecyle($Lead_Id, $UserID){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',14,'$UserID',now())";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function CreateInvoice($Quot_Id, $Lead_Id, $UserID, $noww){
		$sql = "INSERT INTO `ainitcrm_invoices`(`Invc_Id`, `Quot_Id`, `Lead_Id`, `Created_by`, `Date_Added`) VALUES ('','$Quot_Id','$Lead_Id','$UserID','$noww')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function createLogQG($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',8,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}
	public function UpdateStatusLeadQG($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Quotation_Given=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function UpdateConvertLead($Lead_Id, $Client_FileNo, $Client_Address, $noww){
		$sql = "INSERT INTO `ainitcrm_convertedlead`(`CID`, `Lead_Id`, `Client_FileNo`, `Client_Address`, `Date_Added`) VALUES ('','$Lead_Id','$Client_FileNo', '$Client_Address','$noww')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function FetchDataConvertLead($Lead_Id){
		$sql = "SELECT * FROM `ainitcrm_convertedlead` WHERE Lead_Id='$Lead_Id'";
		$result=mysqli_query($this->db, $sql);
		return $result;
	}

	

	public function FetchInvoice($Lead_Id){
		$sql = "SELECT * FROM `ainitcrm_invoices` t1 INNER JOIN ainitcrm_services t2 ON t2.Quot_Id = t1.Quot_Id where t1.Lead_Id = '$Lead_Id' ";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function FetchCustomeInvoice($Lead_Id){
		$sql = "SELECT * FROM `ainitcrm_invoices` t1 INNER JOIN ainitcrm_customquotation t2 ON t2.Quot_Id = t1.Quot_Id where t1.Lead_Id = '$Lead_Id' ";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function UpdateInvoice($Lead_Id, $pay_confd){
		$sql = "UPDATE ainitcrm_invoices SET Invc_Status=1 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;

		$sqll = "UPDATE leads_crm SET Lead_Status='Payment Received' WHERE Lead_Id='$Lead_Id'";
	    $resultt = mysqli_query($this->db, $sqll);
	    return $resultt;
	}
	public function createLogPC($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',11,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}
	public function createLogAgS($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',10,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}
	public function UpdateStatusLeadPC($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Payment_Confirmed=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function UpdateStatusLeadAG($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Agreement_Signed=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function Case_HandedOver($Lead_Id, $case_hnd, $chk_snd ){

		$sqll = "UPDATE leads_crm SET Lead_Status='Case Handed Over' WHERE Lead_Id='$Lead_Id'";
	    $resultt = mysqli_query($this->db, $sqll);
	    return $resultt;

	    
	}
	public function createLogCHO($Lead_Id, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id',12,'$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

	public function UpdateStatusLeadCHO($Lead_Id){
		$sql = "UPDATE `ainitcrm_leadstatus` SET Case_Handed_Over=1 WHERE Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

	public function InsertCaseHandOver($Lead_Id, $assessbody, $case_hnd, $chk_snd, $UserID, $noww){
		$sql = "INSERT INTO `ainitcrm_leadhandover`(`Id`, `Lead_Id`, `assessbody`, `case_hnd`, `chklist_snd`, `Addedby`, `date_added`) VALUES ('','$Lead_Id','$assessbody','$case_hnd','$chk_snd','$UserID','$noww')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function CaseHandOverDetails($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_leadhandover WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function StatusHistory($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_leadlog WHERE Lead_Id='$Lead_Id'";
		$result=mysqli_query($this->db, $sql);
		return $result;

	}

	public function CheckLeadStatus($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function UpdLeadEC($Lead_Id){
		$sql = "UPDATE leads_crm SET Lead_Status=6 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}
	 
	 public function UpdLeadVC($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=7 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }

	 public function UpdLeadCV($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=9 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }

	 public function UpdLeadQG($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=8 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }
	 public function UpdLeadAG($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=10 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }
	 public function UpdLeadPC($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=11 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }
	 public function UpdLeadCHO($Lead_Id){
	 	$sql = "UPDATE leads_crm SET Lead_Status=12 WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	 }

	 public function FetchQuotation(){
		$sql = "SELECT * FROM ainitcrm_services";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	 public function FetchCustomeQuotation($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_customquotation WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}
	public function Custom_Quotation($Lead_Id, $Visa_Type, $reg_fee, $fst_fee, $scd_fee, $thd_fee, $fth_fee, $state_sponsor, $noww){
		$sql = "INSERT INTO `ainitcrm_customquotation`(`Quot_Id`, `Lead_Id`, `Visa_Type`, `Reg_Fee`, `Inst_One`, `Inst_Two`, `Inst_Three`, `Inst_Four`, `State_Fee`, `Date_Added`) VALUES ('','$Lead_Id','$Visa_Type','$reg_fee','$fst_fee','$scd_fee','$thd_fee','$fth_fee','$state_sponsor','$noww')";
		$result = mysqli_query($this->db, $sql);
		return $result;

		
	}

	public function Payment_ConfirmedLead($Lead_Id){
		$sql = "SELECT * FROM `ainitcrm_leadlog` where Lead_Id='$Lead_Id'";
	    $result = mysqli_query($this->db, $sql);
	    return $result;
	}

// search filter for consultant
	public function SearchResult($from1d, $to1d, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$from1d' AND '$to1d' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultF($from1d, $to1d){
		$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$from1d' AND '$to1d')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyName($ClientName, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Full_Name` LIKE '$ClientName%' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyNameF($ClientName){
		$sql = "SELECT * FROM `leads_crm` WHERE `Full_Name` LIKE '$ClientName%')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyEmail($Email_Address, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Email_Address`='$Email_Address' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyEmailF($Email_Address){
		$sql = "SELECT * FROM `leads_crm` WHERE `Email_Address`='$Email_Address')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyPhone($Mobile_No, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Mobile_No`='$Mobile_No' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyPhoneF($Mobile_No){
		$sql = "SELECT * FROM `leads_crm` WHERE `Mobile_No`='$Mobile_No')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyInquiry($inqno, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Lead_Id`='$inqno' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            //$i=0;
//	            while($lead_data = mysqli_fetch_array($result)){
//	            $Destination= $lead_data['Destination'];
//	            $Email_Address= $lead_data['Email_Address'];	
//	            $Home_Country= $lead_data['Home_Country'];
//	            $Mobile_No= $lead_data['Mobile_No'];
//	            $Your_Occupation= $lead_data['Your_Occupation'];
//	            $Near_Office= $lead_data['Near_Office'];
//		        $Full_Name= $lead_data['Full_Name'];
//		        $Visa_Type= $lead_data['Visa_Type'];
//		        $Date_Added= $lead_data['Date_Added'];
//		        $User_Assign= $lead_data['User_Assign'];
//		        $CV= $lead_data['CV'];
//		        $Referral_ID= $lead_data['Referral_ID'];
//		        $Lead_Status=$lead_data['Lead_Status'];
//		        $i++;
//		        echo $extract_lead ='<tr class="info">
  //                                              <td>'.$i.'</td>
    //                                            <td>'.$Full_Name.'</td>
      //                                          <td>'.$Destination.'</td>
        //                                        <td>'.$Home_Country.'</td>
          //                                      <td>'.$Your_Occupation.'</td>
            //                                    <td>'.$Mobile_No.'</td>
              //                                  <td>'.$Email_Address.'</td>
                //                                <td>'.$Visa_Type.'</td>
                  //                              <td>'.$Date_Added.'</td>
                    //                            <td><a href="client/'.$CV.'">CV</a></td>
                      //                          <td>'.$Lead_Status.'</td>
                        //                    </tr>';

		  //  }
	}

	public function SearchResultbyInquiryF($inqno){
		$sql = "SELECT * FROM `leads_crm` WHERE `Lead_Id`='$inqno')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	      
	}

	public function SearchResultbyRef($refcode, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Referral_ID`='$refcode' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyRefF($refcode){
		$sql = "SELECT * FROM `leads_crm` WHERE `Referral_ID`='$refcode')";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyVT($VisType, $UserID, $Reports_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Visa_Type`='$VisType' AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to') ORDER BY `Date_Added` DESC";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	//Edit Lead

	public function EditHomeCountry($EditHome_Country, $enqno){
			$sql = "UPDATE leads_crm SET Home_Country='$EditHome_Country' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditEmail($EditEmail_Address, $enqno){
			$sql = "UPDATE leads_crm SET Email_Address='$EditEmail_Address' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditMobile($EditMobile_No, $enqno){
			$sql = "UPDATE leads_crm SET Mobile_No='$EditMobile_No' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditFullName($EditFull_Name, $enqno){
			$sql = "UPDATE leads_crm SET Full_Name='$EditFull_Name' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditYourOccupation($EditYour_Occupation, $enqno){
			$sql = "UPDATE leads_crm SET Your_Occupation='$EditYour_Occupation' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditVisaType($EditVisa_Type, $enqno){
			$sql = "UPDATE leads_crm SET Visa_Type='$EditVisa_Type' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditNearOffice($EditNear_Office, $enqno){
			$sql = "UPDATE leads_crm SET Near_Office='$EditNear_Office' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditClientDestination($EditDestination, $enqno){
			$sql = "UPDATE leads_crm SET Destination='$EditDestination' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditYourDob($dob, $enqno){
/*			$sql = "INSERT INTO `ainitcrm_otherdetails`(`id`, `Lead_Id`, `dtbirthh`, `messaggee`) VALUES ('','$enqno','$dob','')";*/
			$sql = "UPDATE leads_crm SET date_of_birth='$dob' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function EditCreatedby($Createdby, $enqno){
			$sql = "UPDATE leads_crm SET Referral_ID='$Createdby' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function fetchDob($Lead_Id){
			$sql = "SELECT date_of_birth as dtbirthh FROM leads_crm WHERE `Lead_Id`='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function LeadStatusReset($Lead_Id){
			$sql = "UPDATE leads_crm SET Lead_Status=1 WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function UpdateCV($enqno, $newfilename){
			$sql = "UPDATE leads_crm SET CV='$newfilename' WHERE Lead_Id='$enqno'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}


		public function DeleteLeadPoints($Lead_Id){
			$sqll = "DELETE FROM ainitcrm_leadstatus WHERE Lead_Id='$Lead_Id'";
	    	$resultt = mysqli_query($this->db, $sqll);
	    	return $resultt;	
		}

		public function LeadStatusReason($Lead_Id, $Status_Reason){
			$sql = "UPDATE leads_crm SET Status_Reason='$Status_Reason' WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}
		public function LeadStatusRemarks($Lead_Id, $sugg){
			$sql = "UPDATE leads_crm SET Status_Remarks='$sugg' WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}
		public function NotValidateLead($leadstatus, $Lead_Id ){
			$sql = "UPDATE leads_crm SET Lead_Status='$leadstatus' WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function DeleteEligibilityPoints($Lead_Id){
			$sql = "DELETE FROM ainitcrm_auseligibility WHERE inqno='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;	
		}

		public function TransferLead($Lead_Id, $tad){
			$sql = "UPDATE leads_crm SET Destination='$tad' WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function TransferLeadVisa($Lead_Id, $vtyp){
			$sql = "UPDATE leads_crm SET Visa_Type='$vtyp' WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
		}

		public function RecycleLead($Lead_Id){
		$sql = "UPDATE leads_crm SET Lead_Status=14 WHERE Lead_Id='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
	    }

	    public function UpdTaskValidate($Lead_Id){
		$sql = "UPDATE tasks SET task_status='Yes' WHERE assign_tsk='Lead Validate' and inqno='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
	    }
	    public function UpdTaskdt($Lead_Id){
		$sql = "UPDATE tasks SET date_completion=now() WHERE assign_tsk='Lead Validate' and inqno='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
	    }

	    public function UpdTaskCMValidate($Lead_Id){
		$sql = "UPDATE tasks SET task_status='Yes' WHERE assign_tsk='Case Validate' and inqno='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
	    }

	    public function UpdTaskPC($Lead_Id){
	    	$sql = "UPDATE tasks SET task_status='Yes' WHERE assign_tsk='Payment Confirmation' and inqno='$Lead_Id'";
	    	$result = mysqli_query($this->db, $sql);
	    	return $result;
	    }

	    public function FetchConsultant(){
	    	$sql = "SELECT `UserID`, `UserName` FROM `tbl_users` WHERE `type`='Consultant' AND `status`='active'";
	    	$result=mysqli_query($this->db, $sql);
	    	//return $result;
	    	while($show=mysqli_fetch_array($result)){

			echo '<option value="'.$show['UserID'].'">'.$show['UserName'].'</option>';
			}
	    }

	    public function SearchResultCons($from1d, $to1d, $assign_to){
		$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$from1d' AND '$to1d' AND Assigned_to='$assign_to' ";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);

		//return $result;
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];

		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyNameAdmin($ClientName){
		$sql = "SELECT * FROM `leads_crm` WHERE `Full_Name`LIKE '$ClientName%'";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		//return $result;
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyEmailAdmin($Email_Address){
		$sql = "SELECT * FROM `leads_crm` WHERE `Email_Address`='$Email_Address'";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		//return $result;
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyPhoneAdmin($Mobile_No){
		$sql = "SELECT * FROM `leads_crm` WHERE `Mobile_No`='$Mobile_No'";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		//return $result;
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyInquiryAdmin($inqno){
		$sql = "SELECT * FROM `leads_crm` WHERE `Lead_Id`='$inqno'";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
	            
	}

	public function SearchResultbyReferIDAdmin($refcode){
		$sql = "SELECT * FROM `leads_crm` WHERE `Referral_ID`='$refcode'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyOccupationAdmin($occupation){
		$sql = "SELECT * FROM `leads_crm` WHERE `Your_Occupation`='$occupation'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyDestinationAdmin($destination){
		$sql = "SELECT * FROM `leads_crm` WHERE `Destination`='$destination'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyCountryResAdmin($country_res){
		$sql = "SELECT * FROM `leads_crm` WHERE `Home_Country`='$country_res'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyLeadBehaviourAdmin($Lead_Behaviour){
		$sql = "SELECT * FROM `leads_crm` WHERE `Lead_Source`='$Lead_Behaviour'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function SearchResultbyLeadStatusAdmin($ledst){
		$sql = "SELECT * FROM `leads_crm` WHERE `Lead_Status`='$ledst'";
		$result = mysqli_query($this->db, $sql);
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}
///end search filter for admin

	public function GetLeadID(){
		$sql="SELECT Lead_Id FROM leads_crm";
		$result=mysqli_query($this->db, $sql);
		while($row=mysqli_fetch_array($result)){
    $id = $row['Lead_Id'];
     $idn = mysqli_insert_id();
     $newno = $id+1;
	  }
		
	//	return $result;
	}

	public function GetLeadStatuses($Lead_Status){
		$sql="SELECT * FROM ainitcrm_leadstatuses WHERE Status_ID='$Lead_Status'";
		$result = mysqli_query($this->db, $sql);
		$row = mysqli_fetch_array($result);
			$StatusID=$row['Status_ID'];
			echo $StatusName=$row['Status_Name'];
		}

		public function FetchOccupation(){
			$sql = "SELECT * FROM ainitcrm_anzco ORDER BY ANZSCO_Code ASC";
			$result = mysqli_query($this->db, $sql);
			//return $result;
			
			while($row=mysqli_fetch_array($result)){
				$OccupationName= $row['OccupationName'];
				$ANZSCO_Code=$row['ANZSCO_Code'];
				echo $extract_lead ='<option value="'.$ANZSCO_Code.'">'.$OccupationName.'</option>';
			}
		}

		public function GetUserName($UserAdded){
			$sql = "SELECT * FROM tbl_users WHERE UserID='$UserAdded'";
			$result=mysqli_query($this->db, $sql);
			//return $result;

			while($row = mysqli_fetch_array($result)){
			$UID=$row['UserID'];
			echo $Addedby=$row['Name'];
			}
		}

		  function search_type($by_name, $by_email, $by_phone, $by_enqno, $by_assign_to, $by_Referrence, $by_occupation, $by_refcode, $by_destination, $by_country_res, $from1d, $to1d, $by_Lead_Status, $by_Lead_Behaviour) {




    $query = "SELECT * FROM leads_crm";
    $conditions = array();

    if(! empty($_POST['$by_name'])) {
      $conditions[] = "Full_Name LIKE '$by_name%'";
    }
    if(! empty($_POST['$by_email'])) {
      $conditions[] = "Email_Address='$by_email'";
    }
    if(! empty($_POST['$by_phone'])) {
      $conditions[] = "Mobile_No='$PhoneNo'";
    }
    if(! empty($_POST['$by_enqno'])) {
      $conditions[] = "Lead_Id='$by_enqno'";
    }
    if(! empty($by_assign_to)) {
      $conditions[] = "Assigned_to='$by_assign_to'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Referrence)) {
      $conditions[] = "Referral_ID='$by_Referrence'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_occupation)) {
      $conditions[] = "Your_Occupation='$by_occupation'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_refcode)) {
      $conditions[] = "Referral_ID='$by_refcode'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_destination)) {
      $conditions[] = "Destination='$by_destination'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_country_res)) {
      $conditions[] = "Home_Country='$by_country_res'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Behaviour)) {
      $conditions[] = "Lead_Source='$by_Lead_Behaviour'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Status)) {
      $conditions[] = "Lead_Status='$by_Lead_Status'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Behaviour)) {
      $conditions[] = "Lead_Source='$by_Lead_Behaviour'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($from1d)) {
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }

    $sql = $query;
    if (count($conditions) > 0) {
      $sql .= " WHERE " . implode(' AND ', $conditions);
    }

    $result = mysqli_query($this->db, $sql);
    return $result;
		}

		function TotalRC($by_name, $by_email, $by_phone, $by_enqno, $by_assign_to, $by_Referrence, $by_occupation, $by_refcode, $by_destination, $by_country_res, $from1d, $to1d, $by_Lead_Behaviour, $by_Lead_Status, $by_Lead_Behaviour) {


    $query = "SELECT COUNT(Lead_Id) FROM leads_crm";
    $conditions = array();

    if(! empty($by_name)) {
      $conditions[] = "Full_Name LIKE '$$by_name%'";
    }
    if(! empty($by_email)) {
      $conditions[] = "Email_Address='$by_email'";
    }
    if(! empty($by_phone)) {
      $conditions[] = "Mobile_No='$PhoneNo'";
    }
    if(! empty($by_enqno)) {
      $conditions[] = "Lead_Id='$by_enqno'";
    }
    if(! empty($by_assign_to)) {
      $conditions[] = "Assigned_to='$by_assign_to'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Referrence)) {
      $conditions[] = "Referral_ID='$by_Referrence'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_occupation)) {
      $conditions[] = "Your_Occupation='$by_occupation'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_refcode)) {
      $conditions[] = "Referral_ID='$by_refcode'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_destination)) {
      $conditions[] = "Destination='$by_destination'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_country_res)) {
      $conditions[] = "Home_Country='$by_country_res'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Behaviour)) {
      $conditions[] = "Lead_Source='$by_Lead_Behaviour'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Status)) {
      $conditions[] = "Lead_Status='$by_Lead_Status'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($by_Lead_Behaviour)) {
      $conditions[] = "Lead_Source='$by_Lead_Behaviour'";
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }
    if(! empty($from1d)) {
      $conditions[] = "(`Date_Added` BETWEEN  '$from1d' AND  '$to1d')";
    }

    $sql = $query;
    if (count($conditions) > 0) {
      $sql .= " WHERE " . implode(' AND ', $conditions);
    }

    $result = mysqli_query($this->db, $sql);
    while($row = mysqli_fetch_array($result)){
    	echo $TRC = $row['COUNT(Lead_Id)'];
    }
		}

		public function SearchResultAdminP($from1d, $to1d ){
		$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$from1d' AND '$to1d'";
		//$sql = "SELECT * FROM `leads_crm` WHERE `Date_Added` BETWEEN '$to1' AND '$from1'  AND (Assigned_to='$UserID' OR Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		//return $result;
	            $i=0;
	            while($lead_data = mysqli_fetch_array($result)){
	            $Destination= $lead_data['Destination'];
	            $Email_Address= $lead_data['Email_Address'];	
	            $Home_Country= $lead_data['Home_Country'];
	            $Mobile_No= $lead_data['Mobile_No'];
	            $Your_Occupation= $lead_data['Your_Occupation'];
	            $Near_Office= $lead_data['Near_Office'];
		        $Full_Name= $lead_data['Full_Name'];
		        $Visa_Type= $lead_data['Visa_Type'];
		        $Date_Added= $lead_data['Date_Added'];
		        $User_Assign= $lead_data['User_Assign'];
		        $CV= $lead_data['CV'];
		        $Referral_ID= $lead_data['Referral_ID'];
		        $Lead_Status=$lead_data['Lead_Status'];
		        $i++;
		        echo $extract_lead ='<tr class="info">
                                                <td>'.$i.'</td>
                                                <td>'.$Full_Name.'</td>
                                                <td>'.$Destination.'</td>
                                                <td>'.$Home_Country.'</td>
                                                <td>'.$Your_Occupation.'</td>
                                                <td>'.$Mobile_No.'</td>
                                                <td>'.$Email_Address.'</td>
                                                <td>'.$Visa_Type.'</td>
                                                <td>'.$Date_Added.'</td>
                                                <td><a href="client/'.$CV.'">CV</a></td>
                                                <td>'.$Lead_Status.'</td>
                                            </tr>';

		    }
	}

	public function search_taskrpt($assign_to, $taskstat, $from1d, $to1d){
		$sql = "SELECT * FROM tasks WHERE assign_to='$assign_to' AND task_status='$taskstat' AND `date_added` BETWEEN '$from1d' AND '$to1d'";
		$result=mysqli_query($this->db, $sql);
		//return $result;
		while($show=mysqli_fetch_array($result)){
			$inqno = $show['inqno'];
			$fileno = $show['fileno'];
			$name = $show['name'];
			$assign_tsk = $show['assign_tsk'];
			$tsk_period = $show['tsk_period'];
			$date_assign = $show['date_assign'];
			$tskst = $show['task_status'];
			echo $extract_tasks = ' <tr>
                                          <td>'.$inqno.'</td>
                                            <td>'.$fileno.'</td>
                                            <td>'.$name.'</td>
                                            <td>'.$assign_tsk.'</td>
                                            <td>'.$tsk_period.'</td>
                                            <td>'.$date_assign.'</td>
                                            <td>'.$tskst.'</td>
                                        </tr>';
		}
	}

	public function checkInvoice($Lead_Id){
		$sql = "SELECT * FROM ainitcrm_invoices WHERE Lead_Id='$Lead_Id'";
		$result = mysqli_query($this->db, $sql);
		return $result;
	}

	public function LeadComplains($Mobile_No, $Email_Address, $leavemessage){
		date_default_timezone_set('Asia/Pakistan'); 
		$noww = date('Y-m-d H:i:s', strtotime($nowtime . ' + 5 hours'));

		$sql="INSERT INTO `ainitcrm_leadcomplaints`(`Comp_Id`, `Mobile_No`, `Email_Address`, `Messages`, `Date_Added`) VALUES ('%s','%s','%s','%s','%s')";

		$query = sprintf($sql, mysqli_real_escape_string($this->db, ''), mysqli_real_escape_string($this->db, $Mobile_No), mysqli_real_escape_string($this->db, $Email_Address), mysqli_real_escape_string($this->db, $leavemessage), mysqli_real_escape_string($this->db, $noww));
		$result=mysqli_query($this->db, $query);
		return $result;
	}

	public function GetVisas($Visa_Type){
			$sql = "SELECT * FROM ainitcrm_visastype WHERE VisaID='$Visa_Type'";
			$result=mysqli_query($this->db, $sql);
			//return $result;

			while($row = mysqli_fetch_array($result)){
			$VID=$row['VisaID'];
			echo $VisaName=$row['VisaName'];
			}
		}

		public function getQuotPart(){
			$sql = "SELECT * FROM ainitcrm_quotationParticulars";
			$result=mysqli_query($this->db, $sql);
			return $result;
		}

		public function getCurrency(){
			$sql = "SELECT * FROM ainitcrm_currency";
			$result=mysqli_query($this->db, $sql);
			return $result;
		}

		public function NewQuotation($Lead_Id, $constancy_name, $price, $Currency, $discount, $dispercent, $UserID, $QuotSt ){
			$sql = "INSERT INTO `ainitcrm_quot`(`Quot_Id`, `Lead_Id`, `Particulars`, `Price`, `Currency`, `DiscountName`, `DiscountPercent`, `Added_by`, `Date_Added`, `QuotSt`) VALUES ('','$Lead_Id','$constancy_name','$price','$Currency','$discount','$dispercent','$UserID', now(), '$QuotSt')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function SumPrice($Lead_Id){
			$sql = "SELECT SUM(Price) FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' AND Particulars BETWEEN 1 AND 9 ";
			$result = mysqli_query($this->db, $sql);
			while($row=mysqli_fetch_array($result)){
				echo $totalPrice = $row['SUM(Price)'];
			}
		}

		public function SumPriceNow($Lead_Id){
			$sql = "SELECT SUM(Price) FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' AND QuotSt=1 ";
			$result = mysqli_query($this->db, $sql);
			while($row=mysqli_fetch_array($result)){
				echo $CurrentPrice = $row['SUM(Price)'];
			}
		}

		public function quotlists($Lead_Id){
			$sql = "SELECT * FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' ";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}
		public function quotlistsstage1($Lead_Id){
			$sql = "SELECT * FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' AND Particulars IN (1,3,14,5)";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}
		public function quotlistsstage2($Lead_Id){
			$sql = "SELECT * FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' AND Particulars IN (7)";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function quotlistsstage4($Lead_Id){
			$sql = "SELECT * FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id' AND Particulars IN (9)";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}
		 

		public function QuotName($Particulars){
			$sql = "SELECT * FROM ainitcrm_quotationParticulars WHERE Part_Id='$Particulars'";
			$result = mysqli_query($this->db, $sql);
			$row = mysqli_fetch_array($result);
			$PId = $row['Part_Id'];
			echo $ParticularName = $row['ParticularName'];
		}

		public function CurrName($Currency){
			$sql = "SELECT * FROM ainitcrm_currency WHERE CurrencyId='$Currency'";
			$result = mysqli_query($this->db, $sql);
			$row = mysqli_fetch_array($result);
			$CId = $row['CurrencyId'];
			echo $CurrencyName = $row['CurrencyName'];
		}

		public function DeleteQuots($Lead_Id){
			$sql = "DELETE FROM ainitcrm_quot WHERE Lead_Id='$Lead_Id'";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function DeleteInvoice($Lead_Id){
			$sql = "DELETE FROM ainitcrm_invoices WHERE Lead_Id='$Lead_Id'";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function DeleteValidatedPoints($Lead_Id){
			$sql = "DELETE FROM ainitcrm_ausvalidate WHERE inqno='$Lead_Id'";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function TotalLead(){
			$sql = "SELECT COUNT(Lead_Id) FROM leads_crm WHERE Lead_Status=1 AND Assigned_to IS NULL";
			$result = mysqli_query($this->db, $sql);
			while($row=mysqli_fetch_array($result)){
				echo $TotalLeads = $row['COUNT(Lead_Id)'];
			}
		}

		public function addEmail($Lead_Id, $subject, $message, $UserEmailID, $UserID, $noww){
			$sql = "INSERT INTO `ainitcrm_emaildetails`(`EID`, `Lead_Id`, `Email_Subject`, `Email_Message`, `Client_Email`, `Email_by`, `Date_Sent`) VALUES ('','$Lead_Id','$subject','$message','$UserEmailID','$UserID','$noww')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function updateStatusEmail($EmailID){
				$sql = "UPDATE leads_crm SET Status_EmailAddress='%s' WHERE Email_Address='%s'";
				$query = sprintf($sql, mysqli_real_escape_string($this->db, '1'), mysqli_real_escape_string($this->db, $EmailID));
				$result = mysqli_query($this->db, $query);
				return $result;
		}

		public function UpdateCommission($Lead_Id, $RefId, $ComName, $status, $UserID, $noww){
		$sql = "INSERT INTO `ainitcrm_commissions`(`ComID`, `Lead_Id`, `Referral_ID`, `CommissionName`, `Status`, `Addedby`, `Date_Added`) VALUES ('','$Lead_Id','$RefId','$ComName','$status','$UserID','$noww')";
		$result = mysqli_query($this->db, $sql);
		return $result;
		}

		public function check_lead($Email_Address, $Mobile_No){

	            $sql="SELECT * FROM leads_crm WHERE Email_Address='$Email_Address' OR Mobile_No='$Mobile_No'";
	            $check =  $this->db->query($sql) ;
	            $count_row = $check->num_rows;
	            if ($count_row != 0){
	                $result = mysqli_query($this->db,$sql);
	                return $result;
	        	}
	        }

	        public function SendSMSClient($Lead_Id, $subjectt, $messagee, $UserID, $noww){
			$sql = "INSERT INTO `ainitcrm_smsclients`(`SmsID`, `Lead_Id`, `SMSSubject`, `SMSMessage`, `SMSby`, `SMSDated`) VALUES ('','$Lead_Id','$subjectt','$messagee','$UserID','$noww')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function fetchReorcds($where){
			$sql = "SELECT * FROM leads_crm" .$where;
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function fetchUnattendedLeads($UserID, $Reports_to){
      $sql = "SELECT  *
FROM    leads_crm as t1
WHERE   Lead_Id NOT IN (SELECT ClientID FROM tbl_client_narratives as t2) AND (t1.Assigned_to='$UserID' OR t1.Assigned_to='$Reports_to')";
		$result = mysqli_query($this->db, $sql);
		return $result;
		}

		public function fetchOtherStatus(){
			$sql = "SELECT * FROM ainitcrm_otherstatuses";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function AddAppointmentss($app_name, $app_type, $destination, $consultant, $time, $datepicker, $UserID, $noww, $Lead_Id, $status_app, $apptpe){
			$sql = "INSERT INTO appointment (`id`, `app_name`, `app_type`, `destination`, `consultant`, `time`, `date`, `visit`, `added`, `iqno`, `status_app`, `AppointmentType`) VALUES('','$app_name','$app_type','$destination','$consultant','$time','$datepicker','$UserID','$noww', '$Lead_Id', '$status_app', '$apptpe')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function DelAppointment($id, $UserID){
			$sql = "DELETE from `appointment` WHERE  `id`='$id' AND (`consultant`='$UserID' OR `visit`='$UserID')  ";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function FetchConsultantAppointment($current_date, $UserID, $Reports_to, $status_app){
			$sql = "SELECT * from `appointment` WHERE `date` ='$current_date' and (`visit`='$UserID' OR `consultant`='$UserID' OR `consultant`='$Reports_to') and `status_app`='$status_app'";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function FetchConsultantAppointmentAll($status_app, $UserID, $Reports_to){
			$sql = "SELECT * from `appointment` WHERE `status_app` ='$status_app' and (`consultant`='$UserID' OR `visit`='$UserID'  OR `consultant`='$Reports_to')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function createLogOther($Lead_Id, $StatusName, $UserID, $noww){
		$query = "INSERT INTO `ainitcrm_leadlog`(`Log_Id`, `Lead_Id`, `Lead_Status`, `Updated_By`, `Date_Added`) VALUES ('','$Lead_Id','$StatusName','$UserID','$noww')";
	    $upd_log = mysqli_query($this->db, $query);
	    return $upd_log;
	}

		public function CreateTasks($TaskName, $noww, $FinishDate, $TaskPriority, $TaskDescription, $TaskAssignTo, $UserID, $tskSt, $inqno){
			$sql = "INSERT INTO `ainitcrm_managementtasks`(`TID`, `TaskName`, `AssignedDate`, `FinishDate`, `TaskPriority`, `TaskDescription`, `TaskAssignTo`, `TaskAssignBy`, `TaskStatus`, `inqno`) VALUES ('','$TaskName','$noww','$FinishDate','$TaskPriority','$TaskDescription','$TaskAssignTo','$UserID',1, '$inqno')";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function FetchNewTask($UserID){
			$sql = "SELECT * FROM `ainitcrm_managementtasks` WHERE `TaskStatus`=1 and (`TaskAssignTo` ='$UserID' OR `TaskAssignBy`='$UserID')  ORDER BY `AssignedDate` DESC";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function FetchPendingTask($UserID){
			$sql = "SELECT * FROM `ainitcrm_managementtasks` WHERE `TaskStatus`=2 and (`TaskAssignTo` ='$UserID' OR `TaskAssignBy`='$UserID')  ORDER BY `AssignedDate` DESC";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function FetchCompletedTask($UserID){
			$sql = "SELECT * FROM `ainitcrm_managementtasks` WHERE `TaskStatus`=3 and (`TaskAssignTo` ='$UserID' OR `TaskAssignBy`='$UserID')  ORDER BY `AssignedDate` DESC";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

		public function CheckLead($emailaddress, $phn){
			$sql = "SELECT `Lead_Id`, `Assigned_to` FROM `leads_crm` WHERE `Email_Address`='$emailaddress' OR `Mobile_No`='$phn'";
			$result = mysqli_query($this->db, $sql);
			return $result;
		}

    }
?>