<?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];
$zroot = $dbname;

if (isset($_REQUEST['shift'])) {
    $shift = $_REQUEST['shift'];
    $vouchdt = $_REQUEST['vouchdt'];
    $vouchno = $_REQUEST['vouchno'];

    ///////////////////////

    $pumqry = mysqli_query($conn, "SELECT * FROM  `stkmain` 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));
            if ($mno > 0) {
                $fetsh = mysqli_fetch_array($getsh);
                $tvouchdt = $fetsh['vouchdt'];
            } else {
                $tvouchdt = $vouchdt;
            }
//            $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'];
        } else {

            $tvouchdt = $vouchdt;
            $getshfn = mysqli_query($conn, "SELECT a.* from (SELECT max(shift) as shift from stkmain 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 stkmain 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 stkmain where vouchdt = '$tvouchdt'") or die(mysqli_error($conn));
                $fetshf = mysqli_fetch_array($getshf);
                $pshift = $fetshf['shift'];
            }
        }

        $ctr = 0;
        $pumq = mysqli_query($conn, "SELECT * FROM  `oil` order by  srno") or die(mysqli_error($conn));
        $rd = "";
        while ($row = mysqli_fetch_array($pumq)) {

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


            $qryacc = mysqli_query($conn, "select prodcode, sum(qty) as crqty, rate, cost from saletran 
                                            where vouchno in (select vouchno from salemain where direct != 'Y' 
                                            and vouchdt = '$vouchdt' and shift = '$shift' and dept = 'DW') 
                                            and product = '$row[prodcode]' group by prodcode") or die(mysqli_error($conn));
            $fetacc = mysqli_fetch_array($qryacc);
            if ($fetacc['crqty'] > 0) {
                $credsale = $fetacc['crqty'];
            } else {
                $credsale = 0;
            }
            $ctr++;
            Echo "<tr>";
            Echo "<td><input type='text' class='srno text-right form-control  form'     name='srno_$ctr'      value='$ctr' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form tprodcode'            name='tprodcode_$ctr' value='$row[prodcode]' readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form tproduct'             name='tproduct_$ctr'  value='$lpd[name]'     readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form text-right tsale'     name='tsale_$ctr'     value='0' ></td>";
            Echo "<td><input type='text' class='form-control form text-right credsale'  name='credsale_$ctr'  value='$credsale'      readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form text-right cashsale'  name='cashsale_$ctr'  value='0'      readonly tabindex='-1'></td>";
            Echo "<td><input type='text' class='form-control form text-right trate'     name='trate_$ctr'     value='$lpd[rate]'      readonly tabindex='-1'>";
            Echo "<td><input type='text' class='form-control form text-right cashamt'   name='cashamt_$ctr'   value='0'      readonly tabindex='-1'>"
              .  "<input type='hidden' class='form-control form text-right tcost'     name='tcost_$ctr'     value='$lpd[cost]'      readonly tabindex='-1'></td>";
            Echo "</tr>";
        }
        ?>
        <input type="hidden" id="items" name="items" value="<?php Echo $ctr; ?>">  
        <?php
    }
}
