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

$paxdb = "visaform";
$bookdb = "bookform";
$formname = "Outstanding Report - Ticket &  Tour";

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

$regnocond = $paxnocond = $groupcond = $crnocond = $classcond = $tournocond = $citycond = $statecond = $infcond = $cancond = $addcond = $ppexpcond = $ppinhand = $dpdtcond = $dppnrcond = $aircond = $prregnocond = $prphcond = $agentcond = $arrvdatecond = $arrvpnrcond = $buscond = $roomcond = "";

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

$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];
$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 `tourno` = '$tourcode' ";
}

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


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

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

if (!empty($_POST['class'])) {
    foreach ($_POST['class'] as $cucheck2) {
        $ccheck2[$ccount2++] .= "OR `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 (`batchno` = '' " . $cuscheck2 . " )";
//    $classcond1 = " AND (`batchno` = '' " . $cuscheckoth . " )";
    
    $classprn = left($classprn,-4);
}


if (!empty($regnno)) {
    $regnocond = " AND `regnno`= '$regnno' ";
}

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

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

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



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

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

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

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

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

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

$mainqry = mysqli_query($conn, "SELECT * FROM `bookform` WHERE `vouchno` != '' $tourcond $namecond $regnocond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
                                  $ppexpirycond $ppinhandcond $depdatecond $deppnrcond $airlinecond $agentcond $arrvdatecond $arrvpnrcond $canccond  ")or die(mysqli_error($conn));
//print table($mainqry);
$m1 = mysqli_num_rows($mainqry);
$m = $m1;
if ($m <= 0) {
    // $a = 0;
}
if ($a == 0) {
    // echo "<script>"
    //     . "window.close()"
    //     . "</script>";
}
if (isset($_POST["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" style="width:100%">
            <?php include("includes/report_head.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%"  rowspan="2">Sr.</th>
                                <th style="width:4%"  rowspan="2" class="text-right">Regn.</th>
                                <th style="width:18%" rowspan="2">Name</th>
                                <th style="width:5%"  rowspan="2" class="text-right">No. of Pax.</th>
                                <th style="width:5%"  rowspan="2" class="text-right">Cancel Pax</th>
                                <th style="width:24%" colspan="4" class="text-center">Ticket</th>
                                <th style="width:24%" colspan="4" class="text-center">Tour</th>
                                <th style="width:5%"  rowspan="2" class="text-right">Total Balance</th>
                                <th style="width:12%" rowspan="2">Agent</th>
                            </tr>
                            <tr>
                                <th style="width:6%" class="text-right">Net Amount</th>
                                <th style="width:6%" class="text-right">Paid Cash</th>
                                <th style="width:6%" class="text-right">Paid Bank</th>
                                <th style="width:6%" class="text-right">Balance</th>
                                <th style="width:6%" class="text-right">Net Amount</th>
                                <th style="width:6%" class="text-right">Paid Cash</th>
                                <th style="width:6%" class="text-right">Paid Bank</th>
                                <th style="width:6%" class="text-right">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $discamt = 0;
                            $totamt = 0;
                            $touramt = $tiktamt = $fulla = $halfa = $childwoba = $infa = $disca = $farediffa = $cancelchga = 0;
                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $gpaxno = mysqli_query($conn, "SELECT * FROM $paxdb WHERE `vregnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $paxnum = mysqli_num_rows($gpaxno);
                                $fulla      = $fmain['full']      * $fmain['fullno'];
                                $halfa      = $fmain['half']      * $fmain['halfno'];
                                $childwoba  = $fmain['childwob']  * $fmain['childwobno'];
                                $infa       = $fmain['inf']       * $fmain['infno'];
                                $disca      = $fmain['disc']      * $fmain['discno'];
                                $farediffa  = $fmain['farediff']  * $fmain['farediffno'];
                                $cancelchga = $fmain['cancelchg'] * $fmain['cancelchgno'];

                                $tiktamt = $fulla + $halfa + $childwoba + $infa - $disca + $farediffa + $cancelchga;
                                
                                $touramt = $fmain['netamt'] - $tiktamt;

                                $gcasht = mysqli_query($conn, "SELECT sum(amount) as tcashtikt FROM cashrecp WHERE recpagnst = 'U' and payfor='T' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fcasht = mysqli_fetch_array($gcasht);
                                $tcashamt = $fcasht['tcashtikt'];

                                $gcashu = mysqli_query($conn, "SELECT sum(amount) as ucashtikt FROM cashrecp WHERE recpagnst = 'U' and payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fcashu = mysqli_fetch_array($gcashu);
                                $ucashamt = $fcashu['ucashtikt'];

                                $gbankt = mysqli_query($conn, "SELECT sum(amount) as tbanktikt FROM bankrecp WHERE recpagnst = 'U' and payfor='T' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fbankt = mysqli_fetch_array($gbankt);
                                $tbankamt = $fbankt['tbanktikt'];

//                                $gbanku = mysqli_query($conn, "SELECT sum(amount) as ubanktikt FROM bankrecp WHERE payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $gbanku = mysqli_query($conn, "SELECT sum(amount) as ubanktikt FROM bankrecp WHERE recpagnst = 'U' and payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fbanku = mysqli_fetch_array($gbanku);
                                $ubankamt = $fbanku['ubanktikt'];
                                

                                $tttikt = $tcashamt + $tbankamt;
                                $ttour = $ucashamt + $ubankamt;
                                $baltikt = $tiktamt - $tttikt;
                                $baltour = $touramt - $ttour;
                                $totbalamt = $baltikt + $baltour;
                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td class='text-right'>$fmain[regnno]</td>";
                                echo "<td>$fmain[haji]</td>";
                                echo "<td class='text-right'>$paxnum</td>";
                                echo "<td class='text-right'></td>";
                                echo "<td class='text-right'>$tiktamt</td>";
                                echo "<td class='text-right'>$tcashamt</td>";
                                echo "<td class='text-right'>$tbankamt</td>";
                                echo "<td class='text-right' style='background-color:#B4D1F7'>$baltikt</td>";
                                echo "<td class='text-right'>$touramt</td>";
                                echo "<td class='text-right'>$ucashamt</td>";
                                echo "<td class='text-right'>$ubankamt</td>";
                                echo "<td class='text-right' style='background-color:#B4D1F7'>$baltour</td>";
                                echo "<td class='text-right' style='background-color:pink'>$totbalamt</td>";
                                echo "<td>$fmain[agent]</td>";
                                echo "</tr>";
                                $getpaxnum += $paxnum;
                                $tottiktamt += $tiktamt;
                                $tottcashamt += $tcashamt;
                                $tottbankamt += $tbankamt;
                                $totbaltikt += $baltikt;
                                $tottouramt += $touramt;
                                $totucashamt += $ucashamt;
                                $totubankamt += $ubankamt;
                                $totbaltour += $baltour;
                                $ttotbalamt += $totbalamt;

                            }
                            // $colr = "red";
                            echo "<tr style='color:$colr'>";
                            echo "<td class='text-right' colspan='3'><h4><b>Total</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$getpaxnum</b></h4></td>";
                            echo "<td class='text-right'><h4><b></b></h4></td>";
                            echo "<td class='text-right'><h4><b>$tottiktamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$tottcashamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$tottbankamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totbaltikt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$tottouramt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totucashamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totubankamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totbaltour</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$ttotbalamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b></b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}

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

    $contents .= "Sr.,Tenant,Amount\n";

    $ctr = 0;
    $gtotamt = 0;
    $totamt = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {

        $monthno = $fmain['monthno'];
        $monname = $fmain['monname'];
        $accode = $fmain['accode'];
        $amount = $fmain['amt'];
        $gettenant = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode' ") or die(mysqli_error($conn));
        $ftenant = mysqli_fetch_array($gettenant);
        $tenant = $ftenant['name'];

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$monname" . ",";
        $contents .= "$amount" . "\n";


        $totamt = $totamt + $amount;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totamt" . "\n";
    $contents .= "" . "\n";


    $contents .= "Sr.,Tenant,Amount\n";

    $ctr2 = 0;
    $totamt2 = 0;

    while ($fmain2 = mysqli_fetch_array($mainqry2)) {

        $accode2 = $fmain2['accode'];
        $monname2 = $fmain2['monname'];
        $amount2 = $fmain2['amt'];
        $gettenant2 = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode = '$accode2' ") or die(mysqli_error($conn));
        $ftenant2 = mysqli_fetch_array($gettenant2);
        $tenant2 = $ftenant2['name'];

        $ctr2++;

        $contents .= "$ctr2" . ",";
        $contents .= "$monname2" . ",";
        $contents .= "$amount2" . "\n";


        $totamt2 = $totamt2 + $amount2;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totamt2" . "\n";
    $contents .= "" . "\n";

    $gtotamt = $totamt + $totamt2;

    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$gtotamt" . "\n";

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