<?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 = 'Laundry Sale Register';
$file = 'laundrysalereg_fr.php';
$maindb = 'pax';
$trandb = 'laundent';

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


if (!empty($hotel)) {
    $hotelcond = " AND b.`hotel`= '$hotel' ";
    $hotelcond2 = " AND `hotel`= '$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");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}



$getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`date`,'%d-%m-%Y')as date  FROM `$maindb` a, $trandb b  
                                  WHERE a.vouchno = b.vouchno  AND a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate' 
                                  AND  a.laundinramt > 0  $hotelcond $checkcond
                                  GROUP BY a.`vouchno` ORDER BY a.`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;
        }
    </style>
    </head>

    <body>


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

            if ($getno2 > 0) {
            ?>
                        <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="12%;">For</th>
                                    <th width="30%;">Pax </th>
                                    <th style="width:10%; text-align:right">FE Amount</th>
                                    <th width="8%;" class="text-right">INR Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $totlaundamt = 0;
                                $totlaundinramt = 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'];
                                    $laundinramt = $pmain['laundinramt'];
                                    $laundamt = $pmain['laundamt'];
                                    $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

                                    $pctr++;
                                    echo "<tr data-href='fit_ent.php?mode=edit&paxid=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pmain[date]</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$visiting</td>";
                                    echo "<td>$fullname</td>";
                                    echo "<td class='text-right'>$laundamt</td>";
                                    echo "<td class='text-right'>$laundinramt</td>";
                                    echo "</tr>";

                                    $totlaundamt = $totlaundamt + $laundamt;
                                    $totlaundinramt = $totlaundinramt + $laundinramt;
                                    
                                    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'>";
                                        echo "<th style='width:5%;'>Sr</th>";
                                        echo "<th style='width:10%;'>Check In</th>";
                                        echo "<th style='width:10%;'>Check Out</th>";
                                        echo "<th style='width:30%;'>Hotel</th>";
                                        echo "<th style='width:10%;'>No. Nights</th>";
                                        echo "<th style='width:10%;'>No. Pax</th>";
                                        echo "<th style='width:10%;' class='text-right'>Rate</th>";
                                        echo "<th style='width:10%;' class='text-right'>Amount</th>";
                                        echo "</tr>";
                                        echo "</thead>";

                                        $ctr2 = 0;
                                        $totlaund = 0;
                                        $totnight = 0;
                                        $totpax = 0;

                                        $getlaunddtl = 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]' order by srno") or die(mysqli_error($conn));
                                        while ($fetlaund = mysqli_fetch_array($getlaunddtl)) {

                                            $getho = mysqli_query($conn, "SELECT `code`,`hotelnm` FROM `hotels` where code='$fetlaund[hotel]'") or die(mysqli_error($conn));
                                            $fetho = mysqli_fetch_array($getho);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$fetlaund[checkin]</td>";
                                            echo "<td>$fetlaund[checkout]</td>";
                                            echo "<td>$fetho[hotelnm]</td>";
                                            echo "<td>$fetlaund[nonights]</td>";
                                            echo "<td>$fetlaund[nopax]</td>";
                                            echo "<td class='text-right'>$fetlaund[rate]</td>";
                                            echo "<td class='text-right'>$fetlaund[amount]</td>";
                                            echo "</tr>";
                                            
                                            $totnight = $totnight + $fetlaund['nonights'];
                                            
                                            $totpax = $totpax + $fetlaund['nopax'];
                                            
                                            $totlaund = $totlaund + $fetlaund['amount'];

                                        }

                                        echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                        echo "<td colspan='4' class='text-right'; 'font-bolder';>Total</td>";
                                        echo "<td class='font-bolder'>$totnight</td>";
                                        echo "<td class='font-bolder'>$totpax</td>";
                                        echo "<td class='text-right ; font-bolder'></td>";
                                        echo "<td class='text-right ; font-bolder'>$totlaund</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 style='font-size:17px' class='text-right'><b>Total</b></td>";
                                echo "<td style='font-size:17px' class='text-right'>$totlaundamt</td>";
                                echo "<td style='font-size:17px' class='text-right'>$totlaundinramt</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";

$totlaundamt = 0;
$totlaundinramt = 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'];
    $laundinramt = $pmain['laundinramt'];
    $laundamt = $pmain['laundamt'];
    $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

    $pctr++;

$contents .="$pmain[date]" . ",";
$contents .="$voucher" . ",";
$contents .="$visiting" . ",";
$contents .="$fullname" . ",";
$contents .="$laundamt" . ",";
$contents .="$laundinramt" . "\n";

    $totlaundamt = $totlaundamt + $laundamt;
    $totlaundinramt = $totlaundinramt + $laundinramt;
    
    if ($_POST["dtype"] == 2) {

$contents .="Sr, Check In, Check Out, Hotel, No. Nights, No. Pax, Rate, Amount\n";

        $ctr2 = 0;
        $totlaund = 0;
        $totnight = 0;
        $totpax = 0;

        $getlaunddtl = 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]' order by srno") or die(mysqli_error($conn));
        while ($fetlaund = mysqli_fetch_array($getlaunddtl)) {

            $getho = mysqli_query($conn, "SELECT `code`,`hotelnm` FROM `hotels` where code='$fetlaund[hotel]'") or die(mysqli_error($conn));
            $fetho = mysqli_fetch_array($getho);

            $ctr2++;
            $contents .="$ctr2" . ",";
            $contents .="$fetlaund[checkin]" . ",";
            $contents .="$fetlaund[checkout]" . ",";
            $contents .="$fetho[hotelnm]" . ",";
            $contents .="$fetlaund[nonights]" . ",";
            $contents .="$fetlaund[nopax]" . ",";
            $contents .="$fetlaund[rate]" . ",";
            $contents .="$fetlaund[amount]" . "\n";
            
            $totnight = $totnight + $fetlaund['nonights'];
            
            $totpax = $totpax + $fetlaund['nopax'];
            
            $totlaund = $totlaund + $fetlaund['amount'];

        }

        $contents .=",," . ",";
        $contents .="Total" . ",";
        $contents .="$totnight" . ",";
        $contents .="$totpax" . ",";
        $contents .="" . ",";
        $contents .="$totlaund" . "\n";
    }

}

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