/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DataEntryBean.DepositMiscellaneousBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;
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 javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author 986137
 */
public class MiscChargeDeductionServlet 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 {

        HttpSession session = request.getSession(false);
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");

            Connection con = null;
            CallableStatement proc = null;
            //String txnAmt = request.getParameter("txnAmt");
                    //String narration = request.getParameter("narration");
                   // String type = request.getParameter("methodType");
                   // String fromDt = request.getParameter("fromdate");
                   // String toDt = request.getParameter("todate");
                  //  String prodCode = request.getParameter("prodCode");
                  //  String intCat = request.getParameter("intCat");
                   // String minB = request.getParameter("minBal");
                    String maxCash = request.getParameter("maxCashTra");

              try {
                    con = DbHandler.getDBConnection();

                    proc = con.prepareCall("{ call operations2.post_yearend_charges(?,?,?,?,?,?,?,?,?,?,?,?) }");

                    proc.setString(1, pacsId);
                    proc.setString(2, user);
                    proc.setString(3, txnAmt);
                    proc.setString(4, narration);
                    proc.setString(5, type);
                    proc.setString(6, fromDt);
                    proc.setString(7, toDt);
                    proc.setString(8, prodCode);
                    proc.setString(9, intCat);
                    proc.registerOutParameter(10, java.sql.Types.VARCHAR);
                    proc.registerOutParameter(11, java.sql.Types.VARCHAR);
                    proc.setString(12, minB);
                   // proc.setString(13, maxCash);

                    proc.execute();
                   // message = proc.getString(10);

                    request.setAttribute("message", message);

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

             catch (SQLException ex) {
                // ex.printStackTrace();
                System.out.println("Error occurred during processing.");
                System.out.println("Exception occured  in MiscChargeDeductionServlet for user Id :" + user);
                message= type+" Charge deduction is already done today for Product Code: " +prodCode+ " and Int cat: " +intCat+ ". Check again tomorrow.";

                request.setAttribute("message", message);
                request.getRequestDispatcher("/Deposit/MiscChargeDeduction.jsp").forward(request, response);
            } finally {
                try {
                    if(proc !=null)
                        proc.close();
                    if (con != null)
                    con.close();

            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection 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);
    }

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