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

$maindb = 'settmain';
$trandb = 'setttran';
$formname = 'Setting Register';
$file = "settreg_fr.php";

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

$settcond = "";
$settbycond = "";

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

if (!empty($sett)) {
    $settcond = "AND a.`setting` = '$sett'";
}
if (!empty($settby)) {
    $settbycond = "AND a.`supplier` = '$settby'";
}

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 a.*,DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y')as vouchdt, b.name,b.accode  FROM $maindb a
                                        LEFT JOIN accmast b ON b.accode=a.supplier
                                        -- LEFT JOIN setting c ON c.code=c.setting
                                        WHERE a.vouchno != '' AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $settcond $settbycond") 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:6%">File No.</th>
                                <th style="width:20%;">Setting</th>
                                <th style="width:20%;">Setting By</th>
                                <th style="width:5%;text-align:right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = 0;
                            $tothotelamt = 0;
                            $tothotelinramt = 0;

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

                                $getsett = mysqli_query($conn, "SELECT * FROM `setting` WHERE code='$pmain[setting]'") or die(mysqli_error($conn));
                                $sett = mysqli_fetch_array($getsett);

                                $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>$pmain[fileno]</td>";
                                echo "<td>$sett[name]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td style='text-align:right;'>$pmain[totamt]</td>";
                                echo "</tr>";

                                $total = $pmain['totamt'] + $total;
                                $totalamt = number_format($total, 2);

                                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%;'>Passport</th>";
                                    echo "<th style='width:15%;'>Pax Name</th>";
                                    echo "<th style='width:5%;'>Agent</th>";
                                    echo "<th style='width:10%;text-align:right'>Amount</th>";
                                    echo "<th style='width:7%;'>Subject</th>";
                                    echo "</tr>";
                                    echo "</thead>";

                                    $ctr2 = 0;
                                    $ttotamt = 0;

                                    $gettran = mysqli_query($conn, "SELECT * 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>$tran[srno]</td>";
                                        echo "<td>$tran[passport]</td>";
                                        echo "<td>$tran[paxname]</td>";
                                        echo "<td>$tran[agent]</td>";
                                        echo "<td class='text-right'>$tran[memcash]</td>";
                                        echo "<td>$tran[subject]</td>";
                                        echo "</tr>";

                                        $ttotamt = $ttotamt + $tran['memcash'];
                                        $grandttotamt = number_format($ttotamt, 2);
                                    }
                                    echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                    echo "<td colspan='4' class='text-right'>Total</td>";
                                    echo "<td class='text-right ; font-bolder'>$grandttotamt</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></td>";
                            echo "<td style='font-size:17px' class='text-right'><b>Total</b></td>";
                            echo "<td style='font-size:17px' class='text-right'>$totalamt</td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}


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


    $contents .="Voucher, Date, File No., Setting, Setting By, Amount\n";

                            $pctr = 0;
                            $tothotelamt = 0;
                            $tothotelinramt = 0;

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

                                $getsett = mysqli_query($conn, "SELECT * FROM `setting` WHERE code='$pmain[setting]'") or die(mysqli_error($conn));
                                $sett = mysqli_fetch_array($getsett);

                                $pctr++;
                                $contents.="$pmain[vouchno]" . ",";
                                $contents.="$pmain[vouchdt]" . ",";
                                $contents.="$pmain[fileno]" . ",";
                                $contents.="$sett[name]" . ",";
                                $contents.="$pmain[name]" . ",";
                                $contents.="$pmain[totamt]" . "\n";

                                $total = $pmain['totamt'] + $total;

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

                                $contents .="Sr, Passport, Pax Name, Agent, Amount, Subject\n";

                                    $ctr2 = 0;
                                    $ttotamt = 0;

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

                                        $ctr2++;
                                        $contents .="$tran[srno]" . ",";
                                        $contents .="$tran[passport]" . ",";
                                        $contents .="$tran[paxname]]" . ",";
                                        $contents .="$tran[agent]" . ",";
                                        $contents .="$tran[memcash]" . ",";
                                        $contents .="$tran[subject]" . "\n";

                                        $ttotamt = $ttotamt + $tran['memcash'];
                                        $grandttotamt = number_format($ttotamt, 2);
                                    }
                                    $contents .="Total" . ",,,,";
                                    $contents .="$grandttotamt" . ",";
                                    $contents .="" . "\n";
                                }
                            }

                            $contents .="" . ",,,,";
                            $contents .="Total" . ",";
                            $contents .="$total" . ",";
    $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;
    }
}
?>