<?php
require_once("../../includes/functions_internal_user_dryout.inc.php");
$obj        = new data();
$connect    = $obj->funConnectMysqli();
// $headers    = getallheaders();
// $auth_token = addslashes(trim($headers['Auth-Token']));

if ($_REQUEST['auth_token'] != "6obDl3xsdutQ2Co1n3T4al") {
    echo json_encode(array('status' => 401, 'message' => 'Invalid Auth Token'));
    die;
}

if ($_SERVER['REQUEST_METHOD'] != "POST") {
    echo json_encode(array('status' => 400, 'message' => 'Bad Request, Only POST Method is Allowed'));
    die;
}




$id                = addslashes(trim($_REQUEST['id']));
$voucherType       = addslashes(trim($_REQUEST['voucherType']));
if ($_REQUEST['voucherType'] == 'FREIGHT INVOICE RECEIPT') {
    $sql_main          = "select * from freight_invoice_master where INVOICEID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update freight_invoice_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where INVOICEID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'Vc payment') {

    $sql_main          = "select * from request_master where REQ_ID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update request_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where REQ_ID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'VC HIRE PAYMENT') {

    $sql_main          = "select * from invoice_hire_master where INVOICEID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update invoice_hire_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where INVOICEID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'Tc Hire Invoice') {

    $sql_main          = "select * from invoice_tchire_master where INVOICEID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update invoice_tchire_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where INVOICEID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'Tc Hire Payment') {

    $sql_main          = "select * from invoice_hiretc_master where INVOICEID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update invoice_hiretc_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where INVOICEID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'Tc Other Invoice') {

    $sql_main          = "select * from invoice_tcother_master where INVOICEID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update invoice_tcother_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where INVOICEID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else if ($_REQUEST['voucherType'] == 'Tc Other Payment') {

    $sql_main          = "select * from payment_tcother_master where PAYMENTID='" . $id . "' and SYNC_STATUS=1";
    // echo  $sql_main;
    $res_main          = mysqli_query($connect, $sql_main) or ($sql_main);
    $rec_main          = mysqli_num_rows($res_main);
    $arraydata         = array();
    if ($rec_main > 0) {
        while ($rows_main = mysqli_fetch_assoc($res_main)) {
            // $sync_status = ($Interface_Success == "true") ? 2 : 1;

            $sql_com   = "update payment_tcother_master set SYNC_STATUS=2, RECEIPT_DATE=NOW() where PAYMENTID='" . $id . "'";
            $res_main  = mysqli_query($connect, $sql_com) or ($sql_com);
            if ($res_main) {
                echo json_encode(array('status' => 200, 'data' => [], 'message' => 'Request Successfull'));
                die;
            } else {
                echo json_encode(array('status' => 503, 'data' => [], 'message' => 'Request UnSuccessfull'));
                die;
            }
        }
    } else {
        echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
        die;
    }
} else {
    echo json_encode(array('status' => 404, 'data' => $arraydata, 'message' => 'Data Not Found'));
    die;
}
