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

$maindb = "billsett";
$formname = "Reciept Received Register";
$file = "rectrecv_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);


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

$pcond = '';
if (!empty($party)) {
    $pcond = "AND party = '$party' ";
}
//if (empty($_POST["frmdt"])) {
//    browse("From Date Cannot Be Empty");
//    header("refresh:1; rentrecvreg.php");
//}
//if (empty($_POST["todt"])) {
//    browse("To Date Cannot Be Empty");
//    header("refresh:1; rentrecvreg.php");
//}
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


$mainqry = mysqli_query($conn, "SELECT * FROM `$maindb` where  `vouchdt` >= '$mfrmdate' AND 
                               `vouchdt` <= '$mtodate' and (source = 'BR' or source= 'CR') $pcond")or die(mysqli_error($conn));

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



    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    </head>
    <body>
        <div class="container" style="width:50%">
            <?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:5%">Sr.</th>
                                <th style="width:12%">Date</th>
                                <th style="width:10%">Voucher</th>
                                <th style="width:38%">Tenant</th>
                                <th style="width:10%" class="text-right">Amount</th>
                                <th style="width:10%">Chq. No.</th>
                                <th style="width:15%">Cheque Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $accode = $fmain['party'];
                                $gettenant = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode' ")or die(mysqli_error($conn));
                                $ftenant = mysqli_fetch_array($gettenant);
                                $tenant = $ftenant[name];

                                $source = $fmain['source'];
                                $vouchno = $source . " " . $fmain['vouchno'];
                                $amount = $fmain['setoff'];
                                $bnkvouch = $fmain['bnkvouch'];

                                if ($source == 'BR') {
                                    $gchq = mysqli_query($conn, "SELECT chqno,DATE_FORMAT(`chqdate`, '%d-%m-%Y')as chqdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt FROM bankrecp where vouchno = '$bnkvouch' ")or die(mysqli_error($conn));
                                    $fchq = mysqli_fetch_array($gchq);
                                    $chqno = $fchq['chqno'];
                                    $chqdt = $fchq['chqdt'];
                                    $vouchdt = $fchq['vouchdt'];
                                }

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$vouchdt</td>";
                                Echo "<td>$vouchno</td>";
                                Echo "<td>$tenant</td>";
                                Echo "<td class='text-right'>$amount</td>";
                                Echo "<td>$chqno</td>";
                                Echo "<td>$chqdt</td>";
                                Echo "</tr>";

                                $totamt = $totamt + $amount;
                            }
                            $colr = "red";
                            echo "<tr>";
                            Echo "<td colspan='4' class='text-right'><h5><b>Total</b></h5></td>";
                            Echo "<td class='text-right'><h5><b>$totamt</b></h5></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


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

    $contents .= "Sr.,Date,Voucher,Tenant,Amount,Chq.No.,Cheque Date\n";

    $ctr = 0;
    $totamt = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {

        $accode = $fmain['party'];
        $gettenant = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode' ")or die(mysqli_error($conn));
        $ftenant = mysqli_fetch_array($gettenant);
        $tenant = $ftenant[name];

        $source = $fmain['source'];
        $vouchno = $source . " " . $fmain['vouchno'];
        $amount = $fmain['setoff'];
        $bnkvouch = $fmain['bnkvouch'];

        if ($source == 'BR') {
            $gchq = mysqli_query($conn, "SELECT chqno,DATE_FORMAT(`chqdate`, '%d-%m-%Y')as chqdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt FROM bankrecp where vouchno = '$bnkvouch' ")or die(mysqli_error($conn));
            $fchq = mysqli_fetch_array($gchq);
            $chqno = $fchq['chqno'];
            $chqdt = $fchq['chqdt'];
            $vouchdt = $fchq['vouchdt'];
        }

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$vouchdt" . ",";
        $contents .= "$vouchno" . ",";
        $contents .= "$tenant" . ",";
        $contents .= "$amount" . ",";
        $contents .= "$chqno" . ",";
        $contents .= "$chqdt" . "\n";


        $totamt = $totamt + $amount;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $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;
    }
}
?>
