Merge pull request #7671 from bcornwellmott/link_mrs

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 bc53efc..46a59b7 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -221,7 +221,68 @@
 
 	tc_name: function() {
 		this.get_terms();
-	}
+	},
+	link_to_mrs: 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) { 
+						var i = 0;
+						var item_length = cur_frm.doc.items.length;
+						while (i < item_length) {
+							var qty = cur_frm.doc.items[i].qty;
+							(r.message[0] || []).forEach(function(d) {
+								if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
+								{
+									cur_frm.doc.items[i].material_request = d.mr_name;
+									cur_frm.doc.items[i].material_request_item = d.mr_item;
+									my_qty = Math.min(qty, d.qty);
+									qty = qty - my_qty;
+									d.qty = d.qty  - my_qty;
+									cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
+									cur_frm.doc.items[i].qty = my_qty;
+									
+									frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
+									if (qty > 0)
+									{
+										frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
+										var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
+										item_length++;
+										
+										for (key in cur_frm.doc.items[i])
+										{
+											newrow[key] = cur_frm.doc.items[i][key];
+										}
+										
+										newrow.idx = item_length;
+										newrow["stock_qty"] = newrow.conversion_factor*qty;
+										newrow["qty"] = qty;
+										
+										newrow["material_request"] = "";
+										newrow["material_request_item"] = "";
+										
+									}
+									
+									
+									
+								}
+							
+							});
+							i++;
+						}
+						refresh_field("items");
+						//cur_frm.save();
+					}
+				});
+			}
 });
 
 cur_frm.add_fetch('project', 'cost_center', 'cost_center');
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.py b/erpnext/buying/doctype/purchase_common/purchase_common.py
index 79820fa..b35044a 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 _
 
@@ -78,4 +78,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.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 09ffe71..e776b3e 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -1201,6 +1201,34 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
+   "fieldname": "link_to_mrs", 
+   "fieldtype": "Button", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Link to material requests", 
+   "length": 0, 
+   "no_copy": 0, 
+   "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, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "sb_last_purchase", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -3168,7 +3196,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-02-22 18:20:15.650815", 
+ "modified": "2017-02-28 18:20:15.650815", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
@@ -3266,4 +3294,4 @@
  "title_field": "title", 
  "track_changes": 0, 
  "track_seen": 0
-}
\ No newline at end of file
+}
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 3ed822c..4b28ee8 100644
--- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js
+++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.js
@@ -47,6 +47,7 @@
 				});
 			});
 		}
+		
 	},
 
 	make_suppplier_quotation: function(frm) {
@@ -138,7 +139,6 @@
 						}
 					})
 				}, __("Get items from"));
-				
 				// 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
@@ -174,6 +174,7 @@
 					}	
 					d.show();
 				}, __("Get items from"));
+				
 		}
 	},
 
diff --git a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json
index 7f2802c..c626f22 100644
--- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json
+++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json
@@ -296,6 +296,34 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
+   "fieldname": "link_to_mrs", 
+   "fieldtype": "Button", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Link to material requests", 
+   "length": 0, 
+   "no_copy": 0, 
+   "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, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "supplier_response_section", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -731,7 +759,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-02-17 16:44:39.531065", 
+ "modified": "2017-02-28 16:44:39.531065", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Request for Quotation", 
@@ -890,4 +918,4 @@
  "title_field": "", 
  "track_changes": 0, 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
index 223f9fd..5ed210c 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
@@ -24,6 +24,7 @@
 
 		}
 		else if (this.frm.doc.docstatus===0) {
+			
 			cur_frm.add_custom_button(__('Material Request'),
 				function() {
 					erpnext.utils.map_current_doc({
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json
index e1319ed..4eb8788 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json
@@ -789,6 +789,34 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
+   "fieldname": "link_to_mrs", 
+   "fieldtype": "Button", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Link to material requests", 
+   "length": 0, 
+   "no_copy": 0, 
+   "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, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "section_break_22", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -2115,7 +2143,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2017-02-17 16:45:32.200667", 
+ "modified": "2017-02-28 16:45:32.200667", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Supplier Quotation", 
@@ -2233,4 +2261,4 @@
  "title_field": "title", 
  "track_changes": 0, 
  "track_seen": 0
-}
\ No newline at end of file
+}