<?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  `dipmain` 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 dipmain 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 dipmain 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 dipmain 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 dipmain 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 dipmain where vouchdt = '$tvouchdt'") or die(mysqli_error($conn));
                $fetshf = mysqli_fetch_array($getshf);
                $pshift = $fetshf['shift'];
            }
        }
        $ctr = 0;
        $pumq = mysqli_query($conn, "SELECT * FROM  `tanktran` group by tankno order by  pumpno") or die(mysqli_error($conn));
        $rd = "";
        $getshp = mysqli_query($conn, "SELECT vouchno from dipmain where shift='$pshift' and vouchdt='$tvouchdt'") or die(mysqli_error($conn));
        $fetshp = mysqli_fetch_array($getshp);
        $mvo = $fetshp['vouchno'];
        while ($row = mysqli_fetch_array($pumq)) {
//
            $pumqryt = mysqli_query($conn, "SELECT * FROM  `diptran` WHERE vouchno = '$mvo' and tankno ='$row[tankno]' ") or die(mysqli_error($conn));
            $mnot = mysqli_num_rows($pumqryt);
            if ($mnot > 0) {
                $fetst = mysqli_fetch_array($pumqryt);

                $opendip = $fetst['closedip'];
                $openltrs = $fetst['closeltrs'];
                $rd = "readonly tabindex='-1'";
            } else {
                $opendip = 0;
                $openltrs = 0;
            }

            $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `name`= '$row[product]'") or die(mysqli_error($conn));
            $lpd = mysqli_fetch_array($getpd);


            Echo "<tr>";
//            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
            Echo "<td><input type='text' name='tankno_$ctr' class='tank form-control tankno form'  value='$row[tankno]' readonly tabindex='-1'></td>";
            Echo "<td><input type='hidden' class='form-control form prodcode'             name='prodcode_$ctr'  value='$lpd[prodcode]' readonly tabindex='-1'>"
                    . "<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 opendip'     name='opendip_$ctr'  value='$opendip'></td>";
            Echo "<td><input type='text' class='form-control form text-right openltrs'    name='openltrs_$ctr'  value='$openltrs'></td>";
            Echo "<td><input type='text' class='form-control form text-right adddip'      name='adddip_$ctr' value='0'></td>";
            Echo "<td><input type='text' class='form-control form text-right addltrs'     name='addltrs_$ctr'  value='0'></td>";
            Echo "<td><input type='text' class='form-control form text-right closedip'    name='closedip_$ctr'  value='0'></td>";
            Echo "<td><input type='text' class='form-control form text-right closeltrs'   name='closeltrs_$ctr'  value='0' ></td>";
            Echo "<td><input type='text' class='form-control form text-right saleltrs'    name='saleltrs_$ctr'  value='0'  readonly tabindex='-1'></td>";
            Echo "</tr>";
            $ctr++;
        }
        ?>
        <input type="hidden" id="items" name="items" value="<?php Echo $ctr; ?>">  
        <?php
    }
}
