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

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

$formname = 'Hotel Sale Register';

$maindb = 'pax';
$trandb = 'tranpurch';
$file = 'hotelsalereg_fr.php';

$dtype = $_POST["dtype"];
$city = $_POST["city"];
$hotel = $_POST["hotel"];
$agent = $_POST["agent"];

$citycond = "";
$citycond2 = "";
$hotelcond = "";
$hotelcond2 = "";
$checkcond = "";
$agentcond = "";
$custcond = "";


if (!empty($agent)) {
    $agentcond = " AND a.`agent`= '$agent' ";
    $custcond = " AND a.`customer`= '$agent' ";
}

if (!empty($city)) {
    $citycond = " AND b.`city`= '$city' ";
    $citycond2 = " AND `city`= '$city' ";
}

if (!empty($hotel)) {
    $hotelcond = " AND b.`hotelnm`= '$hotel' ";
    $hotelcond2 = " AND `hotelnm`= '$hotel' ";
}

$checkin = trim($_POST["checkin"]);
$checkout = trim($_POST["checkout"]);

if ($checkin != "" && $checkout == "") {
    browse("Check Out Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}

if ($checkout != "" && $checkin == "") {
    browse("Check In Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}

if (!empty($checkin)) {
    $checkcond = "AND b.`checkin` >= '$checkin' AND b.`checkout` <= '$checkout'";
    $checkcond2 = "AND `checkin` >= '$checkin' AND `checkout` <= '$checkout'";
}

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

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

    if (empty($_POST["frmdt"])) {
        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>";
    }

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

$hotelamt = $pmain['hotelamt'];
$hotelinramt = $pmain['hotelinramt'];
$fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];


$getpmain2 = mysqli_query($conn, "SELECT * from 
                                  (select 'QI' as source, a.vouchno, a.date, a.hotelamt, hotelinramt, concat(surname, '' , `name`, '' , midname) as fullname,
                                          a.agent, a.visitingfor, DATE_FORMAT(a.`date`,'%d-%m-%Y') as vdate 
                                          FROM `$maindb` a, $trandb b  
                                          WHERE a.vouchno = b.vouchno  AND a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate' 
                                          AND  a.hotelinramt > 0  $agentcond $citycond $hotelcond $checkcond 
                                   union all
                                   select 'HS' as source, a.vouchno, a.vouchdt as date, a.hotelamt, a.hotelinramt, a.prname as fullname, 
                                          a.customer as agent, 'Sale' as visitingfor,  DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as vdate 
                                          FROM `hotelslmain` a, hotelsltran b  
                                          WHERE a.vouchno = b.vouchno AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
                                          AND  a.hotelinramt > 0  $custcond $citycond $hotelcond $checkcond) as a
                                  GROUP BY source, `vouchno` ORDER BY date, `vouchno` ") or die(mysqli_error($conn));
$getno2 = mysqli_num_rows($getpmain2);

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


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
        .table-condensed>tbody>tr>td{
            padding: 4px;
        }
        .table-condensed>thead>tr>th{
            padding: 3px;
        }
    </style>
    </head>

    <body>


        <div class="container-fluid">
            <?php
            include("includes/head_rep.php");

            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 ">
                        <table class="table table-condesed table-bordered">
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>

                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="7%;">Date</th>
                                    <th width="7%;">Voucher</th>
                                    <th width="7%;">For</th>
                                    <th width="30%;">Agent</th>
                                    <th width="30%;">Pax</th>
                                    <th width="9%;" class="text-right">FE Amount</th>
                                    <th width="10%;" class="text-right">INR Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $tothotelamt = 0;
                                $tothotelinramt = 0;

                                while ($pmain = mysqli_fetch_array($getpmain2)) {
                                    $voucher = $pmain['vouchno'];
                                    $source = $pmain['source'];
                                    $visiting = $pmain['visitingfor'];
                                    $agent = $pmain['agent'];
                                    $vouchdt = $pmain['vdate'];
                                    $hotelamt = $pmain['hotelamt'];
                                    $hotelinramt = $pmain['hotelinramt'];
                                    $fullnm = $pmain['fullname'];
                                    $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

                                    $agqry = mysqli_query($conn, "SELECT name FROM accmast WHERE accode='$agent' ") or die(mysqli_error($conn));
                                    $agfet = mysqli_fetch_array($agqry);
                                    $agentname = $agfet['name'];

                                    if ($source == 'QI') {
                                        $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";
                                    } else {
                                        $url = "hotelsl.php?mode=edit&vouchno=$pmain[vouchno]";
                                    }

                                    $pctr++;
                                    echo "<tr data-href='$url' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$vouchdt</td>";
                                    echo "<td>$source $voucher</td>";
                                    echo "<td>$visiting</td>";
                                    echo "<td>$agentname</td>";
                                    echo "<td>$fullnm</td>";
                                    echo "<td class='text-right'>$hotelamt</td>";
                                    echo "<td class='text-right'>$hotelinramt</td>";
                                    echo "</tr>";

                                    $tothotelamt = $tothotelamt + $hotelamt;
                                    $tothotelinramt = $tothotelinramt + $hotelinramt;

                                    if ($_POST["dtype"] == 2) {

                                        echo "<tr style='height:10px;'>";
                                        echo "<td colspan='25'>";
                                        echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        echo "<thead>";
                                        echo "<tr class='' style='background-color:#BABFC2;font-size:14px;font-weight:bold'>";

                                        echo "<th style='width:3%;'>Sr</th>";
                                        echo "<th style='width:5%;'>City</th>";
                                        echo "<th style='width:15%;'>Hotel Name</th>";
                                        echo "<th style='width:5%;'>Check In</th>";
                                        echo "<th style='width:5%;'>Check Out</th>";
                                        echo "<th style='width:5%;'>No. Nights</th>";
                                        echo "<th style='width:9%;'>Room Category</th>";
                                        echo "<th style='width:8%;'>Room View</th>";
                                        echo "<th style='width:5%;'>Type</th>";
                                        echo "<th style='width:3%;text-align:right'>Qty.</th>";
                                        echo "<th style='width:4%;text-align:right'>Rate</th>";
                                        echo "<th style='width:5%;text-align:right'>Amount</th>";
                                        echo "<th style='width:7%;'>Meal Type</th>";
                                        echo "<th style='width:7%;'>CNF No.</th>";
                                        echo "<th style='width:10%;'>Remark</th>";
                                        echo "</tr>";
                                        echo "</thead>";

                                        $ctr2 = 0;
                                        $totamt = 0;
                                        if ($source == 'QI') {
                                            $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`checkin`, '%d-%m-%Y')as checkin,DATE_FORMAT(`checkout`, '%d-%m-%Y')as checkout
                                                                        FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' 
                                                                        $citycond2 $hotelcond2 $checkcond2  ORDER BY `srno`") or die(mysqli_error($conn));
                                        } else {
                                            $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`checkin`, '%d-%m-%Y')as checkin,DATE_FORMAT(`checkout`, '%d-%m-%Y')as checkout
                                                                        FROM `hotelsltran` WHERE `vouchno`='$pmain[vouchno]' 
                                                                        $citycond2 $hotelcond2 $checkcond2  ORDER BY `srno`") or die(mysqli_error($conn));
                                        }
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $mhotcatg = mysqli_query($conn, "SELECT code,name FROM roomcatg WHERE code='$tran[roomcatg]'") or die(mysqli_error($conn));
                                            $thotcatg = mysqli_fetch_array($mhotcatg);

                                            $mroomview = mysqli_query($conn, "SELECT code,name FROM roomview WHERE code='$tran[roomview]'") or die(mysqli_error($conn));
                                            $troomview = mysqli_fetch_array($mroomview);

                                            $mrtype = mysqli_query($conn, "SELECT code,name FROM roomtype WHERE code='$tran[roomtype]'") or die(mysqli_error($conn));
                                            $trtype = mysqli_fetch_array($mrtype);

                                            $mhotcity = mysqli_query($conn, "SELECT code,name FROM city WHERE code='$tran[city]'") or die(mysqli_error($conn));
                                            $thotcity = mysqli_fetch_array($mhotcity);

                                            $mhotel = mysqli_query($conn, "SELECT code,hotelnm FROM hotels WHERE code ='$tran[hotelnm]'") or die(mysqli_error($conn));
                                            $thotel = mysqli_fetch_array($mhotel);

                                            $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$tran[mealtype]'") or die(mysqli_error($conn));
                                            $tmeal = mysqli_fetch_array($mmeal);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$thotcity[name]</td>";
                                            echo "<td>$thotel[hotelnm]</td>";
                                            echo "<td>$tran[checkin]</td>";
                                            echo "<td>$tran[checkout]</td>";
                                            echo "<td>$tran[nonights]</td>";
                                            echo "<td>$thotcatg[name]</td>";
                                            echo "<td>$troomview[name]</td>";
                                            echo "<td>$trtype[name]</td>";
                                            echo "<td class='text-right'>$tran[qty]</td>";
                                            echo "<td class='text-right'>$tran[rate]</td>";
                                            echo "<td class='text-right'>$tran[amount]</td>";
                                            echo "<td>$tmeal[name]</td>";
                                            echo "<td>$tran[cnfno]</td>";
                                            echo "<td>$tran[remarks]</td>";
                                            echo "</tr>";

                                            $totamt = $totamt + $tran['amount'];
                                        }

                                        echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                        echo "<td colspan='11' class='text-right'>Total</td>";
                                        echo "<td class='text-right ; font-bolder'>$totamt</td>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "</tr>";
                                        echo "</table>";
                                        echo "</td>";
                                        echo "</tr>";
                                    }
                                }

                                echo "<tr style='background-color: #98F9F0;color: red;'>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td style='font-size:17px' class='text-right'><b>Total</b></td>";
                                echo "<td style='font-size:17px' class='text-right'>$tothotelamt</td>";
                                echo "<td style='font-size:17px' class='text-right'>$tothotelinramt</td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
    <?php
}

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

    $contents .=", Date, Voucher, For, Pax , FE Amount, INR Amount\n";

    $tothotelamt = 0;
    $tothotelinramt = 0;

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

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $voucher = $pmain['vouchno'];
        $visiting = $pmain['visitingfor'];
        $vouchdt = $pmain['date'];
        $hotelamt = $pmain['hotelamt'];
        $hotelinramt = $pmain['hotelinramt'];
        $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];


        $contents .="" . ",";
        $contents .="$vouchdt" . ",";
        $contents .="$voucher" . ",";
        $contents .="$visiting" . ",";
        $contents .="$fullname" . ",";
        $contents .="$hotelamt" . ",";
        $contents .="$hotelinramt" . "\n";

        $tothotelamt = $tothotelamt + $hotelamt;
        $tothotelinramt = $tothotelinramt + $hotelinramt;

        if ($_POST["dtype"] == 2) {

            $contents .="Sr, City, Hotel Name, Check In, Check Out, No. Nights, Room Category, Room View, Type, Qty., Rate, Amount, Meal Type, Cnf No. ,Remark\n";

            $ctr2 = 0;
            $tot = 0;

            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {


                $mhotcity = mysqli_query($conn, "SELECT code,name FROM city WHERE code='$tran[city]'") or die(mysqli_error($conn));
                $thotcity = mysqli_fetch_array($mhotcity);

                $mhotcatg = mysqli_query($conn, "SELECT code,name FROM roomcatg WHERE code='$tran[roomcatg]'") or die(mysqli_error($conn));
                $thotcatg = mysqli_fetch_array($mhotcatg);

                $mroomview = mysqli_query($conn, "SELECT code,name FROM roomview WHERE code='$tran[roomview]'") or die(mysqli_error($conn));
                $troomview = mysqli_fetch_array($mroomview);

                $mrtype = mysqli_query($conn, "SELECT code,name FROM roomtype WHERE code='$tran[roomtype]'") or die(mysqli_error($conn));
                $trtype = mysqli_fetch_array($mrtype);

                $mhotcity = mysqli_query($conn, "SELECT code,name FROM city WHERE code='$tran[city]'") or die(mysqli_error($conn));
                $thotcity = mysqli_fetch_array($mhotcity);

                $mhotel = mysqli_query($conn, "SELECT code,hotelnm FROM hotels WHERE code ='$tran[hotelnm]'") or die(mysqli_error($conn));
                $thotel = mysqli_fetch_array($mhotel);

                $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$tran[mealtype]'") or die(mysqli_error($conn));
                $tmeal = mysqli_fetch_array($mmeal);

                $ctr2++;
                $contents .="$ctr2" . ",";
                $contents .="$thotcity[name]" . ",";
                $contents .="$thotel[hotelnm]" . ",";
                $contents .="$tran[checkin]" . ",";
                $contents .="$tran[checkout]" . ",";
                $contents .="$tran[nonights]" . ",";
                $contents .="$thotcatg[name]" . ",";
                $contents .="$troomview[name]" . ",";
                $contents .="$trtype[name]" . ",";
                $contents .="$tran[qty]" . ",";
                $contents .="$tran[rate]" . ",";
                $contents .="$tran[amount]" . ",";
                $contents .="$tmeal[name]" . ",";
                $contents .="$tran[cnfno]" . ",";
                $contents .="$tran[remarks]" . "\n";

                $tot += $tran['amount'];
            }

            $contents .=",,,,,,,,,,Total" . ",";
            $contents .="$tot" . ",";
            $contents .="" . ",";
            $contents .="" . ",";
            $contents .="" . "\n";
        }
    }

    $contents .="" . ",";
    $contents .="" . ",";
    $contents .="" . ",";
    $contents .="Total" . ",";
    $contents .="$tothotelamt" . ",";
    $contents .="$tothotelinramt" . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>