<?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 a.vouchdt ASC")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'];
                            }
                            $qvouch = $pmain['vouchno'];
                            $qyear = $pmain['yearno'];
                            $passport = $pmain['passport'];
                            $qparty = $pmain['agent'];
//                            browse($qvouch);
//                            browse($qyear);
//                            browse($passport);
//                            browse($qparty);


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

                            $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND 
                                                                       `vouchno`='$qvouch' and passport = '$passport'") or die(mysqli_error($conn));

                            $gtrn = mysqli_fetch_object($trn);

                            $msetoff = $gtrn->setoff;
                            $mbalamt = $pmain['memcash'] - $msetoff;
                            $rwclr = ""; 
                            if ($mbalamt > 0) {
                                $rwclr = "style='color:red'";
                            }
                            $ctr++;
                            echo "<tr $rwclr 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 style='text-align:right';>$msetoff</td>";
                            Echo "<td style='text-align:right';>$mbalamt</td>";
                            echo "</tr>";
                            $tmemcash = $tmemcash + $pmain['memcash'];
                            $tsetoff = $tsetoff + $msetoff;
                            $tbalamt = $tbalamt + $mbalamt;
                        }
                        echo "<tr>";
                        Echo "<td colspan='6' class='text-right'><b>Total</b></td>";
                        Echo "<td style='text-align:right'><b>$tmemcash</b></td>";
                        Echo "<td style='text-align:right';><b>$tsetoff</b></td>";
                        Echo "<td style='text-align:right';><b>$tbalamt</b></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'];
        }
        $qvouch = $pmain['vouchno'];
        $qyear = $pmain['yearno'];
        $passport = $pmain['passport'];
        $qparty = $pmain['agent'];
//                            browse($qvouch);
//                            browse($qyear);
//                            browse($passport);
//                            browse($qparty);


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

        $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND 
                                                                       `vouchno`='$qvouch' and passport = '$passport'") or die(mysqli_error($conn));

        $gtrn = mysqli_fetch_object($trn);

        $msetoff = $gtrn->setoff;
        $mbalamt = $pmain['memcash'] - $msetoff;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$pmain[manualno]" . ",";
        $contents .= "$pmain[broker]" . ",";
        $contents .= "$pmain[passport]" . ",";
        $contents .= "$pmain[paxname]" . ",";
        $contents .= "$pmain[mofano]" . ",";
        $contents .= "$pmain[mofano2]" . ",";
        $contents .= "$pmain[memcash]" . ",";
        $contents .= "$msetoff" . ",";
        $contents .= "$mbalamt" . "\n";
        $ctr++;

        $tmemcash = $tmemcash + $pmain['memcash'];
        $tsetoff = $tsetoff + $msetoff;
        $tbalamt = $tbalamt + $mbalamt;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$tmemcash" . ",";
    $contents .= "$tsetoff" . ",";
    $contents .= "$tbalamt" . ",";


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

