<?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 = $_POST["name"];

    $namecond = "";
    
    if (!empty($name)) {
        $namecond = " AND `itemname` LIKE '%$name%' ";
    }
}
// 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 `itemname`!='' $namecond order by vouchdt, source, vouchno")or die(mysqli_error($conn));

include("includes/linkshead.php");
?>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <label class="label-control marg">Date - </label>
                <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
            </div>
            <div class="col-lg-5">
                <label class="label-control marg">Print - </label>
                <label class="label-control marg"><?php Echo $formname; ?></label>
            </div>
            <div class="col-lg-1">
                <a onclick="window.close()" class="btn btn-primary">GO Back</a>
            </div>
        </div>
        <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 style='text-align:right;'>Act Batch</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $grbal = 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++;
                            $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[actqty]</td>";
                            Echo "<td style='text-align:right;'>$pmain[actbatch]</td>";
                            Echo "</tr>";
                            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>$grbal</b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

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