<%--
    Document   : BatchTransaction
    Created on : Jan 16, 2017, 3:21:24 PM
    Author     : 594267
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.SQLException"%>
<%@page import="LoginDb.DbHandler"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Date"%>
<%@page import="DataEntryBean.BatchProcessingBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.UUID" %>
<%String uuid = UUID.randomUUID().toString();%>
<% try {
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>Batch Transaction</title>
        <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>
        <%
             String handle_idVal = "";
             Object handle_id = request.getAttribute("handle_id");
             if (handle_id != null) {
                 handle_idVal = handle_id.toString();
             }
        %>

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

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

            function checkboxTrue(i){

                if(document.getElementById("chckbox"+i).checked == true){

                    var counter = $("#chkboxCounter").val();
                    counter++;
                    $("#chkboxCounter").val(counter);

                }
                else{

                    var counter = $("#chkboxCounter").val();
                    counter--;
                    $("#chkboxCounter").val(counter);
                }
            }

            function executeform(batchForm){
                if(document.getElementById("batchID").value == "") {
                        alert("Please enter the batch ID to search");
                        return false;
                    }
                document.getElementById("handle_id").value = 'execute';
                document.batchForm.action = '/PacsApplication/BatchTransactionServlet';
                document.batchForm.submit();
            }

            function submitform(batchForm){

                //alert(document.getElementById("handle_id").value);
                if(document.getElementById("handle_id").value == "search"){
                    if(document.getElementById("batchID").value == "") {
                        alert("Please enter the batch ID to search");
                        return false;
                    }
                    document.batchForm.action = '/PacsApplication/BatchTransactionServlet';
                    document.batchForm.submit();
                    return true;
                }
                if(validate()){
                    if(document.getElementById("total").value == "0"){
                        document.batchForm.action = '/PacsApplication/BatchTransactionServlet';
                        document.batchForm.submit();
                    }
                    else {
                        alert("Total Debit should match total credit. Total Amount should be 0.");
                        return false;
                    }
                }
            }

            function validate(){
                var counter = $("#rowCounter").val();
                //var i = 1;
                for(i=1;i<=counter;i++){
                    re = /^(?=.*\d)\d*(?:\.\d*)?$/;

                    try {
                        if (document.getElementById("accNo"+i).value == "")
                        {
                            alert("Account Number cannot be empty");
                            document.getElementById("accNo"+i).value="";
                            document.getElementById("accNo"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                        if (document.getElementById("txnAmt"+i).value == "")
                        {
                            alert("Transaction Amount cannot be empty");
                            document.getElementById("txnAmt"+i).value="";
                            document.getElementById("txnAmt"+i).focus();
                            //resetRow(i);
                            return false;
                        }


                        if (!re.test(document.getElementById("accNo"+i).value))
                        {
                            alert("Account Number could only contain numbers");
                            document.getElementById("accNo"+i).value="";
                            document.getElementById("accNo"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                        if (!re.test(document.getElementById("txnAmt"+i).value))
                        {
                            alert("Transaction amount could only contain numbers");
                            document.getElementById("txnAmt"+i).value="";
                            document.getElementById("txnAmt"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                        if (document.getElementById("txnInd"+i).value == "")
                        {
                            alert("Transaction indicator should be selected");
                            document.getElementById("txnInd"+i).value="CR";
                            document.getElementById("txnInd"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                        if ((document.getElementById("txnNarration"+i).value.trim()) == "")
                        {
                            alert("Transaction Narration should be filled.");
                            document.getElementById("txnNarration"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                        if(document.getElementById("searched").value!="1")
                            {
                                alert("Kindly Search and select the A/c Number.")
                                document.getElementById("accNo"+i).focus;
                                return false;
                            }
                       if (document.getElementById("txnInd"+i).value == "DR" && document.getElementById("accType"+i).value == "L")
                        {
                            alert("Loan cannot be debited from batch transaction");
                            document.getElementById("txnInd"+i).focus();
                            //resetRow(i);
                            return false;
                        }
                    }
                    catch(err){

                    }
                }
                return true;
            }

            function SearchAccount(width, height, textIndex)

            {
                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 accType = document.getElementById("accType"+textIndex).value;
                //var accNo = document.getElementById("accNo"+textIndex).value;
                var screenName = "BatchTransaction.jsp";
                var lovKey = "AccountSearch";

                popWin = open('${pageContext.request.contextPath}/CommonSearchInformationDepositLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey + '&accType=' + accType+ '&accNo=' + accNo + '&textIndex=' + textIndex, 'AccountSearch', attr);
                popWin.focus();

            }

            function setVisibility(){
                //document.getElementById("searchBatch").style.visibility='visible';
                $('select#batchOption').change();
            }

            function textChanged(){
                document.getElementById("searched").value="0";
                //alert("IN");
            }

            $(document).ready(function () {
                $('#create').hide();
                $('#handle_id').val();
                $('select#batchOption').change(function () {
                    var shouldEnable = $(this).val();
                    switch(shouldEnable)
                    {
                        case 'C':
                            $('#create').show(500);
                            $('#searchBatch').hide();
                            $('#handle_id').val('create');
                            break;

                        case 'S':
                            $('#searchBatch').show();
                            $('#create').hide();
                            $('#handle_id').val('search');
                            break;

                        default:
                            $('#create').hide();
                            $('#searchBatch').hide();
                            $('#handle_id').val();

                            break;
                    }
                });
            });


        </script>

        <%
             int count = 1;
             String error_msg = "";

             Object error = request.getAttribute("message");
             if (error != null) {
                 error_msg = error.toString();
             }

             Object alBatchProcessingBeanObj = request.getAttribute("alBatchProcessingBean");
             ArrayList<BatchProcessingBean> alBatchProcessingBean = new ArrayList<BatchProcessingBean>();
             BatchProcessingBean oBatchProcessingBean = new BatchProcessingBean();
             if (alBatchProcessingBeanObj != null) {
                 alBatchProcessingBean = (ArrayList<BatchProcessingBean>) request.getAttribute("alBatchProcessingBean");

             }
             error = request.getAttribute("batchID");
             String batchID = "";
             if (request.getAttribute("batchID") != null) {
                 batchID = error.toString();
             }
        %>


        <%!
            String blankNull(String s) {
                return (s == null) ? "" : s;
            }

        %>
    </head>
    <body  onload="javascript:<%if (alBatchProcessingBeanObj != null) {%>setVisibility();<%}%>noBack();">

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




        <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">BATCH PROCESSING</legend>

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

            <center>
                <label>Select Operation : </label>
                <select id="batchOption" name="batchOption">
                    <option value="" selected >--Select--</option>
                    <option <%if (blankNull(handle_idVal).equalsIgnoreCase("C")) {%> selected <%}%> value="C">Create</option>
                    <option <%if (blankNull(handle_idVal).equalsIgnoreCase("S")) {%> selected <%}%> value="S">Search</option>

                </select>
            </center>


            <form action="" method="post" name="batchForm" id="batchForm">

                <div id="create" name="create">
                    <br>


                    <div align="center">
                        <table id="batchData" name="batchData" border="2" width="auto" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px">
                            <thead class="CSSTableGenerator">
                                <tr>
                                    <td></td>
                                    <td>Account Type</td>
                                    <td>Account Number</td>
                                    <td>Transaction Amount</td>
                                    <td>Transaction Indicator</td>
                                    <td>Narration</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="tRow">
                                    <td><input type="checkbox" name="chckbox<%=count%>" id="chckbox<%=count%>" onclick="checkboxTrue(<%=count%>);" /></td>
                                    <td>
                                        <select  style="width: 100px" name="accType<%=count%>" id="accType<%=count%>" onchange="javascript:resetRow(<%=count%>)">
                                            <option selected value="S">Savings/CA</option>
                                            <option value="R">Recurring Deposit</option>
                                            <option value="G">GL</option>
                                            <option value="L">Loan</option>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text"  style="width: 120px" name="accNo<%=count%>" id="accNo<%=count%>" onkeyup="textChanged()"/>
                                        <input type="button" style="width: 70px" name="search<%=count%>" id="search<%=count%>" value="Search"  onclick="javascript:SearchAccount(550,240,<%=count%>);" /></td>
                                    <td>
                                        <input type="text" name="txnAmt<%=count%>" id="txnAmt<%=count%>" onkeypress='return event.charCode >= 48 && event.charCode <= 57 && event.keyCode = 46' onkeyup="javascript:calcAmt()"/>
                                    </td>
                                    <td>
                                        <select style="width: 150px" name="txnInd<%=count%>" id="txnInd<%=count%>" onchange="javascript:calcAmt();">
                                            <option value="CR" selected>Credit</option>
                                            <option value="DR">Debit</option>
                                        </select>
                                    </td>

                                    <td>
                                       <input type="text" name="txnNarration<%=count%>" id="txnNarration<%=count%>" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <br><br><br>
                        <center>
                            <table>
                                <tr>
                                    <td align="center"><label>Credit Total</label></td>
                                    <td align="center"><label>Debit Total</label></td>
                                    <td align="center"><label>Total</label></td>
                                </tr>
                                <tr>
                                    <td ><input type="text" id="crTotal" name="crTotal" readonly style="font:bold"/></td>
                                    <td ><input type="text" id="drTotal" name="drTotal" readonly style="font:bold"/></td>
                                    <td ><input type="text" id="total" name="total" readonly style="font:bold"/></td>

                                </tr>
                            </table>
                        </center>
                        <br><br><br>
                        <center>
                            <table>
                                <tr>
                                    <td ><input type="button" id="addRow" value="Add Row"/></td>
                                    <td><input type="button" id="deleteRow" value="Delete Row"/></td>
                                    <td><input type="button" value="Submit" onclick="javascript:submitform(batchForm)"></td>
                                    <td><input type="button" value="Reset" onclick="ShowParent();" /></td>
                                </tr>
                            </table>
                        </center>
                    </div>

                    <input type="hidden" name="rowCounterDeleteCreate" id="rowCounterDeleteCreate" value="<%=count%>">
                    <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
                    <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0"/>
                    <input type="hidden" name="all_rowsAdded" id="all_rowsAdded" value="1">
                </div>

                <br>
                <center>
                    <div id="searchBatch" style="display:none">
                        <label>Batch ID: </label>
                        <input type="text" name="batchID" id="batchID" value="<%=blankNull(batchID)%>" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
                        <input type="button" name="searchBatch" id="searchBatch" value="Search" onclick="javascript:submitform(batchForm)"/>
                        <br><br>

                        <%if (handle_idVal.equalsIgnoreCase("S")) {
                                 double crTotal = 0;
                                 double drTotal = 0;
                                 double total = 0;
                        %>

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

                                        <td>Account Type</td>
                                        <td>Account Number</td>
                                        <td>Transaction Amount</td>
                                        <td>Transaction Indicator</td>
                                        <td>Narration</td>
                                        <td>Transaction Status</td>
                                        <td>Transaction Ref No</td>
                                    </tr>
                                </thead>
                                <tbody>
                                    <%
                                        for (int k = 0; k < alBatchProcessingBean.size(); k++) {
                                            oBatchProcessingBean = alBatchProcessingBean.get(k);
                                            if (oBatchProcessingBean.getTxnInd().equalsIgnoreCase("DR")) {
                                                drTotal = drTotal + Double.parseDouble(oBatchProcessingBean.getTxnAmt());
                                            } else if (oBatchProcessingBean.getTxnInd().equalsIgnoreCase("CR")) {
                                                crTotal = crTotal + Double.parseDouble(oBatchProcessingBean.getTxnAmt());
                                            }
                                    %>
                                    <tr class="tRow">

                                        <td>
                                            <select  disabled style="width: 100px" name="accType<%=k%>" id="accType<%=k%>" onchange="javascript:resetRow(<%=count%>)">
                                                <option <%if (oBatchProcessingBean.getAccType().equalsIgnoreCase("S")) {%>selected <%}%> value="S">Savings/CA</option>
                                                <option <%if (oBatchProcessingBean.getAccType().equalsIgnoreCase("G")) {%>selected <%}%> value="G">GL</option>
                                                <option <%if (oBatchProcessingBean.getAccType().equalsIgnoreCase("R")) {%>selected <%}%> value="R">Recurring Deposit</option>
                                                <option <%if (oBatchProcessingBean.getAccType().equalsIgnoreCase("L")) {%>selected <%}%> value="L">Loan</option>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text"  readonly style="width: 120px" name="accNo<%=k%>" id="accNo<%=k%>" value="<%=oBatchProcessingBean.getAccNo()%>" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/> </td>

                                        <td>
                                            <input type="text" readonly name="txnAmt<%=k%>" id="txnAmt<%=k%>" value="<%=oBatchProcessingBean.getTxnAmt()%>"  onkeypress='return event.charCode >= 48 && event.charCode <= 57 && event.keyCode = 46' onkeyup="javascript:calcAmt()"/>
                                        </td>
                                        <td>
                                            <select disabled style="width: 150px" name="txnInd<%=k%>" id="txnInd<%=k%>" onchange="javascript:calcAmt();">
                                                <option <%if (oBatchProcessingBean.getTxnInd().equalsIgnoreCase("CR")) {%>selected <%}%> value="CR">Credit</option>
                                                <option <%if (oBatchProcessingBean.getTxnInd().equalsIgnoreCase("DR")) {%>selected <%}%> value="DR">Debit</option>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text" readonly name="txnNarrationTwo<%=k%>" id="txnNarrationTwo<%=k%>" value="<%=oBatchProcessingBean.getNarration()%>" />
                                        </td>
                                        <td>
                                            <input type="text"  readonly name="txnStatus<%=k%>" id="txnStatus<%=k%>" value="<%=oBatchProcessingBean.getTxnStatus()%>" /> </td>
                                        <td>
                                            <input type="text"  readonly name="txnRefNo<%=k%>" id="txnRefNo<%=k%>" value="<%=oBatchProcessingBean.getTxnRefNo()%>" /> </td>
                                    </tr>
                                    <%}
                                        total = crTotal - drTotal;
                                    %>
                                </tbody>
                            </table>
                            <br><br><br>
                            <center>
                                <table>
                                    <tr>
                                        <td align="center"><label>Credit Total</label></td>
                                        <td align="center"><label>Debit Total</label></td>
                                        <td align="center"><label>Total</label></td>
                                    </tr>
                                    <tr>
                                        <td ><input type="text" id="crTotal" name="crTotal" value="<%=crTotal%>" readonly style="font:bold"/></td>
                                        <td ><input type="text" id="drTotal" name="drTotal" value="<%=drTotal%>" readonly style="font:bold"/></td>
                                        <td ><input type="text" id="total" name="total" value="<%=total%>" readonly style="font:bold"/></td>

                                    </tr>
                                </table>
                            </center>
                            <br><br><br>
                            <center>
                                <table>
                                    <tr>

                                        <td><input type="button" value="Execute" onclick="javascript:executeform(batchForm)"></td>
                                        <td><input type="button" value="Reset" onclick="ShowParent();" /></td>
                                    </tr>
                                </table>
                            </center>
                        </div>

                        <%}%>
                    </div>
                </center>

                <input type="hidden" name="handle_id" id="handle_id" value="" />
                <input type="hidden" name="searched" id="searched" />
            </form>


            <br><br>

            <script  type="text/javascript">
                var x=1;
                $("#addRow").click(function () {
                    if(!validate()){
                        alert('Please fillup one row at a time');
                    } else {
                        document.getElementById("searched").value="0";
                        x=x+1;
                        $("#all_rowsAdded").val(x);
                        var counter = $("#rowCounter").val();
                        counter++;
                        $('#batchData tr:last').after('<tr class="tRow">    <td><input type="checkbox" name="chckbox' + x + '" id="chckbox' + x + '" onclick="checkboxTrue(' + x + ');" /></td>    <td>        <select  style="width: 100px" name="accType' + x + '" id="accType' + x + '" onchange="javascript:resetRow(' + x + ')">            <option selected value="S">Savings/CA</option>    <option value="R">Recurring Deposit</option>        <option value="G">GL</option>      <option value="L">Loan</option>     </select>    </td>    <td>        <input type="text"  style="width: 120px" name="accNo' + x + '" id="accNo' + x + '"/>        <input type="button" style="width: 70px" name="search' + x + '" id="search' + x + '" value="Search"  onclick="javascript:SearchAccount(550,240,' + x + ');" /></td>    <td>        <input type="text" name="txnAmt' + x + '" id="txnAmt' + x + '" onkeypress="return event.charCode >= 48 && event.charCode <= 57  && event.keyCode = 46"  onkeyup="javascript:calcAmt()"/>    </td>    <td>        <select style="width: 150px" name="txnInd' + x + '" id="txnInd' + x + '"  onchange="javascript:calcAmt();">            <option value="CR" selected>Credit</option>            <option value="DR">Debit</option>        </select>    </td><td>        <input type="text" name="txnNarration' + x + '" id="txnNarration' + x + '"/>    </td></tr>');

                        $("#rowCounter").val(counter);
                        counter = $("#rowCounterDeleteCreate").val();
                        counter++;
                        $("#rowCounterDeleteCreate").val(counter);
                    }
                });


                $( "#deleteRow" ).click(function() {

                    var len = ($("#batchData tr").length)-1;
                    var counter = $("#rowCounterDeleteCreate").val();
                    var chkboxCounter=$("#chkboxCounter").val();
                    var counterDelete=counter-chkboxCounter;
                    if (counterDelete==0){
                        alert("All rows cannot be deleted");
                    }
                    else{
                        if (len == 0) {
                            alert("All rows can not be deleted");
                            return false;
                        }

                        $('input:checkbox:checked').closest('tr').remove();
                        $("#rowCounterDeleteCreate").val(counterDelete);

                        chkboxCounter=0;
                        $("#chkboxCounter").val(chkboxCounter);
                        calcAmt();
                        if(isNaN(document.getElementById("grandTotal").value)){
                            document.getElementById("grandTotal").value = 0;
                        }
                    }

                });

                function resetRow(i) {
                    document.getElementById("accNo"+i).value = "";
                    document.getElementById("txnAmt"+i).value = "";
                    document.getElementById("txnInd"+i).value = "CR";
                }

                function calcAmt() {
                    var counter = $("#rowCounter").val();
                    var crTotal = parseFloat(0);
                    var drTotal = parseFloat(0);
                    var total = parseFloat(0);
                    //var i = 1;
                    for(i=1;i<=counter;i++) {
                        try {
                            if(document.getElementById("txnInd"+i).value == "CR")
                                crTotal = crTotal + parseFloat(document.getElementById("txnAmt"+i).value);
                            else if(document.getElementById("txnInd"+i).value == "DR")
                                drTotal = drTotal + parseFloat(document.getElementById("txnAmt"+i).value);
                        }
                        catch(err){
                            total=0;
                        }
                    }
                    if(isNaN(crTotal)){
                        crTotal = parseFloat(0);
                    }
                    if(isNaN(drTotal)){
                        drTotal = parseFloat(0);
                    }
                    total = crTotal - drTotal;
                    document.getElementById("crTotal").value = crTotal;
                    document.getElementById("drTotal").value = drTotal;
                    document.getElementById("total").value = total;
                }



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