Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/journal_voucher/journal_voucher.js b/accounts/doctype/journal_voucher/journal_voucher.js
index 2e89a67..ab4b2b0 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.js
+++ b/accounts/doctype/journal_voucher/journal_voucher.js
@@ -104,14 +104,6 @@
 	if (doc.is_opening == 'Yes') unhide_field('aging_date');
 }
 
-cur_frm.fields_dict['entries'].grid.onrowadd = function(doc, cdt, cdn){
-	var d = locals[cdt][cdn];
-	if(d.idx == 1){
-		d.debit = 0;
-		d.credit = 0;
-	}
-}
-
 cur_frm.cscript.against_voucher = function(doc,cdt,cdn) {
 	var d = locals[cdt][cdn];
 	if (d.against_voucher && !flt(d.debit)) {
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.js b/accounts/doctype/purchase_invoice/purchase_invoice.js
index 2bdab8a..e73386f 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -22,7 +22,7 @@
 wn.require('app/accounts/doctype/purchase_taxes_and_charges_master/purchase_taxes_and_charges_master.js');
 wn.require('app/buying/doctype/purchase_common/purchase_common.js');
 
-erpnext.accounts.PurchaseInvoiceController = erpnext.buying.BuyingController.extend({
+erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({
 	onload: function() {
 		this._super();
 		
@@ -103,29 +103,14 @@
 	tc_name: function() {
 		this.get_terms();
 	},
-
+	
+	entries_add: function(doc, cdt, cdn) {
+		var row = wn.model.get_doc(cdt, cdn);
+		this.frm.script_manager.copy_from_first_row("entries", row, ["expense_head", "cost_center"]);
+	}
 });
 
-// for backward compatibility: combine new and previous states
-$.extend(cur_frm.cscript, new erpnext.accounts.PurchaseInvoiceController({frm: cur_frm}));
-
-cur_frm.fields_dict['entries'].grid.onrowadd = function(doc, cdt, cdn){
-	
-	cl = getchildren('Purchase Invoice Item', doc.name, cur_frm.cscript.fname, doc.doctype);
-	acc = '';
-	cc = '';
-
-	for(var i = 0; i<cl.length; i++) {
-		if (cl[i].idx == 1){
-			acc = cl[i].expense_head;
-			cc = cl[i].cost_center;
-		}
-		else{
-			if (! cl[i].expense_head) { cl[i].expense_head = acc; refresh_field('expense_head', cl[i].name, 'entries');}
-			if (! cl[i].cost_center)	{cl[i].cost_center = cc; refresh_field('cost_center', cl[i].name, 'entries');}
-		}
-	}
-}
+cur_frm.script_manager.make(erpnext.accounts.PurchaseInvoice);
 
 cur_frm.cscript.is_opening = function(doc, dt, dn) {
 	hide_field('aging_date');
diff --git a/accounts/doctype/sales_invoice/sales_invoice.js b/accounts/doctype/sales_invoice/sales_invoice.js
index 90239fa..773956a 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/accounts/doctype/sales_invoice/sales_invoice.js
@@ -150,6 +150,11 @@
 	paid_amount: function() {
 		this.write_off_outstanding_amount_automatically();
 	},
+	
+	entries_add: function(doc, cdt, cdn) {
+		var row = wn.model.get_doc(cdt, cdn);
+		this.frm.script_manager.copy_from_first_row("entries", row, ["income_account", "cost_center"]);
+	}
 });
 
 // for backward compatibility: combine new and previous states
@@ -197,25 +202,6 @@
 	cur_frm.cscript.hide_fields(doc, dt, dn);
 }
 
-cur_frm.fields_dict['entries'].grid.onrowadd = function(doc, cdt, cdn){
-
-	cl = getchildren('Sales Invoice Item', doc.name, cur_frm.cscript.fname, doc.doctype);
-	acc = '';
-	cc = '';
-
-	for(var i = 0; i<cl.length; i++) {
-
-		if (cl[i].idx == 1){
-			acc = cl[i].income_account;
-			cc = cl[i].cost_center;
-		}
-		else{
-			if (! cl[i].income_account) { cl[i].income_account = acc; refresh_field('income_account', cl[i].name, 'entries');}
-			if (! cl[i].cost_center)	{cl[i].cost_center = cc;refresh_field('cost_center', cl[i].name, 'entries');}
-		}
-	}
-}
-
 cur_frm.cscript.is_opening = function(doc, dt, dn) {
 	hide_field('aging_date');
 	if (doc.is_opening == 'Yes') unhide_field('aging_date');
diff --git a/stock/doctype/item/item.js b/stock/doctype/item/item.js
index b2a1ffe..b1bb269 100644
--- a/stock/doctype/item/item.js
+++ b/stock/doctype/item/item.js
@@ -14,8 +14,25 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-cur_frm.add_fetch("price_list_name", "currency", "ref_currency");
-cur_frm.add_fetch("price_list_name", "buying_or_selling", "buying_or_selling");
+wn.provide("erpnext.stock");
+
+erpnext.stock.Item = wn.ui.form.Controller.extend({
+	onload: function() {
+		this.frm.add_fetch("price_list_name", "currency", "ref_currency");
+		this.frm.add_fetch("price_list_name", "buying_or_selling", "buying_or_selling");
+	},
+	
+	ref_rate_details_add: function(doc, cdt, cdn) {
+		var row = wn.model.get_doc(cdt, cdn);
+		if(row.price_list_name && !row.ref_currency) {
+			// execute fetch
+			var df = wn.meta.get_docfield(row.doctype, "price_list_name", row.parent);
+			this.frm.script_manager.validate_link_and_fetch(df, row.name, row.price_list_name);
+		}
+	}
+});
+
+cur_frm.script_manager.make(erpnext.stock.Item);
 
 cur_frm.cscript.refresh = function(doc) {
 	// make sensitive fields(has_serial_no, is_stock_item, valuation_method)
@@ -157,11 +174,6 @@
   cur_frm.cscript.weight_to_validate(doc,cdt,cdn);
 }
 
-cur_frm.fields_dict['ref_rate_details'].grid.onrowadd = function(doc, cdt, cdn){
-	locals[cdt][cdn].ref_currency = sys_defaults.currency;
-	refresh_field('ref_currency',cdn,'ref_rate_details');
-}
-
 cur_frm.fields_dict.item_customer_details.grid.get_field("customer_name").get_query = 
 function(doc,cdt,cdn) {
 		return{	query:"controllers.queries.customer_query" } }
diff --git a/stock/doctype/stock_entry/stock_entry.js b/stock/doctype/stock_entry/stock_entry.js
index 123c9fb..7032f92 100644
--- a/stock/doctype/stock_entry/stock_entry.js
+++ b/stock/doctype/stock_entry/stock_entry.js
@@ -234,9 +234,15 @@
 		}
 	},
 
+	mtn_details_add: function(doc, cdt, cdn) {
+		var row = wn.model.get_doc(cdt, cdn);
+		
+		if(!row.s_warehouse) row.s_warehouse = this.frm.doc.from_warehouse;
+		if(!row.t_warehouse) row.t_warehouse = this.frm.doc.to_warehouse;
+	},
 });
 
-cur_frm.cscript = new erpnext.stock.StockEntry({frm: cur_frm});
+cur_frm.script_manager.make(erpnext.stock.StockEntry);
 
 cur_frm.cscript.toggle_related_fields = function(doc) {
 	disable_from_warehouse = inList(["Material Receipt", "Sales Return"], doc.purpose);
@@ -296,19 +302,6 @@
 	cur_frm.cscript.toggle_related_fields(doc, cdt, cdn);
 }
 
-// copy over source and target warehouses
-cur_frm.fields_dict['mtn_details'].grid.onrowadd = function(doc, cdt, cdn){
-	var d = locals[cdt][cdn];
-	if(!d.s_warehouse && doc.from_warehouse) {
-		d.s_warehouse = doc.from_warehouse
-		refresh_field('s_warehouse', cdn, 'mtn_details')
-	}
-	if(!d.t_warehouse && doc.to_warehouse) {
-		d.t_warehouse = doc.to_warehouse
-		refresh_field('t_warehouse', cdn, 'mtn_details')
-	}
-}
-
 // Overloaded query for link batch_no
 cur_frm.fields_dict['mtn_details'].grid.get_field('batch_no').get_query = function(doc, cdt, cdn) {
 	var d = locals[cdt][cdn];