validate with prevdoc
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 8d646ba..b4af02b 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -69,16 +69,7 @@
 		self.clear_unallocated_advances("Purchase Invoice Advance", "advance_allocation_details")
 		self.check_for_acc_head_of_supplier()
 		self.check_for_stopped_status()
-
-		self.po_list, self.pr_list = [], []
-		for d in getlist(self.doclist, 'entries'):
-			self.validate_supplier(d)
-			self.validate_po_pr(d)
-			if not d.purchase_order in self.po_list:
-				self.po_list.append(d.purchase_order)
-			if not d.purhcase_receipt in self.pr_list:
-				self.pr_list.append(d.purchase_receipt)
-
+		self.validate_with_previous_doc()
 
 		if not self.doc.is_opening:
 			self.doc.is_opening = 'No'
@@ -167,10 +158,7 @@
 		if (self.doc.currency == default_currency and flt(self.doc.conversion_rate) != 1.00) or not self.doc.conversion_rate or (self.doc.currency != default_currency and flt(self.doc.conversion_rate) == 1.00):
 			msgprint("Message: Please Enter Appropriate Conversion Rate.")
 			raise Exception				
-
-	# 1. Check whether bill is already booked against this bill no. or not
-	# 2. Add Remarks
-	# ---------------------------------------------------------------------
+			
 	def validate_bill_no(self):
 		if self.doc.bill_no and self.doc.bill_no.lower().strip()	not in ['na', 'not applicable', 'none']:
 			b_no = sql("select bill_no, name, ifnull(is_opening,'') from `tabPurchase Invoice` where bill_no = '%s' and credit_to = '%s' and docstatus = 1 and name != '%s' " % (self.doc.bill_no, self.doc.credit_to, self.doc.name))
@@ -183,16 +171,10 @@
 			if not self.doc.remarks:
 				self.doc.remarks = "No Remarks"
 					
-	# Validate Bill No Date
-	# ---------------------
 	def validate_bill_no_date(self):
 		if self.doc.bill_no and not self.doc.bill_date and self.doc.bill_no.lower().strip() not in ['na', 'not applicable', 'none']:
 			msgprint(_("Please enter Bill Date"), raise_exception=1)
 
-	# 1. Credit To Account Exists
-	# 2. Is a Credit Account
-	# 3. Is not a PL Account
-	# ----------------------------
 	def validate_credit_acc(self):
 		acc = sql("select debit_or_credit, is_pl_account from tabAccount where name = %s", 
 			self.doc.credit_to)
@@ -226,50 +208,30 @@
 				if stopped:
 					msgprint("One cannot do any transaction against 'Purchase Order' : %s, it's status is 'Stopped'" % (d.purhcase_order))
 					raise Exception
-					
-	# Validate Supplier
-	# -----------------
-	def validate_supplier(self, d):
-		supplier = ''
-		if d.purchase_order and not d.purchase_order in self.po_list:
-			supplier = sql("select supplier from `tabPurchase Order` where name = '%s'" % d.purchase_order)[0][0]
-			if supplier and not cstr(self.doc.supplier) == cstr(supplier):
-				msgprint("Supplier name %s do not match with supplier name	of purhase order: %s." %(self.doc.supplier,cstr(d.purchase_order)))
-				raise Exception , " Validation Error "
-
-		if d.purchase_receipt and not d.purchase_receipt in self.pr_list:
-			supplier = sql("select supplier from `tabPurchase Receipt` where name = '%s'" % d.purchase_receipt)[0][0]
-			if supplier and not cstr(self.doc.supplier) == cstr(supplier):
-				msgprint("Supplier name %s do not match with supplier name	of %s %s." %(self.doc.supplier,cstr(d.purchase_receipt)))
-				raise Exception , " Validation Error "
-
-	def validate_reference_value(self):
-		pass
-	
-	# Validate PO and PR
-	# -------------------
-	def validate_po_pr(self, d):
-		# check po / pr for qty and rates and currency and conversion rate
-
-		# currency, import_rate must be equal to currency, import_rate of purchase order
-		if d.purchase_order and not d.purchase_order in self.po_list:
-			# currency
-			currency = cstr(sql("select currency from `tabPurchase Order` where name = '%s'" % d.purchase_order)[0][0])
-			if not cstr(currency) == cstr(self.doc.currency):
-				msgprint("Purchase Order: " + cstr(d.purchase_order) + " currency : " + cstr(currency) + " does not match with currency of current document.")
-				raise Exception
-			# import_rate
-			rate = flt(sql('select import_rate from `tabPurchase Order Item` where item_code=%s and parent=%s and name = %s', (d.item_code, d.purchase_order, d.po_detail))[0][0])
-			if abs(rate - flt(d.import_rate)) > 1 and cint(webnotes.defaults.get_global_default('maintain_same_rate')):
-				msgprint("Import Rate for %s in the Purchase Order is %s. Rate must be same as Purchase Order Rate" % (d.item_code,rate))
-				raise Exception
-									
-		if d.purchase_receipt and not d.purchase_receipt in self.pr_list:
-			# currency , conversion_rate
-			data = sql("select currency, conversion_rate from `tabPurchase Receipt` where name = '%s'" % d.purchase_receipt, as_dict = 1)
-			if not cstr(data[0]['currency']) == cstr(self.doc.currency):
-				msgprint("Purchase Receipt: " + cstr(d.purchase_receipt) + " currency : " + cstr(data[0]['currency']) + " does not match with currency of current document.")
-				raise Exception
+		
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Purchase Order": {
+				"ref_dn_field": "purchase_order",
+				"compare_fields": [["supplier", "="], ["company", "="], ["currency", "="]],
+			},
+			"Purchase Order Item": {
+				"ref_dn_field": "po_detail",
+				"compare_fields": [["export_rate", "="], ["project_name", "="], ["item_code", "="], 
+					["uom", "="]],
+				"is_child_table": True
+			},
+			"Purchase Receipt": {
+				"ref_dn_field": "purchase_receipt",
+				"compare_fields": [["customer", "="], ["company", "="], ["currency", "="]],
+			},
+			"Purchase Receipt Item": {
+				"ref_dn_field": "pr_detail",
+				"compare_fields": [["export_rate", "="], ["project_name", "="], ["item_code", "="], 
+					["uom", "="]],
+				"is_child_table": True
+			}
+		})
 					
 	def set_aging_date(self):
 		if self.doc.is_opening != 'Yes':
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index c155636..3dce461 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -73,6 +73,7 @@
 		self.validate_posting_time()
 		self.so_dn_required()
 		self.validate_proj_cust()
+		self.validate_with_previous_doc()
 		sales_com_obj = get_obj('Sales Common')
 		sales_com_obj.check_stop_sales_order(self)
 		sales_com_obj.check_active_sales_items(self)
@@ -80,7 +81,6 @@
 		sales_com_obj.validate_max_discount(self, 'entries')
 		sales_com_obj.validate_fiscal_year(self.doc.fiscal_year, 
 			self.doc.posting_date,'Posting Date')
-		self.validate_customer()
 		self.validate_customer_account()
 		self.validate_debit_acc()
 		self.validate_fixed_asset_account()
@@ -105,11 +105,9 @@
 		self.set_aging_date()
 		self.set_against_income_account()
 		self.validate_c_form()
-		self.validate_rate_with_refdoc()
 		self.validate_time_logs_are_submitted()
 		self.validate_recurring_invoice()
-		
-		
+
 	def on_submit(self):
 		if cint(self.doc.update_stock) == 1:
 			sl_obj = get_obj("Stock Ledger")
@@ -375,19 +373,6 @@
 				(not acc_head and (self.doc.debit_to != cstr(self.doc.customer) + " - " + self.get_company_abbr())):
 				msgprint("Debit To: %s do not match with Customer: %s for Company: %s.\n If both correctly entered, please select Master Type \
 					and Master Name in account master." %(self.doc.debit_to, self.doc.customer,self.doc.company), raise_exception=1)
-			
-
-	def validate_customer(self):
-		"""	Validate customer name with SO and DN"""
-		if self.doc.customer:
-			for d in getlist(self.doclist,'entries'):
-				dt = d.delivery_note and 'Delivery Note' or d.sales_order and 'Sales Order' or ''
-				if dt:
-					dt_no = d.delivery_note or d.sales_order
-					cust = webnotes.conn.get_value(dt, dt_no, "customer")
-					if cust and cstr(cust) != cstr(self.doc.customer):
-						msgprint("Customer %s does not match with customer of %s: %s." 
-							%(self.doc.customer, dt, dt_no), raise_exception=1)
 
 
 	def validate_debit_acc(self):
@@ -414,6 +399,31 @@
 			elif item and item[0][1] == 'Yes' and not acc[0][0] == 'Fixed Asset Account':
 				msgprint("Please select income head with account type 'Fixed Asset Account' as Item %s is an asset item" % d.item_code)
 				raise Exception
+				
+		
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Sales Order": {
+				"ref_dn_field": "sales_order",
+				"compare_fields": [["customer", "="], ["company", "="], ["project_name", "="],
+					["currency", "="]],
+			},
+			"Sales Order Item": {
+				"ref_dn_field": "so_detail",
+				"compare_fields": [["export_rate", "="]],
+				"is_child_table": True
+			},
+			"Delivery Note": {
+				"ref_dn_field": "delivery_note",
+				"compare_fields": [["customer", "="], ["company", "="], ["project_name", "="],
+					["currency", "="]],
+			},
+			"Delivery Note Item": {
+				"ref_dn_field": "dn_detail",
+				"compare_fields": [["export_rate", "="]],
+				"is_child_table": True
+			}
+		})
 
 	def set_aging_date(self):
 		if self.doc.is_opening != 'Yes':
@@ -489,22 +499,6 @@
 
 			webnotes.conn.set(self.doc, 'c_form_no', '')
 			
-	def validate_rate_with_refdoc(self):
-		"""Validate values with reference document with previous document"""
-		for d in self.doclist.get({"parentfield": "entries"}):
-			if d.so_detail:
-				self.check_value("Sales Order", d.sales_order, d.so_detail, 
-					d.export_rate, d.item_code)
-			if d.dn_detail:
-				self.check_value("Delivery Note", d.delivery_note, d.dn_detail, 
-					d.export_rate, d.item_code)
-				
-	def check_value(self, ref_dt, ref_dn, ref_item_dn, val, item_code):
-		ref_val = webnotes.conn.get_value(ref_dt + " Item", ref_item_dn, "export_rate")
-		if flt(ref_val, 2) != flt(val, 2):
-			msgprint(_("Rate is not matching with ") + ref_dt + ": " + ref_dn + 
-				_(" for item: ") + item_code, raise_exception=True)
-
 	def update_current_stock(self):
 		for d in getlist(self.doclist, 'entries'):
 			if d.item_code and d.warehouse:
diff --git a/accounts/page/accounts_home/accounts_home.js b/accounts/page/accounts_home/accounts_home.js
index 1f41fcc..aa527f7 100644
--- a/accounts/page/accounts_home/accounts_home.js
+++ b/accounts/page/accounts_home/accounts_home.js
@@ -205,16 +205,6 @@
 				doctype: "Journal Voucher"
 			},
 			{
-				"label":wn._("Delivered Items To Be Billed"),
-				route: "query-report/Delivered Items To Be Billed",
-				doctype: "Sales Invoice"
-			},
-			{
-				"label":wn._("Received Items To Be Billed"),
-				route: "query-report/Received Items To Be Billed",
-				doctype: "Purchase Invoice"
-			},
-			{
 				"label":wn._("Ordered Items To Be Billed"),
 				route: "query-report/Ordered Items To Be Billed",
 				doctype: "Sales Invoice"
diff --git a/accounts/report/delivered_items_to_be_billed/__init__.py b/accounts/report/delivered_items_to_be_billed/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/accounts/report/delivered_items_to_be_billed/__init__.py
+++ /dev/null
diff --git a/accounts/report/delivered_items_to_be_billed/delivered_items_to_be_billed.txt b/accounts/report/delivered_items_to_be_billed/delivered_items_to_be_billed.txt
deleted file mode 100644
index a9a18e7..0000000
--- a/accounts/report/delivered_items_to_be_billed/delivered_items_to_be_billed.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-[
- {
-  "creation": "2013-05-02 15:20:25", 
-  "docstatus": 0, 
-  "modified": "2013-07-08 11:08:23", 
-  "modified_by": "Administrator", 
-  "owner": "Administrator"
- }, 
- {
-  "doctype": "Report", 
-  "is_standard": "Yes", 
-  "name": "__common__", 
-  "query": "select \n `tabDelivery Note`.`name` as \"Delivery Note:Link/Delivery Note:120\",\n`tabDelivery Note`.`customer` as \"Customer:Link/Customer:120\",\n`tabDelivery Note`.`status` as \"Status\",\n `tabDelivery Note`.`posting_date` as \"Date:Date\",\n `tabDelivery Note`.`project_name` as \"Project\",\n `tabDelivery Note Item`.item_code as \"Item:Link/Item:120\",\n `tabDelivery Note Item`.amount as \"Amount:Currency:110\",\n (`tabDelivery Note Item`.billed_amt * ifnull(`tabDelivery Note`.conversion_rate, 1)) as \"Billed Amount:Currency:110\",\n (ifnull(`tabDelivery Note Item`.amount,0) - (ifnull(`tabDelivery Note Item`.billed_amt,0) * ifnull(`tabDelivery Note`.conversion_rate, 1))) as \"Pending Amount:Currency:120\",\n `tabDelivery Note Item`.item_name as \"Item Name::150\",\n `tabDelivery Note Item`.description as \"Description:Data:200\",\n  `tabDelivery Note Item`.prevdoc_docname as \"Sales Order:Link/Sales Order:120\",\n  `tabDelivery Note Item`.prevdoc_date as \"SO Date:Date:100\",\n  `tabDelivery Note`.address_display as \"Customer Address::150\"\nfrom\n `tabDelivery Note`, `tabDelivery Note Item`\nwhere\n `tabDelivery Note Item`.`parent` = `tabDelivery Note`.`name`\n and `tabDelivery Note`.docstatus = 1\n and `tabDelivery Note`.status != \"Stopped\"\n and ifnull(`tabDelivery Note Item`.billed_amt,0) < ifnull(`tabDelivery Note Item`.export_amount,0)\norder by `tabDelivery Note`.posting_date asc", 
-  "ref_doctype": "Sales Invoice", 
-  "report_name": "Delivered Items To Be Billed", 
-  "report_type": "Query Report"
- }, 
- {
-  "doctype": "Report", 
-  "name": "Delivered Items To Be Billed"
- }
-]
\ No newline at end of file
diff --git a/accounts/report/received_items_to_be_billed/__init__.py b/accounts/report/received_items_to_be_billed/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/accounts/report/received_items_to_be_billed/__init__.py
+++ /dev/null
diff --git a/accounts/report/received_items_to_be_billed/received_items_to_be_billed.txt b/accounts/report/received_items_to_be_billed/received_items_to_be_billed.txt
deleted file mode 100644
index 90617f8..0000000
--- a/accounts/report/received_items_to_be_billed/received_items_to_be_billed.txt
+++ /dev/null
@@ -1,23 +0,0 @@
-[
- {
-  "creation": "2013-05-28 15:57:59", 
-  "docstatus": 0, 
-  "modified": "2013-06-05 12:59:17", 
-  "modified_by": "Administrator", 
-  "owner": "Administrator"
- }, 
- {
-  "add_total_row": 1, 
-  "doctype": "Report", 
-  "is_standard": "Yes", 
-  "name": "__common__", 
-  "query": "select \n    `tabPurchase Receipt`.`name` as \"Purchase Receipt:Link/Purchase Receipt:120\",\n    `tabPurchase Receipt`.`posting_date` as \"Date:Date:100\",\n    `tabPurchase Receipt`.`supplier` as \"Supplier:Link/Supplier:120\",\n    `tabPurchase Receipt Item`.`project_name` as \"Project\",\n\t`tabPurchase Receipt Item`.item_code as \"Item Code:Link/Item:120\",\n\t`tabPurchase Receipt Item`.amount as \"Amount:Currency:100\",\n\t`tabPurchase Receipt Item`.billed_amt as \"Billed Amount:Currency:100\", \n\t(`tabPurchase Receipt Item`.amount - ifnull(`tabPurchase Receipt Item`.billed_amt, 0)) as \"Amount to Bill:Currency:100\",\n\t`tabPurchase Receipt Item`.item_name as \"Item Name::150\",\n\t`tabPurchase Receipt Item`.description as \"Description::200\"\nfrom\n\t`tabPurchase Receipt`, `tabPurchase Receipt Item`\nwhere\n\t`tabPurchase Receipt Item`.`parent` = `tabPurchase Receipt`.`name`\n\tand `tabPurchase Receipt`.docstatus = 1\n\tand `tabPurchase Receipt`.status != \"Stopped\"\n\tand ifnull(`tabPurchase Receipt Item`.billed_amt, 0) < ifnull(`tabPurchase Receipt Item`.amount, 0)\norder by `tabPurchase Receipt`.posting_date asc", 
-  "ref_doctype": "Purchase Invoice", 
-  "report_name": "Received Items To Be Billed", 
-  "report_type": "Query Report"
- }, 
- {
-  "doctype": "Report", 
-  "name": "Received Items To Be Billed"
- }
-]
\ No newline at end of file
diff --git a/buying/doctype/purchase_common/purchase_common.py b/buying/doctype/purchase_common/purchase_common.py
index 4bccce0..f7db9bf 100644
--- a/buying/doctype/purchase_common/purchase_common.py
+++ b/buying/doctype/purchase_common/purchase_common.py
@@ -17,8 +17,7 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes.utils import add_days, cint, cstr, flt
-from webnotes.model.doc import addchild
+from webnotes.utils import add_days, cstr, flt
 from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
 from webnotes import msgprint, _
@@ -184,29 +183,6 @@
 			if item[0][1] != 'Yes' and item[0][2] != 'Yes':
 				msgprint("Item %s is not a purchase item or sub-contracted item. Please check" % (d.item_code), raise_exception=True)
 			
-			if d.fields.has_key('prevdoc_docname') and d.prevdoc_docname:
-				# check warehouse, uom	in previous doc and in current doc are same.
-				data = sql("select item_code, warehouse, uom from `tab%s` where name = '%s'" % ( self.doctype_dict[d.prevdoc_doctype], d.prevdoc_detail_docname), as_dict = 1)
-				if not data:
-					msgprint("Please fetch data in Row " + cstr(d.idx) + " once again or please contact Administrator.")
-					raise Exception
-				
-				# Check if Item Code has been modified.
-				if not cstr(data[0]['item_code']) == cstr(d.item_code):
-					msgprint("Please check Item %s is not present in %s %s ." % (d.item_code, d.prevdoc_doctype, d.prevdoc_docname))
-					raise Exception
-				
-				if cstr(data[0]['warehouse']) and \
-						not cstr(data[0]['warehouse']) == cstr(d.warehouse):
-					msgprint("""Please check warehouse %s of Item %s 
-						which is not present in %s %s""" % (d.warehouse, d.item_code, 
-						d.prevdoc_doctype, d.prevdoc_docname), raise_exception=1)
-				
-				#	Check if UOM has been modified.
-				if not cstr(data[0]['uom']) == cstr(d.uom) and not cstr(d.prevdoc_doctype) == 'Material Request':
-					msgprint("Please check UOM %s of Item %s which is not present in %s %s ." % \
-						(d.uom, d.item_code, d.prevdoc_doctype, d.prevdoc_docname), raise_exception=True)
-			
 			# list criteria that should not repeat if item is stock item
 			e = [d.schedule_date, d.item_code, d.description, d.warehouse, d.uom, d.fields.has_key('prevdoc_docname') and d.prevdoc_docname or '', d.fields.has_key('prevdoc_detail_docname') and d.prevdoc_detail_docname or '', d.fields.has_key('batch_no') and d.batch_no or '']
 			
@@ -215,7 +191,7 @@
 			
 			ch = sql("select is_stock_item from `tabItem` where name = '%s'"%d.item_code)
 			
-			if ch and ch[0][0] == 'Yes':			
+			if ch and ch[0][0] == 'Yes':	
 				# check for same items
 				if e in check_list:
 					msgprint("""Item %s has been entered more than once with same description, schedule date, warehouse and uom.\n 
@@ -239,8 +215,6 @@
 			msgprint("One cannot do any transaction against %s : %s, it's status is 'Stopped'" % 
 				( doctype, docname), raise_exception=1)
 	
-	def validate_reference_value(self, obj=None):
-		pass
 	
 	# Check Docstatus of Next DocType on Cancel AND of Previous DocType on Submit
 	def check_docstatus(self, check, doctype, docname , detail_doctype = ''):
diff --git a/buying/doctype/purchase_order/purchase_order.py b/buying/doctype/purchase_order/purchase_order.py
index 4972f66..7d76b7f 100644
--- a/buying/doctype/purchase_order/purchase_order.py
+++ b/buying/doctype/purchase_order/purchase_order.py
@@ -47,15 +47,28 @@
 		pc_obj = get_obj(dt='Purchase Common')
 		pc_obj.validate_for_items(self)
 		pc_obj.get_prevdoc_date(self)
-
-		self.validate_doc(pc_obj)
 		self.check_for_stopped_status(pc_obj)
 
+		self.validate_with_previous_doc()
 		self.validate_for_subcontracting()
 		self.update_raw_materials_supplied("po_raw_material_details")
 		
 	def validate_fiscal_year(self):
 		get_obj(dt = 'Purchase Common').validate_fiscal_year(self.doc.fiscal_year,self.doc.transaction_date,'PO Date')
+		
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Supplier Quotation": {
+				"ref_dn_field": "supplier_quotation",
+				"compare_fields": [["supplier", "="], ["company", "="], ["currency", "="]],
+			},
+			"Supplier Quotation Item": {
+				"ref_dn_field": "supplier_quotation_item",
+				"compare_fields": [["export_rate", "="], ["project_name", "="], ["item_code", "="], 
+					["uom", "="]],
+				"is_child_table": True
+			}
+		})
 
 	# get available qty at warehouse
 	def get_bin_details(self, arg = ''):
@@ -69,10 +82,6 @@
 	
 	def get_last_purchase_rate(self):
 		get_obj('Purchase Common').get_last_purchase_rate(self)
-		
-	def validate_doc(self,pc_obj):
-		# Validate values with reference document
-		pc_obj.validate_reference_value(obj = self)
 
 	# Check for Stopped status 
 	def check_for_stopped_status(self, pc_obj):
diff --git a/buying/doctype/supplier_quotation/supplier_quotation.py b/buying/doctype/supplier_quotation/supplier_quotation.py
index 36e8ede..f49782f 100644
--- a/buying/doctype/supplier_quotation/supplier_quotation.py
+++ b/buying/doctype/supplier_quotation/supplier_quotation.py
@@ -36,6 +36,7 @@
 		
 		self.validate_fiscal_year()
 		self.validate_common()
+		self.validate_with_previous_doc()
 
 	def on_submit(self):
 		purchase_controller = webnotes.get_obj("Purchase Common")
@@ -53,11 +54,24 @@
 		get_obj(dt = 'Purchase Common').validate_fiscal_year( \
 			self.doc.fiscal_year, self.doc.transaction_date, 'Quotation Date')
 			
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Material Request": {
+				"ref_dn_field": "prevdoc_docname",
+				"compare_fields": [["company", "="]],
+			},
+			"Material Request Item": {
+				"ref_dn_field": "prevdoc_detail_docname",
+				"compare_fields": [["item_code", "="], ["uom", "="]],
+				"is_child_table": True
+			}
+		})
+
+			
 	def validate_common(self):
 		pc = get_obj('Purchase Common')
 		pc.validate_for_items(self)
 		pc.get_prevdoc_date(self)
-		pc.validate_reference_value(self)
 
 @webnotes.whitelist()
 def make_purchase_order(source_name, target_doclist=None):
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index 5439030..957958c 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -358,6 +358,7 @@
 	def get_company_default(self, fieldname):
 		from accounts.utils import get_company_default
 		return get_company_default(self.doc.company, fieldname)
+			
 		
 	@property
 	def stock_items(self):
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 2f77295..80bb914 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -55,7 +55,7 @@
 	def set_price_list_and_item_details(self):
 		self.set_price_list_currency("Selling")
 		self.set_missing_item_details(get_item_details)
-								
+										
 	def get_other_charges(self):
 		self.doclist = self.doc.clear_table(self.doclist, "other_charges")
 		self.set_taxes("other_charges", "charge")
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 434c132..f3aaa15 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -19,7 +19,7 @@
 	"execute:webnotes.reload_doc('core', 'doctype', 'docfield')",
 	"execute:webnotes.reload_doc('core', 'doctype', 'docperm') # 2013-04-07",
 	"execute:webnotes.reload_doc('core', 'doctype', 'report')",
-	"execute:webnotes.reload_doc('core', 'doctype', 'doctype') # 2013-07-04",
+	"execute:webnotes.reload_doc('core', 'doctype', 'doctype') # 2013-07-08",
 	"patches.mar_2012.clean_property_setter", 
 	"patches.april_2012.naming_series_patch", 
 	"patches.mar_2012.cleanup_control_panel", 
@@ -246,4 +246,6 @@
 	"patches.june_2013.p09_update_global_defaults",
 	"patches.june_2013.p10_lead_address",
 	"patches.july_2013.p01_remove_doctype_mappers",
+	"execute:webnotes.delete_doc('Report', 'Delivered Items To Be Billed')",
+	"execute:webnotes.delete_doc('Report', 'Received Items To Be Billed')",
 ]
\ No newline at end of file
diff --git a/selling/doctype/installation_note/installation_note.py b/selling/doctype/installation_note/installation_note.py
index 74c9478..951f861 100644
--- a/selling/doctype/installation_note/installation_note.py
+++ b/selling/doctype/installation_note/installation_note.py
@@ -52,7 +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)
-		self.validate_reference_value()
  	
 	def validate_prev_docname(self):
 		for d in getlist(self.doclist, 'installed_item_details'): 
@@ -63,9 +62,6 @@
 	def validate_fiscal_year(self):
 		get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year, self.doc.inst_date, 
 			'Installation Date')
-
-	def validate_reference_value(self):
-		pass
 	
 	def is_serial_no_added(self, item_code, serial_no):
 		ar_required = webnotes.conn.get_value("Item", item_code, "has_serial_no")
diff --git a/selling/doctype/sales_order/sales_order.py b/selling/doctype/sales_order/sales_order.py
index af860fc..2761f7e 100644
--- a/selling/doctype/sales_order/sales_order.py
+++ b/selling/doctype/sales_order/sales_order.py
@@ -173,6 +173,8 @@
 		sales_com_obj.validate_max_discount(self,'sales_order_details')
 		self.doclist = sales_com_obj.make_packing_list(self,'sales_order_details')
 		
+		self.validate_with_previous_doc()
+				
 		if not self.doc.status:
 			self.doc.status = "Draft"
 
@@ -183,6 +185,15 @@
 		if not self.doc.billing_status: self.doc.billing_status = 'Not Billed'
 		if not self.doc.delivery_status: self.doc.delivery_status = 'Not Delivered'
 		
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Quotation": {
+				"ref_dn_field": "prevdoc_docname",
+				"compare_fields": [["customer", "="], ["company", "="], ["currency", "="]]
+			}
+		})
+
+		
 	def check_prev_docstatus(self):
 		for d in getlist(self.doclist, 'sales_order_details'):
 			cancel_quo = sql("select name from `tabQuotation` where docstatus = 2 and name = '%s'" % d.prevdoc_docname)
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index aa3a4d1..897990a 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -118,11 +118,26 @@
 
 		# Set actual qty for each item in selected warehouse
 		self.update_current_stock()
-
+		
+		self.validate_with_previous_doc()
+		
 		self.doc.status = 'Draft'
 		if not self.doc.billing_status: self.doc.billing_status = 'Not Billed'
-		if not self.doc.installation_status: self.doc.installation_status = 'Not Installed'
-
+		if not self.doc.installation_status: self.doc.installation_status = 'Not Installed'	
+		
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Sales Order": {
+				"ref_dn_field": "prevdoc_docname",
+				"compare_fields": [["customer", "="], ["company", "="], ["project_name", "="],
+					["currency", "="]],
+			},
+			"Sales Order Item": {
+				"ref_dn_field": "prevdoc_detail_docname",
+				"compare_fields": [["export_rate", "="]],
+				"is_child_table": True
+			}
+		})
 		
 	def validate_proj_cust(self):
 		"""check for does customer belong to same project as entered.."""
diff --git a/stock/doctype/delivery_note/delivery_note.txt b/stock/doctype/delivery_note/delivery_note.txt
index 3b032ae..10a18c0 100644
--- a/stock/doctype/delivery_note/delivery_note.txt
+++ b/stock/doctype/delivery_note/delivery_note.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-24 19:29:09", 
   "docstatus": 0, 
-  "modified": "2013-07-06 19:04:46", 
+  "modified": "2013-07-08 13:17:51", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -101,7 +101,7 @@
   "fieldname": "customer_address", 
   "fieldtype": "Link", 
   "in_filter": 1, 
-  "label": "Select Shipping Address", 
+  "label": "Billing Address Name", 
   "options": "Address", 
   "print_hide": 1, 
   "read_only": 1
@@ -111,6 +111,22 @@
   "fieldname": "address_display", 
   "fieldtype": "Small Text", 
   "hidden": 1, 
+  "label": "Billing Address", 
+  "read_only": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "shipping_address_name", 
+  "fieldtype": "Link", 
+  "label": "Shipping Address Name", 
+  "options": "Address", 
+  "print_hide": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "shipping_address", 
+  "fieldtype": "Small Text", 
+  "hidden": 1, 
   "label": "Shipping Address", 
   "read_only": 1
  }, 
diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py
index 878dfe8..e7d42e7 100644
--- a/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -91,6 +91,20 @@
 			if exists:
 				webnotes.msgprint("Another Purchase Receipt using the same Challan No. already exists.\
 			Please enter a valid Challan No.", raise_exception=1)
+			
+	def validate_with_previous_doc(self):
+		super(DocType, self).validate_with_previous_doc(self.tname, {
+			"Purchase Order": {
+				"ref_dn_field": "prevdoc_docname",
+				"compare_fields": [["supplier", "="], ["company", "="],	["currency", "="]],
+			},
+			"Purchase Order Item": {
+				"ref_dn_field": "prevdoc_detail_docname",
+				"compare_fields": [["export_rate", "="], ["project_name", "="], ["warehouse", "="], 
+					["uom", "="], ["item_code", "="]],
+				"is_child_table": True
+			}
+		})
 
 	def po_required(self):
 		if webnotes.conn.get_single_value("Buying Settings", "po_required") == 'Yes':
@@ -111,6 +125,7 @@
 		import utilities
 		utilities.validate_status(self.doc.status, ["Draft", "Submitted", "Cancelled"])
 
+		self.validate_with_previous_doc()
 		self.validate_accepted_rejected_qty()
 		self.validate_inspection()						 # Validate Inspection
 		get_obj('Stock Ledger').validate_serial_no(self, 'purchase_receipt_details')
@@ -119,7 +134,6 @@
 		pc_obj = get_obj(dt='Purchase Common')
 		pc_obj.validate_for_items(self)
 		pc_obj.get_prevdoc_date(self)
-		pc_obj.validate_reference_value(self)
 		self.check_for_stopped_status(pc_obj)
 
 		# sub-contracting
diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.py b/support/doctype/maintenance_schedule/maintenance_schedule.py
index 1c25e06..ee7f45a 100644
--- a/support/doctype/maintenance_schedule/maintenance_schedule.py
+++ b/support/doctype/maintenance_schedule/maintenance_schedule.py
@@ -20,7 +20,6 @@
 from webnotes.utils import add_days, cstr, getdate
 from webnotes.model.doc import addchild
 from webnotes.model.bean import getlist
-from webnotes.model.code import get_obj
 from webnotes import msgprint
 
 sql = webnotes.conn.sql
@@ -191,11 +190,7 @@
 					msgprint("Maintenance Schedule against "+d.prevdoc_docname+" already exist")
 					raise Exception
 	
-	# Validate values with reference document
-	#----------------------------------------
-	def validate_reference_value(self):
-		pass
-	
+
 	def validate_serial_no(self):
 		for d in getlist(self.doclist, 'item_maintenance_detail'):
 			cur_s_no=[]			
@@ -219,8 +214,6 @@
 	def validate(self):
 		self.validate_maintenance_detail()
 		self.validate_sales_order()
-		if self.doc.sales_order_no:
-			self.validate_reference_value()
 		self.validate_serial_no()
 		self.validate_start_date()
 	
diff --git a/support/doctype/maintenance_visit/maintenance_visit.py b/support/doctype/maintenance_visit/maintenance_visit.py
index 678385d..e5f7280 100644
--- a/support/doctype/maintenance_visit/maintenance_visit.py
+++ b/support/doctype/maintenance_visit/maintenance_visit.py
@@ -19,7 +19,6 @@
 
 from webnotes.utils import cstr
 from webnotes.model.bean import getlist
-from webnotes.model.code import get_obj
 from webnotes import msgprint
 
 sql = webnotes.conn.sql
@@ -38,13 +37,7 @@
 			'item_name' : item and item[0]['item_name'] or '',
 			'description' : item and item[0]['description'] or ''
 		}
-		return ret		
-
-	def validate_reference_value(self, check_for):
-		if check_for == 'Sales Order':
-			pass
-		elif check_for == 'Customer Issue':
-			pass
+		return ret
 			
 	def validate_serial_no(self):
 		for d in getlist(self.doclist, 'maintenance_visit_details'):
@@ -57,16 +50,6 @@
 		if not getlist(self.doclist, 'maintenance_visit_details'):
 			msgprint("Please enter maintenance details")
 			raise Exception
-			
-		check_for = ''
-		for d in getlist(self.doclist, 'maintenance_visit_details'):
-			if d.prevdoc_doctype == 'Sales Order':
-				check_for = 'Sales Order'
-			elif d.prevdoc_doctype == 'Customer Issue':
-				check_for = 'Customer Issue'
-			
-		if check_for:
-			self.validate_reference_value(check_for)
 
 		self.validate_serial_no()
 	
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index 7794444..5994ae5 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -298,6 +298,26 @@
 				})
 			
 			webnotes.bean(event_doclist).insert()
+			
+	def validate_with_previous_doc(self, source_dt, ref):
+		for key, val in ref.items():
+			ref_doc = {}
+			for d in self.doclist.get({"doctype": source_dt}):
+				if d.fields[val["ref_dn_field"]]:
+					ref_doc.setdefault(key, d.fields[val["ref_dn_field"]])
+
+			if val.get("is_child_table"):
+				self.compare_values(ref_doc, val["compare_fields"], d)
+			else:
+				self.compare_values(ref_doc, val["compare_fields"])
+	
+	def compare_values(self, ref_doc, fields, doc=None):
+		for ref_doctype, ref_docname in ref_doc.items():
+			prevdoc_values = webnotes.conn.get_value(ref_doctype, ref_docname, 
+				[d[0] for d in fields], as_dict=1)
+			
+			for field, condition in fields:
+				self.validate_value(field, condition, prevdoc_values[field], doc)
 
 def get_default_address_and_contact(party_field, party_name, fetch_shipping_address=False):
 	out = {}