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

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

$formname = 'Location wise Stock';

$maindb = 'item';
$trandb = 'transacn';

$grid = 'stockloc.php';

if (isset($_POST["view"])) {

    $ncond = "";
    $subcond = "";
    if (!empty($_POST["name"])) {
        $nname = trim($_POST["name"]);
        $ncond = " and `name` LIKE '$nname%' ";
    }

    if (!empty($_POST["location"])) {
        $location = trim($_POST["location"]);
        $subcond = " and `location` = '$location' ";
    }
    if (empty($_POST["frmdt"])) {
        browse("FROM DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }
    if (empty($_POST["todt"])) {
        browse("TO DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }


    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $getbal = mysqli_query($conn, "SELECT a.itemcode, a.location, sum(a.qty) as sqty, b.name as itemname FROM `$trandb` a, `$maindb` b
                           WHERE a.itemcode = b.code  AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $ncond $subcond  
                           group by a.location,a.itemcode order by a.itemcode,a.location") or die(mysqli_error($conn));
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">

        <head>
            <?php
            include("includes/files.php");
            ?>
        </head>
        <body>

            <?php
            require("includes/dbconnect.php");
            $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
            $zcomp = mysqli_fetch_assoc($comp);
            echo "<font size='4px'><b>$zcomp[compname]</font></b></font><br />";
            echo "<font size='2px'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";
            echo "<b><font size='3px'>Date : " . date("d-m-Y") . "</b><br />";
            echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>
                To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
            echo "<b>" . "Total Records - $getno</b><br />";
            echo "<br />";
            echo "</font>";
            ?>
            <div class="row">
                <div class="col-lg-6" style="width:60%;">
                    <div class="box">
                        <header>
                            <h5><?php echo $formname; ?></h5>
                            <div class="toolbar">
                                <div class="btn-group">		  
                                    <div class="btn-group">
                                        <a href="#sortableTable" data-toggle="collapse" class="btn btn-default btn-sm minimize-box">
                                            <i class="fa fa-angle-up"></i>
                                        </a>
                                        <input type='button' name='submit' class='btn btn-sm btn-danger' onClick="window.location.href = '<?php echo $grid; ?>'" value="Exit"  value='Save'/> 
                                    </div>
                                </div>
                            </div>
                        </header>
                        <div id="sortableTable" class="body collapse in">
                            <table class="table table-bordered sortableTable responsive-table">
                                <thead>
                                    <tr>
                                        <th align="left" width='5%'>Sr.No<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='25%'>Item Name<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Location<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $grbal = 0;
                                    $ctr = 0;
                                    while ($row = mysqli_fetch_array($getbal)) {
//                                        $iname =  $row->name ;
                                        $getloc = mysqli_query($conn, "SELECT * FROM `location` WHERE code='$row[location]'") or die(mysqli_error($conn));
                                        $loc = mysqli_fetch_array($getloc);
                                        $ctr++;
                                        
                                        echo "<tr> ";
                                        echo "<td align='left'>$ctr</td>";
                                        echo "<td align='left'>$row[itemname]</td>";
                                        echo "<td align='left'>$loc[name]</td>";
                                        echo "<td align='right'>$row[sqty]</td>";
                                        echo "</tr>";
                                        $grbal  = $grbal  + $row[sqty];
                                    }
                                    echo "<tr> ";
                                    echo "<td align='right'></td>";
                                    echo "<td align='right'></font></td>";
                                    echo "<td align='left'><font size='2px'> Total</font></td>";
                                    echo "<td align='right'>$grbal</td>";
                                    echo "</tr>";
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.col-lg-6 -->
            </div>  
            <?php
            include("includes/files_footer.php");
            ?>
            <script>
                $(function() {
                    metisTable();
                    metisSortable();
                });
            </script>
        </body>
    </html>
    <?php
}// Submit Tag Close
?>