/*
 * 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 DataEntryBean.PayrollBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;

/**
 *
 * @author Tcs Help desk122
 */
public class PayrollBglMasterServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
       // String Action = request.getParameter("handle_id");
        //String checkOpt = request.getParameter("checkOption");

        PayrollBean oPayrollBean = new PayrollBean();
        ArrayList<PayrollBean> alPayrollBean = new ArrayList<PayrollBean>();
        
        try {
               // BeanUtils.populate(oPayrollBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               System.out.println("Error occurred during processing.");
            }

        if ("Create".equalsIgnoreCase(Action)) {            

            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {

                    String salaryGL = request.getParameter("salaryGL" + i);

                    if (salaryGL != null) {
                        oPayrollBean = new PayrollBean();
                       // oPayrollBean.setGlid(request.getParameter("glid" + i));
                        //oPayrollBean.setTaxid(request.getParameter("taxid" + i));
                        //oPayrollBean.setOtherid(request.getParameter("otherid" + i)); 
                        //oPayrollBean.setStaffcrid(request.getParameter("staffcrid" + i));
                        //oPayrollBean.setSocdrid(request.getParameter("socdrid" + i));
                        //oPayrollBean.setSoccrid(request.getParameter("soccrid" + i));
                       // oPayrollBean.setId(request.getParameter("id" + i));                                                
                        alPayrollBean.add(oPayrollBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }
            if ((alPayrollBean.size() > 0)) {

                try {                    
                    con = DbHandler.getDBConnection();
                    con.setAutoCommit(false);
                    try {
                        proc = con.prepareCall("{ call payroll.BGL_Master(?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                       System.out.println("Error occurred during processing.");
                    }
                    
                    for (int j = 0; j < alPayrollBean.size(); j++) {

                        oPayrollBean = alPayrollBean.get(j);
                        proc.setString(1, oPayrollBean.getId());
                        proc.setString(2, pacsId);
                        proc.setString(3, oPayrollBean.getGlid());
                        proc.setString(4, oPayrollBean.getTaxid());
                        proc.setString(5, oPayrollBean.getOtherid());
                        proc.setString(6, oPayrollBean.getStaffcrid());
                        proc.setString(7, oPayrollBean.getSocdrid());
                        proc.setString(8, oPayrollBean.getSoccrid());
                        proc.setString(9, user);
                        proc.setString(10, Action);
                        proc.registerOutParameter(11, java.sql.Types.VARCHAR);

                        proc.executeUpdate();
                       // message = proc.getString(11);
                        if (!message.contains("Successfully")) {
                            con.rollback();
                            break;
                        }
                    }
                    con.commit();
                } catch (SQLException ex) {
                   System.out.println("Error occurred during processing.");
                } finally {
                    try {
                        proc.close();
                    } catch (SQLException e) {
                       System.out.println("Error occurred during proc close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                       System.out.println("Error occurred during connection close.");
                    }
                }
            }
            request.setAttribute("message", message);
            request.getRequestDispatcher("/Payroll/PayrollBglMaster.jsp").forward(request, response);
        
        } else if ("Amend".equalsIgnoreCase(Action)) {            

            int counter = Integer.parseInt(request.getParameter("rowCounter2"));

            for (int i = 2; i <= counter; i++) {
                try {

                    String salaryGL = request.getParameter("salaryGL" + i);

                    if (salaryGL != null) {
                        oPayrollBean = new PayrollBean();
                       // oPayrollBean.setGlid(request.getParameter("glid" + i));
                        //oPayrollBean.setTaxid(request.getParameter("taxid" + i));
                        //oPayrollBean.setOtherid(request.getParameter("otherid" + i)); 
                        //oPayrollBean.setStaffcrid(request.getParameter("staffcrid" + i));
                        //oPayrollBean.setSocdrid(request.getParameter("socdrid" + i));
                        //oPayrollBean.setSoccrid(request.getParameter("soccrid" + i));
                        //oPayrollBean.setId(request.getParameter("id" + i));                        
                        alPayrollBean.add(oPayrollBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }
            if ((alPayrollBean.size() > 0)) {

                try {                    
                    con = DbHandler.getDBConnection();
                    con.setAutoCommit(false);
                    try {
                        proc = con.prepareCall("{ call payroll.BGL_Master(?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                       System.out.println("Error occurred during processing.");
                    }
                    
                    for (int j = 0; j < alPayrollBean.size(); j++) {

                        oPayrollBean = alPayrollBean.get(j);
                        proc.setString(1, oPayrollBean.getId());
                        proc.setString(2, pacsId);
                        proc.setString(3, oPayrollBean.getGlid());
                        proc.setString(4, oPayrollBean.getTaxid());
                        proc.setString(5, oPayrollBean.getOtherid());
                        proc.setString(6, oPayrollBean.getStaffcrid());
                        proc.setString(7, oPayrollBean.getSocdrid());
                        proc.setString(8, oPayrollBean.getSoccrid());
                        proc.setString(9, user);
                        proc.setString(10, Action);
                        proc.registerOutParameter(11, java.sql.Types.VARCHAR);

                        proc.executeUpdate();
                       // message = proc.getString(11);
                        if (!message.contains("Successfully")) {
                            con.rollback();
                            break;
                        }
                    }
                    con.commit();
                } catch (SQLException ex) {
                   System.out.println("Error occurred during processing.");
                } finally {
                    try {
                        proc.close();
                    } catch (SQLException e) {
                       System.out.println("Error occurred during proc close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                       System.out.println("Error occurred during connection close.");
                    }
                }
            }
            request.setAttribute("message", message);
            request.getRequestDispatcher("/Payroll/PayrollBglMaster.jsp").forward(request, response);
        
        } else if ("getDetails".equalsIgnoreCase(Action)) {
            
            ResultSet resultset = null;
            Statement statement = null;            
            
            Connection connection = DbHandler.getDBConnection();
            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               System.out.println("Error occurred during processing.");
            }
            try {
               //resultset = statement.executeQuery(" select id, pacs_id, salarydebitgl, (select gp.gl_name from gl_product gp where gp.id = salarydebitgl) salarydebitgl_name, ptaxcreditgl, (select gp.gl_name from gl_product gp where gp.id = ptaxcreditgl) ptaxcreditgl_name, otherdeducreditgl, (select gp.gl_name from gl_product gp where gp.id = otherdeducreditgl) otherdeducreditgl_name, epfstaffcreditgl, (select gp.gl_name from gl_product gp where gp.id = epfstaffcreditgl) epfstaffcreditgl_name, epfsocdebitgl, (select gp.gl_name from gl_product gp where gp.id = epfsocdebitgl) epfsocdebitgl_name, epfsoccreditgl, (select gp.gl_name from gl_product gp where gp.id = epfsoccreditgl) epfsoccreditgl_name from payroll_bgl_master k where k.pacs_id = '" + pacsId + "' ");               

                while (resultset.next()) {
                    
                    oPayrollBean = new PayrollBean();
                    oPayrollBean.setId(resultset.getString("id"));
                    oPayrollBean.setGlid(resultset.getString("salarydebitgl"));
                    oPayrollBean.setSalaryGL(resultset.getString("salarydebitgl_name"));
                    oPayrollBean.setTaxid(resultset.getString("ptaxcreditgl"));
                    oPayrollBean.setpTax(resultset.getString("ptaxcreditgl_name"));
                    oPayrollBean.setOtherid(resultset.getString("otherdeducreditgl"));
                    oPayrollBean.setOtherDeduct(resultset.getString("otherdeducreditgl_name"));
                    oPayrollBean.setStaffcrid(resultset.getString("epfstaffcreditgl"));
                    oPayrollBean.setEpfStaffCr(resultset.getString("epfstaffcreditgl_name"));
                    oPayrollBean.setSocdrid(resultset.getString("epfsocdebitgl"));
                    oPayrollBean.setEpfSocDr(resultset.getString("epfsocdebitgl_name"));
                    oPayrollBean.setSoccrid(resultset.getString("epfsoccreditgl"));
                    oPayrollBean.setEpfSocCr(resultset.getString("epfsoccreditgl_name"));
                    
                    alPayrollBean.add(oPayrollBean);
                    request.setAttribute("displayFlag", "Y");
                }
            } catch (SQLException ex) {
               System.out.println("Error occurred during processing.");
            } finally {
                try {
                    statement.close();
                    resultset.close();
                } catch (SQLException e) {
                    System.out.println("Error occurred during proc close.");
                }
                try {
                    connection.close();
                } catch (SQLException ex) {
                   System.out.println("Error occurred during processing.");
                }
            }            
            request.setAttribute("checkOption", checkOpt);
            request.setAttribute("oPayrollBean", oPayrollBean);
            request.setAttribute("arrPayrollBean", alPayrollBean);
            request.getRequestDispatcher("/Payroll/PayrollBglMaster.jsp").forward(request, response);
        }
    }

    @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);
    }

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