From 57b56a0932bf6d9fff2be701a20583de78088020 Mon Sep 17 00:00:00 2001
From: Vladimir Hidalgo <vlad@todosv.com>
Date: Tue, 13 Oct 2009 08:52:31 -0600
Subject: [PATCH 2/2] Add JS calculateExtendedRev()

This patch enable the user to type into the extended field and calculate UnitPrice.
theprice = extended / qty
---
 modules/bms/invoices_addedit.php  |    2 +-
 modules/bms/javascript/invoice.js |   58 +++++++++++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/modules/bms/invoices_addedit.php b/modules/bms/invoices_addedit.php
index a138bd1..03ceaf7 100644
--- a/modules/bms/invoices_addedit.php
+++ b/modules/bms/invoices_addedit.php
@@ -412,7 +412,7 @@
 			<td><input name="memo" type="text" id="memo" size="12" maxlength="255" /></td>
 			<td align="right" nowrap="nowrap"><input name="price" type="text" id="price" value="<?php echo htmlQuotes(numberToCurrency(0))?>" size="10" maxlength="16" class="fieldCurrency" /></td>
 			<td align="center" nowrap="nowrap"><input name="qty" type="text" id="qty" value="1" size="5" maxlength="16" /></td>
-			<td align="right" nowrap="nowrap"><input name="extended" type="text" id="extended" class="uneditable fieldCurrency" value="<?php echo htmlQuotes(numberToCurrency(0))?>" size="12" maxlength="16" readonly="readonly" /></td>
+			<td align="right" nowrap="nowrap"><input name="extended" type="text" id="extended" class="fieldCurrency" value="<?php echo htmlQuotes(numberToCurrency(0))?>" size="12" maxlength="16" /></td>
 			<td nowrap="nowrap" align="left"><button type="button" id="lineitemAddButton" class="graphicButtons buttonPlus" title="Add Line Item"><span>+</span></button></td>
 		</tr><?php }//end if
 		
diff --git a/modules/bms/javascript/invoice.js b/modules/bms/javascript/invoice.js
index 03f1155..4f13983 100644
--- a/modules/bms/javascript/invoice.js
+++ b/modules/bms/javascript/invoice.js
@@ -747,8 +747,8 @@ lineitems = {
 
 		tempinput = document.createElement("input");
 		tempinput.id = thetr.id + "Extended";
-		tempinput.className = "uneditable fieldCurrency lineitemExtendeds"
-		tempinput.readOnly = true;
+		tempinput.className = "fieldCurrency lineitemExtendeds"
+		connect(tempinput, "onchange", lineitems.calculateExtendedRev);
 		tempinput.value = extended.value;
 		temptd.appendChild(tempinput);
 
@@ -881,7 +881,57 @@ lineitems = {
 		
 	},//end function
 	
+	calculateExtendedRev: function(e){
+		
+		theTR = e.src().parentNode.parentNode;
+		
+		lineitems._calculateExtendedRev(theTR)
+		
+	}, //end function
+
+
+	_calculateExtendedRev: function(theTR){		
+
+		if(theTR.id == "LIAdd"){
+			
+			var thecurrency = getObjectFromID("price");	
+			var quantity = getObjectFromID("qty");
+			var extField = getObjectFromID("extended");				
+			
+		} else {
+			
+			var thecurrency = getObjectFromID(theTR.id+"UnitPrice");	
+			var quantity = getObjectFromID(theTR.id+"Quantity");
+			var extField = getObjectFromID(theTR.id+"Extended");	
+			
+		}//end if
+	
+		// Last, figure extended and reformat to dollar
+		var extended = currencyToNumber(extField.value);
+		extField.value = numberToCurrency(extended);
+				
+		// Next verify that qty is a number
+		var qty = parseFloat(quantity.value);
 	
+		if(isNaN(qty))
+			qty = 1;
+			
+		quantity.value = qty;
+	
+		// Figure out the price and format it
+		theprice = roundForCurrency(extended / qty);
+		thecurrency.value = theprice;
+		
+		//if this is a modification, we also need to calculate totals and mark line items changed
+		if(theTR.id != "LIAdd"){
+
+			invoice.doAllTotals();
+			lineitems.markChanged();
+
+		}//end if
+		
+	},//end function
+
 	del: function(e){
 		
 		var theTR = e.src().parentNode.parentNode.parentNode;
@@ -1687,6 +1737,10 @@ connect(window,"onload",function() {
 		if(unitprice)
 			connect(unitprice, "onchange", lineitems.calculateExtended)
 
+	var unitprice = getObjectFromID("extended");
+		if(unitprice)
+			connect(unitprice, "onchange", lineitems.calculateExtendedRev)
+
 	var quantity = getObjectFromID("qty");
 		if(quantity)
 			connect(quantity, "onchange", lineitems.calculateExtended)
-- 
1.6.3.3


