Merge pull request #32905 from vishdha/internal_transfer

fix: Internal Transfer Material Request cycle and tracking fixed till purchase receipt
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 62cf0dc..c276be2 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -2165,6 +2165,8 @@
 		if source.doctype == "Purchase Order Item" and target.doctype == "Sales Order Item":
 			target.purchase_order = source.parent
 			target.purchase_order_item = source.name
+			target.material_request = source.material_request
+			target.material_request_item = source.material_request_item
 
 		if (
 			source.get("purchase_order")
diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
index 5a96131..5206a42 100644
--- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
@@ -833,6 +833,10 @@
 		prepare_data_for_internal_transfer()
 		supplier = "_Test Internal Supplier 2"
 
+		mr = make_material_request(
+			qty=2, company="_Test Company with perpetual inventory", warehouse="Stores - TCP1"
+		)
+
 		po = create_purchase_order(
 			company="_Test Company with perpetual inventory",
 			supplier=supplier,
@@ -840,6 +844,8 @@
 			from_warehouse="_Test Internal Warehouse New 1 - TCP1",
 			qty=2,
 			rate=1,
+			material_request=mr.name,
+			material_request_item=mr.items[0].name,
 		)
 
 		so = make_inter_company_sales_order(po.name)
@@ -875,9 +881,11 @@
 		self.assertTrue(pi.items[0].purchase_order)
 		self.assertTrue(pi.items[0].po_detail)
 		pi.submit()
+		mr.reload()
 
 		po.load_from_db()
 		self.assertEqual(po.status, "Completed")
+		self.assertEqual(mr.status, "Received")
 
 
 def prepare_data_for_internal_transfer():
@@ -979,6 +987,8 @@
 				"schedule_date": add_days(nowdate(), 1),
 				"include_exploded_items": args.get("include_exploded_items", 1),
 				"against_blanket_order": args.against_blanket_order,
+				"material_request": args.material_request,
+				"material_request_item": args.material_request_item,
 			},
 		)
 
diff --git a/erpnext/selling/doctype/sales_order_item/sales_order_item.json b/erpnext/selling/doctype/sales_order_item/sales_order_item.json
index 8c7c552..b801de3 100644
--- a/erpnext/selling/doctype/sales_order_item/sales_order_item.json
+++ b/erpnext/selling/doctype/sales_order_item/sales_order_item.json
@@ -95,8 +95,10 @@
   "item_tax_rate",
   "transaction_date",
   "inter_transfer_reference_section",
+  "material_request",
   "purchase_order",
   "column_break_89",
+  "material_request_item",
   "purchase_order_item"
  ],
  "fields": [
@@ -847,12 +849,23 @@
    "label": "quotation_item",
    "no_copy": 1,
    "read_only": 1
+  },
+  {
+   "fieldname": "material_request",
+   "fieldtype": "Link",
+   "label": "Material Request",
+   "options": "Material Request"
+  },
+  {
+   "fieldname": "material_request_item",
+   "fieldtype": "Data",
+   "label": "Material Request Item"
   }
  ],
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-11-10 18:20:30.137455",
+ "modified": "2022-11-18 11:39:01.741665",
  "modified_by": "Administrator",
  "module": "Selling",
  "name": "Sales Order Item",
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 9dd28dc..a1df764 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -902,6 +902,8 @@
 					"serial_no": "serial_no",
 					"purchase_order": "purchase_order",
 					"purchase_order_item": "purchase_order_item",
+					"material_request": "material_request",
+					"Material_request_item": "material_request_item",
 				},
 				"field_no_map": ["warehouse"],
 			},
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py b/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
index fd44e9c..b6b5ff4 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
@@ -11,11 +11,14 @@
 		},
 		"internal_links": {
 			"Sales Order": ["items", "against_sales_order"],
+			"Material Request": ["items", "material_request"],
+			"Purchase Order": ["items", "purchase_order"],
 		},
 		"transactions": [
 			{"label": _("Related"), "items": ["Sales Invoice", "Packing Slip", "Delivery Trip"]},
 			{"label": _("Reference"), "items": ["Sales Order", "Shipment", "Quality Inspection"]},
 			{"label": _("Returns"), "items": ["Stock Entry"]},
 			{"label": _("Subscription"), "items": ["Auto Repeat"]},
+			{"label": _("Internal Transfer"), "items": ["Material Request", "Purchase Order"]},
 		],
 	}
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index 3229463..916ab2a 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -88,9 +88,11 @@
   "allow_zero_valuation_rate",
   "column_break_71",
   "internal_transfer_section",
+  "material_request",
   "purchase_order",
   "column_break_82",
   "purchase_order_item",
+  "material_request_item",
   "accounting_dimensions_section",
   "cost_center",
   "dimension_col_break",
@@ -818,13 +820,24 @@
    "fieldtype": "Check",
    "label": "Has Item Scanned",
    "read_only": 1
+  },
+  {
+   "fieldname": "material_request",
+   "fieldtype": "Link",
+   "label": "Material Request",
+   "options": "Material Request"
+  },
+  {
+   "fieldname": "material_request_item",
+   "fieldtype": "Data",
+   "label": "Material Request Item"
   }
  ],
  "idx": 1,
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-11-02 12:54:07.225623",
+ "modified": "2022-11-09 12:17:50.850142",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Delivery Note Item",
diff --git a/erpnext/stock/doctype/material_request/material_request_dashboard.py b/erpnext/stock/doctype/material_request/material_request_dashboard.py
index 691a8b3..2bba52a 100644
--- a/erpnext/stock/doctype/material_request/material_request_dashboard.py
+++ b/erpnext/stock/doctype/material_request/material_request_dashboard.py
@@ -14,5 +14,6 @@
 			},
 			{"label": _("Stock"), "items": ["Stock Entry", "Purchase Receipt", "Pick List"]},
 			{"label": _("Manufacturing"), "items": ["Work Order"]},
+			{"label": _("Internal Transfer"), "items": ["Sales Order"]},
 		],
 	}