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

$formname = 'Monthly Class 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 = '';
$regnocond = '';
$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 . " )";
    $classcond1 = " AND (`batchno` = '' " . $cuscheckoth . " )";

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


//$string = '0123456789';
////$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//$string_shuffled = str_shuffle($string);
//$qxno = substr($string_shuffled, 1, 6);
//$mtmp = '$qx' . $qxno ;


mysqli_query($conn, "drop temporary table if exists tmpdbf") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table tmpdbf
                     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, 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.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 
                                  $tourcond $namecond $regncond $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));

$getpmain2 = mysqli_query($conn, "select * from tmpdbf ") or die(mysqli_error($conn));

while ($pmain = mysqli_fetch_array($getpmain2)) {

    $paxno = $pmain['paxno'];
    $dob = $pmain['dob'];
    $depdate = $pmain['depdate'];
    $regndate = $pmain['vouchdt'];

    $day = date('j', strtotime($regndate));
    $monno = date('n', strtotime($regndate));
    $fmonth = date('F', strtotime($regndate));

    if ($day < 16) {
        $ftnt = 1;
    } else {
        $ftnt = 2;
    }

    $curdate = date("Y-m-d");
    $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));

    if ($infmon > $curdate) {
        $infant = 'Y';
    } else {
        $infant = 'N';
    }
    mysqli_query($conn, "update tmpdbf set infant = '$infant', monno = '$monno', ftnt = '$ftnt', monname = '$fmonth' where paxno = '$paxno' ") or die(mysqli_error($conn));
}

$getqry = mysqli_query($conn, "select monno, monname, ftnt, depdate, batchno, count(*) as nopax, netamt as netamt from tmpdbf where infant != 'Y'
          group by monno, ftnt, batchno  order by monno, ftnt, depdate, batchno ") or die(mysqli_error($conn));

//print table($getqry);
//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;
        }
        table td {
            font-size: 14px;
        }
        table th {
            font-size: 18px;
        }
    </style>
    </head>

    <body>
        <div class="container-fluid">
            <?php
            include("includes/report_head.php");
            if ($getno2 > 0) {
                ?>
                <table style="background: white;width: 80%" class="table table-condensed table-striped table-hover table-bordered">
                    <thead class="mySearchTr">
                        <tr>
                            <th width="5%;">Month</th>
                            <th width="4%;"></th>
                            <th width="10%;">Class</th>
                            <th width="8%;" style="text-align: right">No.of Pax</th>
                            <th width="8%;" style="text-align: right">Net Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $pctr = 0;
                        $pchtypecond = "";
                        $s1 = $s2 = $s3 = $s4 = $s5 = $s6 = $s0;
                        $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = $m0;

                        $tmon = 0;
                        $a = 0;

                        while ($pm = mysqli_fetch_array($getqry)) {
                            $monno = $pm['monno'];
                            if ($a == 0) {
                                $tmon = $monno;
                                $a++;
                            }

                            if ($monno != $tmon) {
                                echo "<tr style='background-color: #E3F2E1; font-size:25px'>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td style='color:blue;font-size:16px'>Total</td>";
                                echo "<td style='color:blue;font-size:16px;text-align: right'>$ptot</td>";
                                echo "<td style='color:blue;font-size:16px;text-align: right'>$ntot</td>";
                                echo "</tr>";

                                $ptot = 0;
                                $ntot = 0;

                                $monno = $tmon;
                            }

                            echo "<tr>";
                            echo "<td>$pm[monname]</td>";
                            echo "<td>$pm[ftnt]</td>";
                            echo "<td>$pm[batchno]</td>";
                            echo "<td style='text-align: right'>$pm[nopax]</td>";
                            echo "<td style='text-align: right'>" . number_format($pm['netamt'], 0) . "</td>";
                            echo "</tr>";
                            $ptot = $ptot + $pm['nopax'];
                            $ntot = $ntot + $pm['netamt'];

                            $totpax = $totpax + $pm['nopax'];
                            $totnet = $totnet + $pm['netamt'];
                        }
                        echo "<tr style='background-color: #E3F2E1; font-size:25px'>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td style='color:blue;font-size:16px'>Total</td>";
                        echo "<td style='color:blue;font-size:16px;text-align: right'>$ptot</td>";
                        echo "<td style='color:blue;font-size:16px;text-align: right'>" . number_format($ntot, 0) . "</td>";
                        echo "</tr>";

                        echo "<tr style='background-color: #2FBFFE; font-size:25px'>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td style='color:white;font-size:18px'>Total</td>";
                        echo "<td style='color:white;font-size:18px;text-align: right'>$totpax</td>";
                        echo "<td style='color:white;font-size:18px;text-align: right'>" . number_format($totnet, 0) . "</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 .= "Month, , Class, No. of Pax, Net Amount \n";

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

        $tmon = 0;
        $a = 0;

        while ($pm = mysqli_fetch_array($getqry)) {
            $monno = $pm['monno'];
            if ($a == 0) {
                $tmon = $monno;
                $a++;
            }

            if ($monno != $tmon) {
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$ptot" . ",";
                $contents .= "$ntot" . "\n";

                $ptot = 0;
                $ntot = 0;

                $monno = $tmon;
            }

            $contents .= "$pm[monname]" . ",";
            $contents .= "$pm[ftnt]" . ",";
            $contents .= "$pm[batchno]" . ",";
            $contents .= "$pm[nopax]" . ",";
            $contents .= "$pm[netamt]" . "\n";

            $ptot = $ptot + $pm['nopax'];
            $ntot = $ntot + $pm['netamt'];

            $totpax = $totpax + $pm['nopax'];
            $totnet = $totnet + $pm['netamt'];
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$ptot" . ",";
        $contents .= "$ntot" . "\n";

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$totpax" . ",";
        $contents .= "$totnet" . "\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;
    }
}
?>