<?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 = 'transfer.php';

$keyfld = "vouchno";
$series = $_REQUEST["series"];
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["insearch"];
$operator = $_REQUEST["inoperator"];

$cond = "";
if ($_REQUEST["infield"] != "") {
    if ($operator == "=") {
        if ($_REQUEST["infield"] == "1") {
            $cond = "AND `vouchno` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "2") {
            $cond = "AND `vouchdt` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "3") {
            $cond = "AND `fromcode` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "4") {
            $cond = "AND `tocode` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "5") {
            $cond = "AND `chalno` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "6") {
            $cond = "AND `totqty` = '$searchTerm'";
        } else if ($_REQUEST["infield"] == "7") {
            $cond = "AND `rem1` = '$searchTerm'";
        }
    } else {
        if ($_REQUEST["infield"] == "1") {
            $cond = "AND `vouchno` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "2") {
            $cond = "AND `vouchdt` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "3") {
            $cond = "AND `fromcode` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "4") {
            $cond = "AND `tocode` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "5") {
            $cond = "AND `chalno` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "6") {
            $cond = "AND `totqty` LIKE '%$searchTerm%'";
        } else if ($_REQUEST["infield"] == "7") {
            $cond = "AND `rem1` LIKE '%$searchTerm%'";
        }
    }
}

$query = "SELECT `vouchno`, `vouchdt`, `fgodown`, `tgodown`, `chalno`, `totqty`, `remark1` 
        FROM `trnfmain` $cond ORDER BY `vouchno` DESC LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
    $rows[] = [ 'success' => true,
        'vouchno' => $r[vouchno],
        'from' => $r[fromcode],
        'to' => $r[tocode],
        'vouchdt' => $r[vouchdt],
        'chalno' => $r[chalno],
        'totqty' => $r[totqty],
        'rem1' => $r[rem1],
        'action' => "<a href='$file?mode=edit&$keyfld=$r[vouchno]'>"
    ];
}
header("Content-type: application\json");
echo json_encode($rows);
