/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import LoginDb.DbHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Shubhrangshu
 */
public class CashDrawerEnquiryServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet transactionOperationServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet transactionOperationServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);

        Connection connection = null;
        CallableStatement proc = null;

        HttpSession session = request.getSession();
        ResultSet rs = null;
        String pacsid = (String) session.getAttribute("pacsId");
        //String tellerid = (String) request.getParameter("tellerid");
        String adeno2000 = "0";
        String adeno500 = "0";
        String adeno200 = "0";
        String adeno100 = "0";
        String adeno50 = "0";
        String adeno20 = "0";
        String adeno10 = "0";
        String adeno5 = "0";
        String adeno2 = "0";
        String adeno1 = "0";
        String adeno50p = "0";
        String adeno1p = "0";

        String odeno2000 = "0";
        String odeno500 = "0";
        String odeno200 = "0";
        String odeno100 = "0";
        String odeno50 = "0";
        String odeno20 = "0";
        String odeno10 = "0";
        String odeno5 = "0";
        String odeno2 = "0";
        String odeno1 = "0";
        String odeno50p = "0";
        String odeno1p = "0";

        String Hdeno2000 = "0";
        String Hdeno500 = "0";
        String Hdeno200 = "0";
        String Hdeno100 = "0";
        String Hdeno50 = "0";
        String Hdeno20 = "0";
        String Hdeno10 = "0";
        String Hdeno5 = "0";
        String Hdeno2 = "0";
        String Hdeno1 = "0";
        String Hdeno50p = "0";
        String Hdeno1p = "0";

        String open_bal = "0";
        String message = "0";
        String tellerName = "";
        String status = "Not Opened";
        int flag = 0;

        Statement statement = null;
        connection = DbHandler.getDBConnection();
        try {
            statement = connection.createStatement();
            //rs = statement.executeQuery("select teller_id,pacs_id,status,nvl(deno_2000,0),nvl(deno_500,0),nvl(deno_100,0),nvl(deno_50,0),nvl(deno_20,0),nvl(deno_10,0),nvl(deno_5,0),nvl(deno_2,0),nvl(deno_1,0),nvl(OPEN_BALANCE,0),decode(status,'C','Closed','O','Open','Unknown Status'),nvl(DENO_50P,0),nvl(DENO_1P,0),nvl(deno_200,0) from cash_drawer where "
                    + "pacs_id='" + pacsid + "' and teller_id='" + tellerid + "' and entry_date = (select system_date from system_date)");

            while (rs.next()) {
                flag = 1;
                adeno2000 = rs.getString(4);
                adeno500 = rs.getString(5);
                adeno100 = rs.getString(6);
                adeno50 = rs.getString(7);
                adeno20 = rs.getString(8);
                adeno10 = rs.getString(9);
                adeno5 = rs.getString(10);
                adeno2 = rs.getString(11);
                adeno1 = rs.getString(12);
                open_bal = rs.getString(13);
                status = rs.getString(14);
                adeno50p = rs.getString(15);
                adeno1p = rs.getString(16);
                adeno200 = rs.getString(17);
            }
            if (flag == 0) {
                message = "Drawer not yet opened for the teller";
            } else if (flag == 1) {
                message = "";
            }
            request.setAttribute("deno1", adeno1);
            request.setAttribute("deno2", adeno2);
            request.setAttribute("deno5", adeno5);
            request.setAttribute("deno10", adeno10);
            request.setAttribute("deno20", adeno20);
            request.setAttribute("deno50", adeno50);
            request.setAttribute("deno100", adeno100);
            request.setAttribute("deno200", adeno200);
            request.setAttribute("deno500", adeno500);
            request.setAttribute("deno2000", adeno2000);
            request.setAttribute("deno50p", adeno50p);
            request.setAttribute("deno1p", adeno1p);
            request.setAttribute("open_bal", open_bal);
            request.setAttribute("status", status);
            request.setAttribute("message", message);

           // rs = statement.executeQuery("select LOGIN_NAME,LOGIN_ID from login_details where "
                    + "login_id='" + tellerid + "'");
            while (rs.next()) {
                tellerName = rs.getString(1);
            }
            request.setAttribute("tellerName", tellerName);

            //rs = statement.executeQuery("select pacs_id,source,destination,nvl(deno_2000,0),nvl(deno_500,0),nvl(deno_100,0),nvl(deno_50,0),nvl(deno_20,0),nvl(deno_10,0),nvl(deno_5,0),nvl(deno_2,0),nvl(deno_1,0),nvl(deno_50P,0),nvl(deno_1P,0),nvl(deno_200,0) from cash_movement_log where id=(select min(id) from cash_movement_log where source like 'R%' and destination like 'D%' and transaction_date = (select system_date from system_date) and pacs_id='"
                                 + pacsid + "' and teller_to='" + tellerid + "' group by pacs_id,source,destination,transaction_date)");
            while (rs.next()) {
                odeno2000 = rs.getString(4);
                odeno500 = rs.getString(5);
                odeno100 = rs.getString(6);
                odeno50 = rs.getString(7);
                odeno20 = rs.getString(8);
                odeno10 = rs.getString(9);
                odeno5 = rs.getString(10);
                odeno2 = rs.getString(11);
                odeno1 = rs.getString(12);
                odeno50p = rs.getString(13);
                odeno1p = rs.getString(14);
                odeno200 = rs.getString(15);
            }
            request.setAttribute("odeno50p", odeno50p);
            request.setAttribute("odeno1p", odeno1p);
            request.setAttribute("odeno1", odeno1);
            request.setAttribute("odeno2", odeno2);
            request.setAttribute("odeno5", odeno5);
            request.setAttribute("odeno10", odeno10);
            request.setAttribute("odeno20", odeno20);
            request.setAttribute("odeno50", odeno50);
            request.setAttribute("odeno100", odeno100);
            request.setAttribute("odeno200", odeno200);
            request.setAttribute("odeno500", odeno500);
            request.setAttribute("odeno2000", odeno2000);


           // rs = statement.executeQuery("select nvl(sum(nvl(c.deno_2000_in,0) - nvl(c.deno_2000_out,0)),0) as Hdeno_2000,nvl(sum(nvl(c.deno_500_in,0) - nvl(c.deno_500_out,0)),0) as Hdeno_500 ,nvl(sum(nvl(c.deno_100_in,0) - nvl(c.deno_100_out,0)),0) as Hdeno_100 ,nvl(sum(nvl(c.deno_50_in,0) - nvl(c.deno_50_out,0)),0)   as Hdeno_50  ,nvl(sum(nvl(c.deno_20_in,0) - nvl(c.deno_20_out,0)),0) as Hdeno_20,nvl(sum(nvl(c.deno_10_in,0) - nvl(c.deno_10_out,0)),0) as Hdeno_10 ,nvl(sum(nvl(c.deno_5_in,0) - nvl(c.deno_5_out,0)),0) as Hdeno_5 ,nvl(sum(nvl(c.deno_2_in,0) - nvl(c.deno_2_out,0)),0) as Hdeno_2,nvl(sum(nvl(c.deno_1_in,0) - nvl(c.deno_1_out,0)),0) as Hdeno_1,nvl(sum(nvl(c.deno_50p_in,0) - nvl(c.deno_50p_out,0)),0) as Hdeno_50p ,nvl(sum(nvl(c.deno_1p_in,0) - nvl(c.deno_1p_out,0)),0) as Hdeno_1p,nvl(sum(nvl(c.deno_200_in,0) - nvl(c.deno_200_out,0)),0) as Hdeno_200  from cash_txn_hist c where c.status='P' and c.pacs_id='" + pacsid + "' and c.teller_id='"
                    + tellerid + "'");
            while (rs.next()) {
                Hdeno2000 = rs.getString(1);
                Hdeno500 = rs.getString(2);
                Hdeno100 = rs.getString(3);
                Hdeno50 = rs.getString(4);
                Hdeno20 = rs.getString(5);
                Hdeno10 = rs.getString(6);
                Hdeno5 = rs.getString(7);
                Hdeno2 = rs.getString(8);
                Hdeno1 = rs.getString(9);
                Hdeno50p = rs.getString(10);
                Hdeno1p = rs.getString(11);
                Hdeno200 = rs.getString(12);
            }
            request.setAttribute("Hdeno2000", Hdeno2000);
            request.setAttribute("Hdeno500", Hdeno500);
            request.setAttribute("Hdeno200", Hdeno200);
            request.setAttribute("Hdeno100", Hdeno100);
            request.setAttribute("Hdeno50", Hdeno50);
            request.setAttribute("Hdeno20", Hdeno20);
            request.setAttribute("Hdeno10", Hdeno10);
            request.setAttribute("Hdeno5", Hdeno5);
            request.setAttribute("Hdeno2", Hdeno2);
            request.setAttribute("Hdeno1", Hdeno1);
            request.setAttribute("Hdeno50p", Hdeno50p);
            request.setAttribute("Hdeno1p", Hdeno1p);

           // 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.");
            }

            }

        request.getRequestDispatcher("/Deposit/CashDrawerEnquiry.jsp").forward(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
