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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Pending Invoice";
$file = "pendinv_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.* ,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, a.myvouch as voucherno, b.* FROM `$maindb` a ,
                                    `$trandb` b where a.myvouch = b.myvouch and length(b.`paxname`) > 2 AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` 
                                    <= '$mtodate' and b.passport not in (select passport from invtran) $agentcond ")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 = 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%">File No</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%">Passport No.</th>
                            <th class="" style="width: 2%">Pax Name</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 = "";
                            }
                            

                            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[passport]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td>$pmain[createby]</td>";
                            $ctr++;
                        }
                        
                        ?>
                    </table>

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

if (isset($_POST["excel"])) {
    
            $totpaidamt = 0;
            $totbalamt = 0;
            $ctr = 1;
            $ctr2 = 0;

            $contents .= "Sr., Date, File No., Visa Type, Agent, Sponsor Name, Visa No., ID No., Passport No., Paxname, Created By \n";
                            
                        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 = "";
                            }
                            

                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[mydt]" . ",";
                            $contents .= "$pmain[manualno]" . ",";
                            $contents .= "$visatype" . ",";
                            $contents .= "$pmain[broker]" . ",";
                            $contents .= "$pmain[sponsor]" . ",";
                            $contents .= "$pmain[visano]" . ",";
                            $contents .= "$pmain[idno]" . ",";
                            $contents .= "$pmain[passport]" . ",";
                            $contents .= "$pmain[paxname]" . ",";
                            $contents .= "$pmain[createby]" . "\n";
                            $ctr++;
                        }

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

