<?php

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

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

$json_data = json_decode($invoice_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 imtex_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'];
            $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("imtex_invoice", $finalArray);
            unset($f1);
            unset($f2);
            unset($f3);
            unset($f4);
            unset($f5);
            unset($finalArray);
            $i++;
        }
    }
    echo '{"response":"1","message":"success"}';
}
        
    $einvoice_connect->close();    
}else{
$vertical = $json_data["authentication"]["vertical"];

$code = $json_data["authentication"]["invoice_data"]["code"];
$street = $json_data["authentication"]['invoice_data']['street'];
$cityCode = $json_data["authentication"]['invoice_data']['cityCode'];
$cityDescription = $json_data["authentication"]['invoice_data']['cityDescription'];
$stateCode = $json_data["authentication"]['invoice_data']['stateCode'];
$stateDescription = $json_data["authentication"]['invoice_data']['stateDescription'];
$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'];
$invoiceDate = str_replace('/', '-', $invoiceDate);
$final_invoice_date = date('Y-m-d', strtotime($invoiceDate));

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

if (strpos($code, 'MEM') !== false) {
    $code = ltrim(preg_replace("/[^0-9]/", "", $code), '0');
}

$insert_data['custAccount'] = $code;
$insert_data['invoicedate'] = $final_invoice_date;
$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['invoicenumber'] = $invoiceNumber;
$insert_data['currency'] = $currency;
$insert_data['eventCode'] = $eventCode;
$insert_data['type'] = 'Business,Home';
$insert_data['vertical'] = 'Common';
$insert_data['location'] = 'Bangalore';
$insert_data['gstNumber'] = $gstNumber;
$insert_data['sac'] = $sac;

$insert_data['AdCharges'] = $json_data["authentication"]['invoice_data']['invoiceAmount'];

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

unset($insert_data);

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

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