<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$success = false;

$file = $_REQUEST["file"];

$keyfld = "vouchno";
$party = $_REQUEST["party"];
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["isearch"];
$operator = $_REQUEST["ioperator"];
$godown = $_REQUEST["godown"];

if (!empty($godown)) {
    $gdcond = "and `godown`='$godown'";
}
$cond = "";
if ($operator == "=") {
    if ($_REQUEST["ifield"] == "1") {
        $cond = "AND `prodname` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "2") {
        $cond = "AND `size` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "3") {
        $cond = "AND `brand` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "4") {
        $cond = "AND `quality` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "5") {
        $cond = "AND `code` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "6") {
        $cond = "AND `gst` = '$searchTerm'";
    }
} else {
    
    if ($_REQUEST["ifield"] == "1") {
        $cond = "AND `prodname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "2") {
        $cond = "AND `size` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "3") {
        $cond = "AND `brand` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "4") {
        $cond = "AND `quality` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "5") {
        $cond = "AND `code` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "6") {
        $cond = "AND `gst` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `prodname`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `prodname` ";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `size`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `brand`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `quality`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `code`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `gst`";
}
$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

$query = "SELECT * FROM `item` WHERE code != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
$d1 = $d2 = $d3 = $d4 = 0;

mysqli_query($conn, "Create temporary table `stkdb` SELECT sum(qty) as tqty,prodcode FROM `transacn` WHERE prodcode != '' $gdcond group by prodcode,godown")or die(mysqli_error($conn));

while ($r = mysqli_fetch_assoc($result)) {

    $getm = mysqli_query($conn, "SELECT tqty FROM `stkdb` WHERE prodcode = '$r[code]'")or die(mysqli_error($conn));

    if (!empty($getm)) {
        $m = mysqli_fetch_array($getm);
        $stkqty = $m['tqty'];
        if ($stkqty != 0) {
            $stqty = $stkqty;
        } else {
            $stqty = 0;
        }
    } else {
        $stqty = 0;
    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'file' => $file,
        'code' => $r['code'],
        'prodtype' => $r['prodtype'],
        'prodname' => $r['prodname'],
        'size' => $r['size'],
        'brand' => $r['brand'],
        'pcsinunit' => $r['pcsinunit'],
        'hsncode' => $r['hsncode'],
        'quality' => $r['quality'],
        'gst' => $r['gst'],
        'unit' => $r['unit'],
        'stkqty' => $stqty
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
