diff --git a/buying/doctype/purchase_order/purchase_order.js b/buying/doctype/purchase_order/purchase_order.js index 0d3d0ee934..99149bcf3a 100644 --- a/buying/doctype/purchase_order/purchase_order.js +++ b/buying/doctype/purchase_order/purchase_order.js @@ -80,7 +80,8 @@ erpnext.buying.PurchaseOrderController = erpnext.buying.BuyingController.extend( company: cur_frm.doc.company } }) - }); + } + ); cur_frm.add_custom_button(wn._('From Supplier Quotation'), function() { @@ -93,7 +94,20 @@ erpnext.buying.PurchaseOrderController = erpnext.buying.BuyingController.extend( company: cur_frm.doc.company } }) - }); + } + ); + + cur_frm.add_custom_button(wn._('For Supplier'), + function() { + wn.model.map_current_doc({ + method: "stock.doctype.material_request.material_request.make_purchase_order_based_on_supplier", + source_doctype: "Supplier", + get_query_filters: { + docstatus: ["!=", 2], + } + }) + } + ); }, tc_name: function() { diff --git a/stock/doctype/item/item.txt b/stock/doctype/item/item.txt index de9e8f84f9..3e41101413 100644 --- a/stock/doctype/item/item.txt +++ b/stock/doctype/item/item.txt @@ -2,7 +2,7 @@ { "creation": "2013-05-03 10:45:46", "docstatus": 0, - "modified": "2013-07-23 11:54:52", + "modified": "2013-07-26 16:26:15", "modified_by": "Administrator", "owner": "Administrator" }, @@ -36,7 +36,8 @@ "parenttype": "DocType", "permlevel": 0, "read": 1, - "report": 1 + "report": 1, + "submit": 0 }, { "doctype": "DocType", @@ -402,6 +403,13 @@ "read_only": 0, "reqd": 1 }, + { + "doctype": "DocField", + "fieldname": "default_supplier", + "fieldtype": "Link", + "label": "Default Supplier", + "options": "Supplier" + }, { "depends_on": "eval:doc.is_purchase_item==\"Yes\"", "description": "Lead Time days is number of days by which this item is expected in your warehouse. This days is fetched in Material Request when you select this item.", @@ -876,7 +884,6 @@ "create": 1, "doctype": "DocPerm", "role": "Material Master Manager", - "submit": 0, "write": 1 }, { @@ -885,7 +892,6 @@ "create": 0, "doctype": "DocPerm", "role": "Material Manager", - "submit": 0, "write": 0 }, { @@ -894,19 +900,6 @@ "create": 0, "doctype": "DocPerm", "role": "Material User", - "submit": 0, "write": 0 - }, - { - "doctype": "DocPerm", - "role": "Sales User" - }, - { - "doctype": "DocPerm", - "role": "Purchase User" - }, - { - "doctype": "DocPerm", - "role": "Accounts User" } ] \ No newline at end of file diff --git a/stock/doctype/material_request/material_request.py b/stock/doctype/material_request/material_request.py index 86de99e5c6..5bcbaf8bc5 100644 --- a/stock/doctype/material_request/material_request.py +++ b/stock/doctype/material_request/material_request.py @@ -222,18 +222,18 @@ def _update_requested_qty(controller, mr_obj, mr_items): "posting_date": controller.doc.posting_date, }) -def set_missing_values(source, target): - po = webnotes.bean(target) +def set_missing_values(source, target_doclist): + po = webnotes.bean(target_doclist) po.run_method("set_missing_values") + +def update_item(obj, target, source_parent): + target.conversion_factor = 1 + target.qty = flt(obj.qty) - flt(obj.ordered_qty) @webnotes.whitelist() def make_purchase_order(source_name, target_doclist=None): from webnotes.model.mapper import get_mapped_doclist - def update_item(obj, target, source_parent): - target.conversion_factor = 1 - target.qty = flt(obj.qty) - flt(obj.ordered_qty) - doclist = get_mapped_doclist("Material Request", source_name, { "Material Request": { "doctype": "Purchase Order", @@ -257,6 +257,62 @@ def make_purchase_order(source_name, target_doclist=None): return [d.fields for d in doclist] +@webnotes.whitelist() +def make_purchase_order_based_on_supplier(source_name, target_doclist=None): + from webnotes.model.mapper import get_mapped_doclist + if target_doclist: + if isinstance(target_doclist, basestring): + import json + target_doclist = webnotes.doclist(json.loads(target_doclist)) + target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + + material_requests, supplier_items = get_material_requests_based_on_supplier(source_name) + + def postprocess(source, target_doclist): + target_doclist[0].supplier = source_name + set_missing_values(source, target_doclist) + + po_items = target_doclist.get({"parentfield": "po_details"}) + target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + \ + [d for d in po_items + if d.fields.get("item_code") in supplier_items and d.fields.get("qty") > 0] + + return target_doclist + + for mr in material_requests: + target_doclist = get_mapped_doclist("Material Request", mr, { + "Material Request": { + "doctype": "Purchase Order", + }, + "Material Request Item": { + "doctype": "Purchase Order Item", + "field_map": [ + ["name", "prevdoc_detail_docname"], + ["parent", "prevdoc_docname"], + ["parenttype", "prevdoc_doctype"], + ["uom", "stock_uom"], + ["uom", "uom"] + ], + "postprocess": update_item + } + }, target_doclist, postprocess) + + return [d.fields for d in target_doclist] + +def get_material_requests_based_on_supplier(supplier): + supplier_items = [d[0] for d in webnotes.conn.get_values("Item", + {"default_supplier": supplier})] + material_requests = webnotes.conn.sql_list("""select distinct mr.name + from `tabMaterial Request` mr, `tabMaterial Request Item` mr_item + where mr.name = mr_item.parent + and mr_item.item_code in (%s) + and mr.material_request_type = 'Purchase' + and ifnull(mr.per_ordered, 0) < 99.99 + and mr.docstatus = 1 + and mr.status != 'Stopped'""" % ', '.join(['%s']*len(supplier_items)), + tuple(supplier_items)) + return material_requests, supplier_items + @webnotes.whitelist() def make_supplier_quotation(source_name, target_doclist=None): from webnotes.model.mapper import get_mapped_doclist @@ -287,11 +343,6 @@ def make_stock_entry(source_name, target_doclist=None): def set_purpose(source, target): target[0].purpose = "Material Transfer" - - def update_item(source, target, source_parent): - target.conversion_factor = 1 - target.qty = flt(source.qty) - flt(source.ordered_qty) - doclist = get_mapped_doclist("Material Request", source_name, { "Material Request": {