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

$maindb = "invmaincom";
$formname = "Receipt Register - Commercial";
$file = "recpcomreg_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 $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>
    </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:2%">Sr.</th>
                                <th style="width:4%">Recp No.</th>
                                <th style="width:15%">Tenant - Flat No.</th>
                                <!--<th style="width:4%">Flat No</th>-->
                                <th style="width:12%">Period</th>
                                <th style="width:4%; text-align: right; ">Rent</th>
<!--                                <th style="width:4%; 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;">Taxable</th>-->
                                <th style="width:5%; text-align: right;">CGST Rate</th>
                                <th style="width:5%; text-align: right;">CGST Amt.</th>
                                <th style="width:5%; text-align: right;">SGST Rate</th>
                                <th style="width:5%; text-align: right;">SGST Amt.</th>
                                <th style="width:5%; text-align: right;">Net Amt.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totrent = 0;
                            $tottax = 0;
                            $totrep = 0;
                            $tototh = 0;
                            $total = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totaxable = 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'];
                                $rtotal = round($rmain['total']);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$rmain[vouchno]</td>";
                                Echo "<td>$fbuild[name] - $rmno</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[taxable]</td>";
                                Echo "<td style='text-align: right;'>$rmain[cgstperc]</td>";
                                Echo "<td style='text-align: right;'>$rmain[cgstamt]</td>";
                                Echo "<td style='text-align: right;'>$rmain[sgstperc]</td>";
                                Echo "<td style='text-align: right;'>$rmain[sgstamt]</td>";
                                Echo "<td style='text-align: right;'>$rtotal</td>";
                                Echo "</tr>";
                                $totrent = $totrent + $rmain[rent];
                                $tottax = $tottax + $rmain[proptax];
                                $totrep = $totrep + $rmain[repairtax];
                                $tototh = $tototh + $rmain[othertax];
                                $totcgst = $totcgst + $rmain[cgstamt];
                                $totsgst = $totsgst + $rmain[sgstamt];
                                $totaxable = $totaxable + $rmain[taxable];
                                $grtotal = $grtotal + $rtotal;
                            }
                            Echo "<tr  style='font-weight: bolder; text-align: right; color: red;'>";
                            Echo "<td colspan='4'>Total</td>";
                            Echo "<td >$totrent</td>";
//                            Echo "<td >$tottax</td>";
//                            Echo "<td >$totrep</td>";
//                            Echo "<td >$tototh</td>";
//                            Echo "<td >$totaxable</td>";
                            Echo "<td ></td>";
                            Echo "<td >$totcgst</td>";
                            Echo "<td ></td>";
                            Echo "<td >$totsgst</td>";
                            Echo "<td >$grtotal</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, GST No.,Rent,Taxable,CGST %,CST Amount,SGST %,SGST Amount,Net Amount\n";
    $contents .= "Sr.,Recp No.,Tenant,Room No,Month, GST No.,Rent,CGST %,CST Amount,SGST %,SGST Amount,Net Amount\n";

    $ctr = 0;
    $totrent = 0;
    $tottax = 0;
    $totrep = 0;
    $tototh = 0;
    $total = 0;
    $totcgst = 0;
    $totsgst = 0;
    $totaxable = 0;
    while ($rmain = mysqli_fetch_array($getrecp)) {
        $ctr++;
        $getbuild = mysqli_query($conn, "SELECT * FROM `tenant` WHERE `accode`= '$rmain[accode]'") or die(mysqli_error($conn));
        $fbuild = mysqli_fetch_array($getbuild);

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

        $contents .= "$ctr" . ",";
        $contents .= "$rmain[vouchno]" . ",";
        $contents .= "$fbuild[name]" . ",";
        $contents .= "$rmno" . ",";
        $contents .= "$rmain[monthno] - $rmain[monname]" . ",";
        $contents .= "$gstno" . ",";
        $contents .= "$rmain[rent]" . ",";
//        $contents .= "$rmain[taxable]" . ",";
        $contents .= "$rmain[cgstperc]" . ",";
        $contents .= round($rmain[cgstamt]) . ",";
        $contents .= "$rmain[sgstperc]" . ",";
        $contents .= round($rmain[sgstamt]) . ",";
        $contents .= round($rmain['total']) . "\n";

        $totrent = $totrent + $rmain[rent];
        $tottax = $tottax + $rmain[proptax];
        $totrep = $totrep + $rmain[repairtax];
        $tototh = $tototh + $rmain[othertax];
        $total = $total + round($rmain['total']);
        $totcgst = $totcgst + round($rmain[cgstamt]);
        $totsgst = $totsgst + round($rmain[sgstamt]);
        $totaxable = $totaxable + $rmain[taxable];
    }


    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totrent" . ",";
//    $contents .= "$totaxable" . ",";
    $contents .= "" . ",";
    $contents .= "$totcgst" . ",";
    $contents .= "" . ",";
    $contents .= "$totsgst" . ",";
    $contents .= "$total" . "\n";

    $contents = strip_tags($contents);

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