<?php

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

$keycond = " AND `billdone`!='Y' ";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$usertype = $_SESSION["usertype_" . $dbname];

//$zroot = $_SESSION["zdbroot"];

$success = false;

$cust = 'accmast';

$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'kotmain';

$file = 'bill.php';

$keyfld = "vouchno";

$series = $_REQUEST["series"];
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];



$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `vouchtime` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `user` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `totitems` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `totqty` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `netamt` = '$searchTerm'";
    }
} else {

    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `vouchtime` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `user` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `totitems` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `totqty` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `netamt` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `vouchno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `vouchno`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `vouchdt`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `vouchtime`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `name`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `user`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `totitems`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY `totqty`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY `netamt`";
}

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


//$query = "SELECT a.vouchno, a.tableno, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt, a.vouchtime, a.user, a.totitems, a.totqty, a.netamt,  b.name as tablename
//          FROM `$maindb` a, `tablemast` b where a.tableno = b.code $keycond $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$query = "SELECT * FROM `$maindb` where vouchno != '' $keycond $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
$parcel = '';

$delkot = 0;
if ($usertype == "W" || $usertype == "K") {
    $delkot = 0;
} else {
    $delkot = 1;
}
while ($r = mysqli_fetch_assoc($result)) {
    $gttable = mysqli_query($conn, "Select name from tablemast where code='$r[tableno]'") or die(mysqli_error($conn));
    $fttable = mysqli_fetch_array($gttable);
    if ($r['parcel'] == 'Y') {
        $parcel = 'parcel.png';
        $tabname = "<img style='height:25px;width:50px' src='$parcel' alt=''><input type='hidden' class='kotdirect' value='P'>";
    } else {
        $parcel = '';
        $tabname = $fttable['name'] . "<input type='hidden' class='kotdirect' value='I'>";
    }
    if ($delkot == 1) {
        $contvouch = $r['vouchno'];
        $del = "<td align='left' class='deldata'><i class='fa fa-trash'></i></td>" .
                "<td align='center' class='contordtd'><i class='fa fa-plus'></i></td>";
    } else {
        $del = '';
    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'vouchno' => $r['vouchno'],
        'vouchdt' => $r['vouchdt'],
        'vouchtime' => $r['vouchtime'],
        'user' => $r['user'],
        'tablename' => $tabname,
        'totitems' => $r['totitems'],
        'totqty' => $r['totqty'],
        'netamt' => $r['netamt'],
        'parcel' => $parcel,
        'delkot' => $delkot,
        'del' => $del,
        'action' => "$file?mode=edit&$keyfld=$r[$keyfld]&para=P",
    ];
    $ctr++;
}
//browse($rows); 
header("Content-type: application\json");
echo json_encode($rows);
