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

$maindb = "invmainres";
$formname = "Receipt Register - Residential";
$file = "recpreg_fr.php";

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

$tenant = $_POST["tenant"];
$block = $_POST["block"];
$floortype = $_POST["floortype"];
$recptype = $_POST["recptype"];
$monthno = $_POST["monthno"];
$flatno = $_POST["flatno"];

$tenantcond = "";
$blockcond = "";
$recptypecond = "";
$floortypecond = "";
$monthnocond = "";
$flatnocond = "";

if (!empty($flatno)) {
    $flatnocond = " AND `flatno`='$flatno' ";
}
if (!empty($floortype)) {
    $floortypecond = " AND `floortype`='$floortype' ";
}
if (!empty($monthno)) {
    $monthnocond = " AND `monthno`='$monthno' ";
}
if (!empty($tenant)) {
    $tenantcond = " AND `accode`='$tenant' ";
}
if (!empty($recptype)) {
    $recptypecond = " AND `gentype`='$recptype' ";
}

if (!empty($block)) {
    $blockcond = " AND `block`='$block' ";
}
$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);


$getrecp = mysqli_query($conn, "SELECT *,date_format(fromdate,'%d/%M/%Y') as fromdate, date_format(todate,'%d/%M/%Y') as todate  
                                FROM $maindb WHERE vouchno!='' and accode in (Select accode from accmast
                                where accode != '' $floortypecond $blockcond $flatnocond)
                                $tenantcond $recptypecond $monthnocond ORDER BY vouchno") or die(mysqli_error($conn));

$get = mysqli_num_rows($getrecp);

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>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container" style="width:90%">
            <?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:5%">Recp No.</th>
                                <th style="width:15%">Tenant</th>
                                <th style="width:5%">Flat No</th>
                                <th style="width:10%">Period</th>
                                <th style="width:5%; text-align: right; ">Rent</th>
<!--                                <th style="width:5%; text-align: right;">Prop.Tax</th>
                                <th style="width:5%; text-align: right;">Repair Cess</th>
                                <th style="width:5%; text-align: right;">Oth Chgs.</th>
                                <th style="width:5%; text-align: right;">Total</th>-->
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totrent = 0;
                            $tottax = 0;
                            $totrep = 0;
                            $tototh = 0;
                            $total = 0;

                            while ($rmain = mysqli_fetch_array($getrecp)) {

                                $getbuild = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$rmain[accode]'") or die(mysqli_error($conn));
                                $fbuild = mysqli_fetch_array($getbuild);

                                $flrno = $fbuild['floorno'];
                                $rmno = $fbuild['flatno'];




                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$rmain[vouchno]</td>";
                                Echo "<td>$fbuild[name]</td>";
                                Echo "<td>$rmno</td>";
                                Echo "<td>$rmain[fromdate] - $rmain[todate]</td>";
                                Echo "<td style='text-align: right;'>$rmain[rent]</td>";
//                                Echo "<td style='text-align: right;'>$rmain[proptax]</td>";
//                                Echo "<td style='text-align: right;'>$rmain[repairtax]</td>";
//                                Echo "<td style='text-align: right;'>$rmain[othertax]</td>";
//                                Echo "<td style='text-align: right;'>$rmain[total]</td>";
                                Echo "</tr>";
                                $totrent = $totrent + $rmain[rent];
                                $tottax = $tottax + $rmain[proptax];
                                $totrep = $totrep + $rmain[repairtax];
                                $tototh = $tototh + $rmain[othertax];
                                $total = $total + $rmain[total];
                            }
                            Echo "<tr  style='font-weight: bolder; text-align: right; color: red;'>";
                            Echo "<td colspan='5'>Total</td>";
                            Echo "<td >$totrent</td>";
//                            Echo "<td >$tottax</td>";
//                            Echo "<td >$totrep</td>";
//                            Echo "<td >$tototh</td>";
//                            Echo "<td >$total</td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

//    $contents .= "Sr.,Recp No.,Tenant,Room No,Month,Rent,Prop.Tax,Repair Cess,Oth Chgs.,Total\n";
    $contents .= "Sr.,Recp No.,Tenant,Room No,Month,Rent\n";

    $ctr = 0;
    $total = 0;
    while ($rmain = mysqli_fetch_array($getrecp)) {
        $ctr++;
        $getbuild = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$rmain[accode]'") or die(mysqli_error($conn));
        $fbuild = mysqli_fetch_array($getbuild);

        $flrno = $fbuild['floorno'];
        $rmno = $fbuild['room'];

        $contents .= "$ctr" . ",";
        $contents .= "$rmain[vouchno]" . ",";
        $contents .= "$fbuild[name]" . ",";
        $contents .= "$rmno" . ",";
        $contents .= "$rmain[monthno]" . ",";
        $contents .= "$rmain[rent]" . ",";
//        $contents .= "$rmain[proptax]" . ",";
//        $contents .= "$rmain[repairtax]" . ",";
//        $contents .= "$rmain[othertax]" . ",";
//        $contents .= "$rmain[total]" . "\n";

        $totrent = $totrent + $rmain[rent];
        $tottax = $tottax + $rmain[proptax];
        $totrep = $totrep + $rmain[repairtax];
        $tototh = $tototh + $rmain[othertax];
        $total = $total + $rmain[total];
    }

    $contents .= ",,,,Total,$totrent" . "\n";
    Echo "</tr>";

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}
?>