<%@ page language="java" session="true" contentType="text/html;charset=ISO-8859-2" %>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="com.etixpert.evolution.app.bonus.*"%>
<%@ page import="org.apache.log4j.*"%>
<%@ page import="java.util.*"%>
<%@ include file="header.jsp" %>
<%@ include file="check.jsp" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/style.css" rel="stylesheet" type="text/css">
<%
	String   userLoggeds    = (String) session.getAttribute("USER_LOGGED_ID");
	Integer  userLogged     = new Integer(userLoggeds);
	Integer  distributorId  = (Integer) session.getAttribute("BONUS_DISTRIBUTOR_INTEGER");
	Calendar firstOpenMonth = BonusList.getFirstOpenMonth();
	Calendar thisMonth      = BonusList.getMonthBegin(Calendar.getInstance().getTime());
	int      dateval        = 0;
	String   details        = (String) session.getAttribute("DETAILS");
	Calendar cDate          = (Calendar) session.getAttribute("BONUS_DATE");
	if (cDate != null) {
		dateval = cDate.get(java.util.Calendar.YEAR) * 12 + 
		          cDate.get(java.util.Calendar.MONTH);
	}	
	StringBuffer temp       = (StringBuffer) session.getAttribute("BONUS_MESSAGE");
	String message          = temp != null ? temp.toString() : null;
	session.setAttribute("BONUS_MESSAGE", null);
%>
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript code hiding
	function simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}

	function link(origLink){
		if (top.mainFrame.document.forms.length == 0) {
			simpleLink(origLink);
			return;
		}
		else {
			if (top.mainFrame.document.forms[0].saveState == null) {
				simpleLink(origLink); 
				return;
			}
			top.mainFrame.document.forms[0].saveState.value = top.mainFrame.self.location.pathname;
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].submit();
		}
	}
	
	function generatePDF(id) {
		open('process/bonus_print.jsp?pageAction=bonus_print&saveState=nothing&distr_id=' + id + '&date=<%= ("" + (dateval % 12 + 101)).substring(1)%>.<%=dateval / 12%>', '_blank', '')
	}
	
<% if (message != null) { %>
	alert('<%=message%>');
<% } %>
// -->
</SCRIPT>
</head>
<body>

<% if (distributorId == null || distributorId.intValue() >= 0) { %>
  <form action="process/bonus.jsp?pageAction=bonus_count&saveState=nothing&link=process/bonus.jsp" method="post">
  <div align="center">
  <table>
    <col width="130"/>
    <col width="100"/>
    <tr>
      <th><fmt:message key="DISTRIBUTOR_ID"/>:</th>
      <td><input type="text" name="distr_id" size="10" value="<%=distributorId != null ? distributorId : userLogged.intValue() < 1000000 ? new Integer(0) : userLogged%>"/></td>
    </tr>
    <tr>
      <th><fmt:message key="DATE"/>:</th>
      <td>
        <select name="date">
        <% 
          	Calendar cal = Calendar.getInstance();
          	int val = cal.get(java.util.Calendar.YEAR) * 12 + cal.get(java.util.Calendar.MONTH);
          	while (val > 2005 * 12 + 4) {
          		out.println("<option" + (dateval == val ? " selected" : "") + ">" +
          				   ("" + (101 + val % 12)).substring(1) + "." + val / 12 + "</option>");
          		val--;
          	}
        %>  
        </select>
      </td>
    </tr>
  </table>  
  <input type="submit" style="width:230px" value="OK"/><br><br>
  <% 
  	boolean distBool = new Integer(0).equals(distributorId) ||
  	new Integer(5).equals(distributorId) ||
  	new Integer(14).equals(distributorId); 
  
    if (cDate != null && distBool) { %>
    <input type="button" value="Erzeuge Bonusdateien (Bank, Buchhaltung)" onclick="form.action='process/bonus.jsp?pageAction=bonus_generate&saveState=nothing&link=process/bonus.jsp';form.submit()"/>
  <% } %>
  <% if (cDate != null && distBool && cDate.equals(firstOpenMonth) && firstOpenMonth.before(thisMonth)) { %>
    <input type="button" value="Bonus akzeptieren" onclick="form.action='process/bonus.jsp?pageAction=bonus_accept&saveState=nothing&link=process/bonus.jsp';form.submit()"/>
  <% } %>
  <% if (cDate != null) { %>
    <input type="hidden" name="details"/>
    <input type="button" value="Details" onclick="form.details.value='1';form.submit()"/>
  <% } %>
  </div>
  </form>
<% } %>
<% if (distributorId != null && cDate != null && "1".equals(details)) { 
	 int cc = BonusList.TYPE_BONUSLIST;
     int idist = distributorId.intValue();
	 if (idist<0) {
		distributorId = new Integer(-idist);
		cc = BonusList.TYPE_BONUSDETAIL;
	 }
	 out.println(BonusList.generate(distributorId, userLogged, cDate.getTime(), cc, pageContext.getServletContext().getRealPath("WEB-INF")));
	 session.setAttribute("BONUS_DISTRIBUTOR_INTEGER", null);
   }
%>
</body>
</html>
