MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/login_cafsindia_com/app/api_controller_bk27OCT2021.php
 <?php
	$frm  = "";
	/* Module Initilization */
	$json = file_get_contents('php://input');
	$json = json_decode($json);
	if($json){
		require("./api_model.php");
	    $api_model = new api_model;
	}
	$frm                          = $json->frm;	
	$mysql_health_rslt            = "";
	$mysql_portfolio_rslt         = "";
	$mysql_portfolio_insert_rslt  = "";
if($frm === "check_exist"){
	$parameter            = $json->parameter;
	$login_code           = $json->login_code;
	$mobile_no            = $json->contact_no;
	$dial_for             = $json->dial_for;
	if((int)$parameter === 1 || (int)$parameter === 2){
		//mobile is not available create selfdata.	
		$mysql_rslt      = $api_model->get_login_code_number("SELECT count(*) as count FROM (SELECT contact_no FROM cw_portfolio WHERE contact_no = '".$mobile_no."' and trans_status = 1 UNION SELECT contact_number FROM cw_health WHERE contact_number = '".$mobile_no."' and trans_status = 1 UNION SELECT contact_no FROM cw_dump_data WHERE contact_no = '".$mobile_no."' and trans_status = 1) a");
			$mysql_rslt_count = $mysql_rslt[0]->count;
			if((int)$mysql_rslt_count === 0){
				echo json_encode(array('sts' => TRUE, "msg" => "New Mobile Number. You can create as Self or Reference","rslt"=>"","error"=>""));
			}else{
				$mysql_rslt      = $api_model->get_login_code_number("SELECT count(*) as count FROM (SELECT login_code,contact_no FROM cw_portfolio WHERE contact_no = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1 UNION SELECT login_code,contact_number FROM cw_health WHERE contact_number = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1 UNION SELECT login_code,contact_no FROM cw_dump_data WHERE contact_no = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1) a order by login_code");
				$mysql_rslt_count = $mysql_rslt[0]->count;
				if((int)$mysql_rslt_count === 0){
					echo json_encode(array('sts' => FALSE, "msg" => "Already Exist with Someone, So You cannot Add this Lead..","rslt"=>"","error"=>""));
				}else{
					echo json_encode(array('sts' => FALSE, "msg" => "Already Exist. You can create as Upsell","rslt"=>"","error"=>""));
				}
			}
	}else 
	if((int)$parameter === 3){
		//logincode and mobile is me. create upsell allow.
		$mysql_rslt      = $api_model->get_login_code_number("SELECT count(*) as count FROM (SELECT login_code,contact_no FROM cw_portfolio WHERE contact_no = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1 UNION SELECT login_code,contact_number FROM cw_health WHERE contact_number = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1 UNION SELECT login_code,contact_no FROM cw_dump_data WHERE contact_no = '".$mobile_no."' and login_code = '".$login_code."' and trans_status = 1) a order by login_code");
		$mysql_rslt_count = $mysql_rslt[0]->count;
		if((int)$mysql_rslt_count === 0){
			$mysql_rslt      = $api_model->get_login_code_number("SELECT count(*) as count FROM (SELECT contact_no FROM cw_portfolio WHERE contact_no = '".$mobile_no."' and trans_status = 1 UNION SELECT contact_number FROM cw_health WHERE contact_number = '".$mobile_no."' and trans_status = 1 UNION SELECT contact_no FROM cw_dump_data WHERE contact_no = '".$mobile_no."' and trans_status = 1) a");
			$mysql_rslt_count = $mysql_rslt[0]->count;
			if((int)$mysql_rslt_count === 0){
				echo json_encode(array('sts' => FALSE, "msg" => "New Mobile Number. You can create as Self or Reference","rslt"=>"","error"=>""));
			}else{
				echo json_encode(array('sts' => FALSE, "msg" => "Already Exist with Someone, So You cannot Add this Lead..","rslt"=>"","error"=>""));
			}		
		}else{
			echo json_encode(array('sts' => TRUE, "msg" => "You can Proceed..","rslt"=>"","error"=>""));		
		}

	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "Bad Request","rslt"=>"","error"=>""));
	} 
}else
if($frm === "insert_data"){
	$calling_id            = $json->calling_id;
	$contact_no            = $json->contact_no;
	$dial_for              = $json->dial_for;
	$client_name           = $json->client_name;
	$login_code            = $json->login_code;		
	$entry_date            = date("Y-m-d",strtotime($json->entry_date));
	$mysql_select_rslt     = "";
	if($dial_for  === "LIFE") {
		$mysql_select_rslt = $api_model->get_select_qry("Select count(*) as count from cw_portfolio where calling_id = '".$calling_id."' and trans_status = 1");
		if($mysql_select_rslt[0]->count){
			$mysql_portfolio_upd_rslt = $api_model->get_upd_qry("UPDATE cw_portfolio SET calling_id ='".$calling_id."', contact_no ='".$contact_no."', client_name ='".$client_name."', login_id ='".$login_code."', entry_date ='".$entry_date."' WHERE calling_id ='".$calling_id."'");
			if($mysql_portfolio_upd_rslt) {
				echo json_encode(array('sts' => TRUE, "msg" => "Record Successfully Updated","rslt"=>"","error"=>""));
			}else{
				echo json_encode(array('sts' => FALSE, "msg" => "Records Update Error","rslt"=>"","error"=>""));
			}
		}else {
			$mysql_portfolio_insert_rslt = $api_model->get_insert_qry("INSERT INTO cw_portfolio (`calling_id`, `contact_no`, `client_name`, `login_id`, `entry_date`) VALUES ('".$calling_id."','".$contact_no."', '".$client_name."','".$login_code."', '".$entry_date."')");
			if($mysql_portfolio_insert_rslt) {
				echo json_encode(array('sts' => TRUE, "msg" => "Record Successfully Inserted","rslt"=>"","error"=>""));
			}else{
				echo json_encode(array('sts' => FALSE, "msg" => "Records Insert Error","rslt"=>"","error"=>""));
			}
		}
	}else {
		$mysql_select_rslt = $api_model->get_select_qry("Select count(*) as count from cw_health where calling_id = '".$calling_id."' and trans_status = 1");
		if($mysql_select_rslt[0]->count) {
			$mysql_health_upd_rslt = $api_model->get_upd_qry("UPDATE cw_health SET calling_id ='".$calling_id."', contact_number ='".$contact_no."', proposer_name ='".$client_name."', login_code ='".$login_code."', entry_date ='".$entry_date."' WHERE calling_id ='".$calling_id."'");
			if($mysql_health_upd_rslt) {
				echo json_encode(array('sts' => TRUE, "msg" => "Record Successfully Updated","rslt"=>"","error"=>""));
			}else{
				echo json_encode(array('sts' => FALSE, "msg" => "Records Update Error","rslt"=>"","error"=>""));
			}
		}else {
			$mysql_health_insert_rslt = $api_model->get_insert_qry("INSERT INTO cw_health (`calling_id`, `contact_number`, `proposer_name`, `login_code`, `entry_date`) VALUES ('".$calling_id."','".$contact_no."', '".$client_name."','".$login_code."', '".$entry_date."')");
			if($mysql_health_insert_rslt) {
				echo json_encode(array('sts' => TRUE, "msg" => "Record Successfully Inserted","rslt"=>"","error"=>""));
			}else{
				echo json_encode(array('sts' => FALSE, "msg" => "Records Insert Error","rslt"=>"","error"=>""));
			}
		}
	}
}else
if($frm === "get_business"){
	$login_qry      = "";
	$login_code     = implode("','",$json->userId);	
	$pro_mode       = $json->pro_mode;
	if((int)$pro_mode === 2){
		$from_date  = date('Y-m-d', strtotime($json->from_date));
		$to_date    = date('Y-m-d', strtotime($json->to_date));
	}else
	if((int)$pro_mode === 3){
		$from_date  = date('Y-m-d', strtotime($json->from_date));
		$to_date    = date('Y-m-d', strtotime($json->to_date));
	}else{
		$from_date  = date('Y-m-01', strtotime($json->from_date));
		$to_date    = date('Y-m-t', strtotime($json->to_date));
	}

	$life_rslt    = $api_model->get_life_business("SELECT login_date,login_code,IFNULL(SUM(cw_portfolio.total_net_premium),0) as life_business, SUM(CASE WHEN cw_portfolio.login_status = 1 THEN cw_portfolio.total_net_premium ELSE 0 END) pdc,SUM(CASE WHEN cw_portfolio.login_status = 5 THEN cw_portfolio.total_net_premium ELSE 0 END) login from cw_portfolio where cw_portfolio.login_date >= '".$from_date."' and cw_portfolio.login_date <= '".$to_date."' and cw_portfolio.trans_status = 1 and login_code in ('".$login_code."') and cw_portfolio.login_status != 6 GROUP BY cw_portfolio.login_code");

	$life_rslt    =  json_decode(json_encode($life_rslt),true);

	$health_rslt  = $api_model->get_health_business("SELECT login_date,login_code,IFNULL(SUM(cw_health.net_premium),0) as health_business from cw_health where cw_health.login_date >= '".$from_date."' and cw_health.login_date <= '".$to_date."' and cw_health.trans_status = 1 and login_code in ('".$login_code."') and cw_health.login_status != 6 GROUP BY cw_health.login_code");
	
	$health_rslt   =  json_decode(json_encode($health_rslt),true);
	if((int)$pro_mode === 3){
		$life_array    = array_reduce($life_rslt, function ($result, $arr) {
		    $result[$arr['login_date']][$arr['login_code']] = $arr;
		    return $result;
		}, array());

		$health_array  = array_reduce($health_rslt, function ($result, $arr) {
		    $result[$arr['login_date']][$arr['login_code']] = $arr;
		    return $result;
		}, array());

		$replace_array = array_replace_recursive($life_array,$health_array);
		$final_array = array();
		foreach ($replace_array as $key => $values){
			$login_date = $values->login_date;
			foreach ($values as $code_key => $value) {
				$final_array[$key][$code_key]['buiz']['life']   = $value['life_business'];
				$final_array[$key][$code_key]['buiz']['health'] = $value['health_business'];
				$final_array[$key][$code_key]['life']['login']  = $value['login'];
				$final_array[$key][$code_key]['life']['pdc']    = $value['pdc'];
			}
		}
	}else{
		$life_array    = array_reduce($life_rslt, function ($result, $arr){
		    $result[$arr['login_code']] = $arr;
		    return $result;
			}, array());
		$health_array  = array_reduce($health_rslt, function ($result, $arr) {
		    $result[$arr['login_code']] = $arr;
		    return $result;
			}, array());
		$replace_array = array_replace_recursive($life_array,$health_array);	
		$final_array = array();
		foreach ($replace_array as $key => $value){
			$final_array[$key]['buiz']['life']   = $value['life_business'];
			$final_array[$key]['buiz']['health'] = $value['health_business'];
			$final_array[$key]['life']['login']  = $value['login'];
			$final_array[$key]['life']['pdc']    = $value['pdc'];
		}	
	}
	if($final_array){
		echo json_encode(array('sts' => TRUE, "rslt"=>$final_array,"error"=>""));
	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "No Record Found","rslt"=>"","error"=>""));
	}	
}else
if($frm === "get_pickup_business"){
	$login_qry      = "";
	$login_code     = implode("','",$json->userId);	
	$pro_mode       = $json->pro_mode;
	if((int)$pro_mode === 2){
		$from_date  = date('Y-m-d', strtotime($json->from_date));
		$to_date    = date('Y-m-d', strtotime($json->to_date));
	}else
	if((int)$pro_mode === 3){
		$from_date  = date('Y-m-d', strtotime($json->from_date));
		$to_date    = date('Y-m-d', strtotime($json->to_date));
	}else{
		$from_date  = date('Y-m-01', strtotime($json->from_date));
		$to_date    = date('Y-m-t', strtotime($json->to_date));
	}

	$life_rslt    = $api_model->get_life_business("SELECT pickup_date,login_code,IFNULL(SUM(cw_portfolio.total_net_premium),0) as life_business, SUM(CASE WHEN cw_portfolio.login_status = 1 THEN cw_portfolio.total_net_premium ELSE 0 END) pdc,SUM(CASE WHEN cw_portfolio.login_status = 5 THEN cw_portfolio.total_net_premium ELSE 0 END) login from cw_portfolio where cw_portfolio.pickup_date >= '".$from_date."' and cw_portfolio.pickup_date <= '".$to_date."' and cw_portfolio.trans_status = 1 and login_code in ('".$login_code."') and cw_portfolio.login_status != 6 GROUP BY cw_portfolio.login_code");

	$life_rslt    =  json_decode(json_encode($life_rslt),true);

	$health_rslt  = $api_model->get_health_business("SELECT login_date,login_code,IFNULL(SUM(cw_health.net_premium),0) as health_business from cw_health where cw_health.login_date >= '".$from_date."' and cw_health.login_date <= '".$to_date."' and cw_health.trans_status = 1 and login_code in ('".$login_code."') and cw_health.login_status != 6 GROUP BY cw_health.login_code");
	$health_rslt   =  json_decode(json_encode($health_rslt),true);
	if((int)$pro_mode === 3){
		$life_array    = array_reduce($life_rslt, function ($result, $arr) {
		    $result[$arr['pickup_date']][$arr['login_code']] = $arr;
		    return $result;
		}, array());

		$health_array  = array_reduce($health_rslt, function ($result, $arr) {
		    $result[$arr['login_date']][$arr['login_code']] = $arr;
		    return $result;
		}, array());

		$replace_array = array_replace_recursive($life_array,$health_array);
		$final_array = array();
		foreach ($replace_array as $key => $values){
			//$pickup_date = $values->pickup_date;
			foreach ($values as $code_key => $value) {
				$final_array[$key][$code_key]['buiz']['life']   = $value['life_business'];
				$final_array[$key][$code_key]['buiz']['health'] = $value['health_business'];
				$final_array[$key][$code_key]['life']['login']  = $value['login'];
				$final_array[$key][$code_key]['life']['pdc']    = $value['pdc'];
			}
		}
	}else{
		$life_array    = array_reduce($life_rslt, function ($result, $arr){
		    $result[$arr['login_code']] = $arr;
		    return $result;
			}, array());
		$health_array  = array_reduce($health_rslt, function ($result, $arr) {
		    $result[$arr['login_code']] = $arr;
		    return $result;
			}, array());
		$replace_array = array_replace_recursive($life_array,$health_array);	
		$final_array = array();
		foreach ($replace_array as $key => $value){
			$final_array[$key]['buiz']['life']   = $value['life_business'];
			$final_array[$key]['buiz']['health'] = $value['health_business'];
			$final_array[$key]['life']['login']  = $value['login'];
			$final_array[$key]['life']['pdc']    = $value['pdc'];
		}	
	}
	if($final_array){
		echo json_encode(array('sts' => TRUE, "rslt"=>$final_array,"error"=>""));
	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "No Record Found","rslt"=>"","error"=>""));
	}	
}else
if($frm === "get_team_business"){
	$login_qry = "";
	$pro_mode  = $json->pro_mode;
	$details   = $json->details;
	$arr       = array();
	foreach ($details as $key => $value){
		$login_code = $value->userId;
		$level_1    = $value->level_1;
		$level_2    = $value->level_2;
		if((int)$pro_mode === 2){
			$from_date = date('Y-m-d', strtotime($value->from_date));
			$to_date   = date('Y-m-d', strtotime($value->to_date));
		}else{
			$from_date = date('Y-m-01', strtotime($value->from_date));
			$to_date   = date('Y-m-t', strtotime($value->to_date));
		}

		$life_rslt  = $api_model->get_life_business("SELECT login_date,cw_portfolio.login_code,IFNULL(SUM(cw_portfolio.total_net_premium),0) as life_business, SUM(CASE WHEN cw_portfolio.login_status = 1 THEN cw_portfolio.total_net_premium ELSE 0 END) pdc,SUM(CASE WHEN cw_portfolio.login_status = 5 THEN cw_portfolio.total_net_premium ELSE 0 END) login from cw_portfolio inner join cw_employees on cw_employees.employee_code = cw_portfolio.level_1_reporting_person_code inner join cw_employees level2 on cw_portfolio.level_2_reporting_person_code = level2.employee_code where cw_portfolio.login_date >= '".$from_date."' and cw_portfolio.login_date <= '".$to_date."' and cw_portfolio.trans_status = 1 and cw_portfolio.login_code in ('".$login_code."') and cw_employees.login_code in ('".$level_1."') and level2.login_code in ('".$level_2."') and cw_portfolio.login_status != 6");
		$life_rslt  =  json_decode(json_encode($life_rslt),true);
		$life_business = $life_rslt[0]['life_business'];		
		$login         = $life_rslt[0]['login'];
		$pdc           = $life_rslt[0]['pdc'];
		//print_r($life_rslt); die;
		$health_rslt = $api_model->get_health_business("SELECT login_date,cw_health.login_code,IFNULL(SUM(cw_health.net_premium),0) as health_business from cw_health inner join cw_employees on cw_employees.employee_code = cw_health.level_1_reporting_person inner join cw_employees level2 on cw_health.level_2_reporting_person = level2.employee_code  where cw_health.login_date >= '".$from_date."' and cw_health.login_date <= '".$to_date."' and cw_health.trans_status = 1 and cw_health.login_code in ('".$login_code."') and cw_employees.login_code in ('".$level_1."') and level2.login_code in ('".$level_2."') and cw_health.login_status != 6 GROUP BY cw_health.login_code");
		$health_rslt =  json_decode(json_encode($health_rslt),true);
		$health = $health_rslt[0]['health_business'];
		if(!$life_business){ $life_business = '0.00'; }
		if(!$login){ $login = '0.00'; }
		if(!$pdc){ $pdc = '0.00'; }
		if(!$health){ $health = '0.00'; }
		
		$arr[] = array("user_id"=>$login_code,"level_1"=>$level_1,"level_2"=>$level_2,"from_date"=>$from_date,"to_date"=>$to_date,"life"=>$life_business,"health"=>$health,"login"=>$login,"pdc"=>$pdc);
	}
	if($arr){
		echo json_encode(array('sts' => TRUE, "rslt"=>$arr,"error"=>""));
	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "No Record Found","rslt"=>"","error"=>""));
	}	
}else
if($frm === "get_team_pickup_business"){
	$login_qry = "";
	$pro_mode  = $json->pro_mode;
	$details   = $json->details;
	$arr       = array();
	foreach ($details as $key => $value){
		$login_code = $value->userId;
		$level_1    = $value->level_1;
		$level_2    = $value->level_2;
		if((int)$pro_mode === 2){
			$from_date = date('Y-m-d', strtotime($value->from_date));
			$to_date   = date('Y-m-d', strtotime($value->to_date));
		}else{
			$from_date = date('Y-m-01', strtotime($value->from_date));
			$to_date   = date('Y-m-t', strtotime($value->to_date));
		}

		$life_rslt  = $api_model->get_life_business("SELECT pickup_date,cw_portfolio.login_code,IFNULL(SUM(cw_portfolio.total_net_premium),0) as life_business, SUM(CASE WHEN cw_portfolio.login_status = 1 THEN cw_portfolio.total_net_premium ELSE 0 END) pdc,SUM(CASE WHEN cw_portfolio.login_status = 5 THEN cw_portfolio.total_net_premium ELSE 0 END) login from cw_portfolio inner join cw_employees on cw_employees.employee_code = cw_portfolio.level_1_reporting_person_code inner join cw_employees level2 on cw_portfolio.level_2_reporting_person_code = level2.employee_code where cw_portfolio.pickup_date >= '".$from_date."' and cw_portfolio.pickup_date <= '".$to_date."' and cw_portfolio.trans_status = 1 and cw_portfolio.login_code in ('".$login_code."') and cw_employees.login_code in ('".$level_1."') and level2.login_code in ('".$level_2."') and cw_portfolio.login_status != 6");
		$life_rslt  =  json_decode(json_encode($life_rslt),true);
		$life_business = $life_rslt[0]['life_business'];		
		$login         = $life_rslt[0]['login'];
		$pdc           = $life_rslt[0]['pdc'];
		//print_r($life_rslt); die;
		$health_rslt = $api_model->get_health_business("SELECT login_date,cw_health.login_code,IFNULL(SUM(cw_health.net_premium),0) as health_business from cw_health inner join cw_employees on cw_employees.employee_code = cw_health.level_1_reporting_person inner join cw_employees level2 on cw_health.level_2_reporting_person = level2.employee_code where cw_health.login_date >= '".$from_date."' and cw_health.login_date <= '".$to_date."' and cw_health.trans_status = 1 and cw_health.login_code in ('".$login_code."') and cw_employees.login_code in ('".$level_1."') and level2.login_code in ('".$level_2."') and cw_health.login_status != 6 GROUP BY cw_health.login_code");
		$health_rslt =  json_decode(json_encode($health_rslt),true);
		$health = $health_rslt[0]['health_business'];
		if(!$life_business){ $life_business = '0.00'; }
		if(!$login){ $login = '0.00'; }
		if(!$pdc){ $pdc = '0.00'; }
		if(!$health){ $health = '0.00'; }
		$arr[] = array("user_id"=>$login_code,"level_1"=>$level_1,"level_2"=>$level_2,"from_date"=>$from_date,"to_date"=>$to_date,"life"=>$life_business,"health"=>$health,"login"=>$login,"pdc"=>$pdc);
	}
	if($arr){
		echo json_encode(array('sts' => TRUE, "rslt"=>$arr,"error"=>""));
	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "No Record Found","rslt"=>"","error"=>""));
	}	
}else{
	echo json_encode(array('sts' => FALSE, "msg" => "Bad Request","rslt"=>"","error"=>""));
}
function return_rslt($mysql_portfolio_rslt,$mysql_health_rslt){
	if((int)$mysql_portfolio_rslt[0]->count || (int)$mysql_health_rslt[0]->count){
		echo json_encode(array('sts' => TRUE, "msg" => "Record Found","rslt"=>"","error"=>""));
	}else{
		echo json_encode(array('sts' => FALSE, "msg" => "No Record Found","rslt"=>"","error"=>""));
	}
}
?>