<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$spltype = $_SESSION["spltype_" . $dbname];
$series = $_SESSION["series_" . $dbname];
mysqli_select_db($conn, $zdb);
$formname = 'Itemwise Stock';
$maindb = 'item';
$trandb = 'transacn';
$grid = 'stock.php';
$getprevyear = mysqli_query($conn, "SELECT cono, finyear FROM $zroot.`compyear` where `cono`='$compcd' 
                            and `finyear`!='$myyear' ORDER BY `lastdate` DESC LIMIT 0,1 ")or die(mysqli_error($conn));
$showprevyear = mysqli_fetch_array($getprevyear);
$prevyear = $showprevyear[finyear];
$seriescond = " and `series` = '$series' ";

$getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code != '' group by code") or die(mysqli_error($conn));
$getno = mysqli_num_rows($getitem);

if ($getno == 0) {

    browse("Record Not Found");
    echo "<script>
            window.close();
            </script>";
}

include("includes/linkshead.php");
?>
<style type="text/css">
    #tabital{
        font-style: italic;
    }
    #font1{
        font-style: italic;
    }
    #boldfont{
        font-weight: bold; 
    }
</style>

</head>
<body>
    <div class="container" style="width:700px">
        <?php include("includes/head_rep.php"); ?>
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                    <thead class="mySearchTr">
                        <tr>

                            <th align="left" width='3%' style="font-family: arial;">Sr.</th>
                            <th align="left" width='8%'>Barcode</th>
                            <th align="left" width='25%'>Item Name</th>
                            <th align="left" width='10%'>Min Stock</th>
                            <th align="left" width='8%' style="text-align: right;">Balance</th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = $totbal = 0;

                        mysqli_select_db($conn, $zdb);
                        while ($row = mysqli_fetch_object($getitem)) {
                            $icode = $row->code;
                            $ibarcode = $row->barcode;
                            $mopen = $row->opening;
                            $itemname = $row->name;
                            $minqty = $row->minqty;

                            $mainqry = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `transacn` WHERE `itemcode`='$icode' $seriescond")or die(mysqli_error($conn));
                            $fmain = mysqli_fetch_array($mainqry);
                            $currqty = $fmain['sqty'];

                            $ctr++;
                            if ($currqty <= $minqty) {
                                echo "<tr>";
                                echo "<td align='left'>$ctr</td>";
                                echo "<td align='left'>$ibarcode</td>";
                                echo "<td align='left'>$itemname</td>";
                                echo "<td align='left'>$minqty</td>";
                                echo "<td align='right'>$currqty</td>";
                                echo "</tr>";
                                $totbal = $balqty + $totbal;
                            }
                        }
                        echo "<tr style ='background-color:white;'> ";
                        echo "<td align='right' colspan='3'><font size='4px'><b> Total</b></font></td>";
                        echo "<td align='right'><b>$totbal</b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>


</body>
</html>
<?php
?>