<%--
    Document   : pds_stock_register
    Created on : May 5, 2016, 12:31:09 PM
    Author     : 590685
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="DataEntryBean.pdsStockRegisterBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="LoginDb.DbHandler"%>

<% try {
%>
<html>

    <head>
        <title>Stock Register</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>
        <script>

            function stockExpiryEnable(count){

                var x = document.getElementById("perishableFlag"+count).selectedIndex;
                var y = document.getElementById("perishableFlag"+count).options;
                if ((y[x].index) == 1 )
                {

                    document.getElementById("stockExpiryDate"+count).disabled=false;


                }
                else
                {

                    document.getElementById("stockExpiryDate"+count).disabled=true;
                }
            }

            function ShowCalender(i){

                var x = document.getElementById("perishableFlag"+i).selectedIndex;
                var y = document.getElementById("perishableFlag"+i).options;
                if ((y[x].index) == 1 )
                {

                    getCalendar(document.getElementById("stockExpiryDate"+i));


                }
                else
                {

                    return false;
                }

            }

            
            function noBack()
            {
                window.history.forward();
            }
            function submitform(validationform)
            {
                if (checkDiv(validationform))
                {
                    document.forms[0].handle_id.value="enterStock";
                    document.forms[0].action='/PacsApplication/pdsStockRegisterServlet';
                    document.forms[0].submit();
                }
            }
            function searchStock(validationform)
            {
                document.forms[0].handle_id.value="checkStock";
                document.forms[0].action='/PacsApplication/pdsStockRegisterServlet';
                document.forms[0].submit();
            }
            function checkDiv(validationform)
            {
             

                var counter = document.forms[0].rowCounter.value;


                for(var i=1; i<=counter; i++){

                    try{

                        var productType = document.getElementById("productType"+i).value;
                        var productSubType = document.getElementById("productSubType"+i).value;
                        var priceDetails = document.getElementById("price"+i).value;
                        var quantityDetails = document.getElementById("quantity"+i).value;
                        var unitDetails = document.getElementById("unit"+i).value;
                        var perishableFlagDetails= document.getElementById("perishableFlag"+i).value;
                        //alert(document.getElementById("perishableFlag"+i).value);

                        if(productType=="" || productSubType=="" ||priceDetails=="" || quantityDetails=="" || unitDetails==""||perishableFlagDetails==""){

                            alert("All Fields are mandatory");
                            return false;
                        }
                        if (perishableFlagDetails=="Y"){
                            var stockExpiryDate= document.getElementById("stockExpiryDate"+i).value;
                            if(stockExpiryDate==""){

                                alert("All Fields are mandatory");
                                return false;
                            }
                        }

                        var chkdate = document.getElementById("stockExpiryDate" + i).value;
                        var edate = chkdate.split("/");
                        var spdate = new Date();
                        var sdd = spdate.getDate();
                        var smm = spdate.getMonth();
                        var syyyy = spdate.getFullYear()-0;
                        var today = new Date(syyyy,smm,sdd).getTime();
                        var e_date = new Date(edate[2],edate[1]-1,edate[0]).getTime();
                        if(e_date < today)
                        {
                            alert("Please provide appropriate Stock Expiry Date!");
                            return false;
                        }

                    }
                    catch(e){


                    }

                }

                return true;

            }




            function checkop()
            {
                if (document.getElementById("checkOption").value == "enterStock")
                {
                    document.getElementById('enterStock').style.display = 'block';
                    document.getElementById('checkStock').style.display = 'none';
                    document.getElementById('displayDiv').style.display = 'none';
                }
                if (document.getElementById("checkOption").value == "checkStock")
                {
                    document.getElementById('enterStock').style.display = 'none';
                    document.getElementById('checkStock').style.display = 'block';
                    document.getElementById('displayDiv').style.display = 'none';
                }

            }
            function Total(i){

                var priceDetails = document.getElementById("price"+i).value;
                var quantityDetails = document.getElementById("quantity"+i).value;

                document.getElementById("total"+i).value=priceDetails*quantityDetails;

            }
            $('.NumbersOnly').on('keydown','.numbersOn', 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 == 9 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190) {

                } else {
                    event.preventDefault();
                }

                if($(this).val().indexOf('.') !== -1 && event.keyCode == 190)
                { event.preventDefault();}
            });
            function SearchProduct(width, height,i)
            {

                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 = "pds_stock_register.jsp";
                var  lovKey="PdsProductSearch"
                popWin = open('/PacsApplication/CommonSearchInformationPdsLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey+'&textIndex='+i, 'PdsProductSearch', attr);
                popWin.focus();

            }
            function SearchProductSubType(width, height,productTypeSearch,productSubTypeSearch)
            {

                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 = "pds_stock_register.jsp";
                var  lovKey="PdsProductSearch1"
                popWin = open('/PacsApplication/CommonSearchInformationPdsLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey+'&productTypeSearch='+productTypeSearch+'&productSubTypeSearch='+productSubTypeSearch, 'PdsProductSearch1', attr);
                popWin.focus();

            }
            function SearchProductSubType(width, height,productTypeSearch,productSubTypeSearch)
            {

                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 = "pds_stock_register.jsp";
                var  lovKey="PdsProductSearch1"
                popWin = open('/PacsApplication/CommonSearchInformationPdsLOV.jsp?screenName=' + screenName+'&lovKey='+lovKey+'&productTypeSearch='+productTypeSearch+'&productSubTypeSearch='+productSubTypeSearch, 'PdsProductSearch1', attr);
                popWin.focus();

            }
            /*  function expiryDateCheck(i){
                 var perishableFlagDetails= document.getElementById("perishableFlag"+i).value;

if (perishableFlagDetails=="N"){
      $("#stockExpiryDate"+i).val("");
                        }
            }*/


        </script>
    </head>
    <body>

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

        <%
             Object alpdsStockRegisterBeanApendObj = request.getAttribute("alpdsStockRegisterBeanApend");
             ArrayList<pdsStockRegisterBean> alpdsStockRegisterBeanApend = new ArrayList<pdsStockRegisterBean>();
             if (alpdsStockRegisterBeanApendObj != null) {
                 alpdsStockRegisterBeanApend = (ArrayList<pdsStockRegisterBean>) request.getAttribute("alpdsStockRegisterBeanApend");
             }
        %>

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

        <jsp:include page="MenuHead_PDS.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">
                STOCK REGISTER
            </legend>

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

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

                <center><label style=" font-family: Berlin Sans FB; font-weight:  normal;">Operation Selection</label>
                    <select id="checkOption" onchange="checkop()" name="checkOption" >
                        <option value="" disabled selected style="display:none;" >--select--</option>
                        <option value="enterStock" >Enter Stock</option>
                        <option value="checkStock" >Check Stock</option>

                    </select>
                </center>
                <br>

                <div id="enterStock" style="display:none;">
                    <center>
                        <table id="order-list" class="NumbersOnly"  border="2" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px; width: 100%;" >
                            <thead  class="CSSTableGenerator" >
                                <tr>
                                    <td></td>
                                    <td>Product Type</td>
                                    <td  colspan="2">Product Sub Type</td>

                                    <td>Unit</td>
                                    <td>Price/Unit</td>
                                    <td>Quantity</td>
                                    <td>Total</td>
                                    <td style="width: 80px"> Perishable flag</td>
                                    <td>Stock expiry date</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><input type="checkbox" name="chckbox<%=count%>" id="chckbox<%=count%>" onclick="return checkboxTrue(<%=count%>)"/></td>
                                    <td><input size="9" type="text" name="productType<%=count%>" id="productType<%=count%>" readonly="true"/></td>
                                    <td><input size="9" type="text" name="productSubType<%=count%>" id="productSubType<%=count%>" readonly="true"/></td>
                                    <td><input type="button" name="SearchProduct<%=count%>" id="SearchProduct<%=count%>" value="Search"  onclick="SearchProduct(220,180,<%=count%>)" /></td>
                                    <td><input size="5" type="text" name="unit<%=count%>" readonly id="unit<%=count%>" size="15"/></td>
                                    <td><input size="5" type="text"  name="price<%=count%>" class="numbersOn" id="price<%=count%>" class="numbersOn" size="15" onchange="Total(<%=count%>);"/></td>
                                    <td><input size="5" type="text"  name="quantity<%=count%>" class="numbersOn" id="quantity<%=count%>" size="15" class="numbersOn" onchange="Total(<%=count%>);"/></td>
                                    <td><input size="5" type="text" readonly="readonly" name="total<%=count%>" id="total<%=count%>"/></td>
                                    <td><select id="perishableFlag<%=count%>" onchange="stockExpiryEnable(<%=count%>)"  name="perishableFlag<%=count%>" >
                                            <option value=""   disabled selected style="display:none;" >--Please Select--</option>
                                            <option value="Y"  >Yes</option>
                                            <option value="N" >No</option>
                                        </select></td>


                                    <td><input type="text" size="5" disabled="true" name="stockExpiryDate<%=count%>" id="stockExpiryDate<%=count%>"  readonly="readonly"/><a href="#" onclick="return ShowCalender(<%=count%>);"><img src="${pageContext.request.contextPath}/img/calendar.gif" id="imgr<%=count%>" name="imgr<%=count%>" border="0" width="15" height="15" /></a></td>

                                    <td style="display: none"><input type="hidden" name="product_id<%=count%>" id="product_id<%=count%>"/></td>
                                </tr>
                            </tbody>
                        </table>

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


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

                        <tr><td><label style=" font-family: Berlin Sans FB; font-weight:normal; ">All fields are mandatory</label></td></tr>
                        </tbody>
                    </table>
                    <input type="hidden" name="rowCounter" id="rowCounter" value="<%=count%>">
                    <input type="hidden" name="chkboxCounter" id="chkboxCounter" value="0">
                    <input type="hidden" name="rowCounterDelete" id="rowCounterDelete" value="<%=count%>">
                </div>
                <br>


                <div id="checkStock" style="display: none;">
                    <center>
                        <table  cellpadding="5" style="" colspan="15px">
                            <!--     <tr>

                                     <td>Product Type: <input type="text" id="productType" name="productType"/></td>
                                     <td>Product Sub Type: <input type="text" id="productSubType" name="productSubType"/></td>
                                 </tr>-->
                            <tr>
                                <td></td> <td style="text-align: right;"><label style=" font-family: Berlin Sans FB; ">Product Type:</label></td><td><input type="text" name="productTypeSearch" id="productTypeSearch" /></td>
                                <td></td><td><label style=" font-family: Berlin Sans FB; ">Product Sub Type:</label></td><td><input type="text" readonly name="productSubTypeSearch" id="productSubTypeSearch" /></td><td><input type="button" value="Search" id="search" name="search" onclick="SearchProductSubType(220,180,document.getElementById('productTypeSearch').value,document.getElementById('productSubTypeSearch').value)" /></td>
                                <!--/*document.getElementById('productTypeSearch').value,document.getElementById('productSubTypeSearch').value*/-->
                            </tr>

                            <tr>


                                <td></td> <td style="text-align: right;"><label style=" font-family: Berlin Sans FB; ">From Date:</label></td><td><input type="text" size="10" name="fromDate" id="fromDate" readonly="readonly"/><a href="#" onclick="return getCalendar(getElementById('fromDate'));"><img src="${pageContext.request.contextPath}/img/calendar.gif" name="imgr" border="0" width="15" height="15" /></a></td>
                                <td></td><td align="right" ><label style=" font-family: Berlin Sans FB; ">To Date:</label></td><td><input type="text" size="10" name="toDate" id="toDate" readonly="readonly"/><a href="#" onclick="return getCalendar(getElementById('toDate'));"><img src="${pageContext.request.contextPath}/img/calendar.gif" name="imgr" border="0" width="15" height="15" /></a></td>
                            </tr>
                        </table>
                    </center><br>
                    <input type="hidden" name="product_id_search" id="product_id_search" value="" />
                    <center>
                        <input type="button" value="Submit"  onclick="javascript:searchStock(validationform)" >
                    </center>


                </div>
                <%if (displayFlag.equalsIgnoreCase("Y")) {%>
                <div id="displayDiv" >

                    <center>
                        <table id="order-listAmend" border="2" width="auto" cellspacing="1" cellpadding="1" style="text-align: center ;overflow-y: scroll; font-size: 15px;">
                            <thead  class="CSSTableGenerator" >
                                <tr> <td style="text-align:center;"> <b>  Product Details</b></td><td style="text-align:center;"><b>Stock Entry Date</b></td><td style="text-align:center;"><b> Price</b></td><td style="text-align:center;"><b>Base Quantity</b></td><td style="text-align:center;"><b>Quantity Availed</b></td><td style="text-align:center;"><b>Quantity Available</b></td><td style="text-align:center;"><b>Stock Expiry Date</b></td><td style="text-align:center;"><b>Perishable Flag</b></td><td style="text-align:center;"><b>Stock ID</b></td></tr>
                            </thead>
                            <tbody style="background-color: white; font-size: 15px">
                                <%
                                    pdsStockRegisterBean opdsStockRegisterBeanApend = new pdsStockRegisterBean();

                                    for (int i = 0; i < alpdsStockRegisterBeanApend.size(); i++) {
                                        opdsStockRegisterBeanApend = alpdsStockRegisterBeanApend.get(i);
                                %>

                                <tr>

                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getProductDetails())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getStockEntryDate())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getPrice())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getQuantity())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getQuantityAbled())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getQuantityAvailable())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getStockExpiryDate())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getPerishableFlag())%></td>
                                    <td style="text-align:center;"><%=blankNull(opdsStockRegisterBeanApend.getStockId())%></td>



                                </tr>

                                <%}%>
                            </tbody>
                        </table>
                    </center>
                </div>

                <%}%>
                <input type="hidden" name="handle_id" value="" />

            </form>
            <script>



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

                   // var counter = $("#rowCounter").val();
                    counter++;
                    $('#order-list tr:last').after('<tr> <td align="center"><input type="checkbox" name="chckbox'+counter+'" id="chckbox'+counter+'" onclick="return checkboxTrue('+counter+')";/></td><td><input  size="9" type="text" readonly="true" id="productType'+counter+'" name="productType'+counter+'"/></td><td><input  size="9" type="text" id="productSubType'+counter+'" readonly="true" name="productSubType'+counter+'"/></td><td><input type="button" name="SearchProduct'+counter+'" id="SearchProduct'+counter+'" value="Search"  onclick="SearchProduct(220,180,'+counter+')" /></td><td><input type="text"  readonly name="unit'+counter+'" id="unit'+counter+'" size="5" /></td><td><input type="text" class="numbersOn" id="price'+counter+'" size="5" name="price'+counter+'"/></td><td><input type="text" class="numbersOn" id="quantity'+counter+'" size="5" name="quantity'+counter+'" onchange="Total('+counter+')"/></td><td><input type="text" readonly="readonly" id="total'+counter+'"  size="5" name="total'+counter+'"/></td><td><select name="perishableFlag'+counter+'" id="perishableFlag'+counter+'" onchange="stockExpiryEnable('+counter+')"><option value="" disabled selected style="display:none;" >--Please Select--</option><option value="Y">Yes</option><option value="N">No</option></select></td><td><input type="text" disabled="true" name="stockExpiryDate'+counter+'" id="stockExpiryDate'+counter+'"  size="5" readonly="readonly"/><a href="#" onclick="ShowCalender('+counter+');"><img src="${pageContext.request.contextPath}/img/calendar.gif" name="imgr" border="0" width="15" height="15" /></a></td><td style="display: none"><input type="hidden" name="product_id'+counter+'" id="product_id'+counter+'"/></td> </tr>');
                    $("#rowCounter").val(counter);
                    $("#rowCounterDelete").val(counter);

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


                    var len = ($("#order-list tr").length)-1;
                    var counter = $("#rowCounterDelete").val();
                    var chkboxCounter=$("#chkboxCounter").val();
                    counter=counter-chkboxCounter;

                    if (counter==0){

                        alert("All rows cannot be deleted");
                    }
                    else{

                        if (len ==chkboxCounter) {
                            alert("All rows can not be deleted");
                            return false;
                        }

                        $('input:checkbox:checked').closest('tr').remove();
                        $("#rowCounterDelete").val(counter);
                        chkboxCounter=0;
                        $("#chkboxCounter").val(chkboxCounter);

                    }

                });

                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 ShowParent()
                {
                    document.forms[0].action="${pageContext.request.contextPath}/Pds_JSP/pds_stock_register.jsp";
                    document.forms[0].submit();
                }


            </script>
        </fieldset>
    </body>
    <script>
        $('.NumbersOnly').on('keydown','.numbersOn', 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 == 9 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190) {

            } else {
                event.preventDefault();
            }

            if($(this).val().indexOf('.') !== -1 && event.keyCode == 190)
            { event.preventDefault();}
        });
    </script>
</html>
<% 	} catch (Exception e) {
                
            }
%>
