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

$maindb = "courout";
$formname = "Courier Out";
$file = "couroutreg_fr2.php";

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

$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];

$manualno = $_POST["manualno"];
$visatype = $_POST["visatype"];
$visano = $_POST["visano"];
$passport = $_POST["passport"];
$partyname = $_POST["partyname"];
$sponser = $_POST["sponser"];
$contact = $_POST["contact"];
$mobile = $_POST["mobile"];
$dept = $_POST["dept"];
$intloc = $_POST["intloc"];
$interdate = $_POST["interdate"];
$agent = $_POST["agent"];
$dtype = $_POST["dtype"];

$manualnocond = '';
$visatypecond = '';
$visanocond = '';
$passportcond = '';
$deptcond = '';
$intloccond = '';
$interdatecond = '';
$partynamecond = '';
$sponsercond = '';
$contactcond = '';
$mobilecond = '';
$agentcond = '';
$dtypecond = "";

if (!empty($manualno)) {
    $manualnocond = " AND a.`manualno`='$manualno' ";
}
if (!empty($passport)) {
    $passportcond = " AND b.`passport` ='$passport' ";
}
if (!empty($partyname)) {
    $partynamecond = " AND a.`partyname` LIKE '$partyname%' ";
}
if (!empty($sponser)) {
    $sponsercond = " AND a.`sponser` LIKE '$sponser%' ";
}
if (!empty($visano)) {
    $visanocond = " AND a.`visano` = '$visano' ";
}
if (!empty($dept)) {
    $deptcond = " AND b.`department` = '$dept' ";
}
if (!empty($intloc)) {
    $intloccond = " AND b.`intloc` = '$intloc' ";
}
if (!empty($interdate)) {
    $interdatecond = " AND b.`intdate` = '$interdate' ";
}
if (!empty($visatype)) {
    $visatypecond = " AND a.`visatype` = '$visatype' ";
}
if (!empty($contact)) {
    $contactcond = " AND a.`contact` = '$contact' ";
}
if (!empty($mobile)) {
    $mobilecond = " AND a.`mobile` = '$mobile' ";
}
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
// if (empty($_POST["frmdate"])) {
//     browse("From Date Cannot Be Empty");
//     header("refresh:1; courreg.php?para=O");
// }
// if (empty($_POST["todate"])) {
//     browse("To Date Cannot Be Empty");
//     header("refresh:1; courreg.php?para=O");
// }



$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb`  order by vouchdt,vouchno") or die(mysqli_error($conn));
$getno2 = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <style>
        .tblhead > th{
            background-color: #F5F5F5;
            color: #C45254;
            width: 10%;
        }

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

            <?php
            $ctr = 1;
            $ctr2 = 0;
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered" >
                    <tr class="mySearchTr">
                            <th class="">Voucher</th>
                            <th class="">Date</th>
                            <th class="">Pax Name</th>
                            <th class="">Passport No</th>
                            <th class="">Medical</th>
                            <th class="">License</th>
                            <th class="">Photo</th>
                            <th class="">From</th>
                            <th class="">Recieved By</th>
                            <th class="">Courier Date</th>
                            <th class="">Tracking No</th>
                            <th class="">Remarks</th>
                        </tr>
                        <?php
                        $ftot = 0;
                        $mistot = 0;
                        $misctot = 0;
                        $stot = 0;
                        $moftot = 0;
                        $itot = 0;
                        $ttot = 0;
                        $dtot = 0;
//                        $ftot = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $ctr      = $pmain['vouchno'];
                            $vouchdt  = $pmain['vouchdt'];
                            $paxname  = $pmain['paxname'];
                            $passport = $pmain['passport'];
                            $medical  = $pmain['medical'];
                            $license  = $pmain['license'];
                            $photo    = $pmain['photo'];
                            $recdfrom = $pmain['recdfrom'];
                            $recdby   = $pmain['recdby'];
                            $courier  = $pmain['courier'];
                            $courdt   = $pmain['courdt'];
                            $trackid  = $pmain['trackid'];
                            $remark1  = $pmain['remark1'];
                            $remark2  = $pmain['remark2'];

                            Echo "<tr class='clickable-row' data-href='$entfile?mode=edit&b.vouchno='$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[vouchdt]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[medical]</td>";
                            Echo "<td>$pmain[license]</td>";
                            Echo "<td>$pmain[photo]</td>";
                            Echo "<td>$pmain[recdfrom]</td>";
                            Echo "<td>$pmain[recdby]</td>";
                            Echo "<td>$pmain[courier]</td>";
                            Echo "<td>$pmain[courdt]</td>";
                            Echo "<td>$pmain[trackid]</td>";
                            Echo "<td>$pmain[remark1]</td>";
                            Echo "<td>$pmain[remark2]</td>";
                            Echo "</tr>";

                            Echo "</table>";
                        }
                      
                    }

                    if (isset($_POST["excel"])) {
                        $contents .= "Sr., Date, File No., Agent, Sponsor, Visa Type, Visa No., Visa Qty, Id no., Place, Hijri, Pax Name, Sex, Passport no, DOB, POB, POI, Stay Period, DOI, Stamp Date, Immigration status, Category, Mofa no., Mofa no.2, Mofa, Stamping, Immigration, M/s, Miscellaneous, Total, Discount, Final Amt\n";
                        $totamt = 0;

                        echo ucwords($columnHeader) . "\n" . $setData . "\n";
                        mysqli_select_db($conn, $zdb);
                        $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt, DATE_FORMAT(a.`hijridate`, '%d-%m-%Y')
                            as hijridt, b.* FROM `$maindb` a left join `$trandb` b on 
                            a.vouchno = b.vouchno and a.visatype = b.visatype where a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                            $visatypecond $passportcond $manualnocond $visanocond $partynamecond $sponsercond $contactcond $mobilecond $agentcond
                            ")or die(mysqli_error($conn));

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $imig = $pmain['imig'];
                            $stamp = $pmain['stamp'];
                            $mis = $pmain['mis'];
//                            $mofano = $pmain['mofa'];
                            $misc = $pmain['misc'];
                            $total = $pmain['total'];
                            $disc = $pmain['disc'];
                            $tot = $pmain['tot'];
                            $vouchdt = $pmain['vouchdt'];
                            $manualno = $pmain['manualno'];
                            $broker = $pmain['broker'];
                            $sponsor = $pmain['sponsor'];
                            $visatype = $pmain['visatype'];
                            $visano = $pmain['visano'];
                            $visaqty = $pmain['visaqty'];
                            $idno = $pmain['idno'];
                            $place = $pmain['sector'];
                            $hijridate = $pmain['hijridate'];
                            $paxname = $pmain['paxname'];
                            $sex = $pmain['sex'];
                            $passport = $pmain['passport'];
                            $dob = $pmain['dob'];
                            $plbirthr = $pmain['plbirth'];
                            $plbirth = str_replace(",", "  ", "$plbirthr");
                            $poi = $pmain['issplace'];
                            $stayperiod = $pmain['periodstay'];
                            $doi = $pmain['issdate'];
                            $stampdate = $pmain['stampdate'];
                            $immgstatus = $pmain['delvstatus'];
                            $category = $pmain['category'];
                            $mofano = $pmain['mofano'];
                            $mofano2 = $pmain['mofano2'];
                            $mofa = $pmain['mofa'];

                            $ctr++;
                            $contents .= $ctr . ",";
                            $contents .= "$vouchdt" . ",";
                            $contents .= "$manualno" . ",";
                            $contents .= "$broker" . ",";
                            $contents .= "$sponsor" . ",";
                            $contents .= "$visatype" . ",";
                            $contents .= "$visano" . ",";
                            $contents .= "$visaqty" . ",";
                            $contents .= "$idno" . ",";
                            $contents .= "$place" . ",";
                            $contents .= "$hijridate" . ",";
                            $contents .= "$paxname" . ",";
                            $contents .= "$sex" . ",";
                            $contents .= "$passport" . ",";
                            $contents .= "$dob" . ",";
                            $contents .= "$plbirth" . ",";
                            $contents .= "$poi" . ",";
                            $contents .= "$stayperiod" . ",";
                            $contents .= "$doi" . ",";
                            $contents .= "$stampdate" . ",";
                            $contents .= "$immgstatus" . ",";
                            $contents .= "$category" . ",";
                            $contents .= "$mofano" . ",";
                            $contents .= "$mofano2" . ",";
                            $contents .= "$mofa" . ",";
                            $contents .= "$stamp" . ",";
                            $contents .= "$imig" . ",";
                            $contents .= "$mis" . ",";
                            $contents .= "$misc" . ",";
                            $contents .= "$total" . ",";
                            $contents .= "$disc" . ",";
                            $contents .= "$tot" . "\n";
                        }

                        $gtot = $totamt + $totamt1 + $totamt2 + $totamt3;


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