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

$maindb = "invmainres";
$maindb2 = "invmaincom";
$formname = "Receipt Summary";

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


$party = $_POST['party'];
$gentype = $_POST['gentype'];
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);
$a = 1;
$pcond = '';
if (!empty($party)) {
    $pcond = "AND `accode` = '$party' ";
}
$gencond = '';
if (!empty($gentype)) {
    $gencond = "AND `gentype` = '$gentype' ";
}
if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    $a = 0;
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    $a = 0;
}
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$mainqry = mysqli_query($conn, "SELECT sum(rent) as amt,accode,monthno,monname FROM `$maindb` where  `vouchdt` >= '$mfrmdate' AND 
                               `vouchdt` <= '$mtodate' $gencond $pcond group by monthno order by monthno+0")or die(mysqli_error($conn));

$mainqry2 = mysqli_query($conn, "SELECT sum(rent) as amt,accode,monthno,monname FROM `$maindb2` where  `vouchdt` >= '$mfrmdate' AND 
                               `vouchdt` <= '$mtodate' $gencond $pcond group by monthno order by monthno+0")or die(mysqli_error($conn));
$m1 = mysqli_num_rows($mainqry);
$m2 = mysqli_num_rows($mainqry2);
$m = $m1 + $m2;
if ($m <= 0) {
    $a = 0;
}
if ($a == 0) {
    echo "<script>"
    . "window.close()"
    . "</script>";
}
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">
                        <B style="font-size: 19px;color:green">Residential</B>
                        <tr>
                            <th style="width:5%">Sr.</th>
                            <!--<th style="width:70%">Tenant</th>-->
                            <th style="width:10%" class="text-right">Amount</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $gtotamt = 0;
                            $totamt = 0;

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

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

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

                                $totamt = $totamt + $amount;
                            }
                            $colr = "red";
                            echo "<tr style='color:$colr'>";
                            Echo "<td class='text-right'><h4><b>Total</b></h4></td>";
                            Echo "<td class='text-right'><h4><b>$totamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>

                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                        <b style="font-size: 19px;color:blue">Commercial</b>
                        <tr class="mySearchTr">
                            <th style="width:5%">Sr.</th>
                            <!--<th style="width:70%">Tenant</th>-->
                            <th style="width:10%" class="text-right">Amount</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr2 = 0;
                            $totamt2 = 0;

                            while ($fmain2 = mysqli_fetch_array($mainqry2)) {

                                $accode2 = $fmain2['accode'];
                                $monname2 = $fmain2['monname'];
                                $amount2 = $fmain2['amt'];
//                                $gettenant2 = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode2' ")or die(mysqli_error($conn));
//                                $ftenant2 = mysqli_fetch_array($gettenant2);
//                                $tenant2 = $ftenant2['name'];

                                $ctr2++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr2</td>";
//                                Echo "<td>$tenant2</td>";
                                Echo "<td class='text-right'>$amount2</td>";
                                Echo "</tr>";

                                $totamt2 = $totamt2 + $amount2;
                            }
                            $colr = "red";
                            echo "<tr style='color:$colr'>";
                            Echo "<td class='text-right'><h3><b>Total</b></h3></td>";
                            Echo "<td class='text-right'><h4><b>$totamt2</b></h4></td>";
                            echo "</tr>";
                            echo "<tr style='height:15px;'>";
                            echo "</tr>";
                            $gtotamt = $totamt + $totamt2;
                            echo "<tr style='color:green;font-size:25px'>";
                            Echo "<td class='text-right' style='font-size:22px'><b>Grand Total</b></td>";
                            Echo "<td class='text-right' style='font-size:22px'><b>$gtotamt</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>

                </div>
            </div>
        </div>

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

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

    $contents .= "Sr.,Tenant,Amount\n";

    $ctr = 0;
    $gtotamt = 0;
    $totamt = 0;

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

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

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$monname" . ",";
        $contents .= "$amount" . "\n";


        $totamt = $totamt + $amount;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totamt" . "\n";
    $contents .= "" . "\n";


    $contents .= "Sr.,Tenant,Amount\n";

    $ctr2 = 0;
    $totamt2 = 0;

    while ($fmain2 = mysqli_fetch_array($mainqry2)) {

        $accode2 = $fmain2['accode'];
        $monname2 = $fmain2['monname'];
        $amount2 = $fmain2['amt'];
        $gettenant2 = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode2' ")or die(mysqli_error($conn));
        $ftenant2 = mysqli_fetch_array($gettenant2);
        $tenant2 = $ftenant2['name'];

        $ctr2++;

        $contents .= "$ctr2" . ",";
        $contents .= "$monname2" . ",";
        $contents .= "$amount2" . "\n";


        $totamt2 = $totamt2 + $amount2;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totamt2" . "\n";
    $contents .= "" . "\n";

    $gtotamt = $totamt + $totamt2;

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