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

$maindb = "visamain";
$trandb = "visatran";

//$trandb1 = "category";
$formname = "Mofa List";
$file = "mofalist_fr.php";
$entfile = "entry.php";
$keycond = "";
$keyfld = "myvouch";

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

$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];

//$manualno = $_POST["manualno"];
$visatype = $_POST["visatype"];
$visano = $_POST["visano"];
$fileno = $_POST["fileno"];
$mofano1 = $_POST["mofano1"];
$mofano2 = $_POST["mofano2"];
$paxname = $_POST["paxname"];
$passport = $_POST["passport"];
$type = $_POST["type"];
//$sponser = $_POST["sponser"];
//$contact = $_POST["contact"];
//$mobile = $_POST["mobile"];
$agent = $_POST["agent"];
$dtype = $_POST["dtype"];
$place = $_POST["sector"];

$visatypecond = '';
$agentcond = '';
$placecond = '';

if (!empty($visatype)) {
    $visatypecond = " AND a.`visatype` = '$visatype' ";
}
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
if (!empty($place)) {
    $placecond = " AND a.`sector` = '$place' ";
}
if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; mofalist.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; mofalist.php");
}

$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
//
//    browse($mfdt);
$getpmain = mysqli_query($conn, "SELECT a.manualno ,a.broker , a.mofa, b.passport, b.paxname, b.mofano, b.mofano2, a.myvouch,a.vouchno,
                                        DATE_FORMAT(b.mofa_date, '%d-%m-%Y') as mofadate 
                                        FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch 
                                        WHERE b.`mofa_date` >= '$mfrmdate' AND b.`mofa_date` <= '$mtodate' 
                                        $visatypecond  $agentcond $placecond ") or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);
//print table($getpmain);
//die();

if (isset($_POST["view"])) {
    //        $contents .="Sponsor \n";

    include("includes/linkshead.php");
?>
    </head>

    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            $totpaidamt = 0;
            $totbalamt = 0;
            $ctr = 1;
            $ctr2 = 0;
            $paidamt = $cashamt = $bankamt = 0;
            $totmofa = 0;
            $balamt = 0;
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th class="" style="width: 2%">Sr</th>
                            <th class="" style="width: 6%">Date</th>
                            <th class="" style="width: 5%">File No</th>
                            <th class="" style="width: 8%">Agent</th>
                            <th class="" style="width: 10%">Passport No.</th>
                            <th class="" style="width: 20%">Candidate Name</th>
                            <th class="" style="width: 8%">Mofa No 1</th>
                            <th class="" style="width: 8%">Mofa No 2</th>
                            <th class="" style="width: 2%">Payment</th>
                            <th class="" style="text-align:right" style="width: 4%">Amount</th>
                            <th class="" style="text-align:right" style="width: 4%">Paid</th>
                            <th class="" style="text-align:right" style="width: 4%">Balance</th>
                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $myvouch = $pmain['myvouch'];
                            $mofaamt = $pmain['mofa'];
                            $passport = $pmain['passport'];

                            if ($pmain['visatype'] == 'E') {
                                $visatype = "ECR";
                            } elseif ($pmain['delvstatus'] == 'N') {
                                $visatype = "ECNR";
                            } else {
                                $visatype = "";
                            }

                            $mofa1 = $pmain['mofano'];
                            $mofa2 = $pmain['mofano2'];

                            //                            browse($mofa1);
                            //                            browse($mofa2);

                            if ($pmain['mofano2'] > 0) {
                                $mofa = $pmain['mofano2'];
                            } else {
                                $mofa = $pmain['mofano'];
                            }

                            // convert to UTF-16 and add BOM
                            //$contents = chr(255) . chr(254) . mb_convert_encoding($contents, "UTF-16LE", "UTF-8");
                            // add encoding in headers
                            //header("Content-Encoding: UTF-16LE, UTF-8");
                            //header("Content-type: text/csv; charset=UTF-16LE, UTF-8");


                            //                            $getcash = mysqli_query($conn, "SELECT sum(amount)as cashamt FROM `cashrecp` WHERE (`mofa` = '$mofa1' or `mofa` = '$mofa2') ") or die(mysqli_error($conn));
                            //                            $fetcash = mysqli_fetch_array($getcash);
                            //                            $cashamt = $fetcash['cashamt'];
                            //
                            //                            $getbank = mysqli_query($conn, "SELECT sum(amount)as bankamt FROM `bankrecp` WHERE (`mofa` = '$mofa1' or `mofa` = '$mofa2') ") or die(mysqli_error($conn));
                            //                            $fetbank = mysqli_fetch_array($getbank);
                            //                            $bankamt = $fetbank['bankamt'];

                            //                            browse($mofaamt);
                            //                            browse($cashamt);
                            //                            browse($bankamt);

                            $paidamt = $cashamt + $bankamt;

                            $getbank = mysqli_query($conn, "SELECT sum(setoff) as settamt FROM `billsett` WHERE `passport` = '$passport' ") or die(mysqli_error($conn));
                            $fetbank = mysqli_fetch_array($getbank);
                            $paidamt = $fetbank['settamt'];

                            $balamt = $mofaamt - $paidamt;

                            if ($paidamt > 0) {
                                $payment = "YES";
                            } else {
                                $payment = "";
                            }


                            //                            echo "<tr class='clickable-row' data-href='$entfile?mode=edit&vouchno=$pmain[vouchno]' [target]='_blank'>";
                            echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' [target]= '_blank'>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[mofadate]</td>";
                            echo "<td><a target='_blank' href='entry.php?mode=edit&myvouch=$pmain[myvouch]'>$pmain[manualno]</a></td>";
                            echo "<td>$pmain[broker]</td>";
                            echo "<td>$pmain[passport]</td>";
                            echo "<td>$pmain[paxname]</td>";
                            echo "<td>$pmain[mofano]</td>";
                            echo "<td>$pmain[mofano2]</td>";
                            echo "<td style='color:blue'>$payment</td>";
                            echo "<td style='text-align:right'>$mofaamt</td>";
                            echo "<td style='text-align:right'>$paidamt</td>";
                            echo "<td style='text-align:right'>$balamt</td>";
                            $ctr++;
                            $totmofa = $totmofa + $pmain['mofa'];
                            $totpaidamt = $totpaidamt + $paidamt;
                            $totbalamt = $totbalamt + $balamt;
                        }
                        echo "<tr  style='font-weight: bolder; text-align: right; color: blue;'>";
                        echo "<td colspan='9'>Total</td>";
                        echo "<td >$totmofa</td>";
                        echo "<td >$totpaidamt</td>";
                        echo "<td >$totbalamt</td>";
                        echo "</tr>";
                        ?>
                    </table>

                </div>
            </div>
        </div>
    </body>
<?php
}

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

    $totpaidamt = 0;
    $totbalamt = 0;
    $ctr = 1;
    $ctr2 = 0;
    $paidamt = $cashamt = $bankamt = 0;
    $totmofa = 0;
    $balamt = 0;

    $contents .= "Sr., Date, File No, Agent, Passport No, Candidate Name, Mofa No 1, Mofa No 2, Payment, Amount, Paid, Balance \n";

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $myvouch = $pmain['myvouch'];
        $mofaamt = $pmain['mofa'];
        $passport = $pmain['passport'];

        if ($pmain['visatype'] == 'E') {
            $visatype = "ECR";
        } elseif ($pmain['delvstatus'] == 'N') {
            $visatype = "ECNR";
        } else {
            $visatype = "";
        }

        $mofa1 = $pmain['mofano'];
        $mofa2 = $pmain['mofano2'];
        if ($pmain['mofano2'] > 0) {
            $mofa = $pmain['mofano2'];
        } else {
            $mofa = $pmain['mofano'];
        }

        $paidamt = $cashamt + $bankamt;

        $getbank = mysqli_query($conn, "SELECT sum(setoff) as settamt FROM `billsett` WHERE `passport` = '$passport' ") or die(mysqli_error($conn));
        $fetbank = mysqli_fetch_array($getbank);
        $paidamt = $fetbank['settamt'];

        $balamt = $mofaamt - $paidamt;

        if ($paidamt > 0) {
            $payment = "YES";
        } else {
            $payment = "";
        }

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[mofadate]" . ",";
        $contents .= "$pmain[manualno]" . ",";
        $contents .= "$pmain[broker]" . ",";
        $contents .= "$pmain[passport]" . ",";
        $contents .= "$pmain[paxname]" . ",";
        $contents .= "$pmain[mofano]" . ",";
        $contents .= "$pmain[mofano2]" . ",";
        $contents .= "$payment" . ",";
        $contents .= "$mofaamt" . ",";
        $contents .= "$paidamt" . ",";
        $contents .= "$balamt" . "\n";
        $ctr++;
        $totmofa = $totmofa + $pmain['mofa'];
        $totpaidamt = $totpaidamt + $paidamt;
        $totbalamt = $totbalamt + $balamt;
    }
    $contents .= "" . ",,,,,,,,";
    $contents .= "Total" . ",";
    $contents .= "$totmofa" . ",";
    $contents .= "$totpaidamt" . ",";
    $contents .= "$totbalamt" . "\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;
    }
}
?>