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

$maindb = 'tiktnewmain';
$trandb = 'tiktnewtran';
$formname = 'Ticket Purchase Register';
$file = "tiktnewreg_fr.php";

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

$suppcond = "";

$mfrmdate = $_POST["fromdt"];
$mtodate = $_POST["todt"];
$supplier = $_POST["supplier"];
$dtype = $_POST["dtype"];

if (!empty($supplier)) {
    $suppcond = "AND supplier = '$supplier'";
}

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>";
}


$getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as vouchdt  FROM $maindb 
                                      WHERE vouchno != '' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $suppcond") 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">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Voucher</th>
                                <th style="width:6%">Date</th>
                                <th style="width:6%">Invoice No.</th>
                                <th style="width:20%;">Supplier</th>
                                <th style="width:10%;">GST No.</th>
                                <th style="width:10%;">Subtotal</th>
                                <th style="width:10%;">Handling Charges</th>
                                <th style="width:5%;">Per Pax</th>
                                <th style="width:5%;text-align:right">GST%</th>
                                <th style="width:5%;text-align:right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = 0;
                            $tothotelamt = 0;
                            $tothotelinramt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain2)) {
                                $msupp = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$pmain[supplier]'") or die(mysqli_error($conn));
                                $lsupp = mysqli_fetch_array($msupp);

                                $pctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='fit_ent.php?paxid=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$pmain[tiktinvno]</td>";
                                echo "<td>$lsupp[name]</td>";
                                echo "<td>$pmain[gstno]</td>";
                                echo "<td>$pmain[subtot]  </td>";
                                echo "<td>$pmain[perpax]</td>";
                                echo "<td>$pmain[handchg]</td>";
                                echo "<td style='text-align:right;'>$pmain[gstperc]</td>";
                                echo "<td style='text-align:right;'>$pmain[totamt]</td>";
                                echo "</tr>";

                                $total = $pmain['totamt'] + $total;
                                $totalamt = number_format($total, 2);

                                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:15%;'>Pax Name</th>";
                                    echo "<th style='width:5%;'>From</th>";
                                    echo "<th style='width:5%;'>To</th>";
                                    echo "<th style='width:8%;'>Date</th>";
                                    echo "<th style='width:15%;'>Airline</th>";
                                    echo "<th style='width:10%;'>Passport</th>";
                                    echo "<th style='width:12%;'>Ticket No</th>";
                                    echo "<th style='width:12%;'>Flight No</th>";
                                    echo "<th style='width:7%;'>PNR</th>";
                                    echo "<th style='width:10%;text-align:right'>Amount</th>";
                                    echo "</tr>";
                                    echo "</thead>";

                                    $ctr2 = 0;
                                    $ttotamt = 0;

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

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$tran[paxname]</td>";
                                        echo "<td>$tran[from]</td>";
                                        echo "<td>$tran[to]</td>";
                                        echo "<td>$tran[travdate]</td>";
                                        echo "<td>$tran[airline]</td>";
                                        echo "<td>$tran[ppno]</td>";
                                        echo "<td>$tran[ticketno]</td>";
                                        echo "<td>$tran[flightno]</td>";
                                        echo "<td>$tran[pnr]</td>";
                                        echo "<td class='text-right'>$tran[amount]</td>";
                                        echo "</tr>";

                                        $ttotamt = $ttotamt + $tran['amount'];
                                        $grandttotamt = number_format($ttotamt, 2);
                                        // }
                                    }
                                    echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td colspan='8' class='text-right'>Total</td>";
                                    echo "<td class='text-right ; font-bolder'>$grandttotamt</td>";
                                    echo "<td></td>";
                                    echo "</tr>";
                                    echo "</table>";
                                    echo "</td>";
                                    echo "</tr>";
                                }
                            }

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

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

    </html>
<?php
}


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

    $contents .= "Voucher, Date, Supplier, GST NO., Subt Total, Handling Charges, Per Pax, GST%, Amount \n";

    $pctr = 0;
    $tothotelamt = 0;
    $tothotelinramt = 0;

    while ($pmain = mysqli_fetch_array($getpmain2)) {
        $msupp = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$pmain[supplier]'") or die(mysqli_error($conn));
        $lsupp = mysqli_fetch_array($msupp);

        $pctr++;
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$lsupp[name]" . ",";
        $contents .= "$pmain[gstno]" . ",";
        $contents .= "$pmain[subtot]" . ",";
        $contents .= "$pmain[perpax]" . ",";
        $contents .= "$pmain[handchg]" . ",";
        $contents .= "$pmain[gstperc]" . ",";
        $contents .= "$pmain[totamt]" . "\n";

        $total = $pmain['totamt'] + $total;
        // $totalamt = number_format($total, 2);

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

            $contents .= "Sr., Pax Name, From, To, Date, Airline, Passport, Ticket No., Flight No, PNR, Amount \n";

            $ctr2 = 0;
            $ttotamt = 0;

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

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[paxname]" . ",";
                $contents .= "$tran[from]" . ",";
                $contents .= "$tran[to]" . ",";
                $contents .= "$tran[travdate]" . ",";
                $contents .= "$tran[airline]" . ",";
                $contents .= "$tran[ppno]" . ",";
                $contents .= "$tran[ticketno]" . ",";
                $contents .= "$tran[flightno]" . ",";
                $contents .= "$tran[pnr]" . ",";
                $contents .= "$tran[amount]" . "\n";

                $ttotamt = $ttotamt + $tran['amount'];
                // $grandttotamt = number_format($ttotamt, 2);
            }

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

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