<?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 == 'sale_ti.php' || $file == 'crednote.php') {
    $rate = "rate";
} else {
    $rate = "cost";
}

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

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

$sortcond = "ORDER BY `name`+0";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `name`+0";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `hsncode`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `gstperc`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `unit`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY $rate";
}

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

if ($type == 'P') {
    $query = "SELECT `name`,  `unit`, `hsncode`, `gstperc`, `$rate`
          FROM `packing` WHERE name != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
} else {
    $query = "SELECT `name`,  `unit`, `hsncode`, `gstperc`, `$rate`
          FROM `item` WHERE name != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
}


$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
    $ra = $r[$rate];


    if ($n > 0) {
        $success = true;
    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'file' => $file,
        'name' => $r['name'],
        'unit' => $r['unit'],
        'rate' => $ra,
        'hsncode' => $r['hsncode'],
        'gstperc' => $r['gstperc']
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
