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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Submission";
$file = "submission_fr.php";
// $mgrpcode = "010";
$entfile = "entry.php";
$keycond = "";
$keyfld = "myvouch";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
mysqli_query($conn, "set names 'utf8'");

//print table($getpmain);
//die;

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
    $date = date("Y-m-d");

//    browse($count);
    for ($i = 0; $i <= $count; $i++) {


        $myvouch = $_POST["myvouch_$i"];
        $passportno = $_POST["passport_$i"];
        $rejctresn = $_POST["rejctresn_$i"];

        mysqli_query($conn, "UPDATE `visatran` SET `rejctresn` = '$rejctresn' WHERE `myvouch`='$myvouch' and `passport` = '$passportno' ")or die(mysqli_error($conn));
    }
    browse("Record Update Successfully");
    echo "<script>"
    . "window.close();"
    . "</script>";
}


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

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

    $manualno = $_POST["manualno"];
    $visatype = $_POST["visatype"];
    $place = $_POST["place"];
    $visano = $_POST["visano"];
    $passport = $_POST["passport"];
    $partyname = $_POST["partyname"];
    $sponser = $_POST["sponser"];
    $contact = $_POST["contact"];
    $mobile = $_POST["mobile"];
    $agent = $_POST["agent"];
    $stamptype = $_POST["stamptype"];
    $dtype = $_POST["dtype"];

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

    if (!empty($visatype)) {
        $visatypecond = " AND a.`visatype` = '$visatype' ";
    }
    if (!empty($place)) {
        $placecond = " AND a.`sector` = '$place' ";
    }
    if (!empty($agent)) {
        $agentcond = " AND a.`broker` = '$agent' ";
    }
    if (!empty($stamptype)) {
        if ($stamptype == 1) {
            $stamcond = " and `stampdate` != '0000-00-00'";
        } else {
            $stamcond = " and `stampdate` = '0000-00-00'";
        }
    }
    if (empty($_POST["frmdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; submission_sc.php");
    }
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; submission_sc.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.vouchno,a.myvouch, a.manualno, a.broker, a.visano, a.idno, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                         b.vouchno, b.passport, b.paxname, b.mofano, b.mofano2, b.category, b.delvstatus, b.rejctresn,
                          DATE_FORMAT(b.`stampdate`,'%d-%m-%Y') as stampdate, DATE_FORMAT(b.`submission_date`,'%d-%m-%Y') as subdate
                         FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.`submission_date` >= '$mfrmdate' AND b.`submission_date` <= '$mtodate'
                         $visatypecond $agentcond $placecond $stamcond ORDER BY a.broker,a.myvouch")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    </head>
    <!--    <script>
        jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        window.location = $(this).data("href");
    });
    });
    </script>-->
    <style>
        .tblhead > th{
            background-color: #F5F5F5;
            color: #C45254;
            /*width: 10%;*/
        }
        /*        @media print{
                                .pgbrk{
                                    page-break-before: always;
                                }
                }
                * {
                    box-sizing: border-box;
                    -moz-box-sizing: border-box;
                }
                .page {
                    width: 210mm;
                    min-height: 160mm;
                    padding: 4mm;
                    margin: 10mm auto;
                    border: 1px #D3D3D3 solid;
                    border-radius: 5px;
                    background: white;
        
                }
                .subpage {
                    padding: 0cm;
                    border: 1px solid;
                    height: 288mm;
                    outline: 2cm #ffffff solid;
                }
        
                @page {
                    size: A4;
                    margin: 0;
                }
                @media print {
                    html, body {
                        width: 210mm;
                        height: 297mm;
                    }
                    .page {
                        margin: 0;
                        border: initial;
                        border-radius: initial;
                        width: initial;
                        min-height: initial;
                        box-shadow: initial;
                        background: initial;
                        page-break-after: always;
                    }
                }
                .brkpg{
                    page-break-after: always;
                }*/

    </style>
    <body>
        <form action='' method='POST'>

            <div class="container" style="width:80%">
                <?php
                include("includes/head_rep.php");

                if ($stamptype == 2) {
                    ?>
                    <input style="padding-right: 15px;float:right;width:17%;"  type='submit' id="update" name='submit' value='SAVE' class="btn btn-success">

                <?php } ?>

                <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: 5%">File No</th>
                                <th class="" style="width: 9%">Agent</th>
                                <th class="" style="width: 6%">Passport No</th> 
                                <th class="" style="width: 12%">Pax Name</th>
                                <th class="" style="width: 5%">Mofa</th>
                                <th class="" style="width: 7%">Visa No</th>
                                <th class="" style="width: 8%">ID No</th>
                                <th class="" style="width: 5%">Category</th>
                                <th class="" style="width: 4%">DL</th>
                                <th class="" style="width: 4%">MED</th>
                                <th class="" style="width: 4%">Exit</th>
                                <th class="" style="width: 4%">ECR/<br>ECNR</th>
                                <th class="" style="width: 4%">Visa Stamping</th>
                                <th class="" style="width: 6%">Remarks</th>
                                <?php
                                if ($stamptype == 2) {
                                    ?>
                                    <th class="" style="width: 15%">Reject Reason</th>
                                <?php } ?>
                            </tr>
                            <?php
                            $ctr = 1;
                            $ctr2 = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $myvouch = $pmain['myvouch'];

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

                                if ($pmain['mofano2'] > 0) {
                                    $mofa = $pmain['mofano2'];
                                } else {
                                    $mofa = $pmain['mofano'];
                                }
                                $bgcolr = "";
                                if ($pmain['stampdate'] == "00-00-0000") {
                                    $bgcolr = "background-color:#ECEBA3";
                                }

                                if ($pmain['dl'] == 'Y') {
                                    $dl = "YES";
                                } elseif ($pmain['dl'] == 'N') {
                                    $dl = "NO";
                                } else {
                                    $dl = "";
                                }

                                if ($pmain['med'] == 'Y') {
                                    $med = "YES";
                                } elseif ($pmain['med'] == 'N') {
                                    $med = "NO";
                                } else {
                                    $med = "";
                                }

                                if ($pmain['exit'] == 'Y') {
                                    $exit = "YES";
                                } elseif ($pmain['exit'] == 'N') {
                                    $exit = "NO";
                                } else {
                                    $exit = "";
                                }
                                echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                Echo "<td>$ctr</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 class=''>$mofa</td>";
                                Echo "<td class=''>$pmain[visano]</td>";
                                Echo "<td class=''>$pmain[idno]</td>";
                                Echo "<td class=''>$pmain[category]</td>";
                                Echo "<td>$dl</td>";
                                Echo "<td>$med</td>";
                                Echo "<td>$exit</td>";
                                Echo "<td class=''>$dlvstatus</td>";
                                Echo "<td>$pmain[stampdate]</td>";
                                Echo "<td>$pmain[remtran]</td>";
                                if ($stamptype == 2) {
                                    Echo "<td><input type='text' name='rejctresn_$ctr2' class='form-control rejctresn' value='$pmain[rejctresn]' maxlength='30'>"
                                    . "<input type='hidden' name='myvouch_$ctr2' class='form-control passport' value='$pmain[myvouch]'>"
                                    . "<input type='hidden' name='passport_$ctr2' class='form-control passport' value='$pmain[passport]'></td>";
                                }

                                Echo "</tr>";

                                $ctr++;
                                $ctr2++;
                            }
                            ?>
                            <input type = "text" style="display: none;" name = "items" value = "<? Echo $ctr2; ?>">

                        </table>
                    </div>
                </div>
            </div>
        </form>

    </body>
    <?php
}

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

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

    $manualno = $_POST["manualno"];
    $visatype = $_POST["visatype"];
    $place = $_POST["place"];
    $visano = $_POST["visano"];
    $passport = $_POST["passport"];
    $partyname = $_POST["partyname"];
    $sponser = $_POST["sponser"];
    $contact = $_POST["contact"];
    $mobile = $_POST["mobile"];
    $agent = $_POST["agent"];
    $stamptype = $_POST["stamptype"];
    $dtype = $_POST["dtype"];

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

    if (!empty($visatype)) {
        $visatypecond = " AND a.`visatype` = '$visatype' ";
    }
    if (!empty($place)) {
        $placecond = " AND a.`sector` = '$place' ";
    }
    if (!empty($agent)) {
        $agentcond = " AND a.`broker` = '$agent' ";
    }
    if (!empty($stamptype)) {
        if ($stamptype == 1) {
            $stamcond = " and `stampdate` != '0000-00-00'";
        } else {
            $stamcond = " and `stampdate` = '0000-00-00'";
        }
    }
    if (empty($_POST["frmdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; submission_sc.php");
    }
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; submission_sc.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.vouchno,a.myvouch, a.manualno, a.broker, a.visano, a.idno, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                         b.vouchno, b.passport, b.paxname, b.mofano, b.mofano2, b.category, b.delvstatus, 
                          DATE_FORMAT(b.`stampdate`,'%d-%m-%Y') as stampdate, DATE_FORMAT(b.`submission_date`,'%d-%m-%Y') as subdate
                         FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.`submission_date` >= '$mfrmdate' AND b.`submission_date` <= '$mtodate'
                         $visatypecond $agentcond $placecond $stamcond ORDER BY a.broker,a.myvouch")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);

    $contents .= "Sr, File No, Agent, Passport No, Paxname, Mofa, Visa No, ID No, Category, DL, MED, Exit, ECR/ECNR, Visa Stamping, Remarks \n ";

    $ctr = 1;
    $ctr2 = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

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

        if ($pmain['mofano2'] > 0) {
            $mofa = $pmain['mofano2'];
        } else {
            $mofa = $pmain['mofano'];
        }
        if ($pmain['dl'] == 'Y') {
            $dl = "YES";
        } elseif ($pmain['dl'] == 'N') {
            $dl = "NO";
        } else {
            $dl = "";
        }

        if ($pmain['med'] == 'Y') {
            $med = "YES";
        } elseif ($pmain['med'] == 'N') {
            $med = "NO";
        } else {
            $med = "";
        }

        if ($pmain['exit'] == 'Y') {
            $exit = "YES";
        } elseif ($pmain['exit'] == 'N') {
            $exit = "NO";
        } else {
            $exit = "";
        }

        $contents .= $ctr . ",";
        $contents .= $pmain['manualno'] . ",";
        $contents .= $pmain['broker'] . ",";
        $contents .= $pmain['passport'] . ",";
        $contents .= $pmain['paxname'] . ",";
        $contents .= $mofa . ",";
        $contents .= $pmain['visano'] . ",";
        $contents .= $pmain['idno'] . ",";
        $contents .= $pmain['category'] . ",";
        $contents .= $dl . ",";
        $contents .= $med . ",";
        $contents .= $exit . ",";
        $contents .= $dlvstatus . ",";
        $contents .= $pmain['stampdate'] . ",";
        $contents .= $pmain['remtran'] . "\n";
        $ctr++;
    }
    $contents = strip_tags($contents);
    if ($_POST["dtype"] == 1) {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254) . mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        // print $contents;
    } else {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254) . mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        // print $contents;
    }
} 

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

require("library/fpdf/fpdf.php");

class pdf extends fpdf {

    function Footer() {

//Position at 1.5 cm from bottom
        $this->SetY(-15);

//Arial italic 8
        $this->SetFont('Arial', 'I', 8);
//Page number
//        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

}

//browse('udar');

$pdf = new pdf('P', 'mm', 'A4');

$pdf->AliasNbPages();
$pdf->AddPage();

//$pdf->Ln(70);
$pdf->SetFont('Arial', '', 14);
$pdf->SetTextColor(0, 0, 0);
//$pdf->Cell(10, 6, "", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(180, 6, "$comp[name]", 0, 0, 'L');
$pdf->Ln();
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(180, 6, "$comp[add1]", 0, 0, 'L');
$pdf->Ln();
$pdf->Cell(180, 6, "$comp[add2]", 0, 0, 'L');
$pdf->Ln();
$pdf->Cell(180, 6, "$comp[add3]", 0, 0, 'L');
$pdf->Ln(10);

$subject = "Statement:- For the period " . $fdate . ' To ' . $tdate;

$pdf->SetFont('Arial', 'B', 11);
//$pdf->Cell(35, 6, "", 0, 0, 'L');
$pdf->Cell(200, 6, $subject, 0, 0, 'C');
$pdf->Ln(10);
$pdf->Cell(35, 6, "Bill To:", 0, 0, 'L');
$pdf->Cell(80, 6, "", 0, 0, 'L');
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(35, 6, $broker, 0, 0, 'L');
$pdf->Cell(80, 6, "", 0, 0, 'L');
$pdf->Ln();
$pdf->Ln(6);
$pdf->SetFillColor(148,143,227);
$pdf->SetFont('Arial', '', 11);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(10, 6, "Sr", 1,0,'C','R');
$pdf->Cell(25, 6, "Date", 1,0,'C','R');
$pdf->Cell(30, 6, "File No.", 1,0,'C','R');
$pdf->Cell(65, 6, "Pax Name", 1,0,'C','R');
$pdf->Cell(30, 6, "Visa No", 1,0,'C','R');
$pdf->Cell(30, 6, "Passport",1,0,'C','R');
$pdf->SetTextColor(0,0,0);
$pdf->Ln();
$ctr = 0;

while ($pmain = mysqli_fetch_array($mainqry)) {
//    $paxname = $pmain['srno'];
    $ctr++;
    $vouchdt = $pmain['vouchdt'];
    $manualno = $pmain['manualno'];
    $paxname = $pmain['paxname'];
    $visano = $pmain['visano'];
    $passport = $pmain['passport'];

    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(10, 6, "$ctr", 1, 0, 'C');
    $pdf->Cell(25, 6, "$vouchdt", 1, 0, 'C');
    $pdf->Cell(30, 6, "$manualno", 1, 0, 'C');
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(65, 6, "$paxname", 1, 0, 'C');
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(30, 6, "$visano", 1, 0, 'C');
    $pdf->Cell(30, 6, "$passport", 1, 0, 'C');
    $pdf->Ln(6);
}
$pdf->Ln(6);


$tmain = mysqli_fetch_array($tranqry);
$mofa = $tmain['smofa'];
$stamp = $tmain['sstamp'];
$imig = $tmain['simig'];
$mis = $tmain['smis'];
$misc = $tmain['smisc'];
$total = $tmain['stotal'];
$disc = $tmain['sdisc'];
$final = $tmain['stot'];

$rfinal = round($final);
$mfinal = rsword($rfinal);

$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->Cell(20, 6, "Mofa", 0, 0, 'R');
$pdf->Cell(30, 6, $mofa, 1, 0, 'R');
$pdf->Ln();
$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->Cell(20, 6, "Stampping", 0, 0, 'R');
$pdf->Cell(30, 6, $stamp, 1, 0, 'R');
$pdf->Ln();
$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->Cell(20, 6, "Immigration", 0, 0, 'R');
$pdf->Cell(30, 6, $imig, 1, 0, 'R');
$pdf->Ln();
$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->Cell(20, 6, "M/s", 0, 0, 'R');
$pdf->Cell(30, 6, $mis, 1, 0, 'R');
$pdf->Ln();
$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->Cell(20, 6, "Miscellaneous", 0, 0, 'R');
$pdf->Cell(30, 6, $misc, 1, 0, 'R');
$pdf->Ln();
$pdf->Cell(140, 6, "", 0, 0, 'C');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, 6, "Total", 0, 0, 'R');
$pdf->Cell(30, 6, $total, 1, 0, 'R');
$pdf->Ln();
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(52, 6, "INVOICE AMOUNT IN WORDS", 0, 0, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(88, 6, "", 0, 0, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, 6, "Discount", 0, 0, 'R');
$pdf->Cell(30, 6, $disc, 1, 0, 'R');
$pdf->Ln();
//$pdf->MultiCell(52, 6, $mfinal . " Only", '1','L',0);
$pdf->SetFillColor(220,220,220);
$pdf->Cell(130, 6, $mfinal . " Only", 1,0,'L','R');
$pdf->Cell(10, 6, "", 0, 0, 'L');
$pdf->Cell(20, 6, "Final Amount", 0, 0, 'R');
$pdf->Cell(30, 6, $final, 1,0,'R','R');
$pdf->Ln(20);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(130, 6, "", 0, 0, 'L');
$pdf->Cell(60, 6, "For, $comp[name]", 0, 0, 'C');
$pdf->Ln(20);
$pdf->Cell(130, 6, "", 0, 0, 'L');
$pdf->Cell(60, 6, "Authorised Signatory", 0, 0, 'C');


ob_end_clean();
$pdf->Output("Annexure I Normal-" . $getmdata['vouchno'] . ".pdf", "I");
}
?>
