<?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 Purchase Register';
$file = "tiktnewpurchreg_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(`tiktinvdt`,'%d-%m-%Y')as tiktinvdt  FROM `$maindb` 
                                      WHERE `tiktinvdt` >= '$mfrmdate' AND `tiktinvdt` <= '$mtodate' ") or die(mysqli_error($conn));
            $getno2 = mysqli_num_rows($getpmain2);

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:8%">Date</th>
                                <th style="width:12%">Invoice No</th>
                                <th style="width:70%;">Supplier</th>
                                <th style="width:10%;text-align:right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                                <?php
                                $pctr = 0;
                                $tothotelamt = 0;
                                $tothotelinramt = 0;

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

                                    $gettksupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE accode='$pmain[tiktsupp]'") or die(mysqli_error($conn));
                                    $fettksupp = mysqli_fetch_array($gettksupp);
                                    
                                    $pctr++;
                                    echo "<tr id='boldfont' class='clickable-row2' data-href='fit_ent.php?paxid=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                    echo "<td>$pmain[tiktinvdt]</td>";
                                    echo "<td>$pmain[tiktinvno]</td>";
                                    echo "<td>$fettksupp[name]</td>";
                                    echo "<td class='text-right'>$pmain[tiktpuramt]</td>";
                                    echo "</tr>";

                                    $total = $pmain['tiktpuramt'] + $total;
                                    $totalamt = number_format($total, 2);
                                    
                                    }
                                // }
                                echo "<tr style='background-color: #98F9F0;color: red;'>";
                                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;
    }
}
?>