diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js index bc53efcd9df..46a59b7146f 100644 --- a/erpnext/buying/doctype/purchase_common/purchase_common.js +++ b/erpnext/buying/doctype/purchase_common/purchase_common.js @@ -221,7 +221,68 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({ 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 79820fae3dd..b35044ad211 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 @@ class PurchaseCommon(BuyingController): 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 09ffe71ca58..e776b3ea981 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.json +++ b/erpnext/buying/doctype/purchase_order/purchase_order.json @@ -1196,6 +1196,34 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "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, @@ -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 3ed822c0a30..4b28ee854b4 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 @@ frappe.ui.form.on("Request for Quotation",{ }); }); } + }, make_suppplier_quotation: function(frm) { @@ -138,7 +139,6 @@ erpnext.buying.RequestforQuotationController = erpnext.buying.BuyingController.e } }) }, __("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 @@ erpnext.buying.RequestforQuotationController = erpnext.buying.BuyingController.e } 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 7f2802cf348..c626f22f902 100644 --- a/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json +++ b/erpnext/buying/doctype/request_for_quotation/request_for_quotation.json @@ -291,6 +291,34 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "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, @@ -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 223f9fd5f11..5ed210ca98b 100644 --- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js +++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js @@ -24,6 +24,7 @@ erpnext.buying.SupplierQuotationController = erpnext.buying.BuyingController.ext } 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 e1319ed7d3f..4eb8788c44c 100644 --- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json +++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json @@ -784,6 +784,34 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "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, @@ -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 +}