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

error_reporting(E_ERROR);
ini_set('display_errors', 1);

$maindb = "couroutmain";
$trandb = "courouttran";
$formname = "Courier Out Register";
$file = "courout_print.php";

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

$paxname = $_REQUEST["paxname"];

$namecond = "";

if (($_GET["fdt"]) != '') {
    $mfrmdate = $mfdt = $_GET["fdt"];
    $mtodate = $mtdt = $_GET["tdt"];
    $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; courin_print.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; courin_print.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if ($mfrmdate > $mtodate) {
    browse("To Date Cannot Be Less Than From Date");
    header("refresh: 1; courin_print.php");
    exit();
}

if (!empty($paxname)) {
    $namecond = " AND b.`ref`='$paxname' ";
}
// $qry = "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.* FROM `$maindb` a, `$trandb` b 
//                                  WHERE a.vouchno=b.vouchno AND a.`vouchdt` >= '$mfrmdate' and a.`vouchdt` <= '$mtodate' $namecond ";
$qry = "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.* FROM $maindb a LEFT JOIN $trandb b ON a.vouchno=b.vouchno WHERE a.vouchno != ''
AND a.`vouchdt` >= '$mfrmdate' and a.`vouchdt` <= '$mtodate' $namecond ";

$getpmain = mysqli_query($conn, $qry) or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php");
            ?>
            <div class="row" id="printdivcontent">

                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-hover table-bordered">
                        <tr class="mySearchTr">
                            <th width="5%">Sr</th>
                            <th width="20%">File No</th>
                            <th width="20%">Name</th>
                            <th width="15%">Passport</th>
                            <th width="20%">Reference / Agent</th>
                            <th width="10%">Courier Date</th>
                            <th width="15%">To Courier </th>
                            <th width="15%">Status</th>
                        </tr>
                        <tr>
                            <!--<button type="button" name="print" onclick="PrintDiv();">PRINT TEST</button>-->
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            echo "<tr class='bgwhite'>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[fileno]</td>";
                            echo "<td>$pmain[paxname]</td>";
                            echo "<td>$pmain[passport]</td>";
                            echo "<td>$pmain[ref]</td>";
                            echo "<td>$pmain[mydt]</td>";
                            echo "<td>$pmain[courto]</td>";
                            echo "<td>$pmain[status]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

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

    $contents .= "Sr., File No., Name, Passport, Reference / Agent, Courier Date, Courier To, Status \n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[fileno]" . ",";
        $contents .= "$pmain[paxname]" . ",";
        $contents .= "$pmain[passport]" . ",";
        $contents .= "$pmain[ref]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[courto]" . ",";
        $contents .= "$pmain[status]" . "\n";
        $ctr++;
    }
    $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;
    }
}
?>