From 851e05672cfc015b5adf974073f7c81ebff0813b Mon Sep 17 00:00:00 2001 From: pawan Date: Mon, 22 Aug 2016 14:14:52 +0530 Subject: [PATCH] [fix] #6089 --- .../report/gross_profit/gross_profit.py | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index aa0232b3cb..6ae9086956 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") @@ -92,14 +93,14 @@ def get_columns(group_wise_columns, filters): class GrossProfitGenerator(object): def __init__(self, filters=None): - self.data = [] - self.average_buying_rate = {} - self.filters = frappe._dict(filters) - self.load_invoice_items() - self.load_stock_ledger_entries() - self.load_product_bundle() - self.load_non_stock_items() - self.process() + self.data = [] + self.average_buying_rate = {} + self.filters = frappe._dict(filters) + self.load_invoice_items() + self.load_stock_ledger_entries() + self.load_product_bundle() + self.load_non_stock_items() + self.process() def process(self): self.grouped = {} @@ -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 items + item_rate = self.get_last_purchase_rate(item_code) return flt(row.qty) * item_rate else: @@ -225,6 +226,23 @@ 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)) + return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0 + 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: