<?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 = 'genclass.php';

$formname = 'Class Report';

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

if (isset($_POST["genview"])) {
    $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'];

    browse($sex);

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

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

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


    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($regnno)) {
        $regnocond = " AND a.`regnno`= '$regnno' ";
    }

    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' ";
    }

    browse($ppexpiry);
    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 = '' ";
    }

    $mord = ' order by a.groupno, a.agent, b.vregnno, b.vouchno ';

//$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, a.mkroom, a.mdroom, a.othroom, 
//                                  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, b.plbirth, b.mothname, b.spname, b.prof, b.qual, b.redypass, b.issdate, b.expdate, 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.panno, b.cmbgender, b.cancdate, 
//                                  b.qurname, b.tour FROM `bookform` a, visaform b where a.regnno = b.vregnno 
//                                  $tourcond $namecond $regnocond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
//                                  $ppexpirycond $ppinhandcond  $deppnrcond $airlinecond $agentcond  $arrvpnrcond $canccond 
//                                   ") or die(mysqli_error($conn));

    $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, a.mkroom, a.mdroom, a.othroom, 
                                  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, b.plbirth, b.mothname, b.spname, b.prof, b.qual, b.redypass, b.issdate, b.expdate, 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.panno, b.cmbgender, b.cancdate, 
                                  b.qurname, b.tour FROM `bookform` a, visaform b where a.regnno = b.vregnno 
                                  $tourcond $namecond $regnocond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
                                  $ppexpirycond $ppinhandcond $depdatecond $deppnrcond $airlinecond $agentcond $arrvdatecond $arrvpnrcond $canccond 
                                  order by b.vregnno, b.vouchno") or die(mysqli_error($conn));

    print table($getpmain2);
    die();

    $getno2 = mysqli_num_rows($getpmain2);

    $j = 0;
    $i = 0;
    browse($getno2);
    while ($pmain = mysqli_fetch_array($getpmain2)) {
        $dob = $pmain['dob'];
        $depdate = $pmain['depdate'];
        $paxno = $pmain['paxno'];
        browse($paxno);

        IF ($dob != "0000-00-00") {
            $newdate = date('Y-m-d', strtotime($dob . ' + 730 days'));
            $date1 = $newdate;
            $date2 = $depdate;

            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));
            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
            $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

            $j = $days;

            if ($j > 0 || $dob == "0000-00-00") {
                $i = $i + 1;
            }
        } ELSE {
            $i = $i + 1;
        }

        mysqli_query($conn, "update visaform set srno = '$i' where vouchno = '$paxno' ") or die(mysqli_error($conn));
    }

    browse("Processing Complete");
}
?>