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

$maindb = "item";
$formname = "Check Stock";
$file = "check_ctock.php";

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

if (isset($_POST["view"])) {
    $name = mysqli_real_escape_string($conn,$_POST["name"]);

    $namecond = "";

    if (!empty($name)) {
        //        $namecond = " AND `itemname` LIKE '%$name%' ";
        $namecond = " AND `name` = '$name' ";
        $qrycode = mysqli_query($conn, "SELECT * FROM `item` WHERE `name` = '$name' ")or die(mysqli_error($conn));
        $fetcode = mysqli_fetch_object($qrycode);
        $icode = $fetcode->code;
    }
}

// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt  FROM `transacn` WHERE `itemcode` = '$icode' order by vouchdt, source, vouchno")or die(mysqli_error($conn));

include("includes/linkshead.php");
?>
</head>
<body>
    <div class="container">
        <?php include("includes/head_rep.php"); ?>                   
        <div class="row">
            <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>Source</th>
                            <th>Voucher</th>
                            <th>Date</th>
                            <th>Item Name</th>
                            <th style='text-align:right;'>Qty.</th>
                            <th> </th>
                            <th style='text-align:right;'>Act.Qty.</th>
                            <th style='text-align:right;'>Batch</th>
                            <th style='text-align:right;'>Act Batch</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $grbal = 0;
                        $grqty = 0;
                        $grflag = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {
//                            $getstock = mysqli_query($conn, "SELECT *, SUM(actqty) AS qty FROM `transacn` WHERE `itemcode` = '$pmain[code]' order by vouchdt, source, vouchno")or die(mysqli_error($conn));
//                            $getstock = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt  FROM `transacn` WHERE `itemcode` = '$pmain[code]' order by vouchdt, source, vouchno")or die(mysqli_error($conn));
//                            $stock = mysqli_fetch_array($getstock);
                            $ctr++;
                            $grqty = $grqty + $pmain['qty'];
                            $grflag = $grflag + $pmain['flag'];
                            $grbal = $grbal + $pmain['actqty'];
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[source]</td>";
                            Echo "<td>$pmain[vouchno]</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td>$pmain[itemname]</td>";
                            Echo "<td style='text-align:right;'>$pmain[qty]</td>";
                            if ($pmain['flag'] > 0) {
                                Echo "<td style='text-align:right;'>($pmain[flag] F)</td>";
                            } else {
                                echo"<td></td>";
                            }
                            Echo "<td style='text-align:right;'>$pmain[actqty]</td>";
                            Echo "<td style='text-align:right;'>$pmain[batch]</td>";
                            Echo "<td style='text-align:right;'>$pmain[actbatch]</td>";
                            Echo "</tr>";
                        }
                        echo "<tr style ='background-color:white;border-top:2px solid black;'> ";
                        echo "<td align='left'></td>";
                        echo "<td align='left'></td>";
                        echo "<td align='left'></td>";
                        echo "<td align='left'></td>";
                        echo "<td  style='text-align:right;' align='left'><font size='2px'><b>Total</b></font></td>";
                        echo "<td style='text-align:right;' align='right'><b>$grqty</b></td>";
                        echo "<td style='text-align:right;' align='right'><b>($grflag F)</b></td>";
                        echo "<td style='text-align:right;' align='right'><b>$grbal</b></td>";
                        echo "<td style='text-align:right;' align='right'><b></b></td>";
                        echo "<td style='text-align:right;' align='right'><b></b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

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