This commit is contained in:
pawan 2016-08-22 14:14:52 +05:30
parent 17824c74cf
commit 851e05672c

View File

@ -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: