<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = "";
$dateto = "";
$bank_name = "";

$customer_name = "";
$product_id = "";
$productsQtys = "";
$productsUnitCostPrice = 0;
$totalDiscount = 0;
$productQtyTotal = 0;
$costPriceTotal = 0;
$unitPriceTotal = 0;
$totalProfit = 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));


$filterReportOpening = " WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>='2016-01-01' and DATE_FORMAT(create_date,'%Y-%m-%d')<'$qryDate' and status <> 'Cancel' ";
$filterReport = " WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(create_date,'%Y-%m-%d')<='$qryTo' and status <> 'Cancel' ";

if (isset($bank_name) && $bank_name != 'All') {
    $filterReport .= " AND statement_bank_id ='$bank_name'";
	$filterReportOpening .= " AND statement_bank_id ='$bank_name'";
}
$filterReport .= " ORDER BY create_date, 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>Bank Transaction 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 s4">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 s4"> Date To<br>
                            <input type="text" name="dateto" id="datepicker1"
                                   placeholder="Date To"
                                   value="<?= $dateto ?>" class="form-control"/>
                        </td>
                        <td class="col s4">Bank Name<br>
                            <?php
                            $bankfilter = " WHERE status = 'active' ORDER BY bank_name";
                            $banks = StatementBankSetup::GetBankSetupList($bankfilter);
                            ?>
                            <select class="form-control" required id="bank_name"
                                    name="bank_name">
                                <option>All</option>
                                <?php
                                if ($banks) {
                                    foreach ($banks AS $banksRow) {
                                        echo '<option ';
                                        if ($banksRow->id == $bank_name) {
                                            echo 'selected';
                                        }
                                        echo ' value="' . $banksRow->id . '" >' . $banksRow->bank_name . ' - ' . $banksRow->bank_account_number . '</option>';
                                    }
                                }
                                ?>
                            </select>
                        </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
                                    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="customTable">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>Date</th>
                            <th>Particulars</th>
                            <th>Payment Type</th>
                            <th>Bank Name</th>
                            <th>Check Number</th>
                            <th>Debit</th>
                            <th>Credit</th>
                            <th>Balance</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $qryOpening = BankTransactionSetup::GetOpeningbalance($filterReportOpening);
                        $balanceOpening = 0;
                        $balance = 0;
                        if($qryOpening){
                            foreach($qryOpening AS $qryOpeningRow){
                                if($qryOpeningRow->type == 'debit'){
                                    $balanceOpening -= $qryOpeningRow->amount;
                                }else{
                                    $balanceOpening += $qryOpeningRow->amount;
                                }
                            }
                            $balance = $balanceOpening;
                        }
                        $qry = BankTransactionSetup::GetBankTransactionSetupList($filterReport);
                        if($balance > 0) {
                            ?>
                            <tr style="border: 1px solid #666;">
                                <td><?php echo date('d - M', strtotime($qryDate)); ?></td>
                                <td>Balance</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td><?php echo $balance; ?></td>
                            </tr>
                            <?php
                        }
                        if ($qry) {
                            foreach ($qry as $qery) {
                                if($qery->type=='debit'){
                                    $balance -= $qery->amount;
                                }else{
                                    $balance += $qery->amount;
                                } ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?php echo date('d - M', strtotime($qery->create_date)); ?></td>
                                    <td><?php echo $qery->particulars; ?></td>
                                    <td><?php
                                        if($qery->payment_type == 'cash'){
                                            echo 'Cash';
                                        }else if($qery->payment_type == 'cheque'){
                                            echo 'Cheque';
                                        }else if($qery->payment_type == 'online'){
                                            echo 'Online';
                                        } ?>
                                    </td>
                                    <td>
                                        <?php
                                        if($qery->payment_type == 'cheque'){
                                            echo $qery->cheque_bank;
                                        }else if($qery->payment_type == 'online'){
                                            echo $qery->online_bank;
                                        } ?>
                                    </td>
                                    <td><?php echo $qery->cheque_number; ?></td>
                                    <td><?php echo ($qery->type=='debit'?$qery->amount:''); ?></td>
                                    <td><?php echo ($qery->type=='credit'?$qery->amount:''); ?></td>
                                    <td><?php echo $balance; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        <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 () {
        // initialize
        $('#customer_name').material_select();

        // setup listener for custom event to re-initialize on change
        $('#customer_name').on('contentChanged', function () {
            $(this).material_select();
        });
        $('#cat_name').on('change', function () {
            $('body.container').block({message: 'Loading...'});
            $.get('getFilterCustomersAll.php?id=' + $(this).val(), function (data) {
                $("#customer_name").empty();
                $("#customer_name").append(data);
                $('body.container').unblock();
                $('#customer_name').trigger('contentChanged');
            });
        });
        $("#print").on("click", function (e) {
            var fewSeconds = 2;
            e.preventDefault();
            $('#searchfrm').prop('target', '_blank');
            $('#searchfrm').prop('action', "<?=PROJECT_ADMIN_URL?>transaction/print/reportbanktransaction.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>