<?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();
}
if( $json_data["authentication"]["invoice_data"]['einvoice']==1){ //pushing data to einvoice database
    $einvoice = $json_data["authentication"]['invoice_data'];
    $einvoice_connect = new mysqlconnect(EINVOICE_DB_SERVER, EINVOICE_DB_USER, EINVOICE_DB_PASS, EINVOICE_DB_NAME);
    $einvoice_connect->connect(true);

    $dataFinal= array();

    $f1 = $f2 = $f3 = $f4 = $f5= array();
    $array_data = $json_data["authentication"]["invoice_data"];
    
    $sql = "SELECT id FROM membership_invoice Where Document_Num = '" . $array_data['general']['Document_Num'] . "' ";
    $result = $einvoice_connect->query_first($sql);
    if (empty($result['id'])) {
    if(count($array_data['lineItems'])>0){
        $i = 1;
        foreach($array_data['lineItems'] as $key => $value){
            
			$f1 = $GLOBALS['einvoiceSupplier']['general'];
			
			if($array_data['is_sez'] == 1){
				$GLOBALS['einvoiceSupplier']['general']['supply_type_code'] = 'SEZWP';
				$f1 = $GLOBALS['einvoiceSupplier']['general'];
			}
            $f2 = $array_data['general'];
            $f3 = $array_data['lineItems'][$key];
            $f4 = $array_data['finalValues'];

            $f5['SL_No'] = $i;

            $finalArray = array_merge($f1,$f2,$f3,$f4,$f5);
            $insert_id = $einvoice_connect->query_insert("membership_invoice", $finalArray);
            unset($f1);
            unset($f2);
            unset($f3);
            unset($f4);
            unset($f5);
            unset($finalArray);
            $i++;
        }
    }
    echo '{"response":"1","message":"success"}';
}
        
    $einvoice_connect->close();    
}else{
	
$membershipNumber = $json_data["authentication"]["invoice_data"]["membershipNumber"];
$companyName = $json_data["authentication"]['invoice_data']['companyName'];
$street = $json_data["authentication"]['invoice_data']['street'];
$cityDescription = $json_data["authentication"]['invoice_data']['cityDescription'];
$cityCode = $json_data["authentication"]['invoice_data']['cityCode'];
$stateDescription = $json_data["authentication"]['invoice_data']['stateDescription'];
$stateCode = $json_data["authentication"]['invoice_data']['stateCode'];
$countryCode = $json_data["authentication"]['invoice_data']['countryCode'];
$currency = $json_data["authentication"]['invoice_data']['currency'];
$zipCode = $json_data["authentication"]['invoice_data']['zipCode'];

$invoiceDate = $json_data["authentication"]['invoice_data']['invoiceDate'];
$entranceAmount = $json_data["authentication"]['invoice_data']['entranceAmount'];
$membSubFee = $json_data["authentication"]['invoice_data']['subscriptionAmount'];
$invoiceNumber = $json_data["authentication"]['invoice_data']['invoiceNumber'];

$sac = $json_data["authentication"]['invoice_data']['sac'];
$gstNumber = $json_data["authentication"]['invoice_data']['gstNumber'];

$extract_number = ltrim(preg_replace("/[^0-9]/","",$membershipNumber), '0');
$insert_data['custAccount'] = $extract_number;

$insert_data['type'] = "Business, Home";
$insert_data['street'] = $street;
$insert_data['city'] = $cityCode;
$insert_data['cityDescription'] = $cityDescription;
$insert_data['state'] = $stateCode;
$insert_data['stateDescription'] = $stateDescription;
$insert_data['country'] = $countryCode;
$insert_data['zipCode'] = $zipCode;
$insert_data['currency'] = $currency;
$insert_data['invoicedate'] = $invoiceDate;
$insert_data['invoicenumber'] = $invoiceNumber;

$insert_data['sac'] = $sac;
$insert_data['gstNumber'] = $gstNumber;

$insert_data['EntranceFee'] = $entranceAmount;
$insert_data['membSubFee'] = $membSubFee;
$insert_data['vertical'] = "MEMBERSHIP";

$insert_id = $db_erp->query_insert("membershipstagingftidb", $insert_data);

unset($insert_data);

if (!empty($insert_id)) {
    echo '{"response":"1","message":"success"}';
}

$db_erp->close();
}
die();
?>