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

$maindb = "item";
$trandb = "transacn";
$formname = "Stock Old";
$file = "stock_old.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
  $name = $_POST["name"];
    $asdt = $_POST["asdt"];
    $brand = $_POST["brand"];
    $type = $_POST["type"];
    $smode = $_POST["smode"];
    $godown = $_POST["godown"];

    $namecond = "";
    $brandcond = "";
    $typecond = "";
    $smodecond = "";
    $godowncond = "";

    if (!empty($name)) {
        $namecond = " AND b.`name` LIKE '$name%' ";
    }
    if (!empty($brand)) {
        $brandcond = " AND b.`brand`='$brand' ";
    }
    if (!empty($type)) {
        $typecond = " AND a.`type`='$type' ";
    }
    if (!empty($smode)) {
        $smodecond = " AND a.`salemode`='$smode' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND a.`godown`='$godown' ";
    }


    $getitem = mysqli_query($conn, "SELECT * from item where code in (SELECT itemcode FROM `transacn` a, `item` b 
                           WHERE a.`itemcode` =  b.`code` AND a.`vouchdt` <= '$asdt' 
                           $namecond $brandcond $qualitycond $yarncond $typecond $shadecond
                           GROUP BY a.itemcode, a.shade, a.actbatch having sum(a.totqty) > 1) 
                           group by code order by name") or die(mysqli_error($conn));

//    $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`!='' $namecond $brandcond GROUP BY name ORDER BY `name`")or die(mysqli_error($conn));
    
}


include("includes/linkshead.php");
?>
<script src="MyScript/item.js" type="text/javascript"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <?php include("includes/head_rep.php"); ?>
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white" class="table table-condensed table-hover">
<!--                    <thead>
                        <tr style="height:10px;background-color: #1ABC9C">
                            <th>Sr</th>
                            <th>Type</th>
                            <th>Invoice No</th>
                            <th>Date</th>
                            <th>Supplier</th>
                            <th>Amount</th>
                            <th>Discount</th>
                            <th>Freight</th>
                            <th>Qty</th>
                            <th class='numeric'>Qty</th>
                            <th>Net Amount</th>                            
                        </tr>
                    </thead>-->
                    <tbody>
                        <?php
                        $ctr = 0;
                        while ($item = mysqli_fetch_array($getitem)) {
                            
                            Echo "<tr style='background-color: #1ABC9C;color: #ffffff;'>";
                            Echo "<td><label class='label-control'>Item Name</label></td>";
                            Echo "<td><label class='label-control'>$item[name]</label></td>";
                            Echo "<tr>";
                            

                            Echo "<tr style='height:10px;'>";
                            Echo "<td colspan='25'>";
                            Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                            Echo "<th style='background-color: #cccccc'>Sr</th>";
                            Echo "<th style='background-color: #cccccc'>Batch</th>";
                            Echo "<th style='background-color: #cccccc'>Shade</th>";
                            Echo "<th style='background-color: #cccccc' class='numeric'>Qty.</th>";
                            Echo "<th style='background-color: #cccccc' class='numeric'>Flag</th>";
                            Echo "<th style='background-color: #cccccc' class='numeric'>Flag Qty.</th>";
                            Echo "<th style='background-color: #cccccc' class='numeric'>Inv.Qty</th>";
                            Echo "<tr>";

                            $totqty = 0;
                            $totflag = 0;
                            $totfqty = 0;
                            $totact = 0;

                            $ctr2 = 0;
                            $getstock = mysqli_query($conn, "SELECT SUM(qty) as qty, SUM(totqty) as actqty, SUM(flag) as tflag, SUM(flagqty) as flagqty, 
                                                    shade, actbatch FROM `transacn` WHERE `itemcode`='$item[code]' AND `vouchdt`<='$asdt' 
                                                    GROUP BY shade, actbatch having sum(totqty) > 1 ORDER BY vouchdt, actbatch, shade ") or die(mysqli_error($conn));
                            
                            while ($stock = mysqli_fetch_array($getstock)) {
                                $ctr2++;
                                Echo "<tr>";
                                Echo "<td>$ctr2</td>";
                                Echo "<td>$stock[actbatch]</td>";
                                Echo "<td>$stock[shade]</td>";
                                Echo "<td class='numeric'>$stock[qty]</td>";
                                Echo "<td class='numeric'>$stock[flag]</td>";
                                Echo "<td class='numeric'>$stock[flagqty]</td>";
                                Echo "<td class='numeric'><b>$stock[actqty]</b></td>";
                                Echo "</tr>";
                                
                                $totqty = $totqty + $stock[qty];
                                $totflag = $totflag + $stock[flag];
                                $totfqty = $totfqty + $stock[flagqty];
                                $totact = $totact + $stock[actqty];
                            }
                            Echo "<tr style='color: #EF9A9A;'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td><label class='label-control'>TOTAL</label></td>";
                            Echo "<td class='numeric'>$totqty</td>";
                            Echo "<td class='numeric'>$totflag</td>";
                            Echo "<td class='numeric'>$totfqty</td>";
                            Echo "<td class='numeric'><b>$totact</b></td>";
                            Echo "</tr>";
                            Echo "</table>";
                            Echo "</td>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
