<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = "";
$dateto = "";
$cat_name = "";
$productQtyTotal = 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 = " LEFT JOIN customer_setup ON invoice.customer_id = customer_setup.id WHERE DATE_FORMAT(invoice.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(invoice.create_date,'%Y-%m-%d')<='$qryTo' and invoice.status <> 'Cancel' ";
if (isset($cat_name) && $cat_name != 'All') {
    $filterReport .= " AND customer_setup.category_id ='$cat_name'";
}
if (isset($customer_name) && $customer_name != 'All') {
    $filterReport .= " AND invoice.customer_id ='$customer_name'";
}
$filterReport .= " ORDER BY invoice.code ";
$qry = Invoice::GetInvoiceListJoinCustomer($filterReport);
if ($qry) {
    foreach ($qry AS $qryRow) {
        $invoices[] = $qryRow->invoiceid;
    }
}
//echo '<pre>';
//print_r($invoices);
//echo '</pre>';
?>
<!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>Monthly Product 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>
                    <tr>
                        <td class="col s6">Category<br>
                            <?php
                            $catFilter = " JOIN customer_setup ON customer_category.id = customer_setup.category_id GROUP BY catid ORDER BY customer_category.name";
                            $categories = CustomerCategorySetup::GetCustomerCategorySetupList($catFilter);
                            ?>
                            <select class="form-control" required id="cat_name"
                                    name="cat_name">
                                <option>All</option>
                                <?php
                                if ($categories) {
                                    foreach ($categories AS $categoriesRow) {
                                        echo '<option ';
                                        if ($categoriesRow->catid == $cat_name) {
                                            echo 'selected';
                                        }
                                        echo ' value="' . $categoriesRow->catid . '" >' . $categoriesRow->catname . '</option>';
                                    }
                                }
                                ?>
                            </select><br/>
                        </td>
                        <td class="col s6">Customer<br>
                            <?php
                            if ($cat_name == 'All') {
                                $customersfilter = " WHERE status = 'active' AND ";
                            } else {
                                $customersfilter = " WHERE status = 'active' AND category_id = '$cat_name'";
                            }
                            $customers = CustomerSetup::GetCustomerSetupList($customersfilter);
                            ?>
                            <select class="form-control" required id="customer_name"
                                    name="customer_name">
                                <option>All</option>
                                <?php
                                if ($customers) {
                                    foreach ($customers AS $customersRow) {
                                        echo '<option ';
                                        if ($customersRow->id == $customer_name) {
                                            echo 'selected';
                                        }
                                        echo ' value="' . $customersRow->id . '" >' . $customersRow->name . '</option>';
                                    }
                                }
                                ?>
                            </select><br/>
                        </td>
                    </tr>
                    </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('monthlyproduct-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Export CSV
                                </button>
                                <button
                                    id="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>S.No</th>
                            <th>Product</th>
                            <th>Qty</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $products = array();
                        if ($invoices) {
                            foreach ($invoices AS $invoicesRow) {
                                $filterReportProduct = " WHERE invoice_id = '$invoicesRow'";
                                $qrys = Invoice::GetInvoiceProducts($filterReportProduct);
                                if (!empty($qrys)) {
                                    foreach ($qrys AS $qrysRow) {
                                        $products[$qrysRow->product_id] += $qrysRow->product_qty;
                                    }
                                }
                            }
                        }
                        if ($products) {
                            $counter = 0;
                            foreach ($products as $productsqeryKey => $productsqery) {
                                $counter++;
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <?php
                                    $productName = ProductSetup::GetProductSetupById($productsqeryKey);
                                    $productQtyTotal += $productsqery;
                                    ?>
                                    <td><?php echo $counter; ?></td>
                                    <td><?php echo $productName->name; ?></td>
                                    <td><?php echo $productsqery; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        <tr>

                            <th colspan="2" style="text-align: right; border: 1px solid #000;">
                                Total
                            </th>
                            <th style="border: 1px solid #000;"><?= $productQtyTotal ?></th>
                        </tr>
                        </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 () {
        $("#print").on("click", function (e) {
            var fewSeconds = 2;
            e.preventDefault();
            $('#searchfrm').prop('target', '_blank');
            $('#searchfrm').prop('action', "<?=PROJECT_ADMIN_URL?>report/print/monthlyproduct.php?").submit();
            setTimeout(function () {
                $('#searchfrm').prop('target', "");
                $('#searchfrm').prop('action', "");
            }, fewSeconds * 1000);
        });
        $('table.demo1').floatThead({
            position: 'fixed',
            scrollingTop: 65
        });
        $('table.demo1 thead').css('background', '#ECEFF1');

        $('#customer_name').material_select();

        $('#customer_name').on('contentChanged', function () {
            $(this).material_select();
        });

        $('#cat_name').on("change", function () {
            $('body.container').block({message: 'Loading...'});
            $.get('getFilterCustomersMulti.php?id=' + $(this).val(), function (data) {
                $("#customer_name").empty();
                $("#customer_name").append(data);
                $('body.container').unblock();
                $('#customer_name').trigger('contentChanged');
            });
        });
    });
</script>
</body>
</html>