<?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["item"])) {
    $mitem = $_REQUEST["item"];
    $mgodown = $_REQUEST["godown"];
    $getstock = mysqli_query($conn, "SELECT sum(totqty) as totqty, sum(flag) as flag, sum(flagqty) as flagqty , sum(actqty)as actqty,  batch, actbatch  FROM `transacn`
                               WHERE `itemcode`='$mitem' AND `godown`='$mgodown' Group by `actbatch`")or die(mysqli_error($conn));

    Echo "<table class='table table-condensed table-striped'>";
    Echo "<tr style='background-color:#1ABC9C;'>";
    Echo "<th>Check</th>";
    Echo "<th>Actbatch</th>";
    Echo "<th>Total Qty</th>";
    Echo "<th>Flag</th>";
    Echo "<th>Flag Qty</th>";
    Echo "</tr>";
    $ctr = 0;
    while ($stock = mysqli_fetch_array($getstock)) {
        $tots = $stock[totqty];
        $actbatchs = $stock[actbatch];

        $itot = 0;
        for ($i = 0; $i < $count; $i++) {
            $sitem = $_POST["item_$i"];
            $sbatch = $_POST["batch_$i"];
            $sactbatch = $_POST["actbatch_$i"];
            $stotqty = $_POST["totqty_$i"];

            $mflag = $_POST["flag_$i"];
            $mflagqty = $_POST["flagqty_$i"];
            $minvqty = $_POST["invqty_$i"];

            if ($sitem == $mitem && $sactbatch == $actbatchs) {
                $itot = $itot + $stotqty;
            }
        }
        $tot = $tots - $itot;

        if ($tot > 0) {
            Echo "<tr>";
            Echo "<td><input type='radio' class='mycheck' name='mycheck' id='check_$ctr' value='$stock[actbatch]' style='margin-top:11px;'></td>";
            Echo "<td><input type='text'  class='form-control checkactbatch' value='$stock[actbatch]' readonly></td>";
            Echo "<td><input type='text'  class='form-control' value='$tot' readonly></td>";
            Echo "<td><input type='text'  class='form-control' value='$stock[flag]' readonly></td>";
            Echo "<td><input type='text'  class='form-control' value='$stock[flagqty]' readonly></td>";
            Echo "</tr>";
            $ctr++;
        }
    }
    Echo "</table>";
}