<?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 = 'Item Stock - Godownwise';

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

$barcode = $_POST["barcode"];

if (!empty($barcode)) {
    $bcond = " and `prodcode` = '$barcode' ";
}

if (empty($_POST["todt"])) {
    browse("TO DATE CANNOT BE EMPTY");
    header("refresh:1; $grid");
}

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

$getitem = mysqli_query($conn, "Select godown, sum(qty) as balqty from `transacn`  where vouchdt <= '$mtodate' $bcond 
                                       group by godown order by godown") or die(mysqli_error($conn));
//print table($getitem);
$getno = mysqli_num_rows($getitem);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
if ($getno == 0) {

    browse("Record Not Found");
    echo "<script>
            window.close();
            </script>";
}

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

    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" style="width:700px">
            <?php
            include("includes/head_rep.php");
            ?>

            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th align="left" width='3%' style="font-family: arial;">Sr.</th>
                                <th align="left" width='20%'>Godown</th>
                                <th align="right" width='25%'>Stock Qty.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $gropen = 0;
                            $grsale = 0;
                            $grpur = 0;
                            $grbal = 0;
                            $grval = 0;
                            $mopen = 0;

                            $ctr = 0;
                            mysqli_select_db($conn, $zdb);
                            while ($row = mysqli_fetch_object($getitem)) {

                                $gocode = $row->godown;
                                $balqty = $row->balqty;

                                $getgdwn = mysqli_query($conn, "SELECT code, name FROM `godown` WHERE code = '$gocode'")or die(mysqli_error($conn));
                                $fetgdwn = mysqli_fetch_array($getgdwn);
                                $godown = $fetgdwn['name'];



                                $ctr++;
                                echo "<tr>";
                                echo "<td align='left'>$ctr</td>";
                                echo "<td align='left'>$godown</td>";
                                echo "<td align='right'>$balqty</td>";
                                echo "</tr>";

                                $grbal = $grbal + $balqty;
                            }
                            $colspan = '2';

                            echo "<tr>";
                            echo "<td align='right' colspan='$colspan'><font size='4px'><b> Total</b></font></td>";
                            echo "<td align='right'><b>$grbal</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    </html>
    <?php
}

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

    include("includes/head_rep_excel.php");


    $contents .= "Sr.,Godown,Stock Qty\n";

    $gropen = 0;
    $grsale = 0;
    $grpur = 0;
    $grbal = 0;
    $grval = 0;
    $mopen = 0;

    $ctr = 0;
    mysqli_select_db($conn, $zdb);
    while ($row = mysqli_fetch_object($getitem)) {
        $gocode = $row->godown;
        $balqty = $row->balqty;

        $getgdwn = mysqli_query($conn, "SELECT code, name FROM `godown` WHERE code = '$gocode'")or die(mysqli_error($conn));
        $fetgdwn = mysqli_fetch_array($getgdwn);
        $godown = $fetgdwn['name'];



        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $godown . ",";
        $contents .= $balqty . "\n";

        $grbal = $grbal + $balqty;
    }


    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= $grbal . "\n";


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>