<?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;

$vouchdt = $_REQUEST["vouchdt"];
$file = $_REQUEST["file"];
if ($file == 'sale_ti.php' || $file == 'sale.php' || $file == 'crednote.php' || $file == 'invoexp.php' || $file == 'sale_ex.php' || $file == 'damaged.php') {
    $rate = "rate";
} else {
    $rate = "cost";
}

$keyfld = "vouchno";
$party = $_REQUEST["party"];
$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 `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 *, DATE_ADD('$vouchdt', INTERVAL bbf MONTH) AS expdate FROM `product` WHERE barcode != '' $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
//print table($result);
//die();
$rows = array();
$ctr = 0;
$d1 = $d2 = $d3 = $d4 = 0;
while ($r = mysqli_fetch_assoc($result)) {

//    $expdate = date("Y-m-d", strtotime("+" . $r['bbf'] . " months", strtotime($vouchdt)));

    $bbfmon = date("m", strtotime($r['expdate']));
    $bbfyear = date("Y", strtotime($r['expdate']));
    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'file' => $file,
        'name' => $r['name'],
        'barcode' => $r['barcode'],
        'itemcode' => $r['code'],
        'type' => $r['type'],
        'unit' => $r['unit'],
        'bbfmon' => "$bbfmon",
        'bbfyear' => "$bbfyear",
        'batch' => $r['batch'],
        'bbf' => $r['bbf'],
        'origin' => $r['origin']
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
