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

$paxdb = "visaform";
$bookdb = "bookform";
$formname = "Revenue Register";

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

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

$name     = $_POST['name'];
$regno    = $_POST['vregnno'];
$paxno    = $_POST['paxno'];
$group    = $_POST['group'];
$crno     = $_POST['crno'];
$class    = $_POST['class'];
$tourno   = $_POST['tourno'];
$city     = $_POST['city'];
$state    = $_POST['state'];
$inf      = $_POST['inf'];
$canc     = $_POST['canc'];
$address  = $_POST['address'];
$ppexpiry = $_POST['ppexpiry'];
$ppinhand = $_POST['ppinhand'];
$depdate  = $_POST['depdate'];
$deppnr   = $_POST['deppnr'];
$airlines = $_POST['airlines'];
$prregno  = $_POST['prregno'];
$prphoto  = $_POST['prphoto'];
$agent    = $_POST['agent'];
$arrvdate = $_POST['arrvdate'];
$arrvpnr  = $_POST['arrvpnr'];
$busno    = $_POST['busno'];
$roomno   = $_POST['roomno'];

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

$mainqry = mysqli_query($conn, "SELECT * FROM `bookform` WHERE `vouchno` != '' ")or die(mysqli_error($conn));
$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/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                            <th style="width:5%">Sr.</th>
                                <th style="width:5%" class="text-right">Regn.</th>
                                <th style="width:10%">Name</th>
                                <th style="width:5%" class="text-right">No. of Pax.</th>
                                <th style="width:5%" class="text-right">Cancel Pax</th>
                                <th style="width:10%" class="text-right">Net Amount</th>
                                <th style="width:10%" class="text-right">Paid</th>
                                <th style="width:10%" class="text-right">Balance</th>
                                <th style="width:10%">Agent</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $discamt = 0;
                            $totamt = 0;

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

                                $gpaxno = mysqli_query($conn, "SELECT * FROM $paxdb WHERE `vregnno`='$fmain[vouchno]' ") or die(mysqli_error($conn));
                                $paxnum = mysqli_num_rows($gpaxno);
                                $fetgpax = mysqli_fetch_object($gpaxno);

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td class='text-right'>$fmain[vouchno]</td>";
                                echo "<td>$fmain[haji]</td>";
                                echo "<td class='text-right'>$paxnum</td>";
                                echo "<td class='text-right'></td>";
                                echo "<td class='text-right'>$fmain[netamt]</td>";
                                echo "<td class='text-right'></td>";
                                echo "<td class='text-right'></td>";
                                echo "<td>$fmain[agent]</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;
    }
}
?>