<%@ include file="header.jsp"%>
<%@ page buffer="32kb" %>
<%@ page  errorPage="errorPage.jsp" language="java" session="true" contentType="text/html;charset=ISO-8859-2" %>


<%@ page import="java.math.*"%>
<%@ include file="header.jsp" %>
<%@ page pageEncoding="UTF-8"%>
<%@ include file="check.jsp" %>
<%!
	String generateCheckBoxGroup(CheckBox[] group, String caption)  throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("<caption><b>").append(caption).append("</b></caption>\n");
		sb.append("<tr><td>\n");
		if (group != null) {
		 for (int i=0; i<group.length; i++) {
		 	sb.append(group[i].toHTML()).append("</br>");
		 }
		}
		sb.append("</td></tr>\n");
		return sb.toString();

	}

%>
<%
  String orderStatus = (String) session.getAttribute("orderstatus");
  String jsp = "list_orders.jsp"+orderStatus;
  Map m = (Map) session.getAttribute("list_orders.jsp"+orderStatus);
  String search = request.getParameter("search");
  boolean searchEnabled = true;
  if (search!=null && search.equalsIgnoreCase("false")) searchEnabled=false;

  String pid = (String) session.getAttribute("USER_LOGGED_ID");
  int statusFrom = Integer.valueOf(orderStatus).intValue();
  boolean displayBill = statusFrom>=50;
  boolean displayExtendedDateInput = (statusFrom == 60 || statusFrom == 90);
  boolean pdfPrint = (statusFrom == 50 || statusFrom == 60);
  Integer personid = Integer.valueOf(pid);
  Session hsession = HibernateUtil.currentSession();
  Transaction tx = hsession.beginTransaction();
  Connection conn = hsession.connection();
  PersistableObject orderStatusPO = new PersistableObject("order_status", "id");
  orderStatusPO.set("id", new Integer(statusFrom));
  orderStatusPO.load(conn);

  boolean canPaymentBeSet = ((Boolean) orderStatusPO.get("payment_state")).booleanValue();
  String nextProcess = orderStatusPO.getString("next_process","");
  String statusName = orderStatusPO.getString("descr", "");

  Distributor person = new Distributor(personid.intValue());
  person.load(conn);
  int levelId = ((Integer) person.get("level_id")).intValue();
  boolean admin = levelId <100;
  //getting payment right
  PersistableObject generalRightsPO = new PersistableObject("general_rights", "role_id");
  generalRightsPO.set("role_id", person.get("level_id"));
  generalRightsPO.load(conn);
  String rightToChangePayment = generalRightsPO.getString("change_payment_status","x");
  
  boolean canChangePayment = rightToChangePayment.equalsIgnoreCase("x") || rightToChangePayment.equals("")?false:true;
  boolean timeToChangeBonus = OrderListHelper.showChangeBonusDate(conn,generalRightsPO, orderStatusPO);
  

  //getting status rights
  PersistableObject orderStatusRight = new PersistableObject("order_status_rights", new String[]{"role_id", "order_status_id"});
  orderStatusRight.set("role_id", person.get("level_id"));
  orderStatusRight.set("order_status_id", new Integer(statusFrom));
  orderStatusRight.load(conn);
  String rightToNextProc = orderStatusRight.getString("nextproc","x");
  boolean canNextProc = (rightToNextProc.equalsIgnoreCase("x") || rightToNextProc.equals("")) ?false:true;
  String rightToChangeStr =	orderStatusRight.getString("change","");
  boolean canChange = (rightToChangeStr.equalsIgnoreCase("x") || rightToChangeStr.equals(""))?false:true;
  String rightToView = orderStatusRight.getString("view","x");
  String rightToCancel = orderStatusRight.getString("storno","x");
  boolean canCancel = (rightToCancel.equalsIgnoreCase("x") || rightToCancel.equals("")) ?false:true;
  

  Integer INextStatus = OrderListHelper.nextOrderStatus(conn, statusFrom);
  String nextStatus = "";
  if (INextStatus!=null) nextStatus = INextStatus.toString();

  if (rightToView.equalsIgnoreCase("x") || rightToView.equals("")) {
  	tx.commit();
  	HibernateUtil.closeSession();
  	response.sendRedirect("ShowMessage.jsp?msg="+URLEncoder.encode("keine Berechtigung f&uuml;r diese Seite", "ISO-8859-2"));
  	return;
  }

   Set passedOrders = null; if (m!=null) passedOrders = (Set) m.get("passed_orders");
   Set failedOrders = null; if (m!=null) failedOrders = (Set) m.get("failed_orders");
   CheckBox[] countries = CheckBox.generateFromLookup(conn, session, jsp, "country", "country_code", "country_code", "name", true);
   CheckBox[] payments = null;
   if (!orderStatus.equals("20"))
    payments = CheckBox.generateFromLookup(conn, session, jsp, "payment_method", "payment_method_id", "id", "descr", true);
   else
    payments = CheckBox.generateSpecial(conn, session, jsp, true);

   Input name = new Input(session, jsp, "lastname", 40); name.setProperty("id", "medium2");

   //order date
   Input from =  new Input(session, jsp, "from", 10); from.setProperty("id", "medium1");
   Input to = new Input(session, jsp, "to", 10); to.setProperty("id", "medium1");

   //billing date
   Input rfrom =  new Input(session, jsp, "rfrom", 10); rfrom.setProperty("id", "medium1");
   Input rto = new Input(session, jsp, "rto", 10); rto.setProperty("id", "medium1");

   //bonus date
   Input bfrom =  new Input(session, jsp, "bfrom", 10); bfrom.setProperty("id", "medium1");
   Input bto = new Input(session, jsp, "bto", 10); bto.setProperty("id", "medium1");



   CheckBox today = new CheckBox(session, jsp, "today", "on", "", false);
   today.addProperty("onClick","todayOnClick();");

   CheckBox rtoday = new CheckBox(session, jsp, "rtoday", "on", "", false);
   rtoday.addProperty("onClick","rtodayOnClick();");

   CheckBox btoday = new CheckBox(session, jsp, "btoday", "on", "", false);
   btoday.addProperty("onClick","btodayOnClick();");

   /*LookupSelect countries = new LookupSelect(session, jsp, "country_code", "country", "country_code", "name");
   countries.setEmptyRow(true);
   countries.setConnection(conn);*/
   Input orderId = new Input(session, jsp, "orderid", 10); orderId.setProperty("id", "medium1");
   Input billId = new Input(session, jsp, "billid", 10); billId.setProperty("id", "medium1");

   Map boolMap = new HashMap();
   boolMap.put("true","YES");
   boolMap.put("false","NO");

   MapSelect payed = new MapSelect(session, jsp, "payment_status", boolMap);
   payed.setEmptyRow(true);
   payed.setLocalize(true);
   payed.setPageContext(pageContext);


   Map orderByMap = new HashMap();

   orderByMap.put("order_date", "ORDER_DATE");
   orderByMap.put("d.id", "DISTRIBUTOR_NR");
   orderByMap.put("d.lastname", "LASTNAME");
   orderByMap.put("o.id", "ORDER_NR");



   MapSelect orderBySelect= new MapSelect(session, jsp, "order_by", orderByMap,"o.id");
   orderBySelect.setPageContext(pageContext);
   orderBySelect.setLocalize(true);

   CheckBox old = new CheckBox(session, jsp, "old", "on", "OLD_AUSTRIAN_DISTRIBUTORS", false);
   old.setPageContext(pageContext);
   old.setLocalize(true);

   CheckBox creditNotes = new CheckBox(session, jsp, "credit_notes", "on", "CREDIT_NOTES", false);
   creditNotes.setPageContext(pageContext);
   creditNotes.setLocalize(true);
   
   CheckBox custDisc = new CheckBox(session, jsp, "cust_disc", "on", "CUSTOMER_DISCOUNT", false);
   custDisc.setPageContext(pageContext);
   custDisc.setLocalize(true);   

   String[] countrs = OrderListHelper.getVal(m, "country_code");
   String[] pays = OrderListHelper.getVal(m, "payment_method_id");

	String onChange= "link('process/list_orders.jsp?status="+orderStatus+"','orderSearch','');";
	Map events =  new HashMap();
	//events.put("onblur",onChange);
	//FormElement.addProperties(new FormElement[]{name, from, to}, events);
   List result = null;
   if (searchEnabled)
	   result = OrderListHelper.search(conn, rightToView, personid, name.getValue(), countrs, pays, orderId.getValue(), billId.getValue(), orderStatus, payed.getValue(),
            					from.getValue(), to.getValue(), rfrom.getValue(), rto.getValue(), bfrom.getValue(), bto.getValue(), old.getValue(), orderBySelect.getValue(), creditNotes.getValue(), custDisc.getValue());




  tx.commit();
  HibernateUtil.closeSession();
%>
<html>
<head>
 <% if (session.getAttribute("forwardlink")!=null) { %>
  <meta http-equiv="refresh" content="1;URL=<%= session.getAttribute("forwardlink") %>">
 <% } else { %>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8">
 <% } %>
<link href="main.css" rel="stylesheet" type="text/css">
<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
<!-- JavaScript code hiding

	doubleSubmit = 0;

	function changeSel(which){
		window = open("leftMenu.jsp?sel="+which, "_self", "");
	}

	function simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}

	function link2(origLink, page_action){
		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 = 'list_orders.jsp<%=orderStatus%>'
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].pageAction.value = page_action;
			top.mainFrame.document.forms[0].submit();
		}
	}

	function checkDoubleClick() {
	 if (doubleSubmit>0) {
			alert('Die Anforderung wird gerade verarbeitet. Wir bitten Sie um etwas Geduld!');
			return false;
	  }
	}
	
	function checkDoubleClick_pdf() {
	 if (doubleSubmit>0) {
			alert('Die Anforderung wird gerade verarbeitet. Wir bitten Sie um etwas Geduld!');
			return false;
	  }
	  return (confirm('Wollen Sie die Daten wirklich ans PostLabelcenter exportieren?'));
	}

	function link(origLink, page_action, to_status){
		var from_value=top.mainFrame.document.forms[0].from.value;
		var to_value = top.mainFrame.document.forms[0].to.value;
		var current_status = <%=orderStatus%>;
		if (from_value!='' && !/\d\d.\d\d.\d\d\d\d/.test(from_value)
		|| to_value!='' && !/\d\d.\d\d.\d\d\d\d/.test(to_value)) {
			alert('Datum bitte im Format tt.MM.jjjj eingeben');
			return;
		}

		if (page_action=='orderSearchAccept' && !isThereChecked('order_id')) {
			alert('Wählen Sie bitte zuerst eine Bestellung aus!');
			return;

		}

		if (to_status==90) {
		   
		   if (!confirm('Sind Sie sicher, dass Sie die ausgewĂ�Â¤hlten Bestellungen stornieren möchten?'))
		    return;
		    
		   if (current_status==30 || current_status==40)
		      alert('Der Betrag ist bereits abgebucht (Kreditkarte) oder bereits im Edifact-File für den Bankeinzug\n(kann im Bank-Austria Programm eventuell herausgelöscht werden).\nBitte entprechende Massnahmen berücksichtigen (Distributor verständigen,etc.).');
		     
		  
		}


		if (page_action!='orderSearch' && page_action!='print_addresses') doubleSubmit++;
		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 = 'list_orders.jsp<%=orderStatus%>';
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].pageAction.value=page_action;
			top.mainFrame.document.forms[0].status.value=<%=orderStatus%>;
			top.mainFrame.document.forms[0].toStatus.value=to_status;
			top.mainFrame.document.forms[0].action = 'process/order_detail.jsp';
			top.mainFrame.document.forms[0].submit();

		}
	}

	function checkAll() {
		for (var i=0; i<document.forms[0].elements.length; i++) {

			if (document.forms[0].elements[i].type=='checkbox' && document.forms[0].elements[i].name=='order_id') {
				 document.forms[0].elements[i].checked = document.forms[0].check_all.checked;
			}
		}
	}

	function isThereChecked(chkbx_name) {
		for (var i=0; i<document.forms[0].elements.length; i++) {
			if (document.forms[0].elements[i].type=='checkbox' && document.forms[0].elements[i].name==chkbx_name && document.forms[0].elements[i].checked)
				return true;
		}

		return false;

	}
	function checkAllPayments() {
		for (var i=0; i<document.forms[0].elements.length; i++) {

			if (document.forms[0].elements[i].type=='checkbox' && document.forms[0].elements[i].name=='payment_status_cb') {
				 if (!document.forms[0].elements[i].disabled) document.forms[0].elements[i].checked = document.forms[0].payment_status_check_all.checked;
			}
		}
	}

	function todayOnClick() {
		if (top.mainFrame.document.forms[0].today.checked) {
			var d= new Date();
			var day = d.getDate()<10?"0"+d.getDate():d.getDate();
			var month = d.getMonth()+1<10?"0"+(d.getMonth()+1):d.getMonth()+1;
			var currentDate = day+"."+month+"."+d.getFullYear();
			top.mainFrame.document.forms[0].from.value=currentDate;
			top.mainFrame.document.forms[0].to.value=currentDate;

			//top.mainFrame.document.forms[0].from.disabled=true;
			//top.mainFrame.document.forms[0].to.disabled=true;

		} else {
			top.mainFrame.document.forms[0].from.disabled=false;
			top.mainFrame.document.forms[0].to.disabled=false;
			top.mainFrame.document.forms[0].from.value='';
			top.mainFrame.document.forms[0].to.value='';
		}

	}

	function btodayOnClick() {
		if (top.mainFrame.document.forms[0].btoday.checked) {
			var d= new Date();
			var day = d.getDate()<10?"0"+d.getDate():d.getDate();
			var month = d.getMonth()+1<10?"0"+(d.getMonth()+1):d.getMonth()+1;
			var currentDate = day+"."+month+"."+d.getFullYear();
			top.mainFrame.document.forms[0].bfrom.value=currentDate;
			top.mainFrame.document.forms[0].bto.value=currentDate;

			//top.mainFrame.document.forms[0].bfrom.disabled=true;
			//top.mainFrame.document.forms[0].bto.disabled=true;

		} else {
			top.mainFrame.document.forms[0].bfrom.disabled=false;
			top.mainFrame.document.forms[0].bto.disabled=false;
			top.mainFrame.document.forms[0].bfrom.value='';
			top.mainFrame.document.forms[0].bto.value='';
		}

	}

		function rtodayOnClick() {
		if (top.mainFrame.document.forms[0].rtoday.checked) {
			var d= new Date();
			var day = d.getDate()<10?"0"+d.getDate():d.getDate();
			var month = d.getMonth()+1<10?"0"+(d.getMonth()+1):d.getMonth()+1;
			var currentDate = day+"."+month+"."+d.getFullYear();
			top.mainFrame.document.forms[0].rfrom.value=currentDate;
			top.mainFrame.document.forms[0].rto.value=currentDate;

			//top.mainFrame.document.forms[0].rfrom.disabled=true;
			//top.mainFrame.document.forms[0].rto.disabled=true;

		} else {
			top.mainFrame.document.forms[0].rfrom.disabled=false;
			top.mainFrame.document.forms[0].rto.disabled=false;
			top.mainFrame.document.forms[0].rfrom.value='';
			top.mainFrame.document.forms[0].rto.value='';
		}

	}
// -->
</SCRIPT>



</head>
   <body>
   		<h1 align="center"> <%= nextProcess.equals("")?statusName:nextProcess %> </h1>
		<form action="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearch','');" method="post">

			<input type="hidden" name="saveState"/>
			<input type="hidden" name="link" value="process/list_orders.jsp?status=<%=orderStatus %>" />
			<input type="hidden" name="pageAction" />
			<input type="hidden" name="toStatus"/>
			<input type="hidden" name="order_id0"/>
			<input type="hidden" name="status"/>

		  <table border="0">
		    <col width="200"/><col width="60"/><col width="30"/><col width="60"/><col width="30"/><col width="10"/><col width="10"/><col width="30"/><col width="100"/><col width="20"/><col width="100"/>

	<tr>
		   	<td colspan="7">&nbsp;</td>
		   	<td rowspan="6">&nbsp;</td>
		  <td rowspan="6" valign="top" align="right">
			  <table height="120" frame="above"  cellpadding="0">
				<%= generateCheckBoxGroup(countries, LocaleSupport.getLocalizedMessage(pageContext, "COUNTRIES")) %>
			  </table>
			  <span style="font-size:16px; font-family: arial; font-weight: bold;"><fmt:message key="PAYMENT_STATUS"/>:</span>
			 </td>
			 <td rowspan="6">&nbsp;</td>
			 <td rowspan="6" valign="top">
			 	<table height="120" frame="above" cellpadding="0">
			 	<%= generateCheckBoxGroup(payments, LocaleSupport.getLocalizedMessage(pageContext, "PAYMENT_METHOD")) %>
			 	</table><span style="font-size: 4px;"></span>
			 	<%= payed.toHTML() %>
			 </td>
		   </tr>
		   <tr>
		     <th align="right" colspan="1"><fmt:message key="LASTNAME_ID"/>:</th>
			 <td colspan="6"> <%= name.toHTML() %> </td>

			</tr>

		   <tr>
		    <th align="right"><fmt:message key="ORDER_NR"/></th>
		    <td align="left"><%= orderId.toHTML() %></td>
			<th align="right"><fmt:message key="BILL_NR"/>:</th>
		    <td align="left"><%= billId.toHTML() %></td>
		    <th colspan="2" align="right">&nbsp;</th><td>&nbsp;</td>
		   </tr>
		   <tr>
			 <th align="right">Best.datum von:</th> <td><%= from.toHTML() %> </td>
			 <th align="right">Bis:</th> <td><%= to.toHTML() %></td>
			 <td  align="right"><small>heute<small></td>
			 <td  align="left"><%=today.toHTML() %></td>
		    </tr>
<%		   if (displayExtendedDateInput) { %>
		   <tr>
			 <th align="right">Rechn.datum von:</th> <td><%= rfrom.toHTML() %> </td>
			 <th align="right">Bis:</th> <td><%= rto.toHTML() %></td>
			 <td  align="right"><small>heute<small></td>
			 <td  align="left"><%=rtoday.toHTML() %></td>
		   </tr>
		   <tr>
			 <th align="right">Bonusdatum von:</th> <td><%= bfrom.toHTML() %> </td>
			 <th align="right">Bis:</th> <td><%= bto.toHTML() %></td>
			 <td  align="right"><small>heute<small></td>
			 <td  align="left"><%=btoday.toHTML() %></td>
		   </tr>
		   <% } %>
		   <tr>
		    <td colspan="6" align="left"> <%=old.toHTML() %><br><%=creditNotes.toHTML() %> <br> <%=custDisc.toHTML() %></td>
		   </tr>
		   <tr>
		   	<td>&nbsp;</td>
		   </tr>
		   <tr>
		   		<td colspan="11" align="center"><b><fmt:message key="ORDER_BY"/>: </b> <%= orderBySelect.toHTML() %>
		   </tr>
		    <tr>
		   	<td colspan="11" align="center"> <a href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearch','');"><fmt:message key="SEARCH"/></a></td>

		   </tr>
			</table>
			<hr>



	<% if (!searchEnabled) { %>
		<p><fmt:message key="SEARCH_MESSAGE"/></p>
	<% } else { %>
		<table>
			<tr>
			  <col width="200"/><col width="200"/><col width="200"/><col width="200"/><col width="200"/>

			 <td align="left">
			 <% if (canCancel) { %><a onClick="return checkDoubleClick();" style="color:red"  href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearchAccept', 90);"><small><fmt:message key="STORNO_ORDER"/></small></a> <% } %>
			 </td>

			  
			  <td align="center">
			   <% if (timeToChangeBonus) { %>
			   <a onClick="return checkDoubleClick();" style="color:black" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderBonusDateChange', '');"><small><fmt:message key="CHANGE_BONUS_DATE"/></small></a>
			 	<%}  %>
			  </td>
			  
			  <td align="center">
			 <% if (pdfPrint) { %>
				 <a onClick="return checkDoubleClick_pdf();" style="color:black" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','print_addresses', '');"><small><fmt:message key="PDF_PRINT"/></small></a>
			 <%}  %>
			  </td>
			  
			  <td align="center">
			  
			 <% if (canPaymentBeSet && canChangePayment) { %>

				 <a onClick="return checkDoubleClick();" style="color:#FFC000" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderPaymentStatusChange', '');"><small><fmt:message key="PAYMENT_STATUS_CHANGE"/></small></a>
			   <%}%>
			 
			 </td>

			 
			  <td align="right">
			  <% if (canNextProc) { %>

			   <a onClick="return checkDoubleClick();" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearchAccept', '<%= nextStatus %>');"><small><%= nextProcess %></small></a>

			  <%}%>

			  </td>
			</tr>
			</table>
		</br>
			<table>
			<tr style="background-color:teal">
			<th>B.Nr</th>
			<% if (displayBill) { %> <th>R.Nr</th> <%}%>
			<th><fmt:message key="DISTRIBUTOR"/></th><th><fmt:message key="COUNTRY"/></th><th><fmt:message key="B_ART"/></th><th><fmt:message key="B_DATUM"/></th><th><fmt:message key="STATUS"/></th>
			<th>Pr.Datum</th><th><fmt:message key="Z_ART"/></th>
			<% if (canPaymentBeSet) { %> <th style="background-color:#FFCC66">
			     <% if (canChangePayment) { %>
			       <input type="checkbox" name="payment_status_check_all" onclick="checkAllPayments();"><fmt:message key="BZ_STATUS"/></input>
				 <% } else { %>
				  Bz.Status
				  <% } %>
				</th>
			 <%}%>
			<%if (admin) {%> <th>T.Netto</th> <%} %>
			<%if (admin) {%> <th>T.Bonus</th> <%} %>
			<th><fmt:message key="T_BRUTTO"/></th>
			<th>Komm.</th>
			<% if (canNextProc || canCancel) { %> <th><input type="checkbox" name="check_all" onclick="checkAll();"/></th> <% } %>

			</tr>
				<%
				 BigDecimal sumOfTotalNetto = new BigDecimal(0);
				 BigDecimal sumOfTotalBonus = new BigDecimal(0);
				 
				 BigDecimal sumOfTotalBrutto = new BigDecimal(0);
				 for (int i=0; i<result.size(); i++) {
					PersistableObject order = (PersistableObject) result.get(i);
					BigDecimal totalNettoDisc = (BigDecimal)order.get("total_netto_trader");
					BigDecimal totalNetto = (BigDecimal) order.get("total_netto"); 
					if (totalNettoDisc != null) {
						if (totalNettoDisc.floatValue() < totalNetto.floatValue())
							totalNetto = totalNettoDisc;
					}
					
					sumOfTotalNetto = sumOfTotalNetto.add(totalNetto==null?new BigDecimal(0):totalNetto);
					
					BigDecimal totalBonus = (BigDecimal) order.get("total_bonus"); sumOfTotalBonus = sumOfTotalBonus.add(totalBonus==null?new BigDecimal(0):totalBonus);

					BigDecimal totalBruttoDisc = (BigDecimal)order.get("total_brutto_trader");
					BigDecimal totalBrutto = (BigDecimal) order.get("total_brutto"); 
					
					if (totalBruttoDisc != null) {
						if (totalBruttoDisc.floatValue() < totalBrutto.floatValue())
							totalBrutto = totalBruttoDisc;
					}
					
					
					sumOfTotalBrutto = sumOfTotalBrutto.add(totalBrutto==null?new BigDecimal(0):totalBrutto);


				%>

			 <%
				   Boolean b = (Boolean) order.get("active");
				   boolean active = true;
				   
				   boolean cn = totalBrutto.doubleValue() < 0;
				   
				   if (b==null || !b.booleanValue())
				     active = false;

			 	String textStart = "";
			 	String textEnd = "";
			 	if (!active) {
			 	   textStart = "<span class=\"deactivated\">";
			 	   textEnd = "</span>";
			 	}

				if (cn) {
					textStart += "<span class=\"creditnotes\">";
					textEnd += "</span>";
				}

			    Long l = (Long) order.get("order_id");
			    Integer orderer_level_id = (Integer) order.get("orderer_level_id");
			    
			    boolean handler_order = (orderer_level_id.intValue() == 300 || orderer_level_id.intValue() == 350);
			    String dr = order.getString("discount_rate", "0");
			    
			    if (orderer_level_id.intValue() == 100 || orderer_level_id.intValue() == 130 || orderer_level_id.intValue() == 160 || orderer_level_id.intValue() == 200 || orderer_level_id.intValue() == 370) {
			    	
			    	try {
			    		float ff = new Float(dr).floatValue();
			    		if (ff > 0)
			    			handler_order = true;
			    	} catch (Exception ignored) {
			    	}
			    }
			    
			 	String color = i%2==1? " style=\"background-color:#C0C0C0\"":" style=\"background-color:white\"";
			 	if (failedOrders!=null && failedOrders.contains(l))
			 		color = " style=\"background-color:red\"";
			 	%>

			  <tr <%=color %> >
				<td>
				<%if (statusFrom<50) {%>
					<a href="javascript:top.mainFrame.location='process/order_detail.jsp?order_id0=<%=order.getString("order_id","")%>&saveState=nothing&pageAction=load_order&link=process/order_detail.jsp';">
					<%=textStart%>
					<%= order.getString("order_id","") %>
					<%=textEnd%>
					</a>
				<%} else {%>
					<%=textStart%>
					<%= order.getString("order_id","") %>
					<%=textEnd%>
				<%}%>

				</td>
			<% if (displayBill) { %> <td><%=textStart%> <%= OrderListHelper.getBillLink(order) %> <%=textEnd%></td> <%}%>
<% 
	String nameStr = order.getString("firstname","")+" "+order.getString("lastname","");
	String comp = order.getString("company", "");
	if (!"".equals(comp)) {
		nameStr = comp;
	}
%>

				<td> <%=textStart%><%= order.getString("id","")+" "+nameStr %> <%=textEnd%></td>
				<td align="center"><%=textStart%> <%= order.getString("country_code","") %> <%=textEnd%></td>
				<td align="center"><%=textStart%> <%= order.getString("order_type","-") %> <%=textEnd%></td>
				<td align="center"><%=textStart%> <%= OrderListHelper.dateToString((java.sql.Date) order.get("order_date")) %><%=textEnd%> </td>
				<td align="center"><%=textStart%> <%= order.getString("order_status_id","") %><%=textEnd%> </td>
				<td align="center"><%=textStart%> <%= OrderListHelper.getBonusDateField(order, timeToChangeBonus) %> <%=textEnd%></td>
				<td align="center"><%=textStart%> <%= order.getString("pay_method_descr","") %> <%=textEnd%></td>
					<% if (canPaymentBeSet) { %> <td align="center">
				   <% Boolean orderPayed = (Boolean) order.get("payment_status");
					CheckBox chkbx = new CheckBox(session, jsp, "payment_status_cb", order.getString("order_id",""),"", orderPayed==null?false:orderPayed.booleanValue());
					chkbx.setKeepState(false);
					if ((orderPayed!=null && orderPayed.booleanValue()) || !canChangePayment) chkbx.setDisabled(true);
					out.println(chkbx.toHTML());
					%>
				</td> <%}%>
				<%if (admin) {%> <td align="right"><%=textStart%> <%= OrderListHelper.numberToString(order.get(handler_order?"total_netto_trader":"total_netto"),2) %> <%=textEnd%></td> <%}%>
				<%if (admin) {%> <td align="right"><%=textStart%> <%= OrderListHelper.numberToString(order.get("total_bonus"),2) %> <%=textEnd%></td> <%}%>
				<td align="right"><%=textStart%> <%= OrderListHelper.numberToString(order.get(handler_order?"total_brutto_trader":"total_brutto"),2) %><%=textEnd%> </td>

				<% 
				String o_comment = order.getString("comment", "");
				if (!o_comment.equals("")) { %>
					<td><div title="<%=o_comment%>"><img border=\"0\" src=\"img/page.gif\"/></div></td>
				<% } else { %>
					<td> </td>
				<% } %>

			<% if (canNextProc || canCancel) {%><td> <%= new CheckBox(session, jsp, "order_id", order.getString("order_id",""), "", false).toHTML() %> </td> <%}%>


			  </tr>
			  <% } %>
			  <tr style="background-color:#666666;color:white">
			  <% int colspan=8;
			  	 if (displayBill) colspan++;
			  	 if (canPaymentBeSet) colspan++;
			  %>
			   <td colspan="<%=colspan%>" align="right"><i><b><fmt:message key="TOTAL"/></b></i></td>

			   <%if (admin) {%><td align="right"><i><%=OrderListHelper.numberToString(sumOfTotalNetto,2)%></i></td> <%}%>
			  <%if (admin) {%> <td align="right"><i><%=OrderListHelper.numberToString(sumOfTotalBonus,2)%></i></td> <%}%>
			   <td align="right"><i><%=OrderListHelper.numberToString(sumOfTotalBrutto,2)%></i></td>
			   <% if(canNextProc || canCancel) {%><td>&nbsp;</td><% }%>
			  </tr>
			</table>
			</br>
			<table>
			<tr>
			  <col width="200"/><col width="200"/><col width="200"/><col width="200"/><col width="200"/>

			 <td align="left">
			 <% if (canCancel) { %><a onClick="return checkDoubleClick();" style="color:red"  href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearchAccept', 90);"><small><fmt:message key="STORNO_ORDER"/></small></a> <% } %>
			 </td>

			 <td align="center">
			   <% if (timeToChangeBonus) { %>
			   <a onClick="return checkDoubleClick();" style="color:black" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderBonusDateChange', '');"><small><fmt:message key="CHANGE_BONUS_DATE"/></small></a>
			   <%}  %>

			  </td>

			 <td align="center">
			 <% if (pdfPrint) { %>
				 <a onClick="return checkDoubleClick_pdf();" style="color:black" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','print_addresses', '');"><small><fmt:message key="PDF_PRINT"/></small></a>
			 <%}  %>
			  </td>

			  <td align="center">
			 <% if (canPaymentBeSet && canChangePayment) { %><a onClick="return checkDoubleClick();" style="color:#FFC000" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderPaymentStatusChange', '');"><small><fmt:message key="PAYMENT_STATUS_CHANGE"/></small></a><%}%>
			 </td>
			  <td align="right">
			  <% if (canNextProc) { %> <a onClick="return checkDoubleClick();" href="javascript:link('process/list_orders.jsp?status=<%=orderStatus %>','orderSearchAccept', '<%= nextStatus %>');"><small><%= nextProcess %></small></a> <%}%>

			  </td>
			</tr>
			</table>
			

			<p> <FONT color="red">
			<%
			 if (passedOrders!=null) {
			 	for (Iterator it = passedOrders.iterator(); it.hasNext();) { %>

				 	<SMALL color="red">	Status der Bestellung mit Nr. <%= it.next() %> <fmt:message key="TEST_T"/></SMALL></br>

			  <%}
			 	m.remove("passed_orders");
			 	m.remove("failed_orders");
			 }%>
			</FONT></P>
		<%}%>
		</form>
<%
	if (session.getAttribute("ws_called")!=null) {
		session.removeAttribute("ws_called");
		%>
			Der Antrag wurde auf die Post-Labelcenter gesendet.
		<%
	}
%>
   </body>
</html>


