<?php

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

$zdb = $_SESSION["zdbyear_$dbname"];
$ztit = $_SESSION["tit"];
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];
$keyfld = "vouchno";
//$zseries = $_SESSION["user_series"];
//browse($zseries);
$success = true;
$maindb = 'transacn';


mysqli_select_db($conn, $zdb);
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];
$zseries = $_REQUEST["series"];

$cond = "";
if ($_REQUEST["sfield"] != "") {
    if ($operator == "E") {
        if ($_REQUEST["sfield"] == "1") {
            $cond = "AND `itemname` = '$searchTerm'";
        }
    } else {
        if ($_REQUEST["sfield"] == "1") {
            $cond = "AND `itemname`  LIKE '%$searchTerm%'";
        }
    }
}

$query = "SELECT * FROM `$maindb` WHERE `series`='$zseries' and source = 'OP'
                   ";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));
$count = mysqli_num_rows($result);
$rows = array();
$ctr = 1;
while ($r = mysqli_fetch_object($result)) {


    $rows[] = ['success' => $success,
        'count' => $count,
        'ctr' => $ctr,
        'itemcode' => $r[itemcode],
        'itemname' => $r[itemname],
        'size' => $r[size],
        'actsize' => $r[actsize],
        'unit' => $r[unit],
        'opstk' => $r[opstk],
        'series' => $zseries,
        'minstk' => $r[minstk]
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
