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

$maindb = "invmain";
$trandb = "invtran";
//$trandb1 = "category";
$formname = "Passport Amount Received";
$file = "passamtrevd_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"];
$srno_ = $_POST["srno_"];
$vouchdt = $_POST["vouchdt"];
$agent = $_POST["agent"];
$passport_ = $_POST["passport"];
$mofano2 = $_POST["mofano"];
$paxname = $_POST["paxname"];
$type = $_POST["type"];
//$sponser = $_POST["sponser"];
//$contact = $_POST["contact"];
//$mobile = $_POST["mobile"];
$dtype = $_POST["dtype"];
$place = $_POST["sector"];

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


if (!empty($agent)) {
    $agentcond = " AND a.`agent` = '$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));
//
//    browse($mfdt);
$getpmain = mysqli_query($conn, "SELECT a.*  ,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.* FROM `$maindb` a ,
                                    `$trandb` b WHERE a.vouchno = b.vouchno and length (b.`paxname`) > 2 AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` 
                                    <= '$mtodate' $agentcond ORDER BY b.srno")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);
//print table($getpmain);
//die();

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

    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            $totpaidamt = 0;
            $totbalamt = 0;
            $ctr = 0;
            $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: 4%">Sr</th>
                            <th class="" style="width: 10%">Date</th>
                            <th class="" style="width: 15%">Agent</th>
                            <th class="" style="width: 10%">Passport No.</th>
                            <th class="" style="width: 12%">Mofa</th>
                            <th class="" style="width: 15%">Candidate Name</th>
                            <th class="" style="width: 12%; text-align:right">Amount</th>
                            <th class="" style="width: 12%">Recd. Amount</th>
                            <th class="" style="width: 15%">Balance</th> 
                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $myvouch = $pmain['myvouch'];



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

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

                            $findparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$pmain[agent]'") or die(mysqli_error($conn));
                            $lparty = mysqli_fetch_array($findparty);

                            $ctr++;
                            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>$lparty[name]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[mofano]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td style='text-align:right';>$pmain[memcash]</td>";
                            Echo "<td>$pmain[createby]</td>";
                            Echo "<td>$pmain[createby]</td>";
                        }
                        ?>
                    </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[vouchdt]" . ",";
        $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;
    }
}
?>

