mapping of relevant fields
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index d7e14e1..66515e3 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -73,6 +73,7 @@
 		self.validate_fixed_asset_account()
 		self.create_remarks()
 		self.set_status()
+		self.validate_inter_company_supplier()
 
 	def validate_cash(self):
 		if not self.cash_bank_account and flt(self.paid_amount):
@@ -119,6 +120,21 @@
 
 		self.party_account_currency = account.account_currency
 
+	def validate_inter_company_supplier(self):
+		if frappe.db.get_value("Supplier", self.supplier, "is_internal_supplier") == 1:
+			if self.inter_company_invoice_reference:
+				doc = frappe.get_doc("Sales Invoice", self.inter_company_invoice_reference)
+				if not frappe.db.get_value("Supplier", {"represents_company": doc.company}, "name") == self.supplier:
+					frappe.throw(_("Invalid Supplier for Inter Company Invoice"))
+				if not frappe.db.get_value("Customer", {"name": doc.customer}, "represents_company") == self.company:
+					frappe.throw(_("Invalid Company for Inter Company Invoice"))
+			else:
+				companies = frappe.db.sql("""select company from `tabAllowed To Transact With`
+					where parenttype = "Supplier" and parent = '{0}'""".format(self.supplier), as_list = 1)
+				companies = [company[0] for company in companies]
+				if not self.company in companies:
+					frappe.throw(_("Supplier not allowed to transact with {0}. Please change the Company.").format(self.company))
+
 	def check_for_closed_status(self):
 		check_list = []
 
@@ -303,6 +319,12 @@
 
 		self.update_project()
 		self.update_fixed_asset()
+		self.update_linked_invoice()
+
+	def update_linked_invoice(self):
+		if self.inter_company_invoice_reference:
+			frappe.db.set_value("Sales Invoice", self.inter_company_invoice_reference,\
+				"inter_company_invoice_reference", self.name)
 
 	def update_fixed_asset(self):
 		for d in self.get("items"):
@@ -635,6 +657,8 @@
 		self.update_fixed_asset()
 		frappe.db.set(self, 'status', 'Cancelled')
 
+		self.unlink_inter_company_invoice()
+
 	def update_project(self):
 		project_list = []
 		for d in self.items:
@@ -672,6 +696,13 @@
 					pi = pi[0][0]
 					frappe.throw(_("Supplier Invoice No exists in Purchase Invoice {0}".format(pi)))
 
+	def unlink_inter_company_invoice(self):
+		if self.inter_company_invoice_reference:
+			frappe.db.set_value("Purchase Invoice", self.name,\
+				"inter_company_invoice_reference", "")
+			frappe.db.set_value("Sales Invoice", self.inter_company_invoice_reference,\
+				"inter_company_invoice_reference", "")
+
 	def update_billing_status_in_pr(self, update_modified=True):
 		updated_pr = []
 		for d in self.get("items"):
@@ -733,4 +764,9 @@
 		}
 	}, target_doc)
 
-	return doc
\ No newline at end of file
+	return doc
+
+@frappe.whitelist()
+def make_inter_company_sales_invoice(source_name, target_doc=None):
+	from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_inter_company_invoice
+	return make_inter_company_invoice("Purchase Invoice", source_name, target_doc)
\ No newline at end of file