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_bk15JUL2021.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_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 === 1){
		$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_code,IFNULL(SUM(cw_portfolio.net_premium),0) as life_business, SUM(CASE WHEN cw_portfolio.login_status = 1 THEN cw_portfolio.net_premium ELSE 0 END) pdc,SUM(CASE WHEN cw_portfolio.login_status = 5 THEN cw_portfolio.net_premium ELSE 0 END) login from cw_portfolio where cw_portfolio.login_date >= '".$from_date."' and cw_portfolio.login_date <= '".$to_date."' and login_code in ('".$login_code."') and cw_portfolio.trans_status = 1 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_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 login_code in ('".$login_code."') and cw_health.trans_status = 1 and cw_health.login_status != 6 GROUP BY cw_health.login_code");
	//echo "SELECT 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 login_code in ('".$login_code."') and cw_health.trans_status = 1 and cw_health.login_status != 6 GROUP BY cw_health.login_code"; die;
	$health_rslt   =  json_decode(json_encode($health_rslt),true);

	$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{
	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"=>""));
	}
}
?>