<?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];

mysqli_select_db($conn, $zdb);

$formname = 'Minimum Stock Alert';

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$maindb = 'product';
$trandb = 'transacn';

// $grid = 'rawstock.php';

$olddb = $zroot1 . $compcd . '_' . $prevyear;

mysqli_query($conn, "drop temporary table if exists stkd  ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table stkd 
                            SELECT a.itemcode,a.barcode, b.minstk, b.name, b.type, b.stkunit, sum(a.stkqty) as stkqty FROM `$trandb` a, $maindb b 
                                   WHERE a.itemcode = b.code and b.minstk > 0 group by a.itemcode ") or die(mysqli_error($conn));

$getsale = mysqli_query($conn, "SELECT * from 
                                (select *, (stkqty / 1000) as sqty FROM stkd WHERE  stkunit = 1 
                                union all
                                select *, stkqty as sqty FROM stkd WHERE stkunit > 1 ) a 
                                where sqty <= minstk order by name ") or die(mysqli_error($conn));

$getno = mysqli_num_rows($getsale) ;

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">
        <?php
        include("includes/head_rep.php");
        ?>
        <div class="row">
            <div class="col-lg-12 pad">
                <table style="width: 100%;" class="table table-condensed table-hover">
                    <thead class="mySearchTr">
                        <tr>

                            <th align="left" width='3%' style="font-family: arial;">Sr.</th>
                            <th align="left" width='15%'>Barcode </th>
                            <th align="left" width='20%'>Name</th>
                            <th align="left" width='20%'>Type</th>
                            <th align="right" width='10%' style="text-align: right;">Minimum Qty.</th>
                            <th align="left" width='10%' style="text-align: right;">Balance Qty.</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $gropen = 0;
                        $grsale = 0;
                        $grpur = 0;
                        $grbal = 0;
                        $grval = 0;
                        $mopen = 0;

                        $ctr = 0;
                       
                        while ($row = mysqli_fetch_object($getsale)) {
                               $sqty = $row->sqty ;
                               $sunit = $row->sunit ;

                                $ctr++;
                                echo "<tr> ";
                                echo "<td align='left'>$ctr</td>";
                                echo "<td align='left'>$row->barcode</td>";
                                echo "<td align='left'>$row->name</td>";
                                echo "<td align='left'>$row->type</td>";
                                echo "<td align='right'>$row->minstk</td>";
                                if ($sqty < 0) {
                                    echo "<td align='right' style='color:red;'>$sqty</td>";
                                } else {
                                    echo "<td align='right' >$sqty</td>";
                                }
                                echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>