<?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["itemcode"])) {
    $mitemcode = $_REQUEST["itemcode"];
    $brand = $_REQUEST["brand"];
    $getbr = mysqli_query($conn, "SELECT code,name FROM `brand` WHERE `name`='$brand'") or die(mysqli_error($conn));
    $bcode = mysqli_fetch_array($getbr);
    $brcode = $bcode['code'];
    $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$mitemcode' and brand='$brcode'") or die(mysqli_error($conn));
    $fitem = mysqli_fetch_array($finditem);
    $mitem = $fitem['barcode'];
//    $brand = $fitem['brand'];
    $mgodown = $_REQUEST["godown"];
    $vouchno = $_REQUEST["vouchno"];
    $source = $_REQUEST["source"];

    $mqry = "SELECT sum(totqty) as totqty, baleqty, sum(qty) as qty, containerno FROM (
            (SELECT sum(totqty) as totqty, baleqty, sum(qty) as qty, containerno FROM `transacn` 
            WHERE `itemcode`='$mitem' AND `containerno` != '' GROUP BY `containerno`) 
                                    UNION ALL
            (SELECT sum(totqty) as totqty, baleqty, sum(qty) as qty, containerno FROM `dumstock` 
                    WHERE `source`='$source' AND `vouchno`='$vouchno' AND `itemcode`='$mitem' AND `containerno` != '' GROUP BY containerno)) t1 GROUP BY containerno having qty > 0";

    $getstock = mysqli_query($conn, $mqry) or die(mysqli_error($conn));
//    print table($getstock);
    Echo "<table class='table table-condensed table-striped' id='stkview'>";
    Echo "<tr style='background-color:#1ABC9C;'>";
    Echo "<th>Add All</th>";
    Echo "<th>Check</th>";
    Echo "<th>Container</th>";
    Echo "<th class='text-right'>Qty</th>";
    Echo "<th class='text-right'>Bale Qty</th>";
//    Echo "<th class='text-right'>Total Qty</th>";
    Echo "</tr>";
    $ctr = 0;
    while ($stock = mysqli_fetch_array($getstock)) {
        if ($stock['totqty'] > 0) {
            Echo "<tr>";
            Echo "<td><input type='radio' class='mycheck2' name='mycheck2' id='check2_$ctr' value='$stock[containerno]' style='margin-top:11px;'></td>";
            Echo "<td><input type='radio' class='mycheck'  name='mycheck'  id='check_$ctr'  value='$stock[containerno]' style='margin-top:11px;'></td>";
            Echo "<td><input type='text'  class='form-control checkactbatch '    value='$stock[containerno]' readonly></td>";
            Echo "<td><input type='text'  class='form-control checkqty text-right'     value='$stock[qty]' readonly></td>";
            Echo "<td><input type='text'  class='form-control checkbaleqty text-right'        value='$stock[baleqty]' readonly></td>";
//            Echo "<td><input type='text'  class='form-control checktotqty text-right'      value='$stock[totqty]' readonly></td>";
            Echo "</tr>";
            $ctr++;
        }
    }
    Echo "</table>";
}