<%--
    Document   : LoanRepayment
    Created on : Sep 22, 2016, 1:03:06 PM
    Author     : 986137
--%>

<%@page import="DataEntryBean.LoanRepaymentScheduleBean"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.Connection"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Loan Repayment Schedule</title>
        <script src="${pageContext.request.contextPath}/js/Calendar.js"></script>
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/img/favicon.ico" type="image/x-icon" />
        <script src="${pageContext.request.contextPath}/jquery-ui-1.10.4.custom/development-bundle/jquery-1.10.2.js"></script>
        <link rel="stylesheet" type="text/css" href="/PacsApplication/css/TableCSSCode.css" />
        <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>
    </head>
    <body  onload="noBack()">

        <script type="text/javascript">
            function noBack()
            {
                window.history.forward();
            }

            function popup_LoanAccount(width, height, loanAcc) {
                if (window.innerWidth) {
                    LeftPosition = (window.innerWidth - width) / 2;
                    TopPosition = ((window.innerHeight - height) / 2) - 20;
                }
                else {
                    LeftPosition = (parseInt(window.screen.width) - width) / 2;
                    TopPosition = ((parseInt(window.screen.height) - height) / 2) - 20;
                }
                attr = 'resizable=yes,scrollbars=yes,width=' + width + ',height=' +
                    height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' +
                    TopPosition + '';
                var screenName = "LoanRepayment.jsp";
                var  lovKey="LoanAcctSearch"
                popWin = open('${pageContext.request.contextPath}/CommonSearchInformationLoanLOV.jsp?loanAcc=' + loanAcc + '&screenName=' + screenName+'&lovKey='+lovKey, 'LoanAcctSearch', attr);
                popWin.focus();

            }


            function generateSchedule(LoanRepayScheduleForm)
            {

                var duedt = LoanRepayScheduleForm.dueDate.value;
                duedt = duedt.replace("/","");
                duedt = duedt.substring(0,6);

                if (LoanRepayScheduleForm.loanAcc.value == "")
                {
                    alert("Please enter Loan Account Number.");
                    LoanRepayScheduleForm.loanAcc.focus();
                    return false;
                }
                if (LoanRepayScheduleForm.loanRepYYYYMM.value == "")
                {
                    alert("Please enter Repayment Month.");
                    LoanRepayScheduleForm.loanRepYYYYMM.focus();
                    return false;
                }
                re = /^\d+$/; //this checks whether field contains digits
                if (!re.test(document.getElementById("loanRepYYYYMM").value))
                {
                    alert("Repayment Month could only contain numbers.");
                    return false;
                }

                re = /2[01][1-9][0-9](01|02|03|04|05|06|07|08|09|10|11|12)/; //this checks whether field starts with YYYYMM,accepts all months of Year 2010 upto Year 2199
                if (!re.test(document.getElementById("loanRepYYYYMM").value))
                {
                    alert("Repayment Month should be in YYYYMM format only !!(accepts all months of Year 2010 upto Year 2199)");
                    return false;
                }
                if(LoanRepayScheduleForm.loanRepYYYYMM.value < duedt)
                {
                    alert("Repayment Month should not be the previous date of the Pre Scheduled date.");
                    return false;
                }
                if(LoanRepayScheduleForm.loanRepYYYYMM.value > duedt && parseInt(LoanRepayScheduleForm.counter.value) ==0){
                    var answer = confirm("Loan Repay Date you entered is future date with respect to scheduled repayment date. Do you want to continue? ");
                    if(answer){
                        document.forms[0].handle_id.value = "generate";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        document.forms[0].submit();

                    }
                }
                else if(LoanRepayScheduleForm.loanRepYYYYMM.value > duedt && parseInt(LoanRepayScheduleForm.counter.value)>0){
                    var answer = confirm("Loan Repayment has already registered with this A/c. If you continue previous data will be erased. Do you want to continue? ");
                    if(answer){
                        document.forms[0].handle_id.value = "generate";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        document.forms[0].submit();

                    }
                }
                else if(LoanRepayScheduleForm.loanRepYYYYMM.value == duedt && parseInt(LoanRepayScheduleForm.counter.value)>0){
                    var answer = confirm("Loan Repayment has already registered with this A/c. If you continue previous data will be erased. Do you want to continue? ");
                    if(answer){
                        document.forms[0].handle_id.value = "generate";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        document.forms[0].submit();

                    }
                }
                else if(LoanRepayScheduleForm.loanRepYYYYMM.value == duedt && parseInt(LoanRepayScheduleForm.counter.value) == 0){

                    document.forms[0].handle_id.value = "generate";
                    document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                    document.forms[0].submit();


                }
                else{
                    return false;
                }
            }

            function viewSchedule(LoanRepayScheduleForm)
            {
                document.forms[0].totalRecordCount.value=0;
                var duedt = LoanRepayScheduleForm.dueDate.value;
                duedt = duedt.replace("/","");
                duedt = duedt.substring(0,6);
                //alert(duedt);
                if (LoanRepayScheduleForm.loanAcc.value == "")
                {
                    alert("Please enter Loan Account Number.");
                    LoanRepayScheduleForm.loanAcc.focus();
                    return false;
                }
            <%-- if (LoanRepayScheduleForm.loanRepYYYYMM.value == "")
             {
              alert("!! Please enter Loan loanRepYYYYMM Number !!");
              return false;
            }
             re = /^\d+$/; //this checks whether field contains digits
              if (!re.test(document.getElementById("loanRepYYYYMM").value))
              {
                 alert("!! Repayment Month could only contain numbers !!");
                 return false;
             }

                re = /2[01][1-9][0-9](01|02|03|04|05|06|07|08|09|10|11|12)/; //this checks whether field starts with YYYYMM,accepts all months of Year 2010 upto Year 2199
                if (!re.test(document.getElementById("loanRepYYYYMM").value))
                {
                    alert("!! Repayment Month should be in YYYYMM format only !! (accepts all months of Year 2010 upto Year 2199)");
                    return false;
                }--%>
                        if(LoanRepayScheduleForm.loanRepYYYYMM.value > duedt){
                            var answer = confirm("Loan Repay Date you entered is future date with respect to scheduled repayment date. Do you want to continue? ");
                            if(answer){
                                document.forms[0].handle_id.value = "viewschedule";
                                document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                                document.forms[0].submit();

                            }
                            else{
                                return false;
                            }
                        }
                        document.forms[0].handle_id.value = "viewschedule";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        document.forms[0].submit();

                    }

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

                    function excelDownload() {

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

                    function callNextPage()
                    {
                        var totalRecordCount=document.forms[0].totalRecordCount.value;
                        var recordCount=document.forms[0].recordCount.value;

                        if (totalRecordCount==recordCount){

                            alert("No more record to display");
                            return false;

                        }

                        document.forms[0].but_type.value = 'N';
                        document.forms[0].handle_id.value = "viewschedule";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        document.forms[0].submit();
                    }

                    function callPrevPage()
                    {
                        var totalRecordCount= document.forms[0].totalRecordCount.value;
                        var noOfRecords= document.forms[0].noOfRecords.value;
                        document.forms[0].totalRecordCount.value= Number(totalRecordCount) - Number(noOfRecords)-10;
                        document.forms[0].but_type.value = 'P';
                        document.forms[0].handle_id.value = "viewschedule";
                        document.forms[0].action = '/PacsApplication/LoanRepaymentScheduleServlet';
                        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>

        <jsp:include page="MenuHead_Loan.jsp" flush="true" />
        <br>

        <%
                    int count = 1;
                    String error_msg = "";
                    Object error = request.getAttribute("message");
                    if (error != null) {
                        error_msg = error.toString();
                    }
        %>

        <%
                    String displayFlag = "";
                    Object display = request.getAttribute("displayFlag");
                    if (display != null) {
                        displayFlag = display.toString();
                    }
        %>
        <%
                    String displayFlag1 = "";
                    Object display1 = request.getAttribute("displayFlag1");
                    if (display1 != null) {
                        displayFlag1 = display1.toString();
                    }
        %>

        <%
                    Object alLoanRepaymentScheduleBeanObj = request.getAttribute("alLoanRepaymentScheduleBean");
                    Object oLoanRepaymentScheduleBeanObj = request.getAttribute("oLoanRepaymentScheduleBean");
                    ArrayList<LoanRepaymentScheduleBean> alLoanRepaymentScheduleBean = new ArrayList<LoanRepaymentScheduleBean>();
                    LoanRepaymentScheduleBean oLoanRepaymentScheduleBean = new LoanRepaymentScheduleBean();
                    if (alLoanRepaymentScheduleBeanObj != null) {
                        alLoanRepaymentScheduleBean = (ArrayList<LoanRepaymentScheduleBean>) request.getAttribute("alLoanRepaymentScheduleBean");

                    }

                    if (oLoanRepaymentScheduleBeanObj != null) {
                        oLoanRepaymentScheduleBean = (LoanRepaymentScheduleBean) request.getAttribute("oLoanRepaymentScheduleBean");

                    }

                    session.setAttribute("oLoanRepaymentScheduleBean", oLoanRepaymentScheduleBean);
        %>

        <%
                    Object oLoanRepaymentScheduleBeanObj1 = request.getAttribute("oLoanRepaymentScheduleBean1");
                    LoanRepaymentScheduleBean oLoanRepaymentScheduleBean1 = new LoanRepaymentScheduleBean();


                    if (oLoanRepaymentScheduleBeanObj1 != null) {
                        oLoanRepaymentScheduleBean1 = (LoanRepaymentScheduleBean) request.getAttribute("oLoanRepaymentScheduleBean1");

                    }

                    session.setAttribute("oLoanRepaymentScheduleBean1", oLoanRepaymentScheduleBean1);
        %>

        <%
                    int flag2 = 1;
                    int curPage = 0;
                    try {
                        curPage = Integer.parseInt(request.getAttribute("currentPage").toString());
                    } catch (Exception e) {
                        curPage = 1;
                    }
                    if (alLoanRepaymentScheduleBean.size() > 0) {
                        flag2 = 2;
                    }
                    int totalRecordCount = 0;
                    int recordCount = 0;

                    try {
                        totalRecordCount = Integer.parseInt(request.getAttribute("totalRecordCount").toString());
                        recordCount = Integer.parseInt(request.getAttribute("recordCount").toString());
                    } catch (Exception e) {
                        totalRecordCount = 0;
                        recordCount = 0;
                    }
                    totalRecordCount = totalRecordCount + alLoanRepaymentScheduleBean.size();

        %>

        <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 Repayment Schedule</legend>
            <form name="LoanRepayScheduleForm" id="LoanRepayScheduleForm" action="" method="post">


                <%if (!error_msg.equalsIgnoreCase("")) {%>
                <center><div class="info" id="errorMessage"><center><%= error_msg%></center></div></center>
                <% }%>
                <br>
                <br>


                <center>
                    <table class="dialog">
                        <tr><td style="text-align:right;">Enter Loan Account<font color="red" >*</font>:</td><td></td><td><input type="text" name="loanAcc" id="loanAcc" value="<%=blankNull(oLoanRepaymentScheduleBean.getLoanAcc())%>" style="text-align:right;" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
                                <input type="button" value="Search" onclick="javascript:popup_LoanAccount(420, 180, document.LoanRepayScheduleForm.loanAcc.value);"/></td></tr>
                        <tr></tr><tr></tr><tr></tr>
                        <tr><td>Enter Repayment YYYYMM:</td><td></td><td><input type="text" maxlength="6" name="loanRepYYYYMM" id="loanRepYYYYMM" value="<%=blankNull(oLoanRepaymentScheduleBean.getLoanRepYYYYMM())%>" style="text-align:right;" ></td></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                    </table>
                    <br>
                    <input type="button" value="Generate Repayment Schedule" id="generate" onclick="javascript:generateSchedule(LoanRepayScheduleForm)"/>
                    <input type="button" value="View Schedule" id="view" onclick="javascript:viewSchedule(LoanRepayScheduleForm)"/>
                    <input type="button" value="Reset" id="Reset" onclick="ShowParent();" />

                </center>
                <br>

                <%!
                    String blankNull(String s) {
                        return (s == null) ? "" : s;
                    }
                %>
                <%
                            if (displayFlag1.equalsIgnoreCase("Y")) {
                %>
                <center>
                    <table>
                        <tbody>
                            <tr>
                                <td style="font:large;">Loan Account Number:</td>
                                <td><input type="text" readonly="true" style="text-align:right;" name="loanAccountNumber"  id="loanAccountNumber" style="width: 260px" value="<%=blankNull(oLoanRepaymentScheduleBean1.getLoanAccountNumber())%>" /></td>
                                <td></td>
                                <td style="text-align:right;">CIF Number:</td>
                                <td><input type="text" style="text-align:right;" readonly="true" name="cifNo"  id="cifNo" style="width: 260px" value="<%=blankNull(oLoanRepaymentScheduleBean1.getCifNo())%>" /></td>
                            </tr>

                            <tr>
                                <td style="text-align:right;">Customer Name: </td>
                                <td><input type="text" readonly name="CustomerName"  id="CustomerName" style="width: 260px" value="<%=blankNull(oLoanRepaymentScheduleBean1.getCustomerName())%>" /></td>
                                <td></td>

                                <td>Product Description:</td>
                                <td><input type="text" readonly name="productDesc" style="width: 260px" id="productDesc" value="<%=blankNull(oLoanRepaymentScheduleBean1.getProductDesc())%>" /></td>
                            </tr>
                            <tr>
                                <td style="text-align:right;">Loan Outstanding: </td>
                                <td><input type="text" readonly style="text-align:right;" name="loanOutStd" style="width: 260px" id="loanOutStd" value="<%=blankNull(oLoanRepaymentScheduleBean1.getLoanOutStd())%>"/></td>
                                <td></td>

                                <td>Approved Amount: </td>
                                <td><input type="text" readonly style="text-align:right;" name="apprvedAmt" style="width: 260px" id="apprvedAmt" value="<%=blankNull(oLoanRepaymentScheduleBean1.getApprvedAmt())%>"/></td>
                            </tr>
                            <tr>
                                <td style="text-align:right;">Approved Date: </td>
                                <td><input type="text" readonly style="text-align:center;" name="apprvedDate" style="width: 260px" id="apprvedDate" value="<%=blankNull(oLoanRepaymentScheduleBean1.getApprvedDate())%>"/></td>
                                <td></td>

                                <td style="text-align:right;">Loan Category: </td>
                                <td><input type="text" readonly name="inttCatDesc" style="width: 260px" id="inttCatDesc" value="<%=blankNull(oLoanRepaymentScheduleBean1.getInttCatDesc())%>"/></td>
                            </tr>
                            <tr>
                                <td style="text-align:right;">Loan Term: </td>
                                <td><input type="text" readonly style="text-align:right;" name="loanTerm" style="width: 260px" id="loanTerm" value="<%=blankNull(oLoanRepaymentScheduleBean1.getLoanTerm())%>"/></td>
                                <td></td>

                                <td style="text-align:right;">Due Date:</td>
                                <td><input type="text" readonly style="text-align:center;" name="dueDate" style="width: 260px" id="dueDate" value="<%=blankNull(oLoanRepaymentScheduleBean1.getDueDate())%>"/></td>
                            </tr>
                            <tr><td colspan="5"><hr></td></tr>
                        </tbody>
                    </table></center>
                    <%
                                }
                                if (displayFlag.equalsIgnoreCase("Y")) {
                    %>

                <div id="show" align="left" >
                    <center>
                        <br>
                        <table border="2" width="auto" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                            <thead class="CSSTableGenerator" >
                                <tr>

                                    <td style="width:60px;"><div>Install</div><div>Number</div></td>
                                    <td style="width:100px;"><div>Repayment</div><div>Month(YYYYMM)</div></td>
                                    <td style="width:80px;"><div>EMI</div><div>Amount</div></td>

                                    <td style="width:80px;"><div>Pricipal</div><div>Amount</div></td>
                                    <td style="width:80px;"><div>Interest</div><div>Amount</div></td>
                                    <td style="width:100px;"><div>Pricipal</div><div>Outstanding</div></td>


                                    <td style="width:80px;"><div>Opening</div><div>Principal</div> </td>

                                    <td style="width:80px;"><div>Closing</div><div> Principal</div></td>

                                </tr>
                            </thead>
                            <tbody>

                                <%
                                                                    oLoanRepaymentScheduleBean = new LoanRepaymentScheduleBean();
                                                                    count = 0;

                                                                    for (int i = 0; i < alLoanRepaymentScheduleBean.size(); i++) {
                                                                        oLoanRepaymentScheduleBean = alLoanRepaymentScheduleBean.get(i);

                                                                        count++;

                                %>

                                <tr>
                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getInstallNo())%></td>
                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getRepYYYYMM())%></td>
                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getEmiAmt())%></td>

                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getPrincAmt())%></td>
                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getInttAmt())%></td>
                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getPrincOutStd())%></td>


                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getOpenBal())%></td>

                                    <td style="background-color: #ffffff; text-align: right; "><%=blankNull(oLoanRepaymentScheduleBean.getCloseBal())%></td>

                                </tr>

                                <%}%>
                            </tbody>
                        </table><br><br>

                        <%--For displaying Previous link except for the 1st page --%>

                        <table align="right" colspan="2">
                            <% if (curPage > 1 && flag2 == 2) {%>
                            <td><input type="button"  name="nextPage" value="Previous" onclick="callPrevPage()"><input type="button"  name="nextPage" value="Next" onclick="callNextPage()">
                                <% }%>
                                <% if (flag2 == 2) {%>

                                <% if (curPage == 1) {%>
                            <td><input type="button"  name="nextPage" value="Next" onclick="callNextPage()">
                                <% }
                                                                    }%>
                        </table><br><br>

                    </center>
                    <center><table><tr><td><input type="button" value="Export Result" onclick="javascript:excelDownload()"></td></tr></table></center>
                                    <%} else if (displayFlag.equalsIgnoreCase("N")) {%>

                    <center><h2>Repayment Schedule has not Generated yet.</h2></center>
                    <%}%>

                    <input type="hidden" name="loanTerms" id="loanTerms" value="">
                    <input type="hidden" name="sanctionedAmt" id="sanctionedAmt" value="">
                    <input type="hidden" value="LoanRepayment" name="screenName" />
                    <input name="reportname" id="reportname" type="hidden" value="LOAN_REPAYMENT_SCHEDULE_REPORT" />
                    <input type="hidden" name="page" value="<%=request.getAttribute("currentPage")%>">
                    <input type="hidden" name="but_type" >
                    <input type="hidden" name="totalRecordCount" id="totalRecordCount" value="<%=totalRecordCount%>">
                    <input type="hidden" name="recordCount" id="recordCount" value="<%=recordCount%>">
                    <input type="hidden" name="noOfRecords" id="noOfRecords" value="<%=request.getAttribute("noOfRecords")%>">
                    <input type="hidden" name="dueDate" id="dueDate">
                    <input type="hidden" name="counter" id="counter">
                </div>
                <br>
                <br>

                <input type="hidden" name="handle_id" value="" >

                <script>
                    //make all inputs whole numbers only
                    $('table.dialog').on('keydown','#loanRepYYYYMM', function(event) {
                        if (event.shiftKey == true) {
                            event.preventDefault();
                        }

                        if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 8  || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46) {

                        } else {
                            event.preventDefault();
                        }
                    });

                </script>
            </form>

        </fieldset>
    </body>
</html>

