diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index aa0232b3cb..a0528210c5 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -6,6 +6,7 @@ import frappe from frappe import _, scrub from frappe.utils import flt + def execute(filters=None): if not filters: filters = frappe._dict() company_currency = frappe.db.get_value("Company", filters.company, "default_currency") @@ -188,8 +189,8 @@ class GrossProfitGenerator(object): # stock_ledger_entries should already be filtered by item_code and warehouse and # sorted by posting_date desc, posting_time desc if item_code in self.non_stock_items: - # average purchasing rate for non-stock items - item_rate = self.get_average_buying_rate(item_code) + #Issue 6089-Get last purchasing rate for non-stock item + item_rate = self.get_last_purchase_rate(item_code) return flt(row.qty) * item_rate else: @@ -225,6 +226,22 @@ class GrossProfitGenerator(object): return self.average_buying_rate[item_code] + def get_last_purchase_rate(self, item_code): + if self.filters.to_date: + last_purchase_rate = frappe.db.sql(""" + select (a.base_rate / a.conversion_factor) + from `tabPurchase Invoice Item` a + where a.item_code = %s and a.docstatus=1 + and modified <= %s + order by a.modified desc limit 1""", (item_code,self.filters.to_date)) + else: + last_purchase_rate = frappe.db.sql(""" + select (a.base_rate / a.conversion_factor) + from `tabPurchase Invoice Item` a + where a.item_code = %s and a.docstatus=1 + order by a.modified desc limit 1""", item_code) + return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0 + def load_invoice_items(self): conditions = "" if self.filters.company: