<?php

require("../config/config.php");

$member_data = @file_get_contents('php://input');

$json_data = json_decode($member_data, true);

if (empty($json_data)) {
    echo '{"response":"0","message":"Invalid JSON Format"}';
    die();
}

$api_key = $json_data["authentication"]["api_key"];

if (empty($api_key)) {
    echo '{"response":"0","message":"API key cannot be blank"}';
    die();
}

if ($api_key != GLOBAL_APP_API_KEY) {
    echo '{"response":"0","message":"Invalid API Key"}';
    die();
}
$push_type = $json_data["authentication"]["push_type"];

	$membershipNumber = $json_data["authentication"]["member_data"]["membershipNumber"];
	$companyName = $json_data["authentication"]['member_data']['companyName'];
	$memberType = $json_data["authentication"]['member_data']['memberType'];
	$categoryGroup = $json_data["authentication"]['member_data']['categoryGroup'];
	$currency = $json_data["authentication"]['member_data']['currency'];
	$panNumber = $json_data["authentication"]['member_data']['panNumber'];
	$serviceTaxNo = $json_data["authentication"]['member_data']['serviceTaxNo'];
        $gstNumber = $json_data["authentication"]['member_data']['gstNumber'];

	$insert_data['companyName'] = $companyName;
	$extract_number = ltrim(preg_replace("/[^0-9]/","",$membershipNumber), '0');
	$insert_data['membershipNumber'] = $extract_number;
	$insert_data['memberType'] = $memberType;
	$insert_data['categoryGroup'] = $categoryGroup;
	$insert_data['currency'] = $currency;
	$insert_data['panNumber'] = $panNumber;
	$insert_data['serviceTaxNo'] = "";
        $insert_data['gstNumber'] = $gstNumber;
	$insert_data['locationDescription'] = "Business";

	if(!empty($json_data["authentication"]['member_data']['cityCode'])){
		$insert_data['city'] = $json_data["authentication"]['member_data']['cityCode'];
	}else{
		$insert_data['city'] = "Temp";
	}

	if(!empty($json_data["authentication"]['member_data']['cityDescription'])){
		$insert_data['cityDescription'] = $json_data["authentication"]['member_data']['cityDescription'];
	}else{
		$insert_data['cityDescription'] = "Temp";
	}

	if(!empty($json_data["authentication"]['member_data']['stateCode'])){
		$insert_data['state'] = $json_data["authentication"]['member_data']['stateCode'];
	}else{
		$insert_data['state'] = "Temp";
	}

	if(!empty($json_data["authentication"]['member_data']['stateDescription'])){
		$insert_data['stateDescription'] = $json_data["authentication"]['member_data']['stateDescription'];
	}else{
		$insert_data['stateDescription'] = "Temp";
	}

	$insert_data['street'] = $json_data["authentication"]["member_data"]["street"];
	$insert_data['country'] =  $json_data["authentication"]["member_data"]["countryCode"];
	$insert_data['zipCode'] =  $json_data["authentication"]['member_data']['zipCode'];
	$insert_data['phone'] = $json_data["authentication"]['member_data']['phone'];

	$insert_data['type'] = 'Business,Home';

	if($json_data["authentication"]["member_data"]["resignedStatus"] == "no"){
		$insert_data['inactive'] = 0;
	}else{
		$insert_data['inactive'] = 1;
	}

	//$exist_sql = "select membershipNumber,recordID from membershipstagingdb where membershipNumber = '".$db_erp->escape($membershipNumber)."'";
	//$result = $db_erp->query_first($exist_sql);
	//if($db_erp->affected_rows > 0){
	//	$update_id = $result['recordID'];
	//	$db_erp->query_update("membershipstagingdb",$insert_data,"recordId='".$update_id."'");
	//}else{
		$insert_id = $db_erp->query_insert("membershipstagingdb", $insert_data);
	//}

	unset($insert_data);

	if (!empty($insert_id)) {
	    echo '{"response":"1","message":"Success"}';
	}
$db_erp->close();
die();
?>
