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

$maindb = 'tiktnewmain';
$trandb = 'tiktnewtran';
$formname = 'New Ticket & Airline Sale Register';
$file = "tiktnewsalereg_fr.php";

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


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


            $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as vouchdt  FROM $maindb 
                                      WHERE vouchno != '' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ") 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-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Voucher</th>
                                <th style="width:6%">Date</th>
                                <th style="width:20%;">Agent / Customer</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%;">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)) {
                                    $magent = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$pmain[customer]'") or die(mysqli_error($conn));
                                    $lagent = mysqli_fetch_array($magent);
                                    
                                    $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>$lagent[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;
                                        $totamt = 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)) {

                                            $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[paxname]</td>";
                                            echo "<td>$tran[from]</td>";
                                            echo "<td>$tran[to]</td>";
                                            echo "<td>$tran[travdate]</td>";
                                            echo "<td>$fetho[name]</td>";
                                            echo "<td>$tran[ticketno]</td>";
                                            echo "<td>$tran[flightno]</td>";
                                            echo "<td>$tran[pnr]</td>";
                                            echo "<td>$tran[ppno]</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></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                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'>$totalamt</td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

    $contents.="Voucher, Date, For, Pax, Supplier, Invoice No., Invoice Date. Amount\n"; 

    $ctr = 0;
$sontot = 0;
$dautot = 0;

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

    $getsupp = mysqli_query($conn, "SELECT accode,name FROM accmast WHERE accode = '$pmain[tiktsupp]' ") or die(mysqli_error($conn));
    $fetsupp = mysqli_fetch_array($getsupp);

    $tiktinvdt = date('d-m-Y', strtotime($pmain['tiktinvdt']));
    $fullname = $pmain['surname'] ." ". $pmain['name'] ." ". $pmain['midname'] ;

    $ctr++;

$contents .="$pmain[vouchno]" . ",";
$contents .="$pmain[date]" . ",";
$contents .="$pmain[visitingfor]" . ",";
$contents .="$fullname" . ",";
$contents .="$fetsupp[name]" . ",";
$contents .="$pmain[tiktinvno]" . ",";
$contents .="$tiktinvdt" . ",";
$contents .="$pmain[tiktpuramt]" . "\n";
    
    $tottiktpuramt = $pmain['tiktpuramt'] + $tottiktpuramt;
    $ttiktpuramt = number_format($tottiktpuramt, 2);
}

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