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

$maindb = "courinmain";
$trandb = "courintran";
$formname = "Courier In Register";
$file = "courin_print.php";


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

$name = $_POST["name"];

$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($name)) {
    $namecond = " AND b.`ref`='$name' ";
}

$getpmain = mysqli_query($conn, "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 ")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%">Name</th>
                            <th width="15%">Passport</th>
                            <th width="20%">Reference / Agent</th>
                            <th width="10%">Courier Date</th>
                            <th width="15%">Courier From </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[paxname]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[ref]</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td>$pmain[courfrm]</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., Name, Passport, Reference / Agent, Courier Date, Courier From, Status \n";
                            
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[paxname]" . ",";
                            $contents .= "$pmain[passport]" . ",";
                            $contents .= "$pmain[ref]" . ",";
                            $contents .= "$pmain[mydt]" . ",";
                            $contents .= "$pmain[courfrm]" . ",";
                            $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;
    }
}
?>
