<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$zroot = $_SESSION["zdbroot"];
$formname = 'Unsetlled Receipt';

$file = 'rgbillpend_fr.php';
$zroot = $_SESSION['zdir'];

$accode = $_POST["name"];
$var = $_POST["var"];

if ($var == 'R') {
    $formname = "Unsettled Vouchers - Receipt";
    $cond = " and grpcode = '010' ";
} else {
    $formname = "Unsettled Vouchers - Payment";
    $cond = " and grpcode = '009' ";
}

$accond = '';
if (!empty($accode)) {
    $accond = " AND `party` = '$accode' ";
}

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}

if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}

$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

if ($var == 'R') {
    $getpmain = mysqli_query($conn, "(SELECT vouchno, 'BR' as source, bankname, chqno, party, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, 
                                             amount FROM `bankrecp` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and retdate = '0000-00-00' 
                                             $accond and party in (select accode from accmast where grpcode = '010' ) AND 
                                             concat(vouchno,'BR') not in (select concat(bnkvouch,source) from billsett ) order by vouchdt)
                                     union all 
                                     (SELECT vouchno, 'CR' as source, ' ' as bankname, ' ' as chqno, party, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt , 
                                             amount FROM `cashrecp` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  
                                             $accond and party in (select accode from accmast where grpcode = '010' ) AND 
                                             concat(vouchno,'CR') not in (select concat(bnkvouch,source) from billsett ) order by vouchdt) ") or die(mysqli_error($conn));
} else {
    $getpmain = mysqli_query($conn, "(SELECT vouchno, 'BP' as source, bankname, chqno, party, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, 
                                             amount FROM `bankrecp` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and retdate = '0000-00-00' 
                                             $accond and party in (select accode from accmast where grpcode = '009' ) AND 
                                             concat(vouchno,'BP') not in (select concat(bnkvouch,source) from billsett ) order by vouchdt)
                                     union all 
                                     (SELECT vouchno, 'CP' as source, ' ' as bankname, ' ' as chqno, party, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt , 
                                             amount FROM `cashrecp` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  
                                             $accond and party in (select accode from accmast where grpcode = '009' ) AND 
                                             concat(vouchno,'CP') not in (select concat(bnkvouch,source) from billsett ) order by vouchdt) ") or die(mysqli_error($conn));
}
$get = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr</th>
                                <th style="width:8%">Date</th>
                                <th style="width:8%">Voucher</th>
                                <th style="width:30%">Bank</th>
                                <th style="width:8%">Cheque</th>
                                <th style="width:30%">Party</th>
                                <th style="width:10%">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamount = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $vouchno = $pmain['vouchno'];

                                $gschool = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[bankname]' ")or die(mysqli_error($conn));
                                $sschool = mysqli_fetch_array($gschool);
                                $bank = $sschool['name'];

                                $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
                                $fparty = mysqli_fetch_array($gparty);
                                $party = $fparty['name'];

                                if ($var == 'R') {
                                    if ($pmain['source'] == 'BR') {
                                        $href = "bankrecp.php";
                                    } else {
                                        $href = "cashrecp.php";
                                    }
                                } else {
                                    if ($pmain['source'] == 'BP') {
                                        $href = "bankpay.php";
                                    } else {
                                        $href = "cashpay.php";
                                    }
                                }
                                
                                $ctr++;
//                                Echo "<tr id='boldfont'>";
                                echo "<tr data-href='$href?mode=edit&vouchno=$vouchno' class='clickable-row2' [target]='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[source] " . ' ' . "$pmain[vouchno]</td>";
                                Echo "<td>$bank</td>";
                                Echo "<td>$pmain[chqno]</td>";
                                Echo "<td>$party</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "</tr>";
                                $totamount = $totamount + $pmain[amount];
                            }
                            ?>

                        </tbody>
                        <tbody>
                            <tr>
                                <td colspan="10" class="text-right" style="color:red;font-size: 14px;"><b><?= $totamount ?></b></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


if (isset($_POST["excel"])) {

    $zcomp = mysqli_fetch_assoc($comp);

    $contents .= "Sr.,Date, Voucher, Bank, Cheque No., Party Name, Amount \n";

    $ctr = 0;
    $totamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $gschool = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[bankname]' ")or die(mysqli_error($conn));
        $sschool = mysqli_fetch_array($gschool);
        $bank = $sschool['name'];

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $fparty = mysqli_fetch_array($gparty);
        $party = $fparty['name'];

        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $pmain['source'] . ' ' . $pmain['vouchno'] . ",";
        $contents .= $bank . ",";
        $contents .= $pmain['chqno'] . ",";
        $contents .= $party . ",";
        $contents .= $pmain['amount'] . "\n";

        $totamt = $totamt + $pmain['amount'];
    }
    $contents .= ",,,,,";
    $contents .= 'Total' . ",";
    $contents .= $totamt . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>