<?php
include("../../classes/manager.class.php");
$m = new Manager();

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

$min_pending = 2;
extract($_REQUEST);

if (!isset($min_pending) || $min_pending == "") {
    $min_pending = 2;
}
$min_pending = (int)$min_pending;
if ($min_pending < 2) $min_pending = 2;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<div id="container">
    <?php include('../includes/inc_top.php'); ?>

    <div class="row">
        <div class="col s12 m12">

            <div class="">
                <h3>Multiple Pending Invoices Report</h3>
            </div>

            <form action="" method="get" name="search" id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col-lg-4">
                            Min Pending Invoices (>=)<br>
                            <input type="number" name="min_pending" value="<?= $min_pending ?>" min="2" class="form-control">
                            <input type="hidden" name="view" value="1">
                        </td>
                    </tr>
                    <tr>
                        <td class="col-lg-4">
                            <button type="submit" class="btn waves-effect waves-light" style="float:left;">
                                View Report
                            </button>

                            <?php if (isset($_GET['view'])) { ?>
                                <button type="button"
                                        onclick="exportTableToCSV('multiple-pending-invoices-<?= 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'])) {

                $rows = Invoice::GetCustomerMultiplePendingInvoicesReport($min_pending);

                $totalPendingInvoices = 0;
                $totalRemainingBalance = 0;
                $totalReceived = 0;
                ?>

                <div id="floatThead" class="adv-table section">
                    <table class="striped demo1 bordered">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>S.No</th>
                            <th>Customer #</th>
                            <th>Customer</th>
                            <th>Contact</th>
                            <th>Oldest Pending Date</th>
                            <th>Pending Invoices</th>
                            <th>Received (Pending)</th>
                            <th>Remaining Balance</th>
                        </tr>
                        </thead>
                        <tbody>

                        <?php
                        $counter = 0;
                        if ($rows) {
                            foreach ($rows as $r) {
                                $counter++;

                                $totalPendingInvoices += (int)$r->pending_invoices;
                                $totalReceived += (float)$r->total_received_against_pending;
                                $totalRemainingBalance += (float)$r->remaining_balance;
                                ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?= $counter ?></td>
                                    <td><?= $r->customer_code ?></td>
                                    <td><?= $r->customer_name ?></td>
                                    <td><?= $r->customer_contact ?></td>
                                    <td><?= date('d-m-Y', strtotime($r->oldest_pending_date)) ?></td>
                                    <td><?= $r->pending_invoices ?></td>
                                    <td><?= number_format($r->total_received_against_pending, 2, '.', '') ?></td>
                                    <td><?= number_format($r->remaining_balance, 2, '.', '') ?></td>
                                </tr>
                                <?php
                            }
                            ?>
                            <tr>
                                <th colspan="5" style="text-align:right; border:1px solid #000;">TOTAL</th>
                                <th style="border:1px solid #000;"><?= $totalPendingInvoices ?></th>
                                <th style="border:1px solid #000;"><?= number_format($totalReceived, 2, '.', '') ?></th>
                                <th style="border:1px solid #000;"><?= number_format($totalRemainingBalance, 2, '.', '') ?></th>
                            </tr>
                            <?php
                        } else {
                            ?>
                            <tr>
                                <td colspan="8" style="text-align:center; padding: 12px;">
                                    No customers found.
                                </td>
                            </tr>
                            <?php
                        }
                        ?>

                        </tbody>
                    </table>
                </div>
            <?php } ?>

        </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="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/multiple_pending_invoices.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');
    });
</script>
</body>
</html>
