<?php

require_once "LN_rest_include.php";
require_once "LN_loginext_include.php";
require_once "LN_votian_include.php";

checkRequest("orderUpdate");

/*
 * - orderReferenceId
 * - startTimeWindow
 * - endTimeWindow
 * */
function orderUpdate() {

    $postData = json_decode(file_get_contents('php://input'), true);

    $lnOrderReferenceId = $postData['orderReferenceId'];
    $lnStartTimeWindow = $postData['startTimeWindow'];
    $lnEndTimeWindow = $postData['endTimeWindow'];
    $lnOrderNo = $postData['orderNo'];

    if($lnOrderReferenceId && $lnStartTimeWindow && $lnEndTimeWindow) {

        $orderGetData = array(
            "orderReferenceId" => $lnOrderReferenceId,
            "order_no" => $lnOrderNo,
            // "end_date" => $lnEndTimeWindow,
            // "start_date" => $lnStartTimeWindow,
        );

        $myfile = fopen(dirname(__FILE__)."/"."webhookLogNew".date("m-Y").".txt", "a");
        fwrite($myfile,"-------".$postData['orderNo']."----------------REClogiNextMileOrderGetFirst-------------------". gmdate("Y-m-d\TH:i:s\Z") ."--------");
        fclose($myfile);

        $lnOrderGetResponse = logiNextMileOrderGet($orderGetData);

        myfile = fopen(dirname(__FILE__)."/"."webhookLogNew".date("m-Y").".txt", "a");
        fwrite($myfile,"-------".$postData['orderNo']."----------------ANSlogiNextMileOrderGetFirst-------------------". gmdate("Y-m-d\TH:i:s\Z") ."--------");
        fclose($myfile);

        if($lnOrderGetResponse[1]['status'] == 200 && $lnOrderGetResponse[1]['message'] == "SUCCESS") {

            $votianOrderRequestResponse = syncDataWithVotian($postData, $lnOrderGetResponse[1]);

            if($votianOrderRequestResponse) {

                echo json_encode($successArray);

            } else {

                error("The order could not be synchronized with the votian");

            }


        } else {

            error("No order Details could be found in LN-System");

        }

    } else {

        error("Missing order details");

    }

}