/*
 * Created on 2005.07.21.
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.etixpert.evolution.gui.servlets;

import java.util.*;

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

import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.*;

import com.etixpert.evolution.*;
import com.etixpert.evolution.app.statistics.*;

import org.apache.log4j.*;

import java.text.*;


public class StatHelper {

	public static java.sql.Date getNormalDate(String value) throws Exception {
		java.sql.Date date;
		java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, Locale.GERMAN);
		java.util.Date d = df.parse(value);
		date = new java.sql.Date(d.getTime());		
		return date;
	}

	public static java.sql.Date getDate(String value) throws Exception {
		java.sql.Date date;
		java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, Locale.GERMAN);
		String value2 = "";
	
			String year = value.substring(0,4);
			String month = value.substring(4);
		
			value2 = "01."+month+"."+year;
		
			java.util.Date d = df.parse(value2);
			date = new java.sql.Date(d.getTime());
		
			return date;
	}

	public static java.sql.Date getFullDate(String value) throws Exception {
		java.sql.Date date;
		java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, Locale.GERMAN);
		String value2 = "";
	
			String year = value.substring(0,4);
			String month = value.substring(4,6);
			String day = value.substring(6);
		
			value2 = day+"."+month+"."+year;
		
			java.util.Date d = df.parse(value2);
			date = new java.sql.Date(d.getTime());
		
			return date;
	}


	private static String fff(String[] roles) {
		StringBuffer sb = new StringBuffer();
		
		for (int i=0; i<roles.length; i++)
			sb.append(roles[i]+", ");
		
		return sb.toString();
	} 

	public static void printDownline(String id, String orderBy, String[] roles, String[] partner_ids, String countryID, boolean email, boolean email_display, HttpSession session, HttpServletResponse res, String title1, String title2) throws Exception {
		if (roles == null) roles = new String[0];
		org.apache.log4j.Logger logger = MainControl.logger;
		
		if (countryID.equals("all")) countryID= null;
		
		logger.info("PRINT_DOWNLINE-------- id: "+id+", orderBy="+orderBy+", roles: "+fff(roles)+", country_id: "+countryID);
		byte[] data = null;
		try {
			data = DownlineEtiketten.generatePDF(new Integer(id), orderBy, roles, partner_ids, countryID, email, email_display, session.getServletContext().getRealPath("WEB-INF"), title1, title2);
		} catch(Exception e) {
			res.sendRedirect("downline_etiketten.jsp");
			return;
		}
		
		if (data==null) data = new byte[0];
		
		res.setContentType("application/pdf");
		
		res.setHeader("Content-Disposition","attachment; filename=downline_etiketten.pdf");
		
		OutputStream os = res.getOutputStream();
		
		os.write(data);
		
	}




	public static void printTages(String date, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dd = getNormalDate(date);

		org.apache.log4j.Logger logger = MainControl.logger;
		
				
		
		byte[] data = TagesProduktion.generatePDF(dd, session.getServletContext().getRealPath("WEB-INF"));
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/pdf");
		res.setHeader("Content-Disposition","attachment; filename=tages_produktion.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}


	public static void printLiefer(String df, String dt, String id, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		try {
			Integer hjk = new Integer(id);
		} catch (Exception e) {
			res.sendRedirect("lieferschneinsummen.jsp");
			return;
		}
		
		
		byte[] data = Lieferscheinsummen.generatePDF(dfrom, dto, new Integer(id), session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=Lieferscheinsummen.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printDA(String df, String dt, String id, String[] articles, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		try {
			Integer hjk = new Integer(id);
		} catch (Exception e) {
			res.sendRedirect("downline_article.jsp");
			return;
		}
		
		byte[] data = DownlineArticle.generatePDF(dfrom, dto, new Integer(id), articles, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=downline_artikel.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printBDA(String df, String dt, String[] articles, Integer rootID, String[] roles, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = BestDistributorArticles.generatePDF(dfrom, dto, articles, rootID , roles, session.getServletContext().getRealPath("WEB-INF"));
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=top_verkaufer_per_artikel.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}	
	
	public static void printDWO(String df, String dt, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = MembersWithoutOrders.generatePDF(dfrom, dto, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=distr_ohne_bestellungen.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printNM(String df, String dt, String orderBy, Integer rootID, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = NewMember.generatePDF(dfrom, dto, new Integer(orderBy).intValue(), rootID, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=neue_berater.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printTT(String df, String dt, String orderBy, String[] roles, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = TopTenMembers.generatePDF(dfrom, dto, new Integer(orderBy).intValue(), roles, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=distributoren_ranking.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}
	
	public static void printBL(String month, String orderBy, HttpSession session, HttpServletResponse res) throws Exception {
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = BirthdayList.generatePDF(new Integer(month).intValue(), new Integer(orderBy).intValue(), session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=geburtstagsliste.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}
	
	
	
	public static void printTA(String df, String dt, String downline, String downline_name, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = TopArticles.generatePDF(dfrom, dto, downline, downline_name, session.getServletContext().getRealPath("WEB-INF"));
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=artikel_ranking.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printTA_OP(String df, String dt, String downline, String downline_name, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		org.apache.log4j.Logger logger = MainControl.logger;
		
		byte[] data = TopArticlesOP.generatePDF(dfrom, dto, downline, downline_name, session.getServletContext().getRealPath("WEB-INF"));
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition","attachment; filename=artikel_ranking_ek.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}
	
	public static void printUmsatz1(String df, String dt, String[] roles, String id, String country, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		java.sql.Date dto = getNormalDate(dt);
		
		if (country.equals("null")) country = null;
		
		try {
			Integer hjk = new Integer(id);
		} catch (Exception e) {
			res.sendRedirect("umsatzproduktion.jsp");
			return;
		}
		
		byte[] data = Umsatzproduktion.generatePDF(dfrom, dto, roles, new Integer(id), country, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/pdf");
		res.setHeader("Content-Disposition","attachment; filename=Umsatzproduktion.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printUmsatz2(String df, String countryID, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getDate(df);
		
		if (countryID.equals("null")) countryID = null;
		
		byte[] data = Umsatzsteuervoranmeldung.generatePDF(dfrom, countryID, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/pdf");
		res.setHeader("Content-Disposition","attachment; filename=Umsatzsteuervoranmeldung.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}

	public static void printNQ(String df, String levelID, HttpSession session, HttpServletResponse res) throws Exception {
		java.sql.Date dfrom = getNormalDate(df);
		
		Integer ilevelID = null;
		
		try {
			if (levelID.equals("null")) ilevelID = null;
			else ilevelID = new Integer(levelID);
		} catch (Exception e) {
			return;
		}
		
		byte[] data = NewQualification.generatePDF(dfrom, ilevelID, session.getServletContext().getRealPath("WEB-INF"));
		
		
		
		//byte[] bdata = data.getBytes(data.length(), 1);
		res.setContentType("application/pdf");
		res.setHeader("Content-Disposition","attachment; filename=Neuqualifikation.pdf"); 
		OutputStream os = res.getOutputStream();
		os.write(data);
	}
}
