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

$maindb = "checkout";
$formname = 'Lodging Register';
$file = "lodgingreg_fr.php";

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

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);
$paytype = $_POST["paytype"];
$roomno = $_POST["roomno"];

$paytypecond = "";
$roomcond = "";

if (!empty($paytype)){
    $paytypecond = "AND paytype = '$paytype'";
}
if (!empty($roomno)){
    $roomcond = "AND roomno = '$roomno'";
}


if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}


$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt  FROM $maindb WHERE vouchno != ''
                                 AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $paytypecond $roomcond ") 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;
        }
    </style>

    </head>

    <body>
        <div class="container" style="width: 1490px;">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Bill No.</th>
                                <th style="width:5%">Date</th>
                                <th style="width:5%;">Booking No.</th>
                                <th style="width:4%;">Room No.</th>
                                <th style="width:15%;">Party Name</th>
                                <th style="width:5%;">Check In </th>
                                <th style="width:6%;">Check Out </th>
                                <th style="width:4%;">No. Of Days </th>
                                <th style="width:6%;text-align:right">Rate Per Day </th>
                                <th style="width:5%;text-align:right">Bill Amt.</th>
                                <th style="width:5%;text-align:right">Advance</th>
                                <th style="width:5%;text-align:right">Balance</th>
                                <th style="width:7%;">Payment Type</th>
                                <th style="width:15%;">Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $sontot = 0;
                            $dautot = 0;

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

                                $chkin = date('d-m-Y h:i A', strtotime($pmain['chkin']));
                                $chkout = date('d-m-Y h:i A', strtotime($pmain['chkout']));


                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$pmain[bookno]</td>";
                                echo "<td>$pmain[roomno]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td>$chkin</td>";
                                echo "<td>$chkout</td>";
                                echo "<td>$pmain[daysno]</td>";
                                echo "<td style='text-align:right;'>$pmain[rate]</td>";
                                echo "<td style='text-align:right;'>$pmain[billamt]</td>";
                                echo "<td style='text-align:right;'>$pmain[advance]</td>";
                                echo "<td style='text-align:right;'>$pmain[balance]</td>";
                                echo "<td>$pmain[paytype]</td>";
                                echo "<td>$pmain[remark1]   $pmain[remark2] </td>";

                                $totbillamt1 = $pmain['billamt'] + $totbillamt;
                                $totadvance1 = $pmain['advance'] + $totadvance;
                                $totbalance1 = $pmain['balance'] + $totbalance;

                                $totbillamt = number_format($totbillamt1, 2);
                                $totadvance = number_format($totadvance1, 2);
                                $totbalance = number_format($totbalance1, 2);
                                echo "</tr>";
                            }
                            echo "<tr id='boldfont' >";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;font-size:15px'>Total</td>";
                            echo "<td style='text-align:right;font-size:15px'>$totbillamt</td>";
                            echo "<td style='text-align:right;font-size:15px'>$totadvance</td>";
                            echo "<td style='text-align:right;font-size:15px'>$totbalance</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 .="Bill No., Date, Booking No., Room No., Party Name, Check In, Check Out, No. Of Days, Rate Per Day, Bill Amt., Advance, Balance, Payment Type, Remarks\n ";

$ctr = 0;
$sontot = 0;
$dautot = 0;

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

    $chkin = date('d-m-Y h:i A', strtotime($pmain['chkin']));
    $chkout = date('d-m-Y h:i A', strtotime($pmain['chkout']));


    $ctr++;

    $contents .="$pmain[vouchno]" . ",";
    $contents .="$pmain[vouchdt]" . ",";
    $contents .="$pmain[bookno]" . ",";
    $contents .="$pmain[roomno]" . ",";
    $contents .="$pmain[name]" . ",";
    $contents .="$chkin" . ",";
    $contents .="$chkout" . ",";
    $contents .="$pmain[daysno]" . ",";
    $contents .="$pmain[rate]" . ",";
    $contents .="$pmain[billamt]" . ",";
    $contents .="$pmain[advance]" . ",";
    $contents .="$pmain[balance]" . ",";
    $contents .="$pmain[paytype]" . ",";
    $contents .="$pmain[remark1]" . " " . "$pmain[remark2]" . "\n";

    $totbillamt1 = $pmain['billamt'] + $totbillamt;
    $totadvance1 = $pmain['advance'] + $totadvance;
    $totbalance1 = $pmain['balance'] + $totbalance;

    $totbillamt = number_format($totbillamt1, 2);
    $totadvance = number_format($totadvance1, 2);
    $totbalance = number_format($totbalance1, 2);

}

$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="Total" . ",";
$contents .="$totbillamt1" . ",";
$contents .="$totadvance1" . ",";
$contents .="$totbalance1" . ",";
$contents .="" . ",";
$contents .="" . "\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;
    }
}
?>