<?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";
$entfile = "hr.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"];

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

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

if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
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));

$getpmain = mysqli_query($conn, "SELECT a.* ,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, a.myvouch as voucherno, b.* FROM `$maindb` a left join 
                                    `$trandb` b on a.myvouch = b.myvouch WHERE length(b.`paxname`) < 2 AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` 
                                    <= '$mtodate' $agentcond ")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            $totpaidamt = 0;
            $totbalamt = 0;
            $ctr = 1;
            $ctr2 = 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%">Company / Customer</th>
                            <th class="" style="width: 6%">Visa Type</th>
                            <th class="" style="width: 8%">Agent</th>
                            <th class="" style="width: 10%">Sponsor Name</th>
                            <th class="" style="width: 10%">Visa No.</th>
                            <th class="" style="width: 8%">ID No.</th> 
                            <th class="" style="width: 8%">Email</th>
                            <th class="" style="width: 2%">Place</th>
                            <th class="" style="width: 4%">Visa Qty</th>
                            <th class="" style="width: 4%">Hijri</th>
                            <th class="" style="width: 4%" >Paid By</th>
                            <th class="" style="width: 4%" >Created By</th>
                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $myvouch=$pmain['myvouch'];
                            
                            if ($pmain['delvstatus'] == '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'];
                            }
                           
                            if ($pmain['paidby'] ==  "C") {
                                $paidby = "Cash";
                            } elseif ($pmain['paidby'] == "R") {
                                $paidby = "Credit";
                            } elseif ($pmain['paidby'] == "U") {
                                $paidby = "UPI / RTGS";
                            }

                            if ($pmain['visatype'] == 'W') {
                                $visatype = "Work";
                            } elseif ($pmain['visatype'] == 'F') {
                                $visatype = "Family";
                            } elseif ($pmain['visatype'] == 'R') {
                                $visatype = "Residence";
                            } elseif ($pmain['visatype'] == 'V') {
                                $visatype = "Visit";
                            } else {
                                $visatype = "";
                            }
                            // 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'];

                            $paidamt = $cashamt + $bankamt;
                            $balamt = $pmain['mofa'] - $paidamt;

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

                            echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' [target]= '_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td><a target='_blank' href='entry.php?mode=edit&myvouch=$pmain[voucherno]'>$pmain[manualno]</a></td>";
                            Echo "<td>$visatype</td>";
                            Echo "<td>$pmain[broker]</td>";
                            Echo "<td>$pmain[sponsor]</td>";
                            Echo "<td>$pmain[visano]</td>";
                            Echo "<td>$pmain[idno]</td>";
                            Echo "<td>$pmain[email]</td>";
                            Echo "<td>$pmain[sector]</td>";
                            Echo "<td style='text-align:right'>$pmain[visaqty]</td>";
                            Echo "<td style='text-align:right'>$pmain[hijridate]</td>";
                            Echo "<td>$paidby</td>";
                            Echo "<td>$pmain[createby]</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"])) {



    $ctr = 1;
    $ctr2 = 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)) {
        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'];
        }
        $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'];

        $paidamt = $cashamt + $bankamt;
        $balamt = $pmain['mofa'] - $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 .= "$pmain[mofa]" . ",";
        $contents .= "$paidamt" . ",";
        $contents .= "$balamt" . "\n";
        $ctr++;

        $totmofa = $totmofa + $pmain['mofa'];
        $totpaidamt = $totpaidamt + $paidamt;
        $totbalamt = $totbalamt + $balamt;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totmofa" . ",";
    $contents .= "$totpaidamt" . ",";
    $contents .= "$totbalamt" . ",";


    $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;
    }
}
?>

