fixes in gross profit report
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index cb7263b..82881c5 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -396,7 +396,7 @@
def set_buying_amount(self):
from stock.utils import get_buying_amount, get_sales_bom
stock_ledger_entries = self.get_stock_ledger_entries()
- item_sales_bom = get_sales_bom()
+ item_sales_bom = get_sales_bom(self.doc.doctype, self.doc.name)
if stock_ledger_entries:
for item in self.doclist.get({"parentfield": "delivery_note_details"}):
diff --git a/stock/utils.py b/stock/utils.py
index b4d0770..047ff33 100644
--- a/stock/utils.py
+++ b/stock/utils.py
@@ -171,7 +171,8 @@
buying_amount = 0.0
for bom_item in item_sales_bom[item_code]:
buying_amount += _get_buying_amount(voucher_type, voucher_no, "[** No Item Row **]",
- bom_item.item_code, warehouse, bom_item.qty * qty, stock_ledger_entries)
+ bom_item.item_code, bom_item.warehouse or warehouse,
+ bom_item.total_qty or (bom_item.qty * qty), stock_ledger_entries)
return buying_amount
else:
# doesn't have sales bom
@@ -184,21 +185,37 @@
if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
(sle.voucher_detail_no == item_row or (sle.voucher_type != "Stock Reconciliation"
and sle.item_code == item_code and sle.warehouse == warehouse and flt(sle.qty) == qty)):
- # print "previous_sle", stock_ledger_entries[i+1]
- # print "current sle", sle
previous_stock_value = len(stock_ledger_entries) > i+1 and \
flt(stock_ledger_entries[i+1].stock_value) or 0.0
buying_amount = previous_stock_value - flt(sle.stock_value)
+
return buying_amount
return 0.0
-def get_sales_bom():
- item_sales_bom = {}
- # for r in webnotes.conn.sql("""select parent_item, item_code, qty, warehouse, voucher_detail_no
- # from `tabDelivery Note Packing Item` where docstatus = 1""", as_dict=1):
- for r in webnotes.conn.sql("""select parent, item_code, qty from `tabSales BOM Item`""",
- as_dict=1):
- item_sales_bom.setdefault(r.parent, []).append(r)
-
- return item_sales_bom
\ No newline at end of file
+def get_sales_bom(doctype=None, docname=None):
+ item_sales_bom = webnotes._dict()
+
+ query = """select parenttype, parent, parent_item,
+ item_code, warehouse, -1*qty as total_qty
+ from `tabDelivery Note Packing Item` where docstatus=1"""
+
+ args = {}
+ if doctype:
+ query += " and parenttype=%(parenttype)s"
+ args["parenttype"] = doctype
+
+ if docname:
+ query += " and parent=%(parent)s"
+ args["parent"] = docname
+
+ for d in webnotes.conn.sql(query, args, as_dict=1):
+ item_sales_bom.setdefault(d.parenttype, webnotes._dict()).setdefault(d.parent,
+ webnotes._dict()).setdefault(d.parent_item, []).append(d)
+
+ if doctype and docname:
+ return item_sales_bom[doctype].get(docname, webnotes._dict())
+ elif doctype:
+ return item_sales_bom[doctype]
+ else:
+ return item_sales_bom