<%--
    Document   : Loan_Report
    Created on : Jul 28, 2016, 9:18:24 PM
    Author     : 986137
--%>

<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="DataEntryBean.EnquiryBean"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.ArrayList" %>
<%@page import="java.lang.String" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList" %>
<% try {
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="${pageContext.request.contextPath}/js/Calendar.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico?pacs=<%=((new java.util.Date()).getTime())%>" type="image/x-icon" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/styles.css?pacs=<%=((new java.util.Date()).getTime())%>"/>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/ButtonAndText.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/info.css?pacs=<%=((new java.util.Date()).getTime())%>" />
        <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/chromejs/chrome.js?pacs=<%=((new java.util.Date()).getTime())%>"></script>
        <script type="text/javascript" src="/PacsApplication/js/iFrame_killer.js"></script>

        <script>

            function downloadFiles() {

                var e = document.getElementById("reportSelection");

                <%--if (document.forms[0].from_date.value == "" &&  (e.options[e.selectedIndex].value == "1" || e.options[e.selectedIndex].value == "2" || e.options[e.selectedIndex].value == "11" || e.options[e.selectedIndex].value == "12"))--%>
                if (document.forms[0].from_date.value == "" )
                {
                    alert("From Date cannot be empty.");
                    document.forms[0].from_date.focus();
                    return false;
                } if (document.forms[0].to_date.value == "" && (e.options[e.selectedIndex].value == "1" || e.options[e.selectedIndex].value == "2" || e.options[e.selectedIndex].value == "11" || e.options[e.selectedIndex].value == "12" || e.options[e.selectedIndex].value == "3" || e.options[e.selectedIndex].value == "4" || e.options[e.selectedIndex].value == "6" 
                    || e.options[e.selectedIndex].value == "7" || e.options[e.selectedIndex].value == "8" || e.options[e.selectedIndex].value == "10" || e.options[e.selectedIndex].value == "13" || e.options[e.selectedIndex].value == "14" || e.options[e.selectedIndex].value == "15" || e.options[e.selectedIndex].value == "17" ))
                {
                    alert("To Date cannot be empty.");
                    document.forms[0].to_date.focus();
                    return false;
                }

                var startDate = $('#from_date').val();
                var endDate = $('#to_date').val();
                var adata = startDate.split('/');
                var dd = parseInt(adata[0],10);
                var mm = parseInt(adata[1],10);
                var yyyy = parseInt(adata[2],10);
                var from_date = new Date(yyyy,mm-1,dd);

                adata = endDate.split('/');
                var dd = parseInt(adata[0],10);
                var mm = parseInt(adata[1],10);
                var yyyy = parseInt(adata[2],10);
                var to_date = new Date(yyyy,mm-1,dd);

               // if(Date.parse(from_date) > Date.parse(to_date) && (e.options[e.selectedIndex].value == "1" || e.options[e.selectedIndex].value == "2" || e.options[e.selectedIndex].value == "11" || e.options[e.selectedIndex].value == "12")){
               if (Date.parse(from_date) > Date.parse(to_date) && (e.options[e.selectedIndex].value == "1" || e.options[e.selectedIndex].value == "2" || e.options[e.selectedIndex].value == "11" || e.options[e.selectedIndex].value == "12" || e.options[e.selectedIndex].value == "3" || e.options[e.selectedIndex].value == "4" || e.options[e.selectedIndex].value == "6"
                   || e.options[e.selectedIndex].value == "7" || e.options[e.selectedIndex].value == "8" || e.options[e.selectedIndex].value == "10" || e.options[e.selectedIndex].value == "13" || e.options[e.selectedIndex].value == "14" || e.options[e.selectedIndex].value == "15" || e.options[e.selectedIndex].value == "17" ))
                {
                    alert("From Date cannot be greater than To Date.");
                    return false; 
                }

                if (e.options[e.selectedIndex].value == "")
                {
                    alert("Report Name cannot be empty.");
                    document.forms[0].reportSelection.focus();
                    return false;
                }
                
                switch (e.options[e.selectedIndex].value) {

                    case "1" :

                        document.forms[0].reportPath.value = "/Reports/Loan balance Report";
                        document.forms[0].reportName.value = "LOAN BALANCE REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "2" :

                        document.forms[0].reportPath.value = "/Reports/NPA CLASSIFICATION REPORT";
                        document.forms[0].reportName.value = "NPA Classification Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "3" :

                        document.forms[0].reportPath.value = "/Reports/GLCC WISE LOAN ACCOUNT REPORT1";
                        document.forms[0].reportName.value = "GLCC WISE LOAN ACCOUNT REPORT1";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "4" :
                        document.forms[0].reportPath.value = "/Reports/CashTxnReportDenoWise";
                        document.forms[0].reportName.value = "Cash txn Report1";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "5" :
                        document.forms[0].reportPath.value = "/Reports/loanDetailList";
                        document.forms[0].reportName.value = "Loan Detail List";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "6" :
                        document.forms[0].reportPath.value = "/Reports/Loan_Return_Report";
                        document.forms[0].reportName.value = "Loan Return Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "7" :

                        document.forms[0].reportPath.value = "/Reports/consolidated_daybook";
                        document.forms[0].reportName.value = "Consolidated Day Book Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "8" :

                        document.forms[0].reportPath.value = "/Reports/loanDemandList";
                        document.forms[0].reportName.value = "Loan Demand List Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "9" :

                        document.forms[0].reportPath.value = "/Reports/loanOverdueDetailList";
                        document.forms[0].reportName.value = "Loan Overdue Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "10" :

                        document.forms[0].reportPath.value = "/Reports/lien_report";
                        document.forms[0].reportName.value = "Loan Lien Details";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "11" :

                        document.forms[0].reportPath.value = "/Reports/LoanRepayReg";
                        document.forms[0].reportName.value = "Loan Repayment Register";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "12" :

                        document.forms[0].reportPath.value = "/Reports/loanDisburseReg";
                        document.forms[0].reportName.value = "Loan Disbursement Register";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "13" :

                        document.forms[0].reportPath.value = "/Reports/consolidated_npa_report";
                        document.forms[0].reportName.value = "Consolidated NPA Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "14" :

                        document.forms[0].reportPath.value = "/Reports/LOAN DUE LIST REPORT";
                        document.forms[0].reportName.value = "LOAN DUE LIST REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "15" :

                        document.forms[0].reportPath.value = "/Reports/GOLD_COLLATERAL";
                        document.forms[0].reportName.value = "GOLD LOAN COLLATERAL REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "16" :

                        document.forms[0].reportPath.value = "/Reports/village_wise_loanDetailList";
                        document.forms[0].reportName.value = "Village Wise Loan Detail List Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "17" :

                        document.forms[0].reportPath.value = "/Reports/consolidated_loanDemandList";
                        document.forms[0].reportName.value = "CONSOLIDATED LOAN DEMAND COLLECTION REPORT";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "18" :

                        document.forms[0].reportPath.value = "/Reports/loan_NPA_DetailList";
                        document.forms[0].reportName.value = "NPA Detail List Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    case "19" :

                        document.forms[0].reportPath.value = "/Reports/loanDetail_Report";
                        document.forms[0].reportName.value = "Loan Detail Report";
                        document.forms[0].DOWNLOAD.value = "P";
                        break;

                    default :

                        null;

                }

                document.forms[0].action = '/PacsApplication/ExcelReportServlet';
                document.forms[0].submit();

            }

              $(document).ready(function () {                
               $('select#reportSelection').change(function () {
                    var shouldEnable = $(this).val();
                    switch(shouldEnable)
                    {
                        case '5':
                            $('#product').show(500);
                            $('#toDt').hide();
                            break;
                        case '9':
                            $('#product').show(500);
                            $('#toDt').hide();
                            break;
                        case '2':
                            $('#product').show(500);
                            $('#toDt').show(500);
                            break;
                        case '15':
                            $('#product').show(500);
                            $('#toDt').show(500);
                            break;
                        case '16':
                            $('#product').show(500);
                            $('#toDt').hide();
                            break;
                        case '18':
                            $('#product').show(500);
                            $('#toDt').hide();
                            break;
                        case '19':
                            $('#product').show(500);
                            $('#toDt').hide();
                            break;
                        default:
                            $('#product').hide();
                            $('#toDt').show(500);                            
                            break;
                    }
                });
            });
            function ShowParent()
            {

                document.forms[0].action = "${pageContext.request.contextPath}/Loan/Loan_Report.jsp";
                document.forms[0].submit();
            }

        // to restrict back button
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function(event) {
                history.pushState(null, null, location.href);
            });
        
        </script>

        <title>Loan Report</title>

    </head>
    <body  onload="noBack();">
        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
                $('#toDt').show(500);
            }
        </script>
        <jsp:include page="MenuHead_Loan.jsp" flush="true" />
        <br>
        <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }

            String blankNA(String s) {
                return (s == null) ? "NA" : s;
            }
        %>
        <%
             String pacsId = (String) session.getAttribute("pacsId");
             String userRole = (String) session.getAttribute("userRole");
             Object oEnquiryBeanSearchObj = request.getAttribute("oEnquiryBeanSearchObj");
             EnquiryBean oEnquiryBean = new EnquiryBean();
             EnquiryBean oEnquiryBeanSearch = new EnquiryBean();
             if (oEnquiryBeanSearchObj != null) {
                 oEnquiryBean = (EnquiryBean) request.getAttribute("oEnquiryBeanSearchObj");
                 session.setAttribute("oEnquiryBeanSearchObj", oEnquiryBeanSearchObj);
             }
        %>
        <%String subPacsFlag = (String) session.getAttribute("subPacsFlag");
        %>
        <div style="top:500px">
            <fieldset style="border-color: black; background-image: url('/PacsApplication/img/download.jpg'); border-width: 3px">
                <legend style="padding: 8px; border-radius: 20px; background-color: #006600; color: white">Loan Report</legend>

                <form name="pacsreport" method="post" action="/PacsApplication/ExcelReportServlet">
                    <br>

                    <table>
                        <%if (request.getAttribute("error") != null) {%>
                        <tr><td colspan="2" align="center"><%=request.getAttribute("error").toString()%></td></tr>
                        <%}%>

                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>

                        <tr>
                            <td style=" font-family: Berlin Sans FB; font-weight: normal;">From Date: </td>
                            <td><input type="text" style="text-align:center;" id="from_date" name="from_date" maxlength="11" resize="yes" readonly="true"> <a href="#" onclick="return getCalendar(document.forms[0].from_date);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" border="0" width="25" height="20" /></a></td>
                        </tr>
                        <tr id="toDt">                        
                            <td style=" font-family: Berlin Sans FB; font-weight: normal;">To Date: </td>
                            <td><input type="text" style="text-align:center;" id="to_date" name="to_date" maxlength="11" resize="yes" readonly="true"> <a href="#" onclick="return getCalendar(document.forms[0].to_date);"><img src="${pageContext.request.contextPath}/img/calendar.jpg" name="imgr" border="0" width="25" height="20" /></a></td>
                        </tr>

                        <tr>
                            <td style=" font-family: Berlin Sans FB; font-weight:normal; ">Report Name <font color="red" >*</font>: </td>
                            <td> <select id="reportSelection" name="reportSelection" >
                            <%--     <% if (userRole.equalsIgnoreCase("201603000008020")) {%>
                                    <option value="" disabled selected style="display:none;" >--Select--</option>
                                    <option value="11">Loan Repayment Register</option> 
                                    <option value="12">Loan Disbursement Register</option>
                                    <%} else{%>   --%>
                                    <option value="" disabled selected style="display:none;" >--Select--</option>
                                    <%--<option value="1">Loan Balance Report</option>--%>
                                    <option value="2">NPA Classification Report</option>
                                    <%--<option value="3">GLCC Wise Loan Report</option>--%>
                                    <option value="4">Cash Transaction Report</option>
                                    <option value="5">Loan Detail List</option>
                                    <option value="6">Loan Return Report</option>
                                    <option value="7">Consolidated Day Book Report</option>
                                    <option value="8">Loan Demand List Report</option>
                                    <option value="9">Loan Overdue Report</option>
                                    <option value="10">Loan Lien Details</option>
                                    <option value="11">Loan Repayment Register</option>
                                    <option value="12">Loan Disbursement Register</option>
                                    <option value="13">Consolidated NPA Report</option>
                                    <option value="14">LOAN DUE LIST REPORT</option>
                                    <option value="15">GOLD LOAN COLLATERAL REPORT</option>
                                    <option value="16">Village Wise Loan Detail List Report</option>
                                    <option value="17">CONSOLIDATED LOAN DEMAND COLLECTION REPORT</option>
                                    <option value="18">NPA Detail List Report</option>
                                    <option value="19">Loan Detail Report</option>
                                    <%--    <%}%>   --%>  
                                </select></td></tr>
                        <tr><td>Sub-PACS Name: </td>
                            <td><select id="subpacs_id" name="subpacs_id" >
                                    <%if (oEnquiryBean.getSubpacs_id() == null) {%>
                                    <option value="" disabled selected style="display:none;">--Select--</option>
                                    <%if (subPacsFlag.equalsIgnoreCase("N")) {%>
                                    <option value="ALL" >ALL Pacs</option>
                                    <option value="<%=pacsId%>">Own Pacs</option>
                                    <% }%>
                                    <%
                                        Connection connection = null;
                                        ResultSet rs = null;
                                        Statement statement = null;
                                        connection = DbHandler.getDBConnection();
                                        try {
                                            statement = connection.createStatement();
                                            rs = statement.executeQuery("select m.pacs_id,m.pacs_name from pacs_master m where m.head_pacs_id = '" + pacsId + "' and m.pacs_id <> '" + pacsId + "'");
                                            if (!rs.isBeforeFirst()) {%>
                                    <option value="<%=pacsId%>" selected>No Item to Select</option>
                                    <%} else {
                                                                                    while (rs.next()) {%>
                                    <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
                                    <%}
                                            }
                                           // statement.close();
                                           // connection.close();
                                        } catch (Exception e) {
                                            System.out.println("Error occurred during processing.");
                                        } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }
                                    } else {%>
                                    <option value="<%=blankNull(oEnquiryBean.getSubpacs_id())%>" selected style="display:none;"><%=blankNull(oEnquiryBean.getSubpacs_name())%></option>
                                    <%
                                        Connection connection = null;
                                        ResultSet rs = null;
                                        Statement statement = null;
                                        connection = DbHandler.getDBConnection();
                                        try {
                                            statement = connection.createStatement();
                                            rs = statement.executeQuery("select m.pacs_id,m.pacs_name from pacs_master m where m.head_pacs_id = '" + pacsId + "' and m.pacs_id <> m.head_pacs_id ");
                                            if (!rs.isBeforeFirst()) {%>
                                    <option value="" selected>No Item to Select</option>
                                    <%} else {
                                                                                    while (rs.next()) {%>
                                    <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
                                    <%}
                                                 }
                                                // statement.close();
                                                // connection.close();
                                             } catch (Exception e) {
                                                System.out.println("Error occurred during processing.");
                                             } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }
                                         }%>
                                </select></td></tr>
                        <tr style="display: none" id="product"><td>Select Product: </td>
                                <td ><select id="productId" name="productId" >

<!--                                    <option value="ALL" selected>ALL Loan Products</option>-->

                                    <%
                                        Connection connection = null;
                                        ResultSet rs = null;
                                        Statement statement = null;
                                        connection = DbHandler.getDBConnection();
                                        try {
                                            statement = connection.createStatement();
                                            rs = statement.executeQuery("select lp.id,lp.prod_code|| ':' || lp.int_cat || ' - '|| lp.prod_name  from loan_product lp where lp.pacs_id like '%" + pacsId + "#%'");
                                            while (rs.next()) {%>
                                            <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
                                    <%}
                                           // statement.close();
                                           // connection.close();
                                        } catch (Exception e) {
                                            System.out.println("Error occurred during processing.");
                                        } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }
                                    %>

                                </select></td>
                        </tr>
                    </table><br><br><br><br>
                    
                    <center>
                        <input type="button" value="Fetch Report" onclick="javascript:downloadFiles();">
                        <input type="button" value="Reset" id="Reset" onclick="ShowParent();" />
                    </center>

                    <input type="hidden" name="reportPath" value=""/>
                    <input type="hidden" name="reportName" value=""/>
                    <input type="hidden" name="DOWNLOAD" value=""/>


                </form>
            </fieldset>
        </div>
    </body>
</html>
<% 	} catch (Exception e) {
                
            }
%>
