/*
 * TopReportView.java
 *
 * Created on 27 de octubre de 2004, 17:45
 */

package com.fenalco.servlets.supplier;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.Date;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;

import com.fenalco.entities.User;
import com.fenalco.entities.ReportDataAccess;
import com.fenalco.entities.SupplierList;
import com.fenalco.entities.Supplier;
import com.fenalco.entities.ShopKeeperList;
import com.fenalco.entities.ShopKeeper;

import com.fenalco.utilities.MessageBuilder;
import com.fenalco.utilities.lists.SupplierListViewSysManager;
import com.fenalco.utilities.lists.ShopKeeperListViewSysManager;
import com.fenalco.utilities.lists.TopReportListView;

/**
 *
 * @author  Administrador
 * @version
 */
public class TopReportViewSupplier extends HttpServlet {
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Destroys the servlet.
     */
    public void destroy() {
        
    }
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
            String url;
            String message = "";
            HttpSession session = request.getSession(false);
            
	    try
	    {
	        if( session == null || ( User )session.getAttribute( "user" ) == null || ( ( User )session.getAttribute( "user" ) ).getRoleId() != 2 )
			{
				url = "/index.jsp";
				message = MessageBuilder.buildMessage(15);
			}
			else
			{
                                
                                Vector list = new Vector();
                                TopReportListView view = new TopReportListView();
                                String reportTitle;
                                
                                int companyId; 
                                int companyTypeId;
                                int sort = 0;
                                int numRows = 20;
                                int reportType = 1;
                                Date startDate = new Date(0);
                                Date endDate = new Date(0);
                                
                                boolean flag = true;
                                
                                if( request.getParameter("companyTypeId") != null && !request.getParameter("companyTypeId").equals("") )
                                {
                                    
                                    companyTypeId = new Integer( request.getParameter("companyTypeId") ).intValue();
                                    
                                    if( companyTypeId == 1){
                                        
                                        companyId = new Integer( request.getParameter("shopKeeper") ).intValue();
                                        
                                    }else{
                                        
                                        companyId = new Integer( request.getParameter("supplier") ).intValue();
                                        
                                    }
                                    
                                    /*
                                    System.out.println("CompanyType: "+companyTypeId);
                                    System.out.println("CompanyId: "+companyId);
                                    */
                                    
                                    if( !request.getParameter("sort").equals("") ){
                                        
                                        sort = new Integer( request.getParameter("sort") ).intValue();
                                        
                                    }
                                    
                                    if( !request.getParameter("numRows").equals("") ){
                                        
                                        numRows = new Integer( request.getParameter("numRows") ).intValue();
                                        
                                        /*
                                        if(numRows >= 30){
                                            
                                            numRows = 30;
                                            
                                        }
                                        */
                                        
                                    }
                                    
                                    if( !request.getParameter("reportType").equals("") ){
                                        
                                        reportType = new Integer( request.getParameter("reportType") ).intValue();
                                        
                                    }
                                    
                                    
                                    if( !request.getParameter("startDate").equals("") && !request.getParameter("endDate").equals("") )
                                    {
                                        
                                        startDate = Date.valueOf( request.getParameter("startDate") );
                                        endDate = Date.valueOf( request.getParameter("endDate") );
                                        
                                    }
                                /*    
                                    System.out.println("Numrows: "+numRows);
                                    System.out.println("ReportType: "+reportType);
                                    System.out.println("Sort: "+sort);
                                    System.out.println("StartDate :"+startDate);
                                    System.out.println("EndDate :"+endDate);
                                  */  
                                }
                                else
                                {
                                    /*
                                    SupplierList sl = new SupplierList();
                                    Vector aux = sl.getSuppliers();
                                    companyId = ( (Supplier) aux.elementAt(0)).getSupplierId();
                                     */
                                    companyId = ( ( User )session.getAttribute( "user" )).getCompanyId();
                                    companyTypeId = 2;
                                    
                                }
                                
                               
                                list = ReportDataAccess.topReportBuilder( companyId, companyTypeId, sort, numRows, reportType, startDate, endDate );
                                view = new TopReportListView( list );
                                
                                
                                if( reportType == 1){
                                    
                                    if( companyTypeId == 1){
                                        
                                        reportTitle = "PRODUCTOS M&Aacute;S COMPRADOS";
                                        
                                    }else{
                                        
                                        reportTitle = "PRODUCTOS M&Aacute;S VENDIDOS";
                                        
                                    }
                                    
                                }else{  
                                    
                                    if( companyTypeId == 1){
                                        
                                        reportTitle = "COMPRAS POR PROVEEDOR";
                                        
                                    }else{
                                        
                                        reportTitle = "VENTAS POR MINORISTA";
                                        
                                    }
                                }
                                
                                url = "/proveedor/reportestop.jsp";
                                request.setAttribute( "view", view );
                                request.setAttribute( "reportTitle",reportTitle );
                                
                                SupplierList suppliers = new SupplierList();
                                ShopKeeperList shopKeepers = new ShopKeeperList();
                                SupplierListViewSysManager suppliersView = new SupplierListViewSysManager( suppliers );
                                ShopKeeperListViewSysManager shopKeepersView = new ShopKeeperListViewSysManager( shopKeepers );
                                
                                request.setAttribute( "suppliersView", suppliersView );
                                request.setAttribute( "shopKeepersView", shopKeepersView);
                                
                                request.setAttribute( "numRows", new Integer(numRows) );
                                request.setAttribute( "sort", new Integer(sort) );
                                request.setAttribute( "reportType", new Integer(reportType) );
                                request.setAttribute( "companyTypeId", new Integer(companyTypeId) );
                                request.setAttribute( "companyId", new Integer(companyId) );
                                
			}
	    }
	    catch( Exception e )
	    {
                //System.out.println("Error-> "+e);
                e.printStackTrace();
	        url="/logout";
	        message = MessageBuilder.buildMessage(15);
	    }

            request.setAttribute("message", message);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);

    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    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
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    
}
