<?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 = $prphcond = $agentcond = $arrvdatecond = '';
$infcond = $cancond = $addcond = $ppexpcond = $ppinhand = $dpdtcond = $dppnrcond = $aircond = $prregnocond = $arrvpnrcond = $buscond = $roomcond = "";
$tourcond = $tourcond1 = '';

$tourcode = $_SESSION['tourcode'];

if (!empty($tourcode)) {
    $tourcond = " AND `tourno` = '$tourcode' ";
    $tourcond1 = " AND `tour` = '$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 "SELECT * FROM `bookform` WHERE `vouchno` != '' $tourcond $namecond $regnocond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
//                                  $ppexpirycond $ppinhandcond $depdatecond $deppnrcond $airlinecond $agentcond $arrvdatecond $arrvpnrcond $canccond" ;
// print table($mainqry);
// die();

$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");
    ?>
    <script>
        $(document).ready(function () {
            $(".csbill").hide();
            $(".actbal").hide();
            $(".tourcs").attr("colspan", 4);

            document.addEventListener("keydown", function (zEvent) {

                if (zEvent.ctrlKey && zEvent.altKey && zEvent.code === "KeyT") {
                    //            getCashModal();
                    $(".ogbal").hide();
                    $(".actbal").show();
                    $(".csbill").show();
                    $(".tourcs").attr("colspan", 6);
                    //                    alert("Kaise h aap log");
                }
            });
        })
    </script>
    <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%" class="text-center tourcs">Tour</th>
                                <th style="width:6%" rowspan="2" class="text-right">Refund</th>

                                <th style="width:6%" rowspan="2" class="text-right csbill" >Cash2 Refund</th>
                                <th style="width:6%" rowspan="2" class="text-right csbill" >Cash2 Balance</th>
                                <th style="width:5%"  rowspan="2" class="text-right">Total Balance</th>
                                <!--<th style="width:5%"  rowspan="2" class="text-right">Total Balance 2</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 csbill" >Cash Bill</th>
                                <th style="width:6%" class="text-right csbill" >Paid Cash2</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">Balance2</th>-->
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $discamt = 0;
                            $totamt = 0;
                            $touramt = $tcash2bal  = $tiktamt = $fulla = $halfa = $childwoba = $infa = $disca = $farediffa = $cancelchga = $cash2bal = $tpcashamt = 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']) + ($fmain['full_2'] * $fmain['fullno_2']) + ($fmain['full_3'] * $fmain['fullno_3']);
                                $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));

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

//                                browse($tcashamt) ;
//                                browse($ucashamt) ;

                                $gbankt = mysqli_query($conn, "SELECT sum(amount) as tbanktikt FROM bankrecp WHERE recpagnst = 'U' and payfor='T' 
                                                                      and `regnno`='$fmain[regnno]' and retdate = '0000-00-00' $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]' and retdate = '0000-00-00' $tourcond1") or die(mysqli_error($conn));
                                $fbanku = mysqli_fetch_array($gbanku);
                                $ubankamt = $fbanku['ubanktikt'];

                                $gbankp = mysqli_query($conn, "SELECT sum(amount) as ubanktikt FROM bankpay WHERE `regnno`='$fmain[regnno]' and retdate = '0000-00-00' $tourcond1") or die(mysqli_error($conn));
                                $fbankp = mysqli_fetch_array($gbankp);
                                $gbankc = mysqli_query($conn, "SELECT sum(amount) as ubanktikt FROM cashpay WHERE `regnno`='$fmain[regnno]'  $tourcond1") or die(mysqli_error($conn));
                                $fbankc = mysqli_fetch_array($gbankc);

                                $ubankpamt = $fbankp['ubanktikt'] + $fbankc['ubanktikt'];

                                $mregcs = mysqli_query($conn, "SELECT * FROM regncash WHERE vouchno = '$fmain[vouchno]'") or die(mysqli_error($conn));
                                $tregcs = mysqli_fetch_array($mregcs);

                                $gcasho = mysqli_query($conn, "SELECT sum(amount) as ucashtikt FROM cashrecp_kacha WHERE recpagnst = 'U' and payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fcasho = mysqli_fetch_array($gcasho);
                                $ocashamt = $fcasho['ucashtikt'];

                                $gcashp = mysqli_query($conn, "SELECT sum(amount) as ucashtikt FROM cashpay_kacha WHERE recpagnst = 'U' and payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
                                $fcashp = mysqli_fetch_array($gcashp);
                                $pcashamt = $fcashp['ucashtikt'];

                                $tttikt = $tcashamt + $tbankamt;
                                $ttour = $ucashamt + $ubankamt;
                                $baltikt = $tiktamt - $tttikt;
                                $baltour = $touramt - $ttour;
                                $csnettot = $tregcs['csnettot'];
                                $baltour2 = $touramt + $csnettot - $ttour - $ocashamt;
                                $totbalamt = $baltikt + $baltour + $ubankpamt;
                                $totbalamt2 = $totbalamt + $csnettot - $ocashamt;

                                $cash2bal = $csnettot - $ocashamt + $pcashamt;

//                                if ($fmain[regnno] == 4134) {
//                                    browse($totbalamt);
//                                    browse($csnettot);
//                                    browse($ocashamt);
//                                }
                                if ($totbalamt2 != 0) {
                                    $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 csbill' style='background-color:#b8feff;'>$csnettot</td>";
                                    echo "<td class='text-right csbill' style='background-color:#f5bfa4;'>$ocashamt</td>";
                                    echo "<td class='text-right'>$ubankamt</td>";
                                    echo "<td class='text-right ogbal' style='background-color:#B4D1F7'>$baltour</td>";
                                    echo "<td class='text-right actbal' style='background-color:#B4D1F7'>$baltour2</td>";
                                    echo "<td class='text-right'>$ubankpamt</td>";
                                    echo "<td class='text-right csbill' style='background-color:#edc5d7;'>$pcashamt</td>";
                                    echo "<td class='text-right csbill' style='background-color:#dcf4f2;'>$cash2bal</td>";
                                    echo "<td class='text-right actbal' style='background-color:pink'>$totbalamt2</td>";
                                    echo "<td class='text-right ogbal' 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;
                                    $totocashamt += $ocashamt;
                                    $totubankamt += $ubankamt;
                                    $totubankpamt += $ubankpamt;
                                    $totbaltour += $baltour;
                                    $totbaltour2 += $baltour2;
                                    $ttotbalamt += $totbalamt;
                                    $ttotbalamt2 += $totbalamt2;
                                    $tcsnettot += $csnettot;
                                    $tcash2bal += $cash2bal;
                                    $tpcashamt += $pcashamt;
                                }
                            }
                            // $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 csbill'><h4><b>$tcsnettot</b></h4></td>";
                            echo "<td class='text-right csbill'><h4><b>$totocashamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totubankamt</b></h4></td>";
                            echo "<td class='text-right ogbal'><h4><b>$totbaltour</b></h4></td>";
                            echo "<td class='text-right actbal'><h4><b>$totbaltour2</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totubankpamt</b></h4></td>";
                            echo "<td class='text-right csbill'><h4><b>$tpcashamt</b></h4></td>";
                            echo "<td class='text-right csbill'><h4><b>$tcash2bal</b></h4></td>";
                            echo "<td class='text-right actbal'><h4><b>$ttotbalamt2</b></h4></td>";
                            echo "<td class='text-right ogbal'><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., Regn., Name, No. of Pax., Cancel Pax, Ticket, Tour, Total Balance, Total Balance 2, Agent\n";
    $contents .= "Net Amount, Paid Cash, Paid Bank, Balance, Net Amount, Paid Cash, Cash Bill, Paid Cash2, Paid Bank, Balance, Balance2\n";

    $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]' and retdate = '0000-00-00' $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]' and retdate = '0000-00-00' $tourcond1") or die(mysqli_error($conn));
        $fbanku = mysqli_fetch_array($gbanku);
        $ubankamt = $fbanku['ubanktikt'];

        $gbankp = mysqli_query($conn, "SELECT sum(amount) as ubanktikt FROM bankpay WHERE `regnno`='$fmain[regnno]' and retdate = '0000-00-00' $tourcond1") or die(mysqli_error($conn));
        $fbankp = mysqli_fetch_array($gbankp);
        $ubankpamt = $fbankp['ubanktikt'];

        $mregcs = mysqli_query($conn, "SELECT * FROM regncash WHERE vouchno = '$fmain[vouchno]'") or die(mysqli_error($conn));
        $tregcs = mysqli_fetch_array($mregcs);

        $gcasho = mysqli_query($conn, "SELECT sum(amount) as ucashtikt FROM cashrecp_kacha WHERE recpagnst = 'U' and payfor='U' and `regnno`='$fmain[regnno]' $tourcond1") or die(mysqli_error($conn));
        $fcasho = mysqli_fetch_array($gcasho);
        $ocashamt = $fcasho['ucashtikt'];

        $tttikt = $tcashamt + $tbankamt;
        $ttour = $ucashamt + $ubankamt;
        $baltikt = $tiktamt - $tttikt;
        $baltour = $touramt - $ttour;
        $csnettot = $tregcs['csnettot'];
        $baltour2 = $touramt + $csnettot - $ttour - $ocashamt;
        $totbalamt = $baltikt + $baltour;
        $totbalamt2 = $totbalamt + $csnettot - $ocashamt;

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$fmain[regnno]" . ",";
        $contents .= "$fmain[haji]" . ",";
        $contents .= "$paxnum" . ",";
        $contents .= "" . ",";
        $contents .= "$tiktamt" . ",";
        $contents .= "$tcashamt" . ",";
        $contents .= "$tbankamt" . ",";
        $contents .= "$baltikt" . ",";
        $contents .= "$touramt" . ",";
        $contents .= "$ucashamt" . ",";
        $contents .= "$csnettot" . ",";
        $contents .= "$ocashamt" . ",";
        $contents .= "$ubankamt" . ",";
        $contents .= "$baltour" . ",";
        $contents .= "$ubankpamt" . ",";
        $contents .= "$baltour2" . ",";
        $contents .= "$totbalamt" . ",";
        $contents .= "$totbalamt2" . ",";
        $contents .= "$fmain[agent]" . "\n";

        $getpaxnum += $paxnum;
        $tottiktamt += $tiktamt;
        $tottcashamt += $tcashamt;
        $tottbankamt += $tbankamt;
        $totbaltikt += $baltikt;
        $tottouramt += $touramt;
        $totucashamt += $ucashamt;
        $totocashamt += $ocashamt;
        $totubankamt += $ubankamt;
        $totubankpamt += $ubankpamt;
        $totbaltour += $baltour;
        $totbaltour2 += $baltour2;
        $ttotbalamt += $totbalamt;
        $ttotbalamt2 += $totbalamt2;
        $tcsnettot += $csnettot;
    }

    $contents .= "" . ",,";
    $contents .= "Total" . "";
    $contents .= "$getpaxnum" . ",";
    $contents .= "" . ",";
    $contents .= "$tottiktamt" . ",";
    $contents .= "$tottcashamt" . ",";
    $contents .= "$tottbankamt" . ",";
    $contents .= "$totbaltikt" . ",";
    $contents .= "$tottouramt" . ",";
    $contents .= "$totucashamt" . ",";
    $contents .= "$tcsnettot" . ",";
    $contents .= "$totocashamt" . ",";
    $contents .= "$totubankamt" . ",";
    $contents .= "$totbaltour" . ",";
    $contents .= "$totubankpamt" . ",";
    $contents .= "$totbaltour2" . ",";
    $contents .= "$ttotbalamt" . ",";
    $contents .= "$ttotbalamt2" . ",";
    $contents .= "" . "\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;
    }
}
?>