Created button to link buying items to material requests
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index c03ccc8..8030979 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -221,6 +221,44 @@
 
 	tc_name: function() {
 		this.get_terms();
+	},
+	link_requests: function() { 
+		
+		cur_frm.add_custom_button(__("Link to Material Requests"),
+			function() {
+				my_items = []
+				for (var i in cur_frm.doc.items) {
+					if(!cur_frm.doc.items[i].material_request){
+						my_items.push(cur_frm.doc.items[i].item_code);
+					}
+					
+				}
+				frappe.call({	
+					method: "erpnext.buying.doctype.purchase_common.purchase_common.get_linked_material_requests", 
+					args:{
+						items: my_items						
+					}, 
+					callback: function(r) { 
+						//frm.reload_doc();
+						console.log(r.message);
+						for (var i in cur_frm.doc.items) {
+							var qty = cur_frm.doc.items[i].qty;
+							(r.message || []).forEach(function(d) {
+								if (d[0].qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d[0].item_code)
+								{
+									cur_frm.doc.items[i].material_request = d[0].mr_name;
+									cur_frm.doc.items[i].material_request_item = d[0].mr_item;
+									my_qty = Math.min(qty, d[0].qty);
+									qty = qty - my_qty;
+									d[0].qty = d[0].qty  - my_qty;
+								}
+							
+							});
+						}
+					}
+				});
+			});
+
 	}
 });
 
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.py b/erpnext/buying/doctype/purchase_common/purchase_common.py
index 8435f3b..6c10fe1 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.py
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.py
@@ -2,7 +2,7 @@
 # License: GNU General Public License v3. See license.txt
 
 from __future__ import unicode_literals
-import frappe
+import frappe, json
 from frappe.utils import flt, cstr, cint
 from frappe import _
 
@@ -79,4 +79,28 @@
 		status = frappe.db.get_value(doctype, docname, "status")
 
 		if status == "Closed":
-			frappe.throw(_("{0} {1} status is {2}").format(doctype, docname, status), frappe.InvalidStatusError)
\ No newline at end of file
+			frappe.throw(_("{0} {1} status is {2}").format(doctype, docname, status), frappe.InvalidStatusError)
+			
+@frappe.whitelist()
+def get_linked_material_requests(items):
+	items = json.loads(items)
+	mr_list = []
+	for item in items:
+		material_request = frappe.db.sql("""SELECT distinct mr.name AS mr_name, 
+				(mr_item.qty - mr_item.ordered_qty) AS qty, 
+				mr_item.item_code AS item_code,
+				mr_item.name AS mr_item 
+			FROM `tabMaterial Request` mr, `tabMaterial Request Item` mr_item
+			WHERE mr.name = mr_item.parent
+				AND mr_item.item_code = %(item)s 
+				AND mr.material_request_type = 'Purchase'
+				AND mr.per_ordered < 99.99
+				AND mr.docstatus = 1
+				AND mr.status != 'Stopped'
+                        ORDER BY mr_item.item_code ASC""",{"item": item}, as_dict=1)
+		if material_request:
+			mr_list.append(material_request)
+	
+	return mr_list
+	
+	
\ No newline at end of file
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index f2f04fe..2fa3864 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -56,6 +56,7 @@
 			}
 		} else if(doc.docstatus===0) {
 			cur_frm.cscript.add_from_mappers();
+			cur_frm.cscript.link_requests(); 
 		}
 
 		if(doc.docstatus == 1 && in_list(["Closed", "Delivered"], doc.status)) {
diff --git a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js
index 113dbe0..6568b6d 100644
--- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js
+++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js
@@ -43,6 +43,7 @@
 				});
 			});
 		}
+		
 	},
 
 	make_suppplier_quotation: function(frm) {
@@ -134,7 +135,7 @@
 						}
 					})
 				}, __("Get items from"));
-				
+				cur_frm.cscript.link_requests();
 				// Get items from open Material Requests based on supplier
 				cur_frm.add_custom_button(__('Possible Supplier'), function() {
 					// Create a dialog window for the user to pick their supplier
@@ -170,6 +171,7 @@
 					}	
 					d.show();
 				}, __("Get items from"));
+				
 		}
 	},
 
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
index b3bdeb0..5da2d7c 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
@@ -16,6 +16,7 @@
 
 		}
 		else if (this.frm.doc.docstatus===0) {
+			
 			cur_frm.add_custom_button(__('Material Request'),
 				function() {
 					erpnext.utils.map_current_doc({
@@ -30,6 +31,7 @@
 						}
 					})
 				}, __("Get items from"));
+			cur_frm.cscript.link_requests(); 
 		}
 	},