<?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"];

$formname = 'Visa Sale Register';
$file = 'visasalereg_fr.php';
$maindb = 'pax';
$trandb = 'visaent';

$dtype   = $_POST["dtype"];
$country = $_POST["country"];

$countrycond = "";
$countrycond2 = "";

if (!empty($country)) {
    $countrycond = " AND b.`country`= '$country' ";
    $countrycond2 = " AND `country`= '$country' ";
}



if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);


    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}



$getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(`date`, '%d-%m-%Y') AS date FROM `$maindb`a, $trandb b 
                                  WHERE a.vouchno = b.vouchno AND a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate'
                                  AND  a.visaamt > 0 $countrycond 
                                  GROUP BY a.vouchno order by a.`vouchno`") or die(mysqli_error($conn));
$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-condensed>tbody>tr>td{
            padding: 4px;
        }
        .table-condensed>thead>tr>th{
            padding: 3px;
        }
    </style>
    </head>

    <body>


        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-10">
            <?php
            include("includes/head_rep.php");

            if ($getno2 > 0) {
            ?>
                        <table class="table table-condesed table-bordered">

                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>

                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="7%;">Date</th>
                                    <th width="7%;">Voucher</th>
                                    <th width="12%;">For</th>
                                    <th width="30%;">Pax </th>
                                    <th width="8%;" class="text-right">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $totvisaamt = 0;

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

                                    $voucher  = $pmain['vouchno'];
                                    $visiting = $pmain['visitingfor'];
                                    $visaamt  = $pmain['visaamt'];
                                    $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

                                    echo "<tr data-href='fit_ent.php?mode=edit&paxid=$pmain[vouchno]' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pmain[date]</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$visiting</td>";
                                    echo "<td>$fullname</td>";
                                    echo "<td class='text-right'>$visaamt</td>";
                                    echo "</tr>";

                                    $totvisaamt = $totvisaamt + $visaamt;
                                    

                                    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'>";
                                        echo "<th style='width:5%;'>Sr</th>";
                                        echo "<th style='width:30%;'>Country</th>";
                                        echo "<th style='width:10%;'>No. Pax</th>";
                                        echo "<th style='width:10%;text-align:right'>Rate</th>";
                                        echo "<th style='width:10%;text-align:right'>Amount</th>";
                                        echo "<th style='width:10%;'>Visa Type</th>";
                                        echo "<th style='width:10%;'>Validity</th>";
                                        echo "</tr>";
                                        echo "</thead>";

                                        $ctr2 = 0;
                                        $totvisa = 0;
                                        $totpax = 0;

                                        $getvisadtl = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno ='$pmain[vouchno]' $countrycond2 ORDER BY srno") or die(mysqli_error($conn));
                                        while ($fetvisa = mysqli_fetch_array($getvisadtl)) {

                                            $getho = mysqli_query($conn, "SELECT `code`,`country` FROM `visamain` WHERE code='$fetvisa[country]'") or die(mysqli_error($conn));
                                            $fetho = mysqli_fetch_array($getho);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$fetho[country]</td>";
                                            echo "<td>$fetvisa[nopax]</td>";
                                            echo "<td class='text-right'>$fetvisa[rate]</td>";
                                            echo "<td class='text-right'>$fetvisa[amount]</td>";
                                            echo "<td>$fetvisa[type]</td>";
                                            echo "<td>$fetvisa[validity]</td>";
                                            echo "</tr>";

                                            $totvisa = $totvisa + $fetvisa['amount'];
                                            $totpax = $totpax + $fetvisa['nopax'];
                                        }

                                        echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                        echo "<td colspan='2' class='text-right'>Total</td>";
                                        echo "<td class='font-bolder'>$totpax</td>";
                                        echo "<td></td>";
                                        echo "<td class='text-right ; font-bolder'>$totvisa</td>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "</tr>";
                                        echo "</table>";
                                        echo "</td>";
                                        echo "</tr>";
                                    }

                                }

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

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

    </html>
<?php
}

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

    $contents .= ",Date, Voucher, For, Pax , Amount \n";

        $totvisaamt = 0;
        while ($pmain = mysqli_fetch_array($getpmain2)) {

            $voucher  = $pmain['vouchno'];
            $visiting = $pmain['visitingfor'];
            $visaamt  = $pmain['visaamt'];
            $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

            $contents .= "" .  ",";
            $contents .= "$pmain[date]" .  ",";
            $contents .= "$voucher" .  ",";
            $contents .= "$visiting" .  ",";
            $contents .= "$fullname" .  ",";
            $contents .= "$visaamt" . "\n";

            $totvisaamt = $totvisaamt + $visaamt;
            

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

                $contents .= "Sr, Country, No. Pax, Rate, Amount, Visa Type, Validity \n";

                $ctr2 = 0;
                $totvisa = 0;
                $totpax = 0;

                $getvisadtl = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno ='$pmain[vouchno]' $countrycond2 ORDER BY srno") or die(mysqli_error($conn));
                while ($fetvisa = mysqli_fetch_array($getvisadtl)) {

                    $getho = mysqli_query($conn, "SELECT `code`,`country` FROM `visamain` WHERE code = '$fetvisa[country]' ") or die(mysqli_error($conn));
                    $fetho = mysqli_fetch_array($getho);

                    $ctr2++;

                    $contents .= "$ctr2" . ",";
                    $contents .= "$fetho[country]" . ",";
                    $contents .= "$fetvisa[nopax]" . ",";
                    $contents .= "$fetvisa[rate]" . ",";
                    $contents .= "$fetvisa[amount]" . ",";
                    $contents .= "$fetvisa[type]" . ",";
                    $contents .= "$fetvisa[validity]" . "\n";

                    $totvisa = $totvisa + $fetvisa['amount'];
                    $totpax = $totpax + $fetvisa['nopax'];
                }

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