<?php
// inwardent_new_search.php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$file = 'inward.php';
$keyfld = "vouchno";
$rangeFrom = $_REQUEST["rangeFrom"] ?? 0;
$rangeTo = $_REQUEST["rangeTo"] ?? 40;
$searchTerm = $_REQUEST["ssearch"] ?? '';
$operator = $_REQUEST["soperator"] ?? '%';

$cond = "";
$field = $_REQUEST["sfield"] ?? '1';

if ($field != "" && $searchTerm != "") {
    $searchTerm = mysqli_real_escape_string($conn, $searchTerm);
    
    if ($operator == "=") {
        if ($field == "1") {
            $cond = "AND m.`vouchno` = '$searchTerm'";
        } else if ($field == "2") {
            $cond = "AND DATE_FORMAT(m.`vouchdt`, '%d/%m/%Y') = '$searchTerm'";
        } else if ($field == "3") {
            $cond = "AND t.`barcode` = '$searchTerm'";
        } else if ($field == "4") {
            $cond = "AND t.`itemname` = '$searchTerm'";
        } else if ($field == "5") {
            $cond = "AND t.`batch` = '$searchTerm'";
        }
    } else {
        if ($field == "1") {
            $cond = "AND m.`vouchno` LIKE '%$searchTerm%'";
        } else if ($field == "2") {
            $cond = "AND DATE_FORMAT(m.`vouchdt`, '%d/%m/%Y') LIKE '%$searchTerm%'";
        } else if ($field == "3") {
            $cond = "AND t.`barcode` LIKE '%$searchTerm%'";
        } else if ($field == "4") {
            $cond = "AND t.`itemname` LIKE '%$searchTerm%'";
        } else if ($field == "5") {
            $cond = "AND t.`batch` LIKE '%$searchTerm%'";
        }
    }
}

$sortcond = "ORDER BY m.`vouchno`";
$sortBy = $_REQUEST["sortBy"] ?? '1';
if ($sortBy == "1") {
    $sortcond = " ORDER BY m.`vouchno`";
} else if ($sortBy == "2") {
    $sortcond = " ORDER BY m.`vouchdt`";
} else if ($sortBy == "3") {
    $sortcond = " ORDER BY t.`barcode`";
} else if ($sortBy == "4") {
    $sortcond = " ORDER BY t.`itemname`";
} else if ($sortBy == "5") {
    $sortcond = " ORDER BY t.`batch`";
}

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

// Get all transaction records with details
$query = "SELECT 
            m.vouchno,
            DATE_FORMAT(m.vouchdt, '%d/%m/%Y') as vouchdt,
            m.supplier,
            g.name as suppname,
            t.srno,
            t.barcode,
            t.itemname,
            t.batch,
            t.qty,
            t.unit,
            u.name as unitname,
            CONCAT('$file?mode=edit&$keyfld=', m.vouchno) as action
          FROM inwmain m
          INNER JOIN inwtran t ON m.vouchno = t.vouchno
          LEFT JOIN accmast g ON m.supplier = g.accode
          LEFT JOIN $zroot.unit u ON t.unit = u.code
          WHERE m.`vouchno` != ''  $cond 
          $sortcond  $ordcond 
          LIMIT $rangeFrom, $rangeTo";

$result = mysqli_query($conn, $query);

$rows = array();
if ($result && mysqli_num_rows($result) > 0) {
    while ($r = mysqli_fetch_assoc($result)) {
        $rows[] = [
            'success' => true,
            'vouchno'   => $r['vouchno'],
            'vouchdt'   => $r['vouchdt'],
            'srno'      => $r['srno'],
            'barcode'   => $r['barcode'] ?? '',
            'itemname'  => $r['itemname'] ?? '',
            'batch'     => $r['batch'] ?? '',
            'qty'       => $r['qty'] ?? '',
            'unitname'  => $r['unitname'] ?? '',
            'action'    => $r['action']
        ];
    }
}

header("Content-type: application/json");
echo json_encode($rows);
?>