Merge branch 'responsive' of github.com:webnotes/erpnext into responsive
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index f1c9c42..235f34f 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -114,16 +114,6 @@
 		ret={'add_tax_rate' :rate and flt(rate[0][0]) or 0 }
 		return ret
 
-	def validate_duplicate_docname(self,doctype):
-		for d in getlist(self.doclist, 'entries'): 
-			if doctype == 'purchase_receipt' and cstr(self.doc.purchase_receipt_main) == cstr(d.purchase_receipt):
-				msgprint(cstr(self.doc.purchase_receipt_main) + " purchase receipt details have already been pulled.")
-				raise Exception , " Validation Error. "
-
-			if doctype == 'purchase_order' and cstr(self.doc.purchase_order_main) == cstr(d.purchase_order) and not d.purchase_receipt:
-				msgprint(cstr(self.doc.purchase_order_main) + " purchase order details have already been pulled.")
-				raise Exception , " Validation Error. "
-
 	def check_active_purchase_items(self):
 		for d in getlist(self.doclist, 'entries'):
 			if d.item_code:		# extra condn coz item_code is not mandatory in PV
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index 79f757f..93fb47f 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -276,25 +276,6 @@
 		
 		if self.doc.debit_to:
 			self.doc.customer = webnotes.conn.get_value('Account',self.doc.debit_to,'master_name')
-		
-	def update_accounts(self):
-		if not self.doc.debit_to:
-			self.doc.debit_to = self.get_debit_to().get("debit_to")
-
-		self.get_income_expense_account('entries')
-
-	def get_income_expense_account(self,doctype):
-		auto_inventory_accounting = cint(webnotes.defaults.get_global_default("auto_inventory_accounting"))
-		default_cost_center = webnotes.conn.get_value("Company", self.doc.company, "cost_center")
-		for d in getlist(self.doclist, doctype):			
-			if d.item_code:
-				item = webnotes.conn.get_value("Item", d.item_code, ["default_income_account", 
-					"default_sales_cost_center", "purchase_account"], as_dict=True)
-				d.income_account = item['default_income_account'] or ""
-				d.cost_center = item['default_sales_cost_center'] or default_cost_center
-				
-				if auto_inventory_accounting and cint(self.doc.update_stock):
-					d.expense_account = item['purchase_account'] or ""
 
 	def get_barcode_details(self, barcode):
 		return get_obj('Sales Common').get_barcode_details(barcode)
diff --git a/buying/doctype/purchase_order/purchase_order.py b/buying/doctype/purchase_order/purchase_order.py
index 0ba00a6..406f3a9 100644
--- a/buying/doctype/purchase_order/purchase_order.py
+++ b/buying/doctype/purchase_order/purchase_order.py
@@ -231,6 +231,7 @@
 		}, 
 		"Purchase Taxes and Charges": {
 			"doctype": "Purchase Taxes and Charges", 
+			"add_if_empty": True
 		}
 	}, target_doclist, set_missing_values)
 
@@ -271,6 +272,7 @@
 		}, 
 		"Purchase Taxes and Charges": {
 			"doctype": "Purchase Taxes and Charges", 
+			"add_if_empty": True
 		}
 	}, target_doclist, set_missing_values)
 
diff --git a/buying/doctype/supplier_quotation/supplier_quotation.py b/buying/doctype/supplier_quotation/supplier_quotation.py
index 128cee2..ae3fbd3 100644
--- a/buying/doctype/supplier_quotation/supplier_quotation.py
+++ b/buying/doctype/supplier_quotation/supplier_quotation.py
@@ -102,6 +102,7 @@
 		}, 
 		"Purchase Taxes and Charges": {
 			"doctype": "Purchase Taxes and Charges", 
+			"add_if_empty": True
 		},
 	}, target_doclist, set_missing_values)
 
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 80bb914..2cd1be2 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -29,6 +29,9 @@
 		self.set_missing_values()
 		
 		self.set_taxes("other_charges", "charge")
+		
+		if self.meta.get_field("debit_to") and not self.doc.debit_to:
+			self.doc.debit_to = self.get_debit_to().get("debit_to")
 			
 	def set_missing_values(self, for_validate=False):
 		super(SellingController, self).set_missing_values(for_validate)
diff --git a/hr/doctype/salary_structure/salary_structure.py b/hr/doctype/salary_structure/salary_structure.py
index 37b0b47..c7b33e4 100644
--- a/hr/doctype/salary_structure/salary_structure.py
+++ b/hr/doctype/salary_structure/salary_structure.py
@@ -117,7 +117,8 @@
 				["depend_on_lwp", "d_depends_on_lwp"],
 				["d_modified_amt", "d_amount"],
 				["d_modified_amt", "d_modified_amount"]
-			]
+			],
+			"add_if_empty": True
 		}, 
 		"Salary Structure Earning": {
 			"doctype": "Salary Slip Earning", 
@@ -125,7 +126,8 @@
 				["depend_on_lwp", "e_depends_on_lwp"], 
 				["modified_value", "e_modified_amount"],
 				["modified_value", "e_amount"]
-			]
+			],
+			"add_if_empty": True
 		}
 	}, target_doclist, postprocess)
 
diff --git a/selling/doctype/installation_note/installation_note.py b/selling/doctype/installation_note/installation_note.py
index a91db0e..fd85443 100644
--- a/selling/doctype/installation_note/installation_note.py
+++ b/selling/doctype/installation_note/installation_note.py
@@ -52,12 +52,6 @@
 		sales_com_obj = get_obj(dt = 'Sales Common')
 		sales_com_obj.check_active_sales_items(self)
 		sales_com_obj.get_prevdoc_date(self)
- 	
-	def validate_prev_docname(self):
-		for d in getlist(self.doclist, 'installed_item_details'): 
-			if self.doc.delivery_note_no == d.prevdoc_docname:
-				msgprint(cstr(self.doc.delivery_note_no) + 
-					" delivery note details have already been pulled", raise_exception=1)
 
 	def validate_fiscal_year(self):
 		from accounts.utils import validate_fiscal_year
diff --git a/selling/doctype/opportunity/opportunity.py b/selling/doctype/opportunity/opportunity.py
index fe4042b..d4a97b6 100644
--- a/selling/doctype/opportunity/opportunity.py
+++ b/selling/doctype/opportunity/opportunity.py
@@ -192,7 +192,8 @@
 				"parent": "prevdoc_docname", 
 				"parenttype": "prevdoc_doctype", 
 				"uom": "stock_uom"
-			}
+			},
+			"add_if_empty": True
 		}
 	}, target_doclist)
 		
diff --git a/selling/doctype/quotation/quotation.py b/selling/doctype/quotation/quotation.py
index f1e11a0..1b286d1 100644
--- a/selling/doctype/quotation/quotation.py
+++ b/selling/doctype/quotation/quotation.py
@@ -235,7 +235,10 @@
 		if customer:
 			target[0].customer = customer.doc.name
 			target[0].customer_name = customer.doc.customer_name
-	
+			
+		si = webnotes.bean(target)
+		si.run_method("onload_post_render")
+			
 	doclist = get_mapped_doclist("Quotation", source_name, {
 			"Quotation": {
 				"doctype": "Sales Order", 
@@ -247,13 +250,16 @@
 				"doctype": "Sales Order Item", 
 				"field_map": {
 					"parent": "prevdoc_docname"
-				}
+				}, 
+				"add_if_empty": True
 			}, 
 			"Sales Taxes and Charges": {
 				"doctype": "Sales Taxes and Charges",
+				"add_if_empty": True
 			}, 
 			"Sales Team": {
 				"doctype": "Sales Team",
+				"add_if_empty": True
 			}
 		}, target_doclist, set_missing_values, ignore_permissions=ignore_permissions)
 		
diff --git a/selling/doctype/sales_order/sales_order.py b/selling/doctype/sales_order/sales_order.py
index be57853..13f0e9f 100644
--- a/selling/doctype/sales_order/sales_order.py
+++ b/selling/doctype/sales_order/sales_order.py
@@ -340,6 +340,10 @@
 			from tabCurrency where ifnull(enabled,0)=1""")))
 	}
 	
+def set_missing_values(source, target):
+	bean = webnotes.bean(target)
+	bean.run_method("onload_post_render")
+	
 @webnotes.whitelist()
 def make_material_request(source_name, target_doclist=None):	
 	def postprocess(source, doclist):
@@ -395,11 +399,13 @@
 		}, 
 		"Sales Taxes and Charges": {
 			"doctype": "Sales Taxes and Charges", 
+			"add_if_empty": True
 		}, 
 		"Sales Team": {
-			"doctype": "Sales Team", 
+			"doctype": "Sales Team",
+			"add_if_empty": True
 		}
-	}, target_doclist)
+	}, target_doclist, set_missing_values)
 	
 	return [d.fields for d in doclist]
 
@@ -409,10 +415,6 @@
 		target.export_amount = flt(obj.amount) - flt(obj.billed_amt)
 		target.amount = target.export_amount / flt(source_parent.conversion_rate)
 		target.qty = obj.basic_rate and target.amount / flt(obj.basic_rate) or obj.qty
-		
-	def update_accounts(source, target):
-		si = webnotes.bean(target)
-		si.run_method("update_accounts")
 	
 	doclist = get_mapped_doclist("Sales Order", source_name, {
 		"Sales Order": {
@@ -433,11 +435,13 @@
 		}, 
 		"Sales Taxes and Charges": {
 			"doctype": "Sales Taxes and Charges", 
+			"add_if_empty": True
 		}, 
 		"Sales Team": {
 			"doctype": "Sales Team", 
+			"add_if_empty": True
 		}
-	}, target_doclist, update_accounts)
+	}, target_doclist, set_missing_values)
 	
 	return [d.fields for d in doclist]
 	
@@ -462,7 +466,8 @@
 				"doctype": "Maintenance Schedule Item", 
 				"field_map": {
 					"parent": "prevdoc_docname"
-				}
+				},
+				"add_if_empty": True
 			}
 		}, target_doclist)
 	
@@ -491,7 +496,8 @@
 				"field_map": {
 					"parent": "prevdoc_docname", 
 					"parenttype": "prevdoc_doctype"
-				}
+				},
+				"add_if_empty": True
 			}
 		}, target_doclist)
 	
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index 77038bb..77c70ee 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -136,22 +136,19 @@
 	def validate_for_items(self):
 		check_list, chk_dupl_itm = [], []
 		for d in getlist(self.doclist,'delivery_note_details'):
-			ch = sql("select is_stock_item from `tabItem` where name = '%s'"%d.item_code)
-			if d.prevdoc_doctype and d.prevdoc_detail_docname and ch and ch[0][0]=='Yes':
-				self.validate_items_with_prevdoc(d)
-
-			# validates whether item is not entered twice
 			e = [d.item_code, d.description, d.warehouse, d.prevdoc_docname or '', d.batch_no or '']
 			f = [d.item_code, d.description, d.prevdoc_docname or '']
 
-			if ch and ch[0][0] == 'Yes':
+			if webnotes.conn.get_value("Item", d.item_code, "is_stock_item") == 'Yes':
 				if e in check_list:
-					msgprint("Please check whether item %s has been entered twice wrongly." % d.item_code)
+					msgprint("Please check whether item %s has been entered twice wrongly." 
+						% d.item_code)
 				else:
 					check_list.append(e)
-			elif ch and ch[0][0] == 'No':
+			else:
 				if f in chk_dupl_itm:
-					msgprint("Please check whether item %s has been entered twice wrongly." % d.item_code)
+					msgprint("Please check whether item %s has been entered twice wrongly." 
+						% d.item_code)
 				else:
 					chk_dupl_itm.append(f)
 
@@ -162,16 +159,6 @@
 					msgprint("Please enter Warehouse for item %s as it is stock item"
 						% d['item_code'], raise_exception=1)
 
-	def validate_items_with_prevdoc(self, d):
-		"""check if same item, warehouse present in prevdoc"""
-		prev_item_dt = (d.prevdoc_doctype == 'Sales Order') and 'Sales Order Item' or 'Purchase Receipt Item'
-		data = sql("select item_code from `tab%s` where parent = '%s' and name = '%s'"\
-		 	% (prev_item_dt, d.prevdoc_docname, d.prevdoc_detail_docname))
-		if not data or data[0][0] != d.item_code:
-			msgprint("Item: %s is not matching with Sales Order: %s. Sales Order might be modified after \
-				fetching data from it. Please delete items and fetch again." \
-				% (d.item_code, d.prevdoc_docname), raise_exception=1)
-
 
 	def update_current_stock(self):
 		for d in getlist(self.doclist, 'delivery_note_details'):
@@ -375,7 +362,7 @@
 		
 	def update_accounts(source, target):
 		si = webnotes.bean(target)
-		si.run_method("update_accounts")
+		si.run_method("onload_post_render")
 	
 	doclist = get_mapped_doclist("Delivery Note", source_name, 	{
 		"Delivery Note": {
@@ -397,12 +384,14 @@
 		}, 
 		"Sales Taxes and Charges": {
 			"doctype": "Sales Taxes and Charges", 
+			"add_if_empty": True
 		}, 
 		"Sales Team": {
 			"doctype": "Sales Team", 
 			"field_map": {
 				"incentives": "incentives"
-			}
+			},
+			"add_if_empty": True
 		}
 	}, target_doclist, update_accounts)
 	
diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py
index 0ec7336..14d0c03 100644
--- a/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -49,12 +49,6 @@
 	def get_bin_details(self, arg = ''):
 		return get_obj(dt='Purchase Common').get_bin_details(arg)
 
-	# validate if PO has been pulled twice
-	def validate_prev_docname(self):
-		for d in getlist(self.doclist, 'purchase_receipt_details'):
-			if self.doc.purchase_order_no and d.prevdoc_docname and self.doc.purchase_order_no == d.prevdoc_docname:
-				msgprint(cstr(self.doc.purchase_order_no) + " Purchase Order details have already been pulled. ")
-				raise Exception
 
 	# validate accepted and rejected qty
 	def validate_accepted_rejected_qty(self):
@@ -383,6 +377,7 @@
 		}, 
 		"Purchase Taxes and Charges": {
 			"doctype": "Purchase Taxes and Charges", 
+			"add_if_empty": True
 		}
 	}, target_doclist, set_missing_values)
 
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index ed404dd..8f168f6 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -277,12 +277,18 @@
 		for key, val in ref.items():
 			is_child = val.get("is_child_table")
 			ref_doc = {}
+			item_ref_dn = []
 			for d in self.doclist.get({"doctype": source_dt}):
 				ref_dn = d.fields.get(val["ref_dn_field"])
 				if ref_dn:
 					if is_child:
 						self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
-					elif ref_dn:								
+						if ref_dn not in item_ref_dn:
+							item_ref_dn.append(ref_dn)
+						else:
+							webnotes.msgprint(_("Row ") + cstr(d.idx + 1) + 
+								_(": Duplicate row from same ") + key, raise_exception=1)
+					elif ref_dn:
 						ref_doc.setdefault(key, [])
 						if ref_dn not in ref_doc[key]:
 							ref_doc[key].append(ref_dn)