<%
/******************************************************************************
 * Copyright (c) 2000-2005 e-Deal
 *
 * e-Deal S.A.
 * 55 rue Brillat Savarin
 * 75013 Paris
 * France
 *
 * T: +33 (0)1 53 80 80 30
 * F: +33 (0)1 73 01 69 77
 * http://www.e-deal.com
 * 
 * La diffusion de ce code source sous quelque forme que ce soit sans
 * l'autorisation de E-DEAL est interdite.
 *
 * Vous êtes autorisés à modifier ce code source uniquement pour votre usage
 * propre et sous réserve que les mentions de copyright demeurent intactes.
 *
 * Ce code est fourni en l'état. Aucune garantie d'aucune sorte, explicite ou
 * implicite n'est donnée. En aucun cas E-DEAL ne pourra être tenu pour
 * responsable des dommages pouvant résulter de l'utilisation de ce code
 * source.
 ******************************************************************************/
%><%@page session="true" import="com.edeal.frontline.*"%><%@page import="java.util.*" %><%@ page import="java.text.*" %><%@ page import="com.edeal.frontline.navigation.FlRequest" %><%@ page import="com.edeal.frontline.controller.actions.SelectAction" %><%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %><jsp:useBean class="com.edeal.frontline.CategorizationBean" id="Categorization" scope="request"/><%
	final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
	FlRequest flRequest = new FlRequest(request);
	FlContext context = flRequest.getContext();
	
	String id = null;
	try {
		id = Categorization.getID();
	} catch (FieldNotInitializedException fnie) {
	}
	
	String swfID = flRequest.getRequestParameterOrAttribute("SfwID");
	
	InvoiceEntryBean InvoiceEntry = new InvoiceEntryBean(Categorization.getCtnFactLn(), context);
	String invID = InvoiceEntry.getIneInvId();
	
	InvoiceBean Invoice = new InvoiceBean(invID, context);
	String supID = Invoice.getInvSupplID();
	
	SupplierBean Supplier = new SupplierBean(supID, context);
	StageBean stage = null;
	
	request.setAttribute("InvoiceEntry", InvoiceEntry);
	request.setAttribute("Invoice", Invoice);
	request.setAttribute("Supplier", Supplier);

	// Find out how many units are left with this invoice entry
	int categoriesToGo = 0;
	int catsAdjust = 0;
	try {
  		String[] ctnKeys2 = { "Sum" };
  		String query = "SELECT SUM(CtnNumUnits) from Categorization where CtnFactLn = " + Utils.formatToSQL(context, InvoiceEntry.getID());
	  	Vector resp = BasicBean.listSQL(context, ctnKeys2, query);
  		Hashtable hash = (Hashtable) resp.firstElement();
		categoriesToGo = ((Number)hash.get("Sum")).intValue();
	} catch (Exception e) {
	}

	try {
		catsAdjust = Categorization.getCtnNumUnits();
    } catch (Exception e) {
    }
      	
	DecimalFormat intFormat = FlLocale.getInstance(session).getDecimalFormat("#,##0;- #,##0");
	DecimalFormat floatFormat = FlLocale.getInstance(session).getDecimalFormat("#,##0.##;- #,##0.##");
	DecimalFormat moneyFormat = FlLocale.getInstance(session).getDecimalFormat("#,##0.##;- #,##0.##");
	moneyFormat.setMaximumFractionDigits(2);
	moneyFormat.setMinimumFractionDigits(2);
%>
<script type="text/javascript" language="javascript">
	var ctnDoAnother = false;

	function doSubmit() {
		f = document.forms[0];
	
		if (f.CtnNumUnits.value > <%= InvoiceEntry.getIneNumUnits() - categoriesToGo + catsAdjust %>) {
			window.alert('<fl:getMessage code="M60BB" alt="La valeur donné est superieur que la limite" js="true"/>');
			return;
		}

		if (!ctnDoAnother) {
			f.$$returnedHtmlHead.value = '<scr' + 'ipt language=\"javascript\">window.location = \'<fl:webapp/>/read_invoiceEntry.fl?id=<%= InvoiceEntry.getID() %>&SfwID=<%= swfID%>\';</scr' + 'ipt>';
			f.$$returnedHtmlBody.value = '<!-- Nothing to see here -->';
		}
		
		return true;
	}
	
	function submitAndAnother() {
		f = document.forms[0];
	
		if (f.CtnNumUnits.value > <%= InvoiceEntry.getIneNumUnits() - categoriesToGo + catsAdjust %>) {
			window.alert('<fl:getMessage code="M60BB" alt="La valeur donné est superieur que la limite" js="true"/>');
			return;
		}

		// Only permit them to do another if there are numUnits still left to categorize

		if (f.CtnNumUnits.value < <%= InvoiceEntry.getIneNumUnits() - categoriesToGo + catsAdjust %>) {
			f.$$returnedHtmlHead.value = '<scr' + 'ipt language=\"javascript\">window.location = \'<fl:webapp/>/create_categorization.fl?SfwID=<%= swfID%>&CtnFactLn=<%= InvoiceEntry.getID() %>\';</scr' + 'ipt>';
			f.$$returnedHtmlBody.value = '<!-- Nothing to see here -->';
			//doAjax('<fl:link url="/save_categorization.fl?id=<%= InvoiceEntry.getID() %>"/>&successpage=encodeURIComponent(\'/\')');

			ctnDoAnother = true;
			f.submit();
		} else {
			ctnDoAnother = false;
		}
		
		
	}

	function returnToInvoiceEntry() {
		window.location = '<fl:link url="<%= \"/read_invoiceEntry.fl?id=\" + InvoiceEntry.getID() %>"/>';
	}
	
	function selectStage() {
		var f = document.forms[0];
		
		if (f.CtnCampID.value == '') {
			alert('<fl:getMessage code="M6099" alt="Veuillez choisir une campagne" js="true"/>');
			return;
		}
		
		var win = window.open('<fl:webapp/>/select.fl?type=Stage&field=CtnStageID&formIndex=0&query=StgCpgID%3d\'' + f.CtnCampID.value + '\'', 'SelectActor', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=<%= SelectAction.SELECT_WINDOW_WIDTH %>,height=500');
	}
</script>
<fl:form action="/save_categorization.fl" bean="Categorization">
<input type="hidden" name="$$returnedHtmlHead" value="">
<input type="hidden" name="$$returnedHtmlBody" value="">
<input type="hidden" name="CtnFactLn" value="<%= InvoiceEntry.getID() %>">
<fl:bigTabs ignoreList="true" objectTitle="<%= Utils.getMessage(session, \"L63BE\", false, true, \"Imputation\") %>" ou="<%= \"/edit_categorization.fl\" + ( id != null ? \"?id=\" + id : \"\") %>" >
	<table class="container" cellpadding="0">
		<tr>
			<td class="titreFiche" colspan="3">
				<fl:getMessage code="L632B" alt="Imputation de"/> <fl:getMessage code="F60D4" alt="Facture"/> N&deg; <fl:getProperty name="Invoice" property="InvNumInv" alt="&nbsp;"/>&nbsp;<fl:getMessage code="L0045" alt="de"/>&nbsp;<fl:getProperty name="Supplier" property="SplRS" alt="&nbsp;"/>
			</td>
		</tr>
		<tr class="ligne_separation"><td></td></tr>
		<tr>
			<td class="column2">
				<table class="data_column" cellpadding="0">
					<fl:fieldEdit property="CtnCampID" mandatoryMsgCode="A3063" labelCode="L2026" />
					<tr class="ligne_separation"><td></td></tr>
					<tr class="fieldline">
					    <td class="dataLabel"><label for="lblCtnStageID" id="CtnStageID_label"><fl:getMessage code="L3046" alt="Etape"/></label></td>
					    <td class="dataValue"><input type="hidden" name="mandatory" value="CtnStageID"><input type="hidden" id="mandatory.CtnStageID" name="mandatory.CtnStageID" value="<fl:getMessage code="M60D0" alt="Veuillez choisir une stage"/>"><input id="CtnStageID" name="CtnStageID" type="hidden" value="<fl:getProperty name="Categorization" property="CtnStageID" alt="" />"><input id="lblCtnStageID" size="24" name="lblCtnStageID" type="text" value="<fl:getProperty name="Categorization" property="CtnStageID" bean="true" alt="" />" onfocus="this.blur();" readonly class="chpsTextObligatoire">&nbsp;<a href="javascript:selectStage();"><img src="<fl:webapp/>/icons/ico/rechercher.gif" align="middle" border="0" width="17" height="17" alt="S&eacute;lectionner"></a>&nbsp;<a href="javascript:var f = window.document.forms[0]; f.CtnStageID.value = f.lblCtnStageID.value = ''; void('')"><img src="<fl:webapp/>/icons/ico/supprimer.gif" align="middle" border="0" width="17" height="17" alt="Vider le champ"></a><span id="CtnStageID_msg" class="mandatory_alert" style="display: none">!</span></td>
					</tr>
				</table>
			</td>
			<td class="separation"><div class="separation"></div></td>
			<td class="column2">
				<table class="data_column" cellpadding="0">
					<tr class="fieldline">
						<td id="Preix_label" class="dataLabel">
							<fl:getMessage code="F2043" alt="Prix unitaire"/>
						</td>
						<td id="Prix_value" class="dataValue">
							<%= moneyFormat.format(InvoiceEntry.getIneUnitPrice()) %>&nbsp;&euro;
						</td>
					</tr>
					<tr class="ligne_separation"><td></td></tr>
					<fl:fieldEdit property="CtnNumUnits" mandatoryMsgCode="A3064" postText="<%= \"&nbsp;\" + Utils.getMessage(session, \"L6010\", \"sur\") + \"&nbsp;\" + intFormat.format(InvoiceEntry.getIneNumUnits() - categoriesToGo + catsAdjust) %>"/>
				</table>
			</td>
		</tr>
	</table>
	<table class="container" cellpadding="0">
		<tr class="ligne_separation"><td></td></tr>
		<tr>
			<td class="titrePartie"><fl:getMessage code="F2024" alt="Notes"/></td>
		</tr>
		<tr>
			<td class="dataValue noPaddingLeft"><fl:input property="CtnNotes"  nodiv="false"/></td>
		</tr>
		<tr class="ligne_separation"><td></td></tr>
				<%--<tr class="ligne_separation_couleur"><td></td></tr>
				<tr class="ligne_separation"><td></td></tr>--%>
	</table>
</fl:bigTabs>
</fl:form>
