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

/**
 *
 * @author 1320035
 */
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DataEntryBean.pdsSalesRegisterHeaderBean;
import DataEntryBean.pdsSalesRegisterDetailsBean;
import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author 1249241
 */
public class pdsSalesRegisterServlet 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 handle_id = request.getParameter("handle_id");
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        pdsSalesRegisterHeaderBean opdsSalesRegisterHeaderBean = null;
        pdsSalesRegisterDetailsBean opdsSalesRegisterDetailsBean = null;
        ResultSet rs = null;
        ArrayList<pdsSalesRegisterDetailsBean> alpdsSalesRegisterDetailsBeanApend = new ArrayList<pdsSalesRegisterDetailsBean>();
        //String toDate = request.getParameter("toDate");
       // String fromDate = request.getParameter("fromDate");
        //String productId = request.getParameter("productIdSearch");
       // String productType = request.getParameter("productTypeSearch");
        //String productSubType = request.getParameter("productSubTypeSearch");
        //String cardNo = request.getParameter("cardNoSearch");
        //String custName = request.getParameter("custNameSearch");
        //String cardNoPk = request.getParameter("cardNoPk");
        String productTypeDetails;
        String productSubTypeDetails;
        opdsSalesRegisterHeaderBean = new pdsSalesRegisterHeaderBean();
        opdsSalesRegisterHeaderBean.setToDate(toDate);
        opdsSalesRegisterHeaderBean.setFromDate(fromDate);
        opdsSalesRegisterHeaderBean.setProductIdSearch(productId);
        opdsSalesRegisterHeaderBean.setProductTypeSearch(productType);
        opdsSalesRegisterHeaderBean.setProductSubTypeSearch(productSubType);
        opdsSalesRegisterHeaderBean.setCardNoSearch(cardNo);
        opdsSalesRegisterHeaderBean.setCustNameSearch(custName);
        opdsSalesRegisterHeaderBean.setCardNoPk(cardNoPk );

        int searchFound = 0;
        try {
            con = DbHandler.getDBConnection();
            proc = con.prepareCall("{ call pds_operations.pds_salesregisterview(?,?,?,?,?,?,?) }");
            proc.setString(1, opdsSalesRegisterHeaderBean.getProductIdSearch());
            proc.setString(2, opdsSalesRegisterHeaderBean.getToDate());
            proc.setString(3, opdsSalesRegisterHeaderBean.getFromDate());
            proc.setString(4, opdsSalesRegisterHeaderBean.getCardNoPk());
            proc.setString(5, pacsId);
            proc.registerOutParameter(6, OracleTypes.CURSOR);
            proc.registerOutParameter(7, java.sql.Types.VARCHAR);
            proc.execute();
            message = proc.getString(7);
           // rs = (ResultSet) proc.getObject(6);

            while (rs.next()) {
                searchFound = 1;
                opdsSalesRegisterDetailsBean = new pdsSalesRegisterDetailsBean();
                productTypeDetails = rs.getString("ITEM_TYPE") + ":" + rs.getString("TYPE_DESC");
                productSubTypeDetails = rs.getString("ITEM_SUBTYPE") + ":" + rs.getString("SUBTYPE_DESC");

                opdsSalesRegisterDetailsBean.setProductType(productTypeDetails);
                opdsSalesRegisterDetailsBean.setProductSubType(productSubTypeDetails);
                opdsSalesRegisterDetailsBean.setSaleRefNo(rs.getString("SALES_REF_NO"));
                opdsSalesRegisterDetailsBean.setName(rs.getString("NAME"));
                opdsSalesRegisterDetailsBean.setCardNo(rs.getString("CARD_NO"));
                opdsSalesRegisterDetailsBean.setQuantity(rs.getString("QTY"));
                opdsSalesRegisterDetailsBean.setUnitSalePrice(rs.getString("UNIT_SP"));
                opdsSalesRegisterDetailsBean.setStockId(rs.getString("STOCK_ID"));
                opdsSalesRegisterDetailsBean.setSaleDate(rs.getString("SALES_DATE"));
                opdsSalesRegisterDetailsBean.setTotal(rs.getString("TOTAL_AMT"));



                alpdsSalesRegisterDetailsBeanApend.add(opdsSalesRegisterDetailsBean);
                request.setAttribute("displayFlag", "Y");
            }

            if (searchFound == 0) {

                message = "No Sales Exist for the selected criteria";
                request.setAttribute("message", message);

            }

        } catch (SQLException ex) {
           // Logger.getLogger(pdsStockRegisterServlet.class.getName()).log(Level.SEVERE, null, ex);
           // System.out.println(ex.toString());
           System.out.println("Error occurred during processing.");
        } finally {

            try {
                proc.close();
                con.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during connection close.");
            }
        }

        request.setAttribute("opdsSalesRegisterHeaderBean", opdsSalesRegisterHeaderBean);
        request.setAttribute("alpdsSalesRegisterDetailsBeanApend", alpdsSalesRegisterDetailsBeanApend);
        request.getRequestDispatcher("/Pds_JSP/pdsSalesRegister.jsp").forward(request, response);



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