diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json index 25e9c15376..bb2b47def7 100644 --- a/erpnext/buying/doctype/buying_settings/buying_settings.json +++ b/erpnext/buying/doctype/buying_settings/buying_settings.json @@ -225,7 +225,7 @@ "is_submittable": 0, "issingle": 1, "istable": 0, - "modified": "2015-08-25 04:55:06.052342", + "modified": "2017-12-27 15:20:06.052342", "modified_by": "Administrator", "module": "Buying", "name": "Buying Settings", diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js index 0394f74b71..fe8642c311 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.js +++ b/erpnext/buying/doctype/purchase_order/purchase_order.js @@ -21,22 +21,17 @@ frappe.ui.form.on("Purchase Order", { return erpnext.queries.warehouse(frm.doc); }); + frappe.db.get_value('Buying Settings', {name: 'Buying Settings'}, 'disable_fetch_last_purchase_rate', (r) => { + value = r && cint(r.disable_fetch_last_purchase_rate); + frm.toggle_display('get_last_purchase_rate', !value); + }); + frm.set_indicator_formatter('item_code', function(doc) { return (doc.qty<=doc.received_qty) ? "green" : "orange" }) }, }); frappe.ui.form.on("Purchase Order Item", { - item_code: function(frm) { - frappe.call({ - method: "get_last_purchase_rate", - doc: frm.doc, - callback: function(r, rt) { - frm.trigger('calculate_taxes_and_totals'); - } - }) - }, - schedule_date: function(frm, cdt, cdn) { var row = locals[cdt][cdn]; if (row.schedule_date) { diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json index 2191a556c4..a895a54a79 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.json +++ b/erpnext/buying/doctype/purchase_order/purchase_order.json @@ -41,11 +41,11 @@ "search_index": 0, "set_only_once": 0, "unique": 0 - }, + }, { - "allow_bulk_edit": 0, - "allow_on_submit": 1, - "bold": 0, + "allow_bulk_edit": 0, + "allow_on_submit": 1, + "bold": 0, "collapsible": 0, "columns": 0, "default": "{supplier_name}", @@ -292,40 +292,40 @@ "search_index": 1, "set_only_once": 0, "unique": 0 - }, + }, { - "allow_bulk_edit": 0, - "allow_on_submit": 1, - "bold": 0, - "collapsible": 0, - "columns": 0, - "default": "", - "fieldname": "schedule_date", - "fieldtype": "Date", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Reqd By Date", - "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, + "allow_bulk_edit": 0, + "allow_on_submit": 1, + "bold": 0, + "collapsible": 0, + "columns": 0, + "default": "", + "fieldname": "schedule_date", + "fieldtype": "Date", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Reqd By Date", + "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_bulk_edit": 0, + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -1238,6 +1238,37 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", + "fieldname": "get_last_purchase_rate", + "fieldtype": "Button", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Get last purchase rate", + "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_bulk_edit": 0, "allow_on_submit": 0, @@ -3260,9 +3291,9 @@ "is_submittable": 1, "issingle": 0, "istable": 0, - "max_attachments": 0, - "modified": "2017-12-19 14:53:03.986840", - "modified_by": "nabinhait@gmail.com", + "max_attachments": 0, + "modified": "2017-12-21 14:45:34.140128", + "modified_by": "Administrator", "module": "Buying", "name": "Purchase Order", "owner": "Administrator", diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index df65ef9297..1929476cf3 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -111,27 +111,26 @@ class PurchaseOrder(BuyingController): def get_last_purchase_rate(self): """get last purchase rates for all items""" + if cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")): return - if not cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")): - conversion_rate = flt(self.get('conversion_rate')) or 1.0 + conversion_rate = flt(self.get('conversion_rate')) or 1.0 + for d in self.get("items"): + if d.item_code: + last_purchase_details = get_last_purchase_details(d.item_code, self.name) + if last_purchase_details: + d.base_price_list_rate = (last_purchase_details['base_price_list_rate'] * + (flt(d.conversion_factor) or 1.0)) + d.discount_percentage = last_purchase_details['discount_percentage'] + d.base_rate = last_purchase_details['base_rate'] * (flt(d.conversion_factor) or 1.0) + d.price_list_rate = d.base_price_list_rate / conversion_rate + d.rate = d.base_rate / conversion_rate + d.last_purchase_rate = d.rate + else: - for d in self.get("items"): - if d.item_code: - last_purchase_details = get_last_purchase_details(d.item_code, self.name) - - if last_purchase_details: - d.base_price_list_rate = (last_purchase_details['base_price_list_rate'] * - (flt(d.conversion_factor) or 1.0)) - d.discount_percentage = last_purchase_details['discount_percentage'] - d.base_rate = last_purchase_details['base_rate'] * (flt(d.conversion_factor) or 1.0) - d.price_list_rate = d.base_price_list_rate / conversion_rate - d.last_purchase_rate = d.base_rate / conversion_rate - else: - - item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate") - if item_last_purchase_rate: - d.base_price_list_rate = d.base_rate = d.price_list_rate \ - = d.last_purchase_rate = item_last_purchase_rate + item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate") + if item_last_purchase_rate: + d.base_price_list_rate = d.base_rate = d.price_list_rate \ + = d.rate = d.last_purchase_rate = item_last_purchase_rate # Check for Closed status def check_for_closed_status(self): @@ -256,6 +255,21 @@ class PurchaseOrder(BuyingController): if item.delivered_by_supplier == 1: item.received_qty = item.qty +def item_last_purchase_rate(name, conversion_rate, item_code, conversion_factor= 1.0): + """get last purchase rate for an item""" + if cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")): return + + conversion_rate = flt(conversion_rate) or 1.0 + + last_purchase_details = get_last_purchase_details(item_code, name) + if last_purchase_details: + last_purchase_rate = (last_purchase_details['base_rate'] * (flt(conversion_factor) or 1.0)) / conversion_rate + return last_purchase_rate + else: + item_last_purchase_rate = frappe.db.get_value("Item", item_code, "last_purchase_rate") + if item_last_purchase_rate: + return item_last_purchase_rate + @frappe.whitelist() def close_or_unclose_purchase_orders(names, status): if not frappe.has_permission("Purchase Order", "write"): diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 0c9d0c74bd..a7638b4169 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -246,6 +246,7 @@ def get_basic_details(args, item): "is_fixed_asset": item.is_fixed_asset, "weight_per_unit":item.weight_per_unit, "weight_uom":item.weight_uom, + "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0 }) # calculate conversion factor @@ -258,6 +259,10 @@ def get_basic_details(args, item): args.conversion_factor = out.conversion_factor out.stock_qty = out.qty * out.conversion_factor + # calculate last purchase rate + from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate + out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.item_code, out.conversion_factor) + # if default specified in item is for another company, fetch from company for d in [ ["Account", "income_account", "default_income_account"],