<?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"];

$file = 'ticketsalereg_fr.php';
$maindb = 'pax';
$trandb = 'tiktent';

$airline   = $_POST["airline"];
$fromdes   = $_POST["fromdes"];
$todes   = $_POST["todes"];
$pchtype = $_POST["pchtype"];
$dtype   = $_POST["dtype"];
$gsttype = $_POST["gsttype"];
$godown  = $_POST["godown"];
$p       = $_GET["p"];

$formname = "Ticket & Airline Sale Register";

$airlinecond = "";
$airlinecond2 = "";
$fromdescond = "";
$fromdescond2 = "";
$todescond = "";
$todescond2 = "";


if (!empty($airline)) {
    $airlinecond = " AND b.`airline`= '$airline' ";
    $airlinecond2 = " AND `airline`= '$airline' ";
}

if (!empty($fromdes)) {
    $fromdescond = " AND b.`fromdes`= '$fromdes' ";
    $fromdescond2 = " AND `fromdes`= '$fromdes' ";
}

if (!empty($todes)) {
    $todescond = " AND b.`todes`= '$todes' ";
    $todescond2 = " AND `todes`= '$todes' ";
}


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 vdate  FROM `$maindb` a, $trandb b  WHERE a.vouchno = b.vouchno  
                                  AND a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate' AND  a.tiktamt > 0
                                  $airlinecond $fromdescond $todescond
                                  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;
        }
        .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="10%;">For</th>
                                    <th width="20%;">Pax</th>
                                    <th width="10%;" class="text-right">Subtotal</th>
                                    <th width="10%;" class="text-right">Handling Charges</th>
                                    <th width="10%;" class="text-right">GST% </th>
                                    <th width="10%;" class="text-right">GST Amount </th>
                                    <th width="10%;" class="text-right">Final Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php

                                $tottiktsub = 0;
                                $tothandchg = 0;
                                $totgstamt = 0;
                                $tottiktamt = 0;

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

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

                                    echo "<tr data-href='fit_ent.php?mode=edit&paxid=$pmain[vouchno]' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pmain[vdate]</td>";
                                    echo "<td>$pmain[vouchno]</td>";
                                    echo "<td>$pmain[visitingfor]</td>";
                                    echo "<td>$fullname</td>";
                                    echo "<td class='text-right'>$pmain[tiktsubtot]</td>";
                                    echo "<td class='text-right'>$pmain[tikthandchg]</td>";
                                    echo "<td class='text-right'>$pmain[tiktgstperc]</td>";
                                    echo "<td class='text-right'>$pmain[tiktgstamt]</td>";
                                    echo "<td class='text-right'>$pmain[tiktamt]</td>";
                                    echo "</tr>";

                                    $tottiktsub = $tottiktsub + $pmain['tiktsubtot'];
                                    $tothandchg = $tothandchg + $pmain['tikthandchg'];
                                    $totgstamt = $totgstamt + $pmain['tiktgstamt'];
                                    $tottiktamt = $tottiktamt + $pmain['tiktamt'];

                                    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%;'>From</th>";
                                        echo "<th style='width:5%;'>To</th>";
                                        echo "<th style='width:16%;'>Airline</th>";
                                        echo "<th style='width:7%;'>Ticket No.</th>";
                                        echo "<th style='width:6%;'>Flight No.</th>";
                                        echo "<th style='width:5%;'>PNR</th>";
                                        echo "<th style='width:5%;'>Date</th>";
                                        echo "<th style='width:5%;'>No. Adult</th>";
                                        echo "<th style='width:5%;'>No. Child</th>";
                                        echo "<th style='width:5%;'>No. Infant</th>";
                                        echo "<th style='width:5%;text-align:right'>Adult</th>";
                                        echo "<th style='width:5%;text-align:right'>Child</th>";
                                        echo "<th style='width:5%;text-align:right'>Infant</th>";
                                        echo "<th style='width:7%;text-align:right'>Total</th>";
                                        echo "</tr>";
                                        echo "</thead>";

                                        $ctr2 = 0;
                                        $totamt = 0;

                                        $gettran = mysqli_query($conn, "SELECT *, DATE_FORMAT(`travdate`, '%d-%m-%Y')as stravdate FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' 
                                                                        $airlinecond2 $fromdescond2 $todescond2  
                                                                        ORDER BY vouchno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $getho = mysqli_query($conn, "SELECT `code`,`name` FROM `airline` where code='$tran[airline]'") or die(mysqli_error($conn));
                                            $fetho = mysqli_fetch_array($getho);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$tran[fromdes]</td>";
                                            echo "<td>$tran[todes]</td>";
                                            echo "<td>$fetho[name]</td>";
                                            echo "<td>$tran[ticketno]</td>";
                                            echo "<td>$tran[flightno]</td>";
                                            echo "<td>$tran[pnrno]</td>";
                                            echo "<td>$tran[stravdate]</td>";
                                            echo "<td>$tran[nopax]</td>";
                                            echo "<td>$tran[nochild]</td>";
                                            echo "<td>$tran[noinf]</td>";
                                            echo "<td class='text-right'>$tran[adult]</td>";
                                            echo "<td class='text-right'>$tran[child]</td>";
                                            echo "<td class='text-right'>$tran[infant]</td>";
                                            echo "<td class='text-right'>$tran[amount]</td>";
                                            echo "</tr>";

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

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


                                echo "<tr style='background-color: #0375bc;color: white;'>";
                                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'>$tottiktsub</td>";
                                echo "<td style='font-size:17px' class='text-right'>$tothandchg</td>";
                                echo "<td style='font-size:17px' class='text-right'>$totgstamt</td>";
                                echo "<td style='font-size:17px' class='text-right'>$tottiktamt</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, Pax, Subtotal, Handling Charges, GST%, GST Amount, Final Amount\n";

    $pctr = 0;

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

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

        $contents .= $pmain['date'] . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $fullname . ",";
        $contents .= $pmain["tiktsubtot"] . ",";
        $contents .= $pmain["tikthandchg"] . ",";
        $contents .= $pmain["tiktgstperc"] . ",";
        $contents .= $pmain["tiktgstamt"] . ",";
        $contents .= $pmain["tiktamt"] . "\n";

        $tottiktsub = $tottiktsub + $pmain['tiktsubtot'];
        $tothandchg = $tothandchg + $pmain['tikthandchg'];
        $totgstamt = $totgstamt + $pmain['tiktgstamt'];
        $tottiktamt = $tottiktamt + $pmain['tiktamt'];


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

            $contents .= "Sr, From, To, Airline, Ticket No., Flight No., PNR ,Date, No. Adult, No. Child, No. Infant, Adult, Child, Infant, Total\n";

            $ctr2 = 0;
            $tot = 0;


            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' 
                                            $airlinecond2 $fromdescond2 $todescond2  
                                            order by vouchno") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getho = mysqli_query($conn, "SELECT `code`,`name` FROM `airline` where code='$tran[airline]'") or die(mysqli_error($conn));
                $fetho = mysqli_fetch_array($getho);

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[fromdes]" . ",";
                $contents .= "$tran[todes]" . ",";
                $contents .= "$fetho[name]" . ",";
                $contents .= "$tran[ticketno]" . ",";
                $contents .= "$tran[flightno]" . ",";
                $contents .= "$tran[pnrno]" . ",";
                $contents .= "$tran[travdate]" . ",";
                $contents .= "$tran[nopax]" . ",";
                $contents .= "$tran[nochild]" . ",";
                $contents .= "$tran[infant]" . ",";
                $contents .= "$tran[adult]" . ",";
                $contents .= "$tran[child]" . ",";
                $contents .= "$tran[infant]" . ",";
                $contents .= "$tran[amount]" . "\n";

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

            $contents .= ",,,,,,,,,,,,,Total" . ",";
            $contents .= "$tot" . "\n";
        }
    }
    
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$tottiktsub" . ",";
    $contents .= "$tothandchg" . ",";
    $contents .= "$totgstamt" . ",";
    $contents .= "$tottiktamt" . "\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;
    }
}
?>