<?php
include("../classes/manager.class.php");
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL . "index.php");
    exit;
}
//print_r($_SESSION);
//exit;


$m = new Manager();
//$pagesize = (array_key_exists('pagesize', $_REQUEST)) ? $_REQUEST['pagesize'] : 15;
//$pageno = (array_key_exists('pageno', $_REQUEST)) ? $_REQUEST['pageno'] : 1;
//$dbobj = new Manager;
$datefrom = date('Y-m-d', strtotime("-30 days"));
$dateto = date('Y-m-d');
$filters = "";
$str = "";
$searchtxt = "";
$productid = "";
$confirm = "";
$action = '';
$obj = '';
extract($_REQUEST);
//print_r($_SESSION);
if ($_SESSION['nextclass'] == 1) {
    //$sperator = 3;
    $sperator = 4;
} else {
    $sperator = 4;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style>
        .activityicon {
            width: 40px;
            height: 40px;
            display: block;
            text-align: center;
            line-height: 40px;
            font-size: 20px;
            color: #fff;
        }

        .card .card-action {
            padding: 5px 20px;
        }

        .card .card-content {
            padding: 0px;
        }
    </style>
</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" style="background-color: #efefef;">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12 cyan darken-2 white-text">
            <h1 class="center-align"><?php if ($action == 11) { ?>
                    Password Successfully Changed
                <?php } else { ?>
                    <?= PROJECT_NAME ?> Dashboard
                <?php } ?></h1>
        </div>
    </div>
    <!--mini statistics start-->
    <?php
    if ($_SESSION['sess_type'] == 'administrator') {
        $filterInvoiceCondition = " JOIN customer_setup AS cs ON cs.id = invoice.customer_id Where invoice.status not in ('Cancel', 'Draft') AND cs.status = 'active'";
        $invoices = Invoice::GetInvoiceList($filterInvoiceCondition);
        //print_r($invoices);
        if ($invoices) {
            $totalInvoicesAmounts = 0;
            foreach ($invoices AS $invoicesRow) {
                $invoiceTotal = $invoicesRow->total_amount;
                $totalInvoicesAmounts += $invoiceTotal;
            }

        }
        //$totalInvoicesDueAmounts = $totalInvoicesAmounts - $totalInvoicesDueAmounts;
        $filterPaymentCondition = " JOIN customer_setup AS cs ON cs.id = payment.customer_id Where payment.status <> 'Cancel' AND cs.status = 'active'";
        $payments = Payment::GetPaymentListByCondition($filterPaymentCondition);
        if ($payments) {
            $totalPaymentAmounts = 0;
            foreach ($payments AS $paymentsRow) {
                $payment_amount = intval($paymentsRow->amount);
                $totalPaymentAmount[] = $payment_amount;
                $totalPaymentAmounts += $payment_amount;
            }
        }
        $totalPaymentAmountForGraph = $totalPaymentAmount;
        if (!empty($totalPaymentAmountForGraph)) {
            $totalPaymentAmountForGraph = array_slice($totalPaymentAmountForGraph, -37, 37);
            $totalPaymentAmountText = implode(',', $totalPaymentAmountForGraph);
            $totalPaymentPercentage = ($totalPaymentAmounts / $totalInvoicesAmounts) * 100;

            $totalInvoicesDueAmounts = $totalInvoicesAmounts - $totalPaymentAmounts; // added
        }

        $opBalance = 0;
        $customersfilter = " WHERE status = 'active'";
        $customerlistsDue = CustomerSetup::GetCustomerSetupList($customersfilter);
        if ($customerlistsDue) {
            foreach ($customerlistsDue AS $customersRow) {
                $opBalance += $customersRow->balance;
            }
        }
        $totalInvoicesDueAmounts += $opBalance;


        $stock = Stock::GetStockSetupList();
        if ($stock) {
            $totalstock = 0;
            foreach ($stock AS $stockRow) {
                $totalstock += $stockRow->qty;
            }
        }
        ?>
        <div class="row">
            <div class="col s12 m12">
                <div class="card">
                    <div class="card-content">
                        <h5 class="card-stats-title center-align"><i class="mdi-social-group-add"></i> Monthly Payments
                        </h5>

                        <div id="monthly-pay-bar" class="center-align"></div>
                    </div>
                    <div class="card-action cyan darken-2">
                        <h5 class="card-stats-number left white-text"><?= $totalPaymentAmounts ?></h5>

                        <p class="card-stats-compare right-align white-text"><i
                                class="mdi-hardware-keyboard-arrow-up"></i> <?= number_format($totalPaymentPercentage, 2) ?>
                            %</p>
                    </div>
                </div>
            </div>
        </div>
        <!--mini statistics end-->

        <!--card stats start-->
        <div id="card-stats">
            <div class="row">
                <div class="col s12 m6 l<?= $sperator ?>">
                    <div class="card">
                        <div class="card-content">
                            <p class="card-stats-title"><i class="mdi-social-group-add"></i> Total Invoiced Amount</p>
                            <h4 class="card-stats-number"><?= $totalInvoicesAmounts ?></h4>
                        </div>
                        <div class="card-action  cyan darken-4">
                            <div id="clients-bar" class="center-align"></div>
                        </div>
                    </div>
                </div>
                <div class="col s12 m6 l<?= $sperator ?>">
                    <div class="card">
                        <div class="card-content">
                            <p class="card-stats-title"><i class="mdi-editor-insert-drive-file"></i> Total Payments
                                Amount
                            </p>
                            <h4 class="card-stats-number"><?= $totalPaymentAmounts ?></h4>
                        </div>
                        <div class="card-action  cyan darken-3">
                            <div id="invoice-line" class="center-align"></div>
                        </div>
                    </div>
                </div>
                <div class="col s12 m6 l<?= $sperator ?>">
                    <div class="card">
                        <div class="card-content">
                            <p class="card-stats-title"><i class="mdi-action-trending-up"></i> Total Due Payments</p>
                            <h4 class="card-stats-number"><?= $totalInvoicesDueAmounts ?></h4>
                        </div>
                        <div class="card-action cyan darken-2">
                            <div id="profit-tristate" class="center-align"></div>
                        </div>
                    </div>
                </div>
                <!--            --><?php //if($sperator != 4){ ?>
                <!--                <div class="col s12 m6 l--><?php //= $sperator ?><!--">-->
                <!--                    <div class="card">-->
                <!--                        <div class="card-content">-->
                <!--                            <p class="card-stats-title"><i class="mdi-editor-attach-money"></i>Total Stock</p>-->
                <!--                            <h4 class="card-stats-number">--><?php //= $totalstock ?><!--</h4>-->
                <!--                        </div>-->
                <!--                        <div class="card-action cyan darken-1">-->
                <!--                            <div id="sales-compositebar" class="center-align"></div>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                </div>-->
                <!--            --><?php //} ?>
            </div>
        </div>
        <!--card stats end-->

        <!--notification start-->
        <div class="row">
            <div class="col s12 m12">
                <ul class="collapsible collapsible-accordion" data-collapsible="expandable">
                    <li class="active">
                        <div class="collapsible-header active"><i class="mdi-device-now-widgets"></i> Last
                            Activity
                        </div>
                        <div class="collapsible-body orange lighten-5">
                            <?php
                            $filterPayments = " WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='$datefrom' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$dateto' AND inv.status <> 'Draft' ";
                            $filterPayments .= " UNION SELECT 'payment' AS nameTable, pay.create_date, pay.code, pay.customer_id, pay.invoice_reference_code, pay.payment_type, pay.amount,
pay.cheque_bank_name, pay.cheque_number, pay.cheque_payee_name, pay.cheque_date, pay.bank_name, pay.bank_account_number, pay.party_name, pay.from_bank_name,
'', '', '','', '', ''

 from " . TABLE_PAYMENT . " AS pay WHERE DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='$datefrom' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$dateto' ";


                            $filterPayments .= " ORDER BY create_date DESC LIMIT 5";
                            //$filterPayments = " JOIN " . TABLE_PAYMENT . " AS pay ON cus.id = pay.customer_id WHERE (DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='$datefrom' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$dateto' AND DATE_FORMAT(pay.create_date,'%Y-%m-%d')>='$datefrom' and DATE_FORMAT(pay.create_date,'%Y-%m-%d')<='$dateto') ";
                            $qry = Invoice::GetInvoicePaymentReportLists($filterPayments);
                            if ($qry) {
                                echo '<ul class="collection">';
                                foreach ($qry as $qery) { ?>
                                    <li class="collection-item avatar email-unread ">
                                    <span class="circle <?= $qery->nameTable == 'invoice' ? 'blue' : 'red' ?>"><i
                                            class="activityicon <?= $qery->nameTable == 'invoice' ? 'blue mdi-action-assignment' : 'red mdi-action-payment' ?>"></i></span>
                                                <span class="email-title">
                                                    <?php
                                                    echo ucwords($qery->nameTable);
                                                    if ($qery->status == 'Cancel') {
                                                        echo ' - Canceled';
                                                    }
                                                    ?>
                                                </span>
                                    <span
                                        class="black-text ultra-small right"><?= Helper::ago(strtotime($qery->create_date)); ?></span>

                                        <p class="truncate grey-text ultra-small" style="padding: 0;">
                                            <?php
                                            $customer = CustomerSetup::GetCustomerSetupById($qery->customer_id);
                                            echo $customer->name . " ";
                                            ?>
                                            <span class="orange-text ultra-small right">
                                                    <?php
                                                    if ($qery->nameTable == 'invoice') {
                                                        echo '<span class="pull-right">Rs ' . $qery->total_amount . '</span>';
                                                    } else if ($qery->nameTable == 'payment') {
                                                        echo '<span class="pull-right">Rs ' . $qery->amount . '</span>';
                                                    }
                                                    ?>
                                                </span>
                                        </p>
                                    </li>
                                <?php }
                                echo '</ul>';
                            } ?>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
        <!--notification end-->
        <!--mini statistics end-->
    <?php } else { ?>
        <div class="row" style="min-height: 400px;">
            <div class="col s12 m12">
            </div>
        </div>
    <?php } ?>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>
<!--/.fluid-container-->
<?php include('includes/inc_js.php'); ?>
<?php if ($_SESSION['sess_type'] == 'administrator') { ?>
    <script>
        $("#monthly-pay-bar").sparkline([<?= $totalPaymentAmountText ?>], {
            type: 'bar',
            height: '225',
            barWidth: 23,
            barSpacing: 5,
            barColor: '#00BCD4',
        });
        $("#monthly-pay-bar").sparkline([<?= $totalPaymentAmountText ?>], {
            composite: true,
            type: 'line',
            width: '100%',
            height: '25',
            lineWidth: 2,
            lineColor: '#1e88e5',
            fillColor: 'transparent',
            highlightSpotColor: '#eeff41',
            highlightLineColor: '#E1D0FF',
            minSpotColor: '#f44336',
            maxSpotColor: '#4caf50',
            spotColor: '#eeff41',
            spotRadius: 4,
            //tooltipFormat: $.spformat('{{value}}', 'tooltip-class')
        });
    </script>
<?php } ?>
</body>
</html>