<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = "";
$dateto = "";
$product_name = "";
$totalQty = 0;
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
extract($_REQUEST);
if ($datefrom == "") {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}
if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}
$datefrom = date("d-m-Y", strtotime($datefrom));
$dateto = date("d-m-Y", strtotime($dateto));

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));
$filterReport = " WHERE DATE_FORMAT(datetime,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(datetime,'%Y-%m-%d')<='$qryTo'";

if (isset($product_name) && $product_name != 'All') {
    $filterReport .= " AND product_id ='$product_name'";
}
$filterReport .= " ORDER BY product_id";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Stock Report</h3>
            </div>
            <script>
                $(function () {
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                    });
                    $('#datepicker1').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                    });
                });
            </script>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col s6">Date From<br>
                            <input type="text" name="datefrom" id="datepicker"
                                   placeholder="Date From"
                                   value="<?= $datefrom ?>" class="form-control"/>
                            <input type="hidden" name="view" id="view" class="form-control"/>
                        </td>
                        <td class="col s6"> Date To<br>
                            <input type="text" name="dateto" id="datepicker1"
                                   placeholder="Date To"
                                   value="<?= $dateto ?>" class="form-control"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="col s6">Product<br>
                            <?php
                            $productfilter = " WHERE status = 'active' ORDER BY name";
                            $products = ProductSetup::GetProductSetupList($productfilter);
                            ?>
                            <select class="form-control" required id="product_name"
                                    name="product_name">
                                <option>All</option>
                                <?php
                                if ($products) {
                                    foreach ($products AS $productsRow) {
                                        echo '<option ';
                                        if ($productsRow->id == $product_name) {
                                            echo 'selected';
                                        }
                                        echo ' value="' . $productsRow->id . '" >' . $productsRow->name . '</option>';
                                    }
                                }
                                ?>
                            </select><br/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="col s12">
                            <button type="submit" class="btn waves-effect waves-light"
                                    style="float:left;">
                                Search
                            </button>
                            <?php if (isset($_GET['view'])) { ?>
                                <button
                                    onclick="exportTableToCSV('payment-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Export CSV
                                </button>
                                <button
                                    onClick="window.print()"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Print
                                </button>
                            <?php } ?>
                        </td>
                    </tr>
                </table>
            </form>
            <hr><?php
            if (isset($_GET['view'])) {
                ?>
                <div id="floatThead" class="section">
                    <table class="striped demo1 bordered">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>Date</th>
                            <th>User Name</th>
                            <th>Product</th>
                            <th>Qty</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $qry = Stock::GetStockSetupList($filterReport);
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $totalQty += $qery->qty;
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?php echo date('Y-m-d h:i A', strtotime($qery->datetime)); ?></td>
                                    <?php
                                    $product = ProductSetup::GetProductSetupById($qery->product_id);
                                    $totalavailable = $product->availability;
                                    $salesPerson = User_setup::GetUserSetupId($qery->user_id);
                                    ?>
                                    <td><?php echo $salesPerson->username; ?></td>
                                    <td><?php echo $product->name; ?></td>
                                    <td><?php echo $qery->qty; ?></td>
                                </tr>
                                <?php
                            }
                            if ($product_name != 'All' && $product_name != '') {
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <td colspan="3" style="text-align: right;">Total Stock</td>
                                    <td><?php echo $totalQty; ?></td>
                                </tr>
                                <tr style="border: 1px solid #666;">
                                    <td colspan="3" style="text-align: right;">Total Available</td>
                                    <td><?php echo $totalavailable; ?></td>
                                </tr>
                            <?php }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view
            ?>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/download.js"></script>
<?php include('../includes/inc_js.php'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead.min.js"></script>
<script type="text/javascript"
        src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead-slim.min.js"></script>
<script type="application/javascript">
    $(document).ready(function () {
        $('table.demo1').floatThead({
            position: 'fixed',
            scrollingTop: 65
        });
        $('table.demo1 thead').css('background', '#ECEFF1');
    });
</script>
<style>
    @page { size: A4;  margin: 5mm 0mm; }
    @media print
    {
        table { page-break-inside:auto }
        tr    { page-break-inside:avoid; page-break-after:auto }

        #container { page-break-after: always;}

        header, aside, form, footer {
            display: none;
        }
        .adv-table th, .adv-table td{
            padding:5px;
        }
        h2{
            font-size: 18px;
            text-align: center;
        }
        hr{
            margin-top: 5px;
            margin-bottom: 5px;
        }
    }
</style>
</body>
</html>