From 6d8d3b4ceeca8b74ebe790557567e58ec71dbdd9 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 21 Mar 2013 18:45:02 +0530 Subject: [PATCH] fixes in gross profit report --- .../doctype/sales_invoice/sales_invoice.py | 2 +- accounts/report/gross_profit/gross_profit.py | 7 +++- stock/doctype/delivery_note/delivery_note.py | 2 +- stock/utils.py | 41 +++++++++++++------ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py index 22ac8458a8..620859e567 100644 --- a/accounts/doctype/sales_invoice/sales_invoice.py +++ b/accounts/doctype/sales_invoice/sales_invoice.py @@ -793,7 +793,7 @@ class DocType(SellingController): def set_buying_amount(self): if cint(self.doc.is_pos) and cint(self.doc.update_stock): 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) else: stock_ledger_entries = item_sales_bom = None diff --git a/accounts/report/gross_profit/gross_profit.py b/accounts/report/gross_profit/gross_profit.py index 2480e17679..a871bab73e 100644 --- a/accounts/report/gross_profit/gross_profit.py +++ b/accounts/report/gross_profit/gross_profit.py @@ -7,7 +7,8 @@ def execute(filters=None): if not filters: filters = {} stock_ledger_entries = get_stock_ledger_entries(filters) - item_sales_bom = get_sales_bom() + + item_sales_bom = get_sales_bom("Delivery Note") delivery_note_items = webnotes.conn.sql("""select dn.name, dn.posting_date, dn.posting_time, dn.project_name, item.item_code, item.item_name, item.description, item.warehouse, @@ -25,7 +26,9 @@ def execute(filters=None): for row in delivery_note_items: selling_amount = flt(row.amount) buying_amount = get_buying_amount(row.item_code, row.warehouse, -1*row.qty, - "Delivery Note", row.name, row.item_row, stock_ledger_entries, item_sales_bom) + "Delivery Note", row.name, row.item_row, stock_ledger_entries, + item_sales_bom.get(row.name, webnotes._dict())) + buying_amount = buying_amount > 0 and buying_amount or 0 if selling_amount: diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py index cb7263be7c..82881c5af5 100644 --- a/stock/doctype/delivery_note/delivery_note.py +++ b/stock/doctype/delivery_note/delivery_note.py @@ -396,7 +396,7 @@ class DocType(SellingController): 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 b4d07701ff..047ff33a7d 100644 --- a/stock/utils.py +++ b/stock/utils.py @@ -171,7 +171,8 @@ def get_buying_amount(item_code, warehouse, qty, voucher_type, voucher_no, vouch 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 @@ def _get_buying_amount(voucher_type, voucher_no, item_row, item_code, warehouse, 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