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

$maindb = "pax";
$formname = 'Summary';
$file = "fitsummary_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"]);


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 sum(tiktamt)  as tiktamt,  sum(tiktpuramt)  as tiktpuramt, 
                                         sum(hotelamt) as hotelamt, sum(hotelinramt) as hotelinramt,sum(hotelpuramt) as hotelpuramt,
                                         sum(foodamt)  as foodamt,  sum(foodinramt)  as foodinramt,
                                         sum(laundamt) as laundamt, sum(laundinramt) as laundinramt,sum(laundpuramt) as laundpuramt,
                                         sum(transamt) as transamt, sum(transinramt) as transinramt,sum(transpuramt) as transpuramt,
                                         sum(visaamt)  as visaamt,  sum(visapuramt)  as visapuramt  
                                         FROM $maindb WHERE  `date` >= '$mfrmdate' AND `date` <= '$mtodate'  ") or die(mysqli_error($conn));                                   
$getdata = mysqli_fetch_array($getpmain);

$tiktamt     = $getdata['tiktamt'];
$tiktpuramt  = $getdata['tiktpuramt'];

$hotelamt    = $getdata['hotelamt'];
$hotelinramt = $getdata['hotelinramt'];
$hotelpuramt = $getdata['hotelpuramt'];

$foodamt     = $getdata['foodamt'];
$foodinramt  = $getdata['foodinramt'];

$laundamt    = $getdata['laundamt'];
$laundinramt = $getdata['laundinramt'];
$laundpuramt = $getdata['laundpuramt'];

$transamt    = $getdata['transamt'];
$transinramt = $getdata['transinramt'];
$transpuramt = $getdata['transpuramt'];

$visaamt     = $getdata['visaamt'];
$visapuramt  = $getdata['visapuramt'];


$tiktpl   = $tiktamt - $tiktpuramt;
$hotelpl   = $hotelamt - $hotelpuramt;
$laundrypl = $laundamt - $laundpuramt;
$transpl   = $transamt - $transpuramt;
$visapl    = $visaamt  - $visapuramt;


$totinramt  =  $tiktamt    + $hotelinramt + $foodinramt + $laundinramt + $transinramt;
$totfeamt   =  $hotelamt   + $foodamt     + $laundamt   + $transamt    + $visaamt;
$totpuramt  =  $tiktpuramt + $hotelpuramt + $foodpuramt + $laundpuramt + $transpuramt + $visapuramt;
$totpl      =  $tikt       + $hotelpl     + $laundrypl  + $transpl     + $visapl;

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
        table td {
            font-size: 15px;
        }
    </style>

    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-8 ">
                    <table class="table table-condensed table-bordered" style="width:100%;">
                        <tr class="mySearchTr" style="height: 30px;">
                            <td style="width:20%">Details</td>
                            <td style="width:20%;text-align:right">Amount</td>
                            <td style="width:20%;text-align:right">FE SALE </td>
                            <td style="width:20%;text-align:right">FE PURCHASE </td>
                            <td style="width:20%;text-align:right">P/L</td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%;">Ticket & Airline </td>
                            <td style="width:20%;text-align:right"></td>
                            <td style="width:20%;text-align:right"><?= $tiktamt; ?></td>
                            <td style="width:20%;text-align:right"><?= $tiktpuramt; ?></td>
                            <td style="width:20%;text-align:right"><?= $tiktpl; ?></td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%">Hotel </td>
                            <td style="width:20%;text-align:right"><?= $hotelinramt; ?></td>
                            <td style="width:20%;text-align:right"><?= $hotelamt; ?></td>
                            <td style="width:20%;text-align:right"><?= $hotelpuramt; ?></td>
                            <td style="width:20%;text-align:right"><?= number_format($hotelpl,2); ?></td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%">Food </td>
                            <td style="width:20%;text-align:right"><?= $foodinramt; ?></td>
                            <td style="width:20%;text-align:right"><?= $foodamt; ?></td>
                            <td style="width:20%;text-align:right"></td>
                            <td style="width:20%;text-align:right"></td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%">Laundry </td>
                            <td style="width:20%;text-align:right"><?= $laundinramt; ?></td>
                            <td style="width:20%;text-align:right"><?= $laundamt; ?></td>
                            <td style="width:20%;text-align:right"><?= $laundpuramt; ?> </td>
                            <td style="width:20%;text-align:right"><?= number_format($laundrypl,2); ?></td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%">Transportation </td>
                            <td style="width:20%;text-align:right"><?= $transinramt; ?></td>
                            <td style="width:20%;text-align:right"><?= $transamt; ?></td>
                            <td style="width:20%;text-align:right"><?= $transpuramt; ?> </td>
                            <td style="width:20%;text-align:right"><?= number_format($transpl,2); ?></td>
                        </tr>
                        <tr style="height: 30px;">
                            <td style="width:20%">Visa </td>
                            <td style="width:20%;text-align:right"></td>
                            <td style="width:20%;text-align:right"><?= $visaamt; ?></td>
                            <td style="width:20%;text-align:right"><?= $visapuramt; ?> </td>
                            <td style="width:20%;text-align:right"><?= number_format($visapl,2); ?></td>
                        </tr>

                        <tr style="height: 30px;color:#0065b3;font-weight:700">
                            <td style="width:20%;text-align:right">Total </td>
                            <td style="width:20%;text-align:right"><?= number_format($totinramt,2); ?></td>
                            <td style="width:20%;text-align:right"><?= number_format($totfeamt,2); ?></td>
                            <td style="width:20%;text-align:right"><?= number_format($totpuramt,2); ?></td>
                            <td style="width:20%;text-align:right"><?= number_format($totpl,2); ?></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}
if (isset($_POST["excel"])) {


    $contents .= "Details" . ",";
    $contents .= "Amount" . ",";
    $contents .= "FE SALE" . ",";
    $contents .= "FE PURCHASE" . ",";
    $contents .= "PL" . "\n";
    
    $contents .= "Ticket & Airline" . ",";
    $contents .= "" . ",";
    $contents .= "$tiktamt" . ",";
    $contents .= "$tiktpuramt" . ",";
    $contents .= "$tiktpl" . "\n";


    $contents .= "Hotel" . ",";
    $contents .= "$hotelinramt" . ",";
    $contents .= "$hotelamt" . ",";
    $contents .= "$tiktpuramt" . ",";
    $contents .= "$hotelpl" . "\n";
    
    $contents .= "Food" . ",";
    $contents .= "$foodinramt" . ",";
    $contents .= "$foodamt" . ",";
    $contents .= "" . ",";
    $contents .= "" . "\n";
    
    $contents .= "Laundry" . ",";
    $contents .= "$laundinramt" . ",";
    $contents .= "$laundamt" . ",";
    $contents .= "$laundpuramt" . ",";
    $contents .= "$laundrypl" . "\n";
          
    $contents .= "Transportation" . ",";
    $contents .= "$transinramt" . ",";
    $contents .= "$transamt" . ",";
    $contents .= "$transpuramt" . ",";
    $contents .= "$transpl" . "\n";
          
          
    $contents .= "Visa" . ",";
    $contents .= "" . ",";
    $contents .= "$visaamt" . ",";
    $contents .= "$visapuramt" . ",";
    $contents .= "$visapl" . "\n";
    
    $contents .= "Total" . ",";
    $contents .= "$totinramt" . ",";
    $contents .= "$totfeamt" . ",";
    $contents .= "$totpuramt" . ",";
    $contents .= "$totpl" . "\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;
    }
}
?>