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

$maindb = 'pax';
$trandb = 'laundent';
$formname = "Food Arrival Details";
$file = "laundarrvdetl.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];

$tourcode = $_SESSION['tourcode'];
$tourcond = "AND tourno = '$tourcode'";
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$mfrmdate = $_POST["frmdt"];
$mtodate = $_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>";
}

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

$citycond = "";
$hotelcond = "";
$hotelcond1 = "";

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

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

$getpmain = mysqli_query($conn, "SELECT 'FIT' as source, a.vouchno, a.surname, a.name, a.midname, a.agent, b.hotel, b.checkin, b.checkout, 
                                        b.nonights, b.nopax FROM $maindb a, $trandb b 
                                        WHERE  a.vouchno = b.vouchno AND b.`checkin` >= '$mfrmdate' AND b.`checkin` <= '$mtodate' and a.foodaprov = '1'
                                        $hotelcond and b.hotel in (select code FROM hotels where code != '' $citycond) order by b.hotel, b.checkin, a.surname, a.name, a.midname") or die(mysqli_error($conn));

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">
            <div class="row">
                <div class="col-lg-12 ">
                    <?php include("includes/head_rep.php"); ?>

                    <table class="table table-condensed table-bordered" style="width:100%;">

                        <?php
                        $ctr = 0;
                        $ttotamt = 0;
                        $totsaleamt = 0;
                        $totpnl = 0;
                        $thotel = '';
                        $aa = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $mhotel = $pmain['hotel'];
                            $source = $pmain['source'];
                            $roomqty = $pmain['qty'];
                            
                            

                            if ($thotel != $mhotel) {
                                if ($aa == 1) {
                                    echo "<tr style='height: 30px;'>";
                                    echo "<td colspan='12' style='font-size:15px;'>&nbsp;</td>";
                                    echo "</tr>";
                                }
                                $qryhotel = mysqli_query($conn, "SELECT code, hotelnm FROM hotels WHERE code ='$mhotel' ") or die(mysqli_error($conn));
                                $fethotel = mysqli_fetch_array($qryhotel);
                                
                                echo "<tr style='height: 30px;'>";
                                echo "<td colspan='12' style='font-size:18px; background-color:#BDE0FE;text-align:center'>$fethotel[hotelnm]</td>";
                                echo "</tr>";
                                ?>
                                <tr class="" style="height: 30px;background-color:#D0F4DE">
                                    <td style="font-size:16px;width:3%">Sr. </td>
                                    <td style="font-size:16px;width:30%">Name </td>
                                    <td style="font-size:16px;width:20%;">Company/Agent</td>
                                    <td style="font-size:16px;width:9%;">Arrival</td>
                                    <td style="font-size:16px;width:9%;">Departure</td>
                                    <td style="font-size:16px;width:7%;text-align:center">Nights</td>
                                    <td style="font-size:16px;width:7%;text-align:center">Pax</td>
                                </tr>
                                <?php
                                $ctr = 0;
                                $thotel = $mhotel;
                                if ($aa == 0) {
                                    $aa = 1;
                                }
                            }


                            $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agent]' ") or die(mysqli_error($conn));
                            $fetagent = mysqli_fetch_array($getagent);


                            $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";

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

                            $ctr++;
                            echo "<tr style='height: 30px;' class='clickable-row2' data-href='$url' data-target='_blank' >";
                            echo "<td style='font-size:12px;'>$ctr</td>";
                            echo "<td style='font-size:13px;'>$fullname</td>";
                            echo "<td style='font-size:13px;'>$fetagent[name]</td>";
                            echo "<td style='font-size:13px;'>" . format_date($pmain['checkin']) . "</td>";
                            echo "<td style='font-size:13px;'>" . format_date($pmain['checkout']) . "</td>";
                            echo "<td style='font-size:14px;text-align:center'>$pmain[nonights]</td>";
                            echo "<td style='font-size:14px;text-align:center'>$pmain[nopax]</td>";
                            echo "</tr>";
                        }
                        ?>

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

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

    </html>
    <?php
}



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

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $mhotel = $pmain['hotel'];
        $source = $pmain['source'];
        $roomqty = $pmain['qty'];

        if ($thotel != $mhotel) {
            if ($aa == 1) {
                $contentse .="" . ",";
            }
            $qryhotel = mysqli_query($conn, "SELECT code, hotelnm FROM hotels WHERE code ='$mhotel' ") or die(mysqli_error($conn));
            $fethotel = mysqli_fetch_array($qryhotel);

            $contents .="$fethotel[hotelnm]" . "\n";

            $contents .="Sr., Name, Company/Agent, Arrival, Departure, Nights, Pax\n";

            $ctr = 0;
            $thotel = $mhotel;
            if ($aa == 0) {
                $aa = 1;
            }
        }


        $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agent]' ") or die(mysqli_error($conn));
        $fetagent = mysqli_fetch_array($getagent);


        $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";

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

        $ctr++;

        $contents .="$ctr" . ",";
        $contents .="$fullname" . ",";
        $contents .="$fetagent[name]" . ",";
        $contents .="$pmain[checkin]" . ",";
        $contents .="$pmain[checkout]" . ",";
        $contents .="$pmain[nonights]" . ",";
        $contents .="$pmain[nopax]" . "\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" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>