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

if (isset($_REQUEST['shift'])) {
    $shift = $_REQUEST['shift'];
    $vouchdt = $_REQUEST['vouchdt'];
    $vouchno = $_REQUEST['vouchno'];
    $pumqry = mysqli_query($conn, "SELECT * FROM  `prdmain` WHERE shift='$shift' and vouchdt='$vouchdt' and vouchno!='$vouchno'") or die(mysqli_error($conn));
    $mno = mysqli_num_rows($pumqry);
    if ($mno > 0) {
        echo "$mno";
    } else {

        $gets = mysqli_query($conn, "SELECT min(shift+0) as shift from shift") or die(mysqli_error($conn));
        $fets = mysqli_fetch_array($gets);
        $minshift = $fets['shift'];

        if ($shift == $minshift) {
            $getsh = mysqli_query($conn, "SELECT max(vouchdt) as vouchdt from prdmain where vouchdt < '$vouchdt'") or die(mysqli_error($conn));
            $mno = mysqli_num_rows($pumqry);
//            if ($mno > 0) {
                $fetsh = mysqli_fetch_array($getsh);
                $tvouchdt = $fetsh['vouchdt'];
//            } else {
//                $tvouchdt = $vouchdt;
//            }
//            browse($minshift);

            $getshf = mysqli_query($conn, "SELECT max(shift+0) as shift from prdmain where vouchdt = '$tvouchdt'") or die(mysqli_error($conn));
            $fetshf = mysqli_fetch_array($getshf);
            $pshift = $fetshf['shift'];
        } else {

            $tvouchdt = $vouchdt;
            $getshfn = mysqli_query($conn, "SELECT a.* from (SELECT max(shift) as shift from prdmain where vouchdt = '$tvouchdt'  and shift < '$shift') a where shift IS NOT NULL") or die(mysqli_error($conn));
            if ($getshfn != "") {
                $tsn = mysqli_fetch_array($getshfn);
                $mnos = mysqli_num_rows($getshfn);
                $pshift = $tsn['shift'];
            } else {
                $mnos = 0;
            }
            if ($mnos <= 0) {
                $getsh = mysqli_query($conn, "SELECT max(vouchdt) as vouchdt from prdmain where vouchdt < '$vouchdt'") or die(mysqli_error($conn));
                $fetsh = mysqli_fetch_array($getsh);
                $tvouchdt = $fetsh['vouchdt'];
                $getshf = mysqli_query($conn, "SELECT max(shift+0) as shift from prdmain where vouchdt = '$tvouchdt'") or die(mysqli_error($conn));
                $fetshf = mysqli_fetch_array($getshf);
                $pshift = $fetshf['shift'];
            }
        }
        $ctr = 0;
        $pumq = mysqli_query($conn, "SELECT * FROM  `pump` order by  pumpno") or die(mysqli_error($conn));
        $rd = "";
        while ($row = mysqli_fetch_array($pumq)) {
            $getshp = mysqli_query($conn, "SELECT vouchno from prdmain where shift='$pshift' and vouchdt='$tvouchdt'") or die(mysqli_error($conn));
            $fetshp = mysqli_fetch_array($getshp);

            $pumqryt = mysqli_query($conn, "SELECT * FROM  `prdtran` WHERE vouchno = '$fetshp[vouchno]' and pumpno ='$row[pumpno]' ") or die(mysqli_error($conn));
            $mnot = mysqli_num_rows($pumqryt);
            if ($mnot > 0) {
                $fetst = mysqli_fetch_array($pumqryt);

                $opening = $fetst['closing'];
                $closing = $fetst['closing'];
                $rd = "readonly tabindex='-1'";
            } else {
                $opening = 0;
                $closing = 0;
            }


            Echo "<tr>";
            Echo "<td><input type='text' class='srno text-right form-control  form'     name='srno_$ctr'  value='$row[srno]' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='pump text-right form-control  form'     name='pump_$ctr'  value='$row[pumpno]' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form prodcode'             name='prodcode_$ctr'  value='$row[prodcode]' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form product'              name='product_$ctr' value='$row[product]' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form text-right opening'   name='opening_$ctr'  value='$opening' $rd></td>";
            Echo "<td><input type='text' class='form-control form text-right closing'   name='closing_$ctr'  value='$closing'></td>";
            Echo "<td><input type='text' class='form-control form text-right sale'      name='sale_$ctr'       value='0' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form text-right testing'   name='testing_$ctr'      value='0'></td>";
//            Echo "<td><input type='text' class='form-control form text-right transfer'  name='transfer_$ctr'      value='0'></td>";
//            Echo "<td><input type='text' class='form-control form text-right evaporate' name='evaporate_$ctr'    value='0' ></td>";
            Echo "<td><input type='text' class='form-control form text-right netsale'   name='netsale_$ctr'    value='0' readonly tabindex='-1'></td>";
            Echo "</tr>";
            $ctr++;
        }
        ?>
        <input type="hidden" id="items" name="items" value="<?php Echo $ctr; ?>">  
        <?php
    }
}
