fix: buying price for non stock item in gross profit report (#22616)
* fix: buying price for non stock item in gross profit report * fix: refactor query
This commit is contained in:
parent
39969647ca
commit
91fe10666e
@ -223,9 +223,9 @@ class GrossProfitGenerator(object):
|
|||||||
# IMP NOTE
|
# IMP NOTE
|
||||||
# stock_ledger_entries should already be filtered by item_code and warehouse and
|
# stock_ledger_entries should already be filtered by item_code and warehouse and
|
||||||
# sorted by posting_date desc, posting_time desc
|
# sorted by posting_date desc, posting_time desc
|
||||||
if item_code in self.non_stock_items:
|
if item_code in self.non_stock_items and (row.project or row.cost_center):
|
||||||
#Issue 6089-Get last purchasing rate for non-stock item
|
#Issue 6089-Get last purchasing rate for non-stock item
|
||||||
item_rate = self.get_last_purchase_rate(item_code)
|
item_rate = self.get_last_purchase_rate(item_code, row)
|
||||||
return flt(row.qty) * item_rate
|
return flt(row.qty) * item_rate
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -253,12 +253,6 @@ class GrossProfitGenerator(object):
|
|||||||
def get_average_buying_rate(self, row, item_code):
|
def get_average_buying_rate(self, row, item_code):
|
||||||
args = row
|
args = row
|
||||||
if not item_code in self.average_buying_rate:
|
if not item_code in self.average_buying_rate:
|
||||||
if item_code in self.non_stock_items:
|
|
||||||
self.average_buying_rate[item_code] = flt(frappe.db.sql("""
|
|
||||||
select sum(base_net_amount) / sum(qty * conversion_factor)
|
|
||||||
from `tabPurchase Invoice Item`
|
|
||||||
where item_code = %s and docstatus=1""", item_code)[0][0])
|
|
||||||
else:
|
|
||||||
args.update({
|
args.update({
|
||||||
'voucher_type': row.parenttype,
|
'voucher_type': row.parenttype,
|
||||||
'voucher_no': row.parent,
|
'voucher_no': row.parent,
|
||||||
@ -271,20 +265,22 @@ class GrossProfitGenerator(object):
|
|||||||
|
|
||||||
return self.average_buying_rate[item_code]
|
return self.average_buying_rate[item_code]
|
||||||
|
|
||||||
def get_last_purchase_rate(self, item_code):
|
def get_last_purchase_rate(self, item_code, row):
|
||||||
|
condition = ''
|
||||||
|
if row.project:
|
||||||
|
condition += " AND a.project='%s'" % (row.project)
|
||||||
|
elif row.cost_center:
|
||||||
|
condition += " AND a.cost_center='%s'" % (row.cost_center)
|
||||||
if self.filters.to_date:
|
if self.filters.to_date:
|
||||||
|
condition += " AND modified='%s'" % (self.filters.to_date)
|
||||||
|
|
||||||
last_purchase_rate = frappe.db.sql("""
|
last_purchase_rate = frappe.db.sql("""
|
||||||
select (a.base_rate / a.conversion_factor)
|
select (a.base_rate / a.conversion_factor)
|
||||||
from `tabPurchase Invoice Item` a
|
from `tabPurchase Invoice Item` a
|
||||||
where a.item_code = %s and a.docstatus=1
|
where a.item_code = %s and a.docstatus=1
|
||||||
and modified <= %s
|
{0}
|
||||||
order by a.modified desc limit 1""", (item_code, self.filters.to_date))
|
order by a.modified desc limit 1""".format(condition), item_code)
|
||||||
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
|
return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0
|
||||||
|
|
||||||
def load_invoice_items(self):
|
def load_invoice_items(self):
|
||||||
@ -321,7 +317,8 @@ class GrossProfitGenerator(object):
|
|||||||
`tabSales Invoice Item`.brand, `tabSales Invoice Item`.dn_detail,
|
`tabSales Invoice Item`.brand, `tabSales Invoice Item`.dn_detail,
|
||||||
`tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty,
|
`tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty,
|
||||||
`tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount,
|
`tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount,
|
||||||
`tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return
|
`tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return,
|
||||||
|
`tabSales Invoice Item`.cost_center
|
||||||
{sales_person_cols}
|
{sales_person_cols}
|
||||||
from
|
from
|
||||||
`tabSales Invoice` inner join `tabSales Invoice Item`
|
`tabSales Invoice` inner join `tabSales Invoice Item`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user