<?php

include_once "../conn.php";

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

//$link = mssql_connect('192.168.1.30', 'sa', 'sap123');

$_SESSION['itemcode'] = $_GET['itemcode'];
$_SESSION['dep'] = $_GET['dep'];
$_SESSION['database'] = $_GET['database'];
$_SESSION['flag'] = $_GET['flag'];

if ($_SESSION['flag'] == "1") {

    $link = mssql_connect('10.0.0.50', 'sa', 'sap123');

    if (!$link) {
        die('Unable to connect!');
    }

    if (!mssql_select_db('dkfadbnew', $link)) {
        die('Unable to select database!');
    }

    $resultcodigo = mssql_query("select top(1) itemcode,onhand from oitw where itemcode='" . $_SESSION['itemcode'] . "'  and whscode='" . $_SESSION['dep'] . "'  ");

    $msg = array("pr_id" => $SESSION['itemcode'], "cant" => "0", "succes" => "false");

    while ($row = mssql_fetch_array($resultcodigo)) {

        $_SESSION['pr_id'] = $row['itemcode'];

        $_SESSION['cant'] = $row['onhand'];

        $msg = array("pr_id" => $row['itemcode'], "cant" => $row['onhand'], "succes" => "true");

    }

} else {

    $resultcodigo = "select pr_id,p_existencia from " . $_SESSION['database'] . ".prod_existencia where pr_id='" . $_SESSION['itemcode'] . "'  and p_existencia_dep_id='" . $_SESSION['dep'] . "'  ";

//echo $resultcodigo;

    $qur = $connection->query($resultcodigo);

    $msg = array("pr_id" => $SESSION['itemcode'], "cant" => "0", "succes" => "false0");

    while ($row = mysqli_fetch_assoc($qur)) {

        $_SESSION['pr_id'] = $row['pr_id'];

        $_SESSION['cant'] = $row['p_existencia'];

        $msg = array("pr_id" => $row['pr_id'], "cant" => $row['p_existencia'], "succes" => "true");

    }
}

$jsonData = $msg;

// header('content-type: application/json');
echo json_encode($jsonData);
//echo $json;
mssql_free_result($result);

?>
