<?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 = $_REQUEST["file"];
if ($file == 'sale_ti.php' || $file == 'crednote.php' || $file == 'invoexp.php' || $file == 'sale_ex.php' || $file == 'damaged.php') {
    $rate = "rate";
} else {
    $rate = "cost";
}
$maindb = "inwmfmain";
$trandb = "inwmftran";
$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 a*.vouchno, a.vouchdt, a.godown, b.itename, b.qty FROM `inwmfmain` AS a JOIN `inwmftran` AS b WHERE a.vouchno = b.vouchno $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
    $ra = $r[rate];
    $gel = $r[gel];

    if ($gel == 1) {
        $mdisc = mysqli_query($conn, "SELECT `gdisc1`, `gdisc2`, `gdisc3`, `gdisc4` FROM `accmast` WHERE `accode`='$party'") or die(mysqli_error($conn));
        $fdisc = mysqli_fetch_array($mdisc);

        $d1 = $fdisc[gdisc1];
        $d2 = $fdisc[gdisc2];
        $d3 = $fdisc[gdisc3];
        $d4 = $fdisc[gdisc4];
        
    } else {
        $mdisc = mysqli_query($conn, "SELECT `disc1`, `disc2`, `disc3`, `disc4` FROM `accmast` WHERE `accode`='$party'") or die(mysqli_error($conn));
        $fdisc = mysqli_fetch_array($mdisc);

        $d1 = $fdisc[disc1];
        $d2 = $fdisc[disc2];
        $d3 = $fdisc[disc3];
        $d4 = $fdisc[disc4];
    }

    if (!empty($d1)) {
        $discval = ($ra * $d1) / 100;
        $ra = $ra - $discval;
    }

    if (!empty($d2)) {
        $discval = ($ra * $d2) / 100;
        $ra = $ra - $discval;
    }
    if (!empty($d3)) {
        $discval = ($ra * $d3) / 100;
        $ra = $ra - $discval;
    }
    if (!empty($d4)) {
        $discval = ($ra * $d4) / 100;
        $ra = $ra - $discval;
    }
    $ra = round($ra, 2);
    if ($n > 0) {
        $success = true;
    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'file' => $file,
        'name' => $r[name],
        'barcode' => $r[barcode],
        'itemcode' => $r[prodcode],
        'gram' => $r[gram],
        'unit' => $r[unit],
        'rate' => $ra,
        'hsncode' => $r[hsncode],
        'mrp' => $r[mrp],
        'gstperc' => $fgstperc[per]
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
