<?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"];
if ($file == 'saleret.php' || $file == 'sale.php' || $file == 'crednote.php' || $file == 'invoexp.php' || $file == 'sale_ex.php' || $file == 'damaged.php') {
    $rate = "rate";
} else {
    $rate = "(cost-boxcost)";
}

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

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["ifield"] == "1") {
        $cond = "AND `name` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "2") {
        $cond = "AND `barcode` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "3") {
        $cond = "AND `prodcode` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "4") {
        $cond = "AND `gram` = '$searchTerm'";
    } else if ($_REQUEST["ifield"] == "5") {
        $cond = "AND `unit` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["ifield"] == "1") {
        $cond = "AND `name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "2") {
        $cond = "AND `barcode` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "3") {
        $cond = "AND `prodcode` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "4") {
        $cond = "AND `gram` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["ifield"] == "5") {
        $cond = "AND `unit` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `code`+0";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `name`+0";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `barcode`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `prodcode`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `gram`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `unit`";
}

$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

$query = "SELECT *,$rate as price
          FROM `product` WHERE barcode != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));
mysqli_query($conn, "Create temporary table `stktab` SELECT sum(qty) as tqty,itemcode FROM `transacn` where godown = '$godown' group by itemcode")or die(mysqli_error($conn));
$rows = array();
$ctr = 0;
$d1 = $d2 = $d3 = $d4 = 0;
while ($r = mysqli_fetch_assoc($result)) {
    $ra = $r['price'];
    
    $getm = mysqli_query($conn, "SELECT tqty FROM `stktab` WHERE `itemcode`='$r[code]'")or die(mysqli_error($conn));
//    print table($getm);
    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,
        'name'     => $r['name'],
        'barcode'  => $r['barcode'],
        'itemcode' => $r['prodcode'],
        'gram'     => $r['gram'],
        'unit'     => $r['unit'],
        'rate'     => $ra,
        'stk'      => $stqty,
        'hsncode'  => $r['hsncode'],
        'mrp'      => $r['mrp'],
        'cartqty'  => $r['cartqty'],
        'gstperc'  => $r['gstperc']
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
