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

$maindb = 'hrmain';
$trandb = 'hrtran';
$formname = 'HR Register';
$file = "hrreg_fr.php";

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

$suppcond = "";

$mfrmdate = $_POST["fromdt"];
$mtodate = $_POST["todt"];
$customer = $_POST["customer"];
$dtype = $_POST["dtype"];

if (!empty($customer)) {
    $custcond = "AND customer = '$customer'";
}

if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}


$getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`,'%d-%m-%Y')as vouchdt  FROM $maindb 
                                      WHERE vouchno != '' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $custcond") or die(mysqli_error($conn));

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">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Voucher</th>
                                <th style="width:6%">Date</th>
                                <th style="width:20%;">Company / Customer</th>
                                <th style="width:10%;">Visa No </th>
                                <th style="width:10%;">ID No.</th>
                                <th style="width:10%;">Embassy</th>
                                <th style="width:5%;text-align:right">Tot Pax</th>
                                <th style="width:5%;text-align:right">Tot Availability</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = 0;
                            $ttotpax = 0;
                            $ttotavlbl = 0;

                            while ($pmain = mysqli_fetch_array($getpmain2)) {
                                $msupp = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$pmain[customer]'") or die(mysqli_error($conn));
                                $lsupp = mysqli_fetch_array($msupp);
                                if ($pmain['embassy']  == 'N') {
                                    $embassy = "New Delhi";
                                } else {
                                    $embassy = "Mumbai";
                                }

                                $pctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='fit_ent.php?paxid=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$lsupp[name]</td>";
                                echo "<td>$pmain[visano]</td>";
                                echo "<td>$pmain[idno]  </td>";
                                echo "<td>$embassy</td>";
                                echo "<td style='text-align:right;'>$pmain[totpax]</td>";
                                echo "<td style='text-align:right;'>$pmain[totavlbl]</td>";
                                echo "</tr>";

                                $ttotpax += $pmain['totpax'];
                                $ttotavlbl += $pmain['totavlbl'];

                                if ($_POST["dtype"] == 2) {

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    echo "<thead>";
                                    echo "<tr class='' style='background-color:#BABFC2;font-size:14px;font-weight:bold'>";

                                    echo "<th style='width:3%;'>Sr</th>";
                                    echo "<th style='width:15%;'>Person Count</th>";
                                    echo "<th style='width:5%;'>Occupation / Category</th>";
                                    echo "<th style='width:5%;'>Category in English</th>";
                                    echo "<th style='width:8%;'>Nationality</th>";
                                    echo "<th style='width:15%;'>Visa Issue</th>";
                                    echo "<th style='width:10%;'>Availability</th>";
                                    echo "</tr>";
                                    echo "</thead>";

                                    $ctr2 = 0;
                                    $totavlbl = 0;

                                    $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`visaiss` , '%d-%m-%Y') AS visadt FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td class='text-right'>$tran[paxno]</td>";
                                        echo "<td>$tran[category]</td>";
                                        echo "<td>$tran[cateng]</td>";
                                        echo "<td>$tran[nationality]</td>";
                                        echo "<td>$tran[visadt]</td>";
                                        echo "<td class='text-right'>$tran[avlbl]</td>";
                                        echo "</tr>";

                                        $totavlbl += $tran['avlbl'];
                                    }
                                    echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                    echo "<td colspan='6' class='text-right'>Total</td>";
                                    echo "<td class='text-right ; font-bolder'>$totavlbl</td>";
                                    echo "</tr>";
                                    echo "</table>";
                                    echo "</td>";
                                    echo "</tr>";
                                }
                            }

                            echo "<tr style='background-color: #98F9F0;color: red;'>";
                            echo "<td style='font-size:17px' colspan='6' class='text-right'><b>Total</b></td>";
                            echo "<td style='font-size:17px' class='text-right'>$ttotpax</td>";
                            echo "<td style='font-size:17px' class='text-right'>$ttotavlbl</td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}


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

    $contents .= "Voucher, Date, Company / Customer, Visa No, ID No, Embassy, Tot Pax, Tot Availability \n";

    $pctr = 0;
    $ttotpax = 0;
    $ttotavlbl = 0;

    while ($pmain = mysqli_fetch_array($getpmain2)) {
        $msupp = mysqli_query($conn, "SELECT accode,name FROM accmast where accode='$pmain[customer]'") or die(mysqli_error($conn));
        $lsupp = mysqli_fetch_array($msupp);
        if ($pmain['embassy']  == 'N') {
            $embassy = "New Delhi";
        } else {
            $embassy = "Mumbai";
        }

        $pctr++;
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$lsupp[name]" . ",";
        $contents .= "$pmain[visano]" . ",";
        $contents .= "$pmain[idno]" . ",";
        $contents .= "$embassy" . ",";
        $contents .= "$pmain[totpax]" . ",";
        $contents .= "$pmain[totavlbl]" . "\n";

        $ttotpax += $pmain['totpax'];
        $ttotavlbl += $pmain['totavlbl'];

        if ($_POST["dtype"] == 2) {

            $contents .= "Sr., Person Count, Occupation / Category, Category(English), Nationality, Visa Issue, Availability\n";

            $ctr2 = 0;
            $totavlbl = 0;

            $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`visaiss` , '%d-%m-%Y') AS visadt FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[paxno]" . ",";
                $contents .= "$tran[category]" . ",";
                $contents .= "$tran[cateng]" . ",";
                $contents .= "$tran[nationality]" . ",";
                $contents .= "$tran[visadt]" . ",";
                $contents .= "$tran[avlbl]" . "\n";

                $totavlbl += $tran['avlbl'];
            }
            $contents .= "" . ",,,,,,";
            $contents .= "Total" . ",";
            $contents .= "$totavlbl" . "\n";
        }
    }
    $contents .= "" . ",,,,,";
    $contents .= "Total" . ",";
    $contents .= "$ttotpax" . ",";
    $contents .= "$ttotavlbl" . "\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;
    }
}
?>