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

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

$file = 'ppexpiry_fr.php';

$formname = 'Passport Expiry Report';

$saleexec = $_SESSION["saleexec"];
$usertype = $_SESSION["usertype"];
$username = strtoupper($_SESSION["username_" . $dbname]);

$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];
//$tour = $_POST["tour"];


$name = $_POST['name'];
$regnno = $_POST['regnno'];
$paxno = $_POST['paxno'];
$group = $_POST['group'];
$class = $_POST['class'];
$canc = $_POST['canc'];
$prphoto = $_POST['prphoto'];
$city = $_POST['city'];
$state = $_POST['state'];
$sex = $_POST['sex'];
$ppexpiry = $_POST['ppexpiry'];
$ppinhand = $_POST['ppinhand'];
$depdate = $_POST['depdate'];
$deppnr = $_POST['deppnr'];
$airlines = $_POST['airlines'];
$agent = $_POST['agent'];
$arrvdate = $_POST['arrvdate'];
$arrvpnr = $_POST['arrvpnr'];
$infonly = $_POST['inf'];
$canconly = $_POST['canc'];

$tourcond = '';
$namecond = '';
$regncond = '';
$paxcond = '';
$groupcond = '';
$classcond = '';
$canccond = '';
$prphotocond = '';
$citycond = '';
$statecond = '';
$sexcond = '';
$ppexpirycond = '';
$ppinhandcond = '';
$depdatecond = '';
$deppnrcond = '';
$airlinescond = '';
$agentcond = '';
$arrvdatecond = '';
$arrvpnrcond = '';

if (!empty($tourcode)) {
    $tourcond = " AND a.`tourno` = '$tourcode' ";
}

if (!empty($name)) {
    $namecond = " AND b.`name` like '%$name%' ";
}

if (!empty($regnno)) {
    $regncond = " AND a.`regnno` = '$regnno' ";
}

if (!empty($paxno)) {
    $paxcond = " AND b.`vouchno`= '$paxno' ";
}

if (!empty($group)) {
    $groupcond = " AND a.`groupno`= '$group' ";
}

//if (!empty($class)) {
//    $classcond = " AND a.`batchno`= '$class' ";
//}

if (!empty($_POST['class'])) {
    foreach ($_POST['class'] as $cucheck2) {
        $ccheck2[$ccount2++] .= "OR a.`batchno` = '" . $cucheck2 . "' ";
//        $ccheckoth[$ccountoth++] .= "OR `batchno` = '" . $cucheck2 . "' ";
        $classprn = $classprn . $cucheck2 . "  /   ";
    }

    foreach ($ccheck2 as $g => $cusvalues2) {
        $cuscheck2 .= $cusvalues2;
    }
//    foreach ($ccheckoth as $g => $valuesoth) {
//        $cuscheckoth .= $valuesoth;
//    }

    $classcond = " AND (a.`batchno` = '' " . $cuscheck2 . " )";

    $classprn = left($classprn, -4);
}

if (!empty($city)) {
    $citycond = " AND a.`city`= '$city' ";
}

if (!empty($state)) {
    $statecond = " AND a.`state` = '$state' ";
}

if ($sex == '') {
    
} else {
    $sexcond = "AND left(b.`cmbgender`,1) = trim('$sex') ";
}

if ($canconly > 0) {
    $canccond = " and b.cancdate != '0000-00-00' ";
} else {
    $canccond = " and b.cancdate = '0000-00-00' ";
}

if ($ppexpiry != 99) {
    $time = date("Y-m-d");

    $mcurrmon = date("Y-m-d", strtotime($time . "+" . $ppexpiry . " month"));

    $ppexpirycond = " and b.expdate != '0000-00-00' and b.expdate >= '$time' and b.expdate < '$mcurrmon' ";
}

$depdate = $_POST['depdate'];
if (!empty($depdate)) {
    $depdatecond = " and a.depdate = '$depdate' ";
}

$arrvdate = $_POST['arrvdate'];
if (!empty($arrvdate)) {
    $arrvdatecond = " and a.arrdate = '$arrvdate' ";
}

$airlines = $_POST['airlines'];
if ($airlines != '') {
    $airlinecond = " and a.craft = '$airlines' ";
}

$agent = $_POST['agent'];
if ($agent != '') {
    $agentcond = " and a.agent like '%$agent%' ";
}

// browse($deppnr);
if ($deppnr == 'P') {
    $deppnrcond = " and a.deppnr = '' ";
}

if ($arrpnr == 'P') {
    $arrpnrcond = " and a.arrpnr = '' ";
}

$getpmain2 = mysqli_query($conn, "SELECT a.vouchno, a.regnno, a.vouchdt, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt,  a.haji, a.groupno, a.agent, a.agent as monname,
                                  a.fname, a.add1, a.add2, a.add3, a.tel, a.tel2, a.city, a.mobile, a.mobile2, a.pin, a.state, a.tel as infant, a.tcs as monno,
                                  a.fullamt, a.halfamt, a.infamt, a.atamt, a.stamt, a.fareamt, a.cancelchgamt, a.amount, a.discamt, a.tcs as ftnt, 
                                  a.fullno, a.half2, a.half, a.halfno2, a.halfno, a.inf, a.infno, a.visachg, a.visachgno, a.cancelchg2, a.farediffamt,
                                  a.airtax, a.airtaxno, a.st, a.stno, a.farediff, a.farediffno, a.tcsamt, a.tcs, a.bookamt, a.bookamtno,
                                  a.cancelchg, a.cancelchgno, a.disc2, a.disc, a.discno, a.netamt, a.batchno, a.busno, a.craft, a.sector, a.depdate,
                                  a.deptime, a.deppnr, a.depflt, a.arrdate, a.arrtime, a.arrpnr, a.arrflt, a.tourno, a.rem1, a.rem2, a.rem3,
                                  a.discrem, a.email, a.halfpay, a.fullpay, a.cgstno, a.cgstamt, a.sgstno, a.sgstamt, a.nettot, a.agentcomm,
                                  a.mksingle, a.mkdouble, a.mktriple, a.mkquad, a.mkquint, a.mksuite, a.mdsingle, a.mddouble, a.mdtriple,
                                  a.mdquad, a.mdquint, a.mdsuite, b.vouchno as paxno, b.surname, b.name as paxname, b.suffix, b.midname, b.lastname, 
                                  b.vregnno, b.fhname, b.mehram, b.relation, b.caste, b.nation, b.accomp1, b.accomp2, b.comefrom, b.period, b.addindia, b.airline, 
                                  b.draftno, b.dob, DATE_FORMAT(b.`dob`,'%d-%m-%Y') as mydob, b.plbirth, b.mothname, b.spname, b.prof, b.qual, b.redypass, b.issdate, 
                                  b.expdate, DATE_FORMAT(b.`expdate`,'%d-%m-%Y') as mydoe, b.issplace, 
                                  b.visano, b.visadoi, b.visapoi, b.applno, b.appldate, b.applplace, b.passoffice, b.photo, b.tourtype, b.roomno, 
                                  b.policenqno, b.enqdate, b.nominee, b.nrelation, b.nadd1, b.nadd2, b.travsince, b.nopeople, b.fpeople, b.cpeople, 
                                  b.at1, b.at1from, b.at1to, b.at2, b.at2from, b.at2to, b.at3, b.at3from, b.at3to, b.visaplace, b.visadtd, b.sex, b.tkttax, 
                                  b.tktdate, b.mofano, b.bankname, b.appno, b.submdate, b.username, b.airsrno, b.haj_q, b.dum_q, b.oth_q, b.haj_b, b.dum_b, b.oth_b,
                                  b.bran, b.branregno, b.branvouch, b.noupd, b.tourdate, b.finyear, b.vbatch, b.tktamt, b.touramt, b.community, 
                                  b.barcode, b.manual, b.mkroom, b.mdroom, b.othroom, b.panno, b.cmbgender, b.cancdate, 
                                  b.qurname, b.tour FROM `bookform` a, visaform b where a.regnno = b.vregnno and a.tourno = b.tour
                                  $tourcond $namecond $regncond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
                                  $ppexpirycond $ppinhandcond $depdatecond $deppnrcond $airlinecond $agentcond $arrvdatecond $arrvpnrcond $canccond 
                                  order by b.expdate, b.vregnno, b.vouchno") or die(mysqli_error($conn));
//print table($getpmain2);
//die();

$getno2 = mysqli_num_rows($getpmain2);

if (isset($_POST["view"]) || ($_GET["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-fluid">
            <?php
            include("includes/report_head.php");
            if ($getno2 > 0) {
                ?>
                <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                    <thead class="mySearchTr">
                        <tr>
                            <th width="3%;">Sr.</th>
                            <th width="4%;">Regn.</th>
                            <th width="4%;">Pax #</th>
                            <th width="30%;">Name</th>
                            <th width="7%;">Gender</th>
                            <th width="7%;">Passport</th>
                            <th width="10%;">Expiry Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $pctr = 0;
                        $tmon = 0;

                        while ($pmain = mysqli_fetch_array($getpmain2)) {
                            $voucher = $pmain['vouchno'];
                            $vregnno = $pmain['vregnno'];
                            $paxno = $pmain['paxno'];

//                                $partynm = trim($pmain['surname']) . ' ' . trim($pmain['lastname']) . ' ' . trim($pmain['midname']);
                            $partynm = trim($pmain['paxname']);
                            $invno = $pmain['invno'];

                            $gender = '';
                            if (left($pmain['cmbgender'], 1) == "M") {
                                $gender = "Male";
                            } else {
                                $gender = "Female";
                            }

                            $k = 0;

                            $paxcount = 0;
                            $paxqry = mysqli_query($conn, "SELECT vouchno FROM `visaform` WHERE `vregnno` = '$pmain[vouchno]' ") or die(mysqli_error($conn));
                            $paxcount = mysqli_num_rows($paxqry);

                            $pctr++;
                            echo "<tr data-href='bookform.php?mode=edit&vouchno=$vregnno' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                            echo "<td>$pctr</td>";
                            echo "<td>$pmain[vregnno]</td>";
                            echo "<td>$paxno</td>";

                            echo "<td>$partynm</td>";
                            echo "<td>$gender</td>";
                            echo "<td>$pmain[redypass]</td>";
                            echo "<td>$pmain[mydoe]</td>";
                            echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
                <?php
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}


if (isset($_POST["excel"]) || ($_GET["excel"])) {

    if ($getno2 > 0) {
        $contents .= "Sr., Regn., Name, Pax #, Sex, Passport, Craft, Agent, Makka, Room, Madina, Room, Group \n";

        $pctr = 0;
        $pchtypecond = "";
        $s1 = $s2 = $s3 = $s4 = $s5 = $s6 = $s0;
        $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = $m0;

        while ($pmain = mysqli_fetch_array($getpmain2)) {
            $dob = $pmain['dob'];

            $proceed = 1;
            if ($infonly == 1) {
                $curdate = date("Y-m-d");
                $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));
                if ($infmon > $curdate) {
                    $proceed = 1;
                } else {
                    $proceed = 0;
                }
            }
            if ($proceed > 0) {
                if ($pmain['paidby'] == "C") {
                    $ptyped = "CASH";
                } else {
                    $ptyped = "CREDIT";
                }

                $voucher = $pmain['vouchno'];
                $vregnno = $pmain['vregnno'];
                $paxno = $pmain['paxno'];

                $partynm = trim($pmain['surname']) . ' ' . trim($pmain['lastname']) . ' ' . trim($pmain['midname']);
                $invno = $pmain['invno'];
                $totalqty = $pmain['totalqty'];
                $mydt = $pmain['mydt'];
                $mobile = $pmain['mobile'];
                $mkroom = $pmain['mkroom'];
                $mdroom = $pmain['mdroom'];

                $gender = '';
                if (left($pmain['cmbgender'], 1) == "M") {
                    $gender = "Male";
                } else {
                    $gender = "Female";
                }

                $k = 0;
                if ($pmain['mksingle'] != 0) {
                    $mkroom = $pmain['mksingle'];
                    $mk_rtype = 'SGL';
                    $s1++;
                }
                if ($pmain['mkdouble'] > 0) {
                    $mkroom = $pmain['mkdouble'];
                    $mk_rtype = 'DBL';
                    $s2++;
                }
                if ($pmain['mktriple'] != 0) {
                    $mkroom = $pmain['mktriple'];
                    $mk_rtype = 'TRP';
                    $s3++;
                }
                if ($pmain['mkquot'] != 0) {
                    $mkroom = $pmain['mkquot'];
                    $mk_rtype = 'QUAD';
                    $s4++;
                }
                if ($pmain['mkquint'] != 0) {
                    $mkroom = $pmain['mkquint'];
                    $mk_rtype = 'QUINT';
                    $s5++;
                }
                if ($pmain['mksuite'] != 0) {
                    $mkroom = $pmain['mksuite'];
                    $mk_rtype = 'SUITE';
                    $s6++;
                }
                if ($pmain['mknobed'] != 0) {
                    $mkroom = $pmain['mknobed'];
                    $mk_rtype = 'NOBED';
                    $s0++;
                }

                if ($pmain['mdsingle'] != 0) {
                    $mdroom = $pmain['mdsingle'];
                    $md_rtype = 'SGL';
                    $m1++;
                }
                if ($pmain['mddouble'] != 0) {
                    $mdroom = $pmain['mddouble'];
                    $md_rtype = 'DBL';
                    $m2++;
                }
                if ($pmain['mdtriple'] != 0) {
                    $mdroom = $pmain['mdtriple'];
                    $md_rtype = 'TRP';
                    $m3++;
                }
                if ($pmain['mdquot'] != 0) {
                    $mdroom = $pmain['mdquot'];
                    $md_rtype = 'QUAD';
                    $m4++;
                }
                if ($pmain['mdquint'] != 0) {
                    $mdroom = $pmain['mdquint'];
                    $md_rtype = 'QUINT';
                    $m5++;
                }
                if ($pmain['mdsuite'] != 0) {
                    $mdroom = $pmain['mdsuite'];
                    $md_rtype = 'SUITE';
                    $m6++;
                }
                if ($pmain['mdnobed'] != 0) {
                    $mdroom = $pmain['mdnobed'];
                    $md_rtype = 'NOBED';
                    $m0++;
                }

                $paxcount = 0;
                $paxqry = mysqli_query($conn, "SELECT vouchno FROM `visaform` WHERE `vregnno` = '$pmain[vouchno]' ") or die(mysqli_error($conn));
                $paxcount = mysqli_num_rows($paxqry);

                $pctr++;
                $contents .= "$pctr" . ",";
                $contents .= "$pmain[vregnno]" . ",";

                if ($pmain['cancdate'] != "0000-00-00") {
                    $contents .= "$partynm" . ",";
                } else {
                    if ($paxcount == 1) {
                        $contents .= "$partynm" . ",";
                    } else {
                        $curdate = date("Y-m-d");
                        $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));

                        if ($infmon > $curdate) {
                            $contents .= "$partynm" . ",";
                        } else {
                            $contents .= "$partynm" . ",";
                        }
                    }
                }

                $contents .= "$paxno" . ",";
                $contents .= "$gender" . ",";
                $contents .= "$pmain[redypass]" . ",";
                $contents .= "$pmain[craft]" . ",";
                $contents .= "$pmain[agent]" . ",";
                $contents .= "$mk_rtype" . ",";
                $contents .= "$mkroom" . ",";
                $contents .= "$md_roomtype" . ",";
                $contents .= "$mdroom" . ",";
                $contents .= "$pmain[group]" . "\n";
            }
        }
        $stot = $s1 + $s2 + $s3 + $s4 + $s5 + $s6;
        $mtot = $m1 + $m2 + $m3 + $m4 + $m5 + $m6;

        $sr1 = $s1;
        $sr2 = round($s2 / 2, 2);
        $sr3 = round($s3 / 3, 2);
        $sr4 = round($s4 / 4, 2);
        $sr5 = round($s5 / 5, 2);
        $sr6 = round($s6 / 6, 2);
        $sr0 = $s0;

        $mr1 = $m1;
        $mr2 = round($m2 / 2, 2);
        $mr3 = round($m3 / 3, 2);
        $mr4 = round($m4 / 4, 2);
        $mr5 = round($m5 / 5, 2);
        $mr6 = round($m6 / 6, 2);
        $mr0 = $m0;

        $srtot = $sr1 + $sr2 + $sr3 + $sr4 + $sr5 + $sr6;
        $mrtot = $mr1 + $mr2 + $mr3 + $mr4 + $mr5 + $mr6;

        $contents .= "Total No. Of Pax - $stot \n";

        $contents .= " , SINGLE, DOUBLE, TRIPLE, QUAD, QUINT, SUITE, NO BED, TOTAL \n";

        $contents .= "MAKKAH" . ",";
        $contents .= "$sr1" . ",";
        $contents .= "$sr2" . ",";
        $contents .= "$sr3" . ",";
        $contents .= "$sr4" . ",";
        $contents .= "$sr5" . ",";
        $contents .= "$sr6" . ",";
        $contents .= "$sr0" . ",";
        $contents .= "$srtot" . "\n";


        $contents .= "MADINA" . ",";
        $contents .= "$mr1" . ",";
        $contents .= "$mr2" . ",";
        $contents .= "$mr3" . ",";
        $contents .= "$mr4" . ",";
        $contents .= "$mr5" . ",";
        $contents .= "$mr6" . ",";
        $contents .= "$mr0" . ",";
        $contents .= "$mrtot" . "\n";
    }

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>