Merge branch 'develop' of https://github.com/frappe/erpnext into automatic_so_po
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index 28ceab5..489e286 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -138,6 +138,20 @@
 							erpnext.utils.make_subscription(doc.doctype, doc.name)
 						}, __('Create'))
 					}
+
+					if (doc.docstatus == 1 && !doc.inter_company_order_reference) {
+						let me = this;
+						frappe.model.with_doc("Supplier", me.frm.doc.supplier, () => {
+							let supplier = frappe.model.get_doc("Supplier", me.frm.doc.supplier);
+							let internal = supplier.is_internal_supplier;
+							let disabled = supplier.disabled;
+							if (internal == 1 && disabled == 0) {
+								me.frm.add_custom_button("Inter Company Order", function() {
+									me.make_inter_company_order(me.frm);
+								}, __('Create'));
+							}
+						});
+					}
 				}
 				if(flt(doc.per_billed)==0) {
 					this.frm.add_custom_button(__('Payment Request'),
@@ -296,6 +310,13 @@
 		});
 	},
 
+	make_inter_company_order: function(frm) {
+		frappe.model.open_mapped_doc({
+			method: "erpnext.buying.doctype.purchase_order.purchase_order.make_inter_company_sales_order",
+			frm: frm
+		});
+	},
+
 	make_purchase_receipt: function() {
 		frappe.model.open_mapped_doc({
 			method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt",
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 46f48fb..13a097a 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -298,6 +298,7 @@
    "collapsible": 0,
    "columns": 0,
    "default": "Today",
+   "fetch_if_empty": 0,
    "fieldname": "transaction_date",
    "fieldtype": "Date",
    "hidden": 0,
@@ -332,6 +333,7 @@
    "collapsible": 0,
    "columns": 0,
    "default": "",
+   "fetch_if_empty": 0,
    "fieldname": "schedule_date",
    "fieldtype": "Date",
    "hidden": 0,
@@ -365,6 +367,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "eval:doc.docstatus===1",
+   "fetch_if_empty": 0,
    "fieldname": "order_confirmation_no",
    "fieldtype": "Data",
    "hidden": 0,
@@ -398,6 +401,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "eval:doc.order_confirmation_no",
+   "fetch_if_empty": 0,
    "fieldname": "order_confirmation_date",
    "fieldtype": "Date",
    "hidden": 0,
@@ -430,6 +434,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "amended_from",
    "fieldtype": "Link",
    "hidden": 0,
@@ -465,6 +470,7 @@
    "collapsible": 0,
    "collapsible_depends_on": "",
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "drop_ship",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -498,6 +504,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "customer",
    "fieldtype": "Link",
    "hidden": 0,
@@ -532,6 +539,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "customer_name",
    "fieldtype": "Data",
    "hidden": 0,
@@ -564,6 +572,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_19",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -596,6 +605,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "customer_contact_person",
    "fieldtype": "Link",
    "hidden": 0,
@@ -629,6 +639,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "customer_contact_display",
    "fieldtype": "Small Text",
    "hidden": 1,
@@ -661,6 +672,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "customer_contact_mobile",
    "fieldtype": "Small Text",
    "hidden": 1,
@@ -693,6 +705,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "customer_contact_email",
    "fieldtype": "Code",
    "hidden": 1,
@@ -726,6 +739,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "section_addresses",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -758,6 +772,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "supplier_address",
    "fieldtype": "Link",
    "hidden": 0,
@@ -790,6 +805,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "contact_person",
    "fieldtype": "Link",
    "hidden": 0,
@@ -823,6 +839,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "address_display",
    "fieldtype": "Small Text",
    "hidden": 0,
@@ -854,6 +871,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "contact_display",
    "fieldtype": "Small Text",
    "hidden": 0,
@@ -885,6 +903,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "contact_mobile",
    "fieldtype": "Small Text",
    "hidden": 0,
@@ -916,6 +935,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "contact_email",
    "fieldtype": "Small Text",
    "hidden": 0,
@@ -947,6 +967,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "col_break_address",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -979,6 +1000,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "shipping_address",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1012,6 +1034,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "shipping_address_display",
    "fieldtype": "Small Text",
    "hidden": 0,
@@ -1044,6 +1067,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "currency_and_price_list",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1076,6 +1100,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "currency",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1111,6 +1136,7 @@
    "collapsible": 0,
    "columns": 0,
    "description": "",
+   "fetch_if_empty": 0,
    "fieldname": "conversion_rate",
    "fieldtype": "Float",
    "hidden": 0,
@@ -1145,6 +1171,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "cb_price_list",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -1175,6 +1202,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "buying_price_list",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1207,6 +1235,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "price_list_currency",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1239,6 +1268,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "plc_conversion_rate",
    "fieldtype": "Float",
    "hidden": 0,
@@ -1271,6 +1301,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "ignore_pricing_rule",
    "fieldtype": "Check",
    "hidden": 0,
@@ -1302,6 +1333,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "sec_warehouse",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1333,6 +1365,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "set_warehouse",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1366,6 +1399,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "col_break_warehouse",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -1398,6 +1432,7 @@
    "collapsible": 0,
    "columns": 0,
    "default": "No",
+   "fetch_if_empty": 0,
    "fieldname": "is_subcontracted",
    "fieldtype": "Select",
    "hidden": 0,
@@ -1431,6 +1466,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "eval:doc.is_subcontracted==\"Yes\"",
+   "fetch_if_empty": 0,
    "fieldname": "supplier_warehouse",
    "fieldtype": "Link",
    "hidden": 0,
@@ -1464,6 +1500,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "items_section",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1497,6 +1534,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "scan_barcode",
    "fieldtype": "Data",
    "hidden": 0,
@@ -1529,6 +1567,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "items",
    "fieldtype": "Table",
    "hidden": 0,
@@ -1563,6 +1602,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "section_break_48",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1596,6 +1636,7 @@
    "collapsible": 0,
    "collapsible_depends_on": "",
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "pricing_rules",
    "fieldtype": "Table",
    "hidden": 0,
@@ -1630,6 +1671,7 @@
    "collapsible": 0,
    "collapsible_depends_on": "supplied_items",
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "raw_material_details",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1663,6 +1705,7 @@
    "collapsible": 0,
    "columns": 0,
    "depends_on": "",
+   "fetch_if_empty": 0,
    "fieldname": "supplied_items",
    "fieldtype": "Table",
    "hidden": 0,
@@ -1697,6 +1740,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "sb_last_purchase",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1727,6 +1771,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "total_qty",
    "fieldtype": "Float",
    "hidden": 0,
@@ -1759,6 +1804,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -1792,6 +1838,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_net_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -1826,6 +1873,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_26",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -1856,6 +1904,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -1889,6 +1938,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "net_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -1923,6 +1973,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "total_net_weight",
    "fieldtype": "Float",
    "hidden": 0,
@@ -1955,6 +2006,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "taxes_section",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1989,6 +2041,7 @@
    "collapsible": 0,
    "columns": 0,
    "description": "",
+   "fetch_if_empty": 0,
    "fieldname": "taxes_and_charges",
    "fieldtype": "Link",
    "hidden": 0,
@@ -2023,6 +2076,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_50",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -2054,6 +2108,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "shipping_rule",
    "fieldtype": "Link",
    "hidden": 0,
@@ -2087,6 +2142,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "section_break_52",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -2118,6 +2174,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "taxes",
    "fieldtype": "Table",
    "hidden": 0,
@@ -2152,6 +2209,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "sec_tax_breakup",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -2184,6 +2242,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "other_charges_calculation",
    "fieldtype": "Text",
    "hidden": 0,
@@ -2216,6 +2275,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "totals",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -2249,6 +2309,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_taxes_and_charges_added",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2283,6 +2344,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_taxes_and_charges_deducted",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2317,6 +2379,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_total_taxes_and_charges",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2351,6 +2414,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_39",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -2382,6 +2446,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "taxes_and_charges_added",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2416,6 +2481,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "taxes_and_charges_deducted",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2450,6 +2516,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "total_taxes_and_charges",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2484,6 +2551,7 @@
    "collapsible": 1,
    "collapsible_depends_on": "discount_amount",
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "discount_section",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -2517,6 +2585,7 @@
    "collapsible": 0,
    "columns": 0,
    "default": "Grand Total",
+   "fetch_if_empty": 0,
    "fieldname": "apply_discount_on",
    "fieldtype": "Select",
    "hidden": 0,
@@ -2550,6 +2619,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_discount_amount",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2583,6 +2653,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_45",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -2614,6 +2685,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "additional_discount_percentage",
    "fieldtype": "Float",
    "hidden": 0,
@@ -2646,6 +2718,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "discount_amount",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2679,6 +2752,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "totals_section",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -2710,6 +2784,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_grand_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2744,6 +2819,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_rounding_adjustment",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2778,6 +2854,7 @@
    "collapsible": 0,
    "columns": 0,
    "description": "In Words will be visible once you save the Purchase Order.",
+   "fetch_if_empty": 0,
    "fieldname": "base_in_words",
    "fieldtype": "Data",
    "hidden": 0,
@@ -2811,6 +2888,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "base_rounded_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2845,6 +2923,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break4",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -2876,6 +2955,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "grand_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2910,6 +2990,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "rounding_adjustment",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2943,6 +3024,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "rounded_total",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -2975,6 +3057,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "disable_rounded_total",
    "fieldtype": "Check",
    "hidden": 0,
@@ -3007,6 +3090,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "in_words",
    "fieldtype": "Data",
    "hidden": 0,
@@ -3040,6 +3124,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "advance_paid",
    "fieldtype": "Currency",
    "hidden": 0,
@@ -3072,6 +3157,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "payment_schedule_section",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -3104,6 +3190,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "payment_terms_template",
    "fieldtype": "Link",
    "hidden": 0,
@@ -3137,6 +3224,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "payment_schedule",
    "fieldtype": "Table",
    "hidden": 0,
@@ -3171,6 +3259,7 @@
    "collapsible": 1,
    "collapsible_depends_on": "terms",
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "terms_section_break",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -3204,6 +3293,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "tc_name",
    "fieldtype": "Link",
    "hidden": 0,
@@ -3238,6 +3328,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "terms",
    "fieldtype": "Text Editor",
    "hidden": 0,
@@ -3271,6 +3362,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "more_info",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -3304,6 +3396,7 @@
    "collapsible": 0,
    "columns": 0,
    "default": "Draft",
+   "fetch_if_empty": 0,
    "fieldname": "status",
    "fieldtype": "Select",
    "hidden": 0,
@@ -3338,6 +3431,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "ref_sq",
    "fieldtype": "Data",
    "hidden": 1,
@@ -3371,6 +3465,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "party_account_currency",
    "fieldtype": "Link",
    "hidden": 1,
@@ -3404,6 +3499,41 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
+   "fieldname": "inter_company_order_reference",
+   "fieldtype": "Link",
+   "hidden": 0,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_standard_filter": 0,
+   "label": "Inter Company Order Reference",
+   "length": 0,
+   "no_copy": 0,
+   "options": "Sales Order",
+   "permlevel": 0,
+   "precision": "",
+   "print_hide": 0,
+   "print_hide_if_no_value": 0,
+   "read_only": 0,
+   "remember_last_selected_value": 0,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "set_only_once": 0,
+   "translatable": 0,
+   "unique": 0
+  },
+  {
+   "allow_bulk_edit": 0,
+   "allow_in_quick_entry": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break_74",
    "fieldtype": "Column Break",
    "hidden": 0,
@@ -3437,6 +3567,7 @@
    "columns": 0,
    "depends_on": "eval:!doc.__islocal",
    "description": "",
+   "fetch_if_empty": 0,
    "fieldname": "per_received",
    "fieldtype": "Percent",
    "hidden": 0,
@@ -3472,6 +3603,7 @@
    "columns": 0,
    "depends_on": "eval:!doc.__islocal",
    "description": "",
+   "fetch_if_empty": 0,
    "fieldname": "per_billed",
    "fieldtype": "Percent",
    "hidden": 0,
@@ -3505,6 +3637,7 @@
    "bold": 0,
    "collapsible": 1,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "column_break5",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -3906,17 +4039,15 @@
   }
  ],
  "has_web_view": 0,
- "hide_heading": 0,
  "hide_toolbar": 0,
  "icon": "fa fa-file-text",
  "idx": 105,
- "image_view": 0,
  "in_create": 0,
  "is_submittable": 1,
  "issingle": 0,
  "istable": 0,
  "max_attachments": 0,
- "modified": "2019-02-14 19:36:49.390935",
+ "modified": "2019-04-18 19:43:17.239390",
  "modified_by": "Administrator",
  "module": "Buying",
  "name": "Purchase Order",
@@ -4001,7 +4132,6 @@
  ],
  "quick_entry": 0,
  "read_only": 0,
- "read_only_onload": 1,
  "search_fields": "status, transaction_date, supplier,grand_total",
  "show_name_in_global_search": 1,
  "sort_field": "modified",
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 6a4c818..ae610f8 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -17,6 +17,8 @@
 from six import string_types
 from erpnext.stock.doctype.item.item import get_item_defaults
 from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
+from erpnext.selling.doctype.sales_order.sales_order import validate_inter_company_party, update_linked_order,\
+	unlink_inter_company_order
 
 form_grid_templates = {
 	"items": "templates/form_grid/item_grid.html"
@@ -56,6 +58,7 @@
 		self.validate_bom_for_subcontracting_items()
 		self.create_raw_materials_supplied("supplied_items")
 		self.set_received_qty_for_drop_ship_items()
+		validate_inter_company_party(self.doctype, self.supplier, self.company, self.inter_company_order_reference)
 
 	def validate_with_previous_doc(self):
 		super(PurchaseOrder, self).validate_with_previous_doc({
@@ -219,6 +222,7 @@
 
 		self.update_blanket_order()
 
+		update_linked_order(self.doctype, self.name, self.inter_company_order_reference)
 
 	def on_cancel(self):
 		super(PurchaseOrder, self).on_cancel()
@@ -244,6 +248,7 @@
 
 		self.update_blanket_order()
 
+		unlink_inter_company_order(self.doctype, self.name, self.inter_company_order_reference)
 
 	def on_update(self):
 		pass
@@ -490,3 +495,9 @@
 	po = frappe.get_doc("Purchase Order", name)
 	po.update_status(status)
 	po.update_delivered_qty_in_sales_order()
+
+@frappe.whitelist()
+def make_inter_company_sales_order(source_name, target_doc=None):
+	from erpnext.selling.doctype.sales_order.sales_order import make_inter_company_order
+	return make_inter_company_order("Purchase Order", source_name, target_doc)
+
diff --git a/erpnext/selling/doctype/sales_order/sales_order.js b/erpnext/selling/doctype/sales_order/sales_order.js
index adb58a1..064748d 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.js
+++ b/erpnext/selling/doctype/sales_order/sales_order.js
@@ -204,6 +204,20 @@
 							erpnext.utils.make_subscription(doc.doctype, doc.name)
 						}, __('Create'))
 					}
+
+					if (doc.docstatus == 1 && !doc.inter_company_order_reference) {
+						let me = this;
+						frappe.model.with_doc("Customer", me.frm.doc.customer, () => {
+							let customer = frappe.model.get_doc("Customer", me.frm.doc.customer);
+							let internal = customer.is_internal_customer;
+							let disabled = customer.disabled;
+							if (internal == 1 && disabled == 0) {
+								me.frm.add_custom_button("Inter Company Order", function() {
+									me.make_inter_company_order();
+								}, __('Create'));
+							}
+						});
+					}
 				}
 				// payment request
 				if(flt(doc.per_billed)==0) {
@@ -500,6 +514,13 @@
 		})
 	},
 
+	make_inter_company_order: function() {
+		frappe.model.open_mapped_doc({
+			method: "erpnext.selling.doctype.sales_order.sales_order.make_inter_company_purchase_order",
+			frm: this.frm
+		});
+	},
+
 	make_maintenance_visit: function() {
 		frappe.model.open_mapped_doc({
 			method: "erpnext.selling.doctype.sales_order.sales_order.make_maintenance_visit",
diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json
index 30123db..95c803d 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.json
+++ b/erpnext/selling/doctype/sales_order/sales_order.json
@@ -1449,6 +1449,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "pricing_rule_details",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -1481,6 +1482,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "pricing_rules",
    "fieldtype": "Table",
    "hidden": 0,
@@ -1514,6 +1516,7 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
    "fieldname": "section_break_31",
    "fieldtype": "Section Break",
    "hidden": 0,
@@ -3237,6 +3240,40 @@
    "bold": 0,
    "collapsible": 0,
    "columns": 0,
+   "fetch_if_empty": 0,
+   "fieldname": "inter_company_order_reference",
+   "fieldtype": "Link",
+   "hidden": 0,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_standard_filter": 0,
+   "label": "Inter Company Order Reference",
+   "length": 0,
+   "no_copy": 0,
+   "options": "Purchase Order",
+   "permlevel": 0,
+   "precision": "",
+   "print_hide": 0,
+   "print_hide_if_no_value": 0,
+   "read_only": 0,
+   "remember_last_selected_value": 0,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "set_only_once": 0,
+   "translatable": 0,
+   "unique": 0
+  },
+  {
+   "allow_bulk_edit": 0,
+   "allow_in_quick_entry": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "columns": 0,
    "description": "Track this Sales Order against any Project",
    "fetch_if_empty": 0,
    "fieldname": "project",
@@ -4292,17 +4329,15 @@
   }
  ],
  "has_web_view": 0,
- "hide_heading": 0,
  "hide_toolbar": 0,
  "icon": "fa fa-file-text",
  "idx": 105,
- "image_view": 0,
  "in_create": 0,
  "is_submittable": 1,
  "issingle": 0,
  "istable": 0,
  "max_attachments": 0,
- "modified": "2019-02-13 01:02:45.882179",
+ "modified": "2019-04-18 12:05:23.464968",
  "modified_by": "Administrator",
  "module": "Selling",
  "name": "Sales Order",
@@ -4425,7 +4460,6 @@
  ],
  "quick_entry": 0,
  "read_only": 0,
- "read_only_onload": 1,
  "search_fields": "status,transaction_date,customer,customer_name, territory,order_type,company",
  "show_name_in_global_search": 1,
  "sort_field": "modified",
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index d09e281..25740d2 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -42,6 +42,7 @@
 		self.validate_warehouse()
 		self.validate_drop_ship()
 		self.validate_serial_no_based_delivery()
+		validate_inter_company_party(self.doctype, self.customer, self.company, self.inter_company_order_reference)
 
 		from erpnext.stock.doctype.packed_item.packed_item import make_packing_list
 		make_packing_list(self)
@@ -182,6 +183,8 @@
 
 		self.update_blanket_order()
 
+		update_linked_order(self.doctype, self.name, self.inter_company_order_reference)
+
 	def on_cancel(self):
 		super(SalesOrder, self).on_cancel()
 
@@ -198,6 +201,8 @@
 
 		self.update_blanket_order()
 
+		unlink_inter_company_order(self.doctype, self.name, self.inter_company_order_reference)
+
 	def update_project(self):
 		if frappe.db.get_single_value('Selling Settings', 'sales_update_frequency') != "Each Transaction":
 			return
@@ -455,6 +460,42 @@
 				Item {0} is added with and without Ensure Delivery by \
 				Serial No.").format(item.item_code))
 
+def validate_inter_company_party(doctype, party, company, inter_company_order_reference):
+	if doctype == "Sales Order":
+		partytype, ref_partytype, internal = "Customer", "Supplier", "is_internal_customer"
+		ref_doc =  "Purchase Order"
+	else:
+		partytype, ref_partytype, internal = "Supplier", "Customer", "is_internal_supplier"
+		ref_doc =  "Sales Order"
+
+	if inter_company_order_reference:
+		doc = frappe.get_doc(ref_doc, inter_company_order_reference)
+		ref_party = doc.supplier if doctype == "Sales Order" else doc.customer
+		if not frappe.db.get_value(partytype, {"represents_company": doc.company}, "name") == party:
+			frappe.throw(_("Invalid {0} for Inter Company Order.").format(partytype))
+		if not frappe.get_cached_value(ref_partytype, ref_party, "represents_company") == company:
+			frappe.throw(_("Invalid Company for Inter Company Order."))
+
+	elif frappe.db.get_value(partytype, {"name": party, internal: 1}, "name") == party:
+		companies = frappe.db.sql("""select company from `tabAllowed To Transact With`
+			where parenttype = '{0}' and parent = '{1}'""".format(partytype, party), as_list = 1)
+		companies = [d[0] for d in companies]
+		if not company in companies:
+			frappe.throw(_("{0} not allowed to transact with {1}. Please change the Company.").format(partytype, company))
+
+def update_linked_order(doctype, name, inter_company_order_reference):
+	if inter_company_order_reference:
+		frappe.db.set_value(doctype, inter_company_order_reference,\
+			"inter_company_order_reference", name)
+
+def unlink_inter_company_order(doctype, name, inter_company_order_reference):
+	ref_doc = "Purchase Order" if doctype == "Sales Order" else "Sales Order"
+	if inter_company_order_reference:
+		frappe.db.set_value(doctype, name,\
+			"inter_company_order_reference", "")
+		frappe.db.set_value(ref_doc, inter_company_order_reference,\
+			"inter_company_order_reference", "")
+
 def get_list_context(context=None):
 	from erpnext.controllers.website_list_for_contact import get_list_context
 	list_context = get_list_context(context)
@@ -970,4 +1011,88 @@
 	material_request.flags.ignore_permissions = 1
 	material_request.run_method("set_missing_values")
 	material_request.submit()
-	return material_request
\ No newline at end of file
+	return material_request
+
+def get_inter_company_details(doc, doctype):
+	if doctype == "Sales Order":
+		party = frappe.db.get_value("Supplier", {"disabled": 0, "is_internal_supplier": 1, "represents_company": doc.company}, "name")
+		company = frappe.get_cached_value("Customer", doc.customer, "represents_company")
+	else:
+		party = frappe.db.get_value("Customer", {"disabled": 0, "is_internal_customer": 1, "represents_company": doc.company}, "name")
+		company = frappe.get_cached_value("Supplier", doc.supplier, "represents_company")
+
+	return {
+		"party": party,
+		"company": company
+	}
+
+def validate_inter_company_order(doc, doctype):
+
+	details = get_inter_company_details(doc, doctype)
+	price_list = doc.selling_price_list if doctype == "Sales Order" else doc.buying_price_list
+	valid_price_list = frappe.db.get_value("Price List", {"name": price_list, "buying": 1, "selling": 1})
+	if not valid_price_list:
+		frappe.throw(_("Selected Price List should have buying and selling fields checked."))
+
+	party = details.get("party")
+	if not party:
+		partytype = "Supplier" if doctype == "Sales Order" else "Customer"
+		frappe.throw(_("No {0} found for Inter Company Transactions.").format(partytype))
+
+	company = details.get("company")
+	default_currency = frappe.get_cached_value('Company',  company,  "default_currency")
+	if default_currency != doc.currency:
+		frappe.throw(_("Company currencies of both the companies should match for Inter Company Transactions."))
+
+	return
+
+@frappe.whitelist()
+def make_inter_company_purchase_order(source_name, target_doc=None):
+	return make_inter_company_order("Sales Order", source_name, target_doc)
+
+def make_inter_company_order(doctype, source_name, target_doc=None):
+	if doctype == "Sales Order":
+		source_doc = frappe.get_doc("Sales Order", source_name)
+		target_doctype = "Purchase Order"
+	else:
+		source_doc = frappe.get_doc("Purchase Order", source_name)
+		target_doctype = "Sales Order"
+
+	validate_inter_company_order(source_doc, doctype)
+	details = get_inter_company_details(source_doc, doctype)
+
+	def set_missing_values(source, target):
+		target.run_method("set_missing_values")
+
+	def update_details(source_doc, target_doc, source_parent):
+		target_doc.inter_company_order_reference = source_doc.name
+		if target_doc.doctype == "Purchase Order":
+			target_doc.company = details.get("company")
+			target_doc.supplier = details.get("party")
+			target_doc.buying_price_list = source_doc.selling_price_list
+		else:
+			target_doc.company = details.get("company")
+			target_doc.customer = details.get("party")
+			target_doc.selling_price_list = source_doc.buying_price_list
+
+	doclist = get_mapped_doc(doctype, source_name,	{
+		doctype: {
+			"doctype": target_doctype,
+			"postprocess": update_details,
+			"field_no_map": [
+				"taxes_and_charges"
+			]
+		},
+		doctype +" Item": {
+			"doctype": target_doctype + " Item",
+			"field_no_map": [
+				"income_account",
+				"expense_account",
+				"cost_center",
+				"warehouse"
+			]
+		}
+
+	}, target_doc, set_missing_values)
+
+	return doclist
\ No newline at end of file