From 94c90bdbf9db15ef7c71ad3969caf3a4c34a342e Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 30 Aug 2013 22:48:19 +0530 Subject: [PATCH] [fix] [minor] buying amount for sales bom item --- accounts/report/gross_profit/gross_profit.py | 15 ++++++++---- controllers/selling_controller.py | 19 +++++++++------ .../stock_reconciliation.py | 2 +- stock/utils.py | 24 +++++++++---------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/accounts/report/gross_profit/gross_profit.py b/accounts/report/gross_profit/gross_profit.py index 3aba234c19..590babbf6b 100644 --- a/accounts/report/gross_profit/gross_profit.py +++ b/accounts/report/gross_profit/gross_profit.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import webnotes from webnotes.utils import flt -from stock.utils import get_buying_amount +from stock.utils import get_buying_amount, get_sales_bom_buying_amount def execute(filters=None): if not filters: filters = {} @@ -21,10 +21,15 @@ def execute(filters=None): data = [] for row in source: selling_amount = flt(row.amount) - - buying_amount = get_buying_amount(row.item_code, row.parenttype, row.name, row.item_row, - stock_ledger_entries.get((row.item_code, row.warehouse), []), - item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict())) + + item_sales_bom_map = item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict()) + + if item_sales_bom_map.get(row.item_code): + buying_amount = get_sales_bom_buying_amount(row.item_code, row.warehouse, + row.parenttype, row.name, row.item_row, stock_ledger_entries, item_sales_bom_map) + else: + buying_amount = get_buying_amount(row.parenttype, row.name, row.item_row, + stock_ledger_entries.get((row.item_code, row.warehouse), [])) buying_amount = buying_amount > 0 and buying_amount or 0 diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py index 4b4b1c71a3..a13d747279 100644 --- a/controllers/selling_controller.py +++ b/controllers/selling_controller.py @@ -85,7 +85,7 @@ class SellingController(StockController): self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency) def set_buying_amount(self, stock_ledger_entries = None): - from stock.utils import get_buying_amount + from stock.utils import get_buying_amount, get_sales_bom_buying_amount if not stock_ledger_entries: stock_ledger_entries = self.get_stock_ledger_entries() @@ -99,13 +99,18 @@ class SellingController(StockController): for item in self.doclist.get({"parentfield": self.fname}): if item.item_code in self.stock_items or \ (item_sales_bom and item_sales_bom.get(item.item_code)): - buying_amount = get_buying_amount(item.item_code, self.doc.doctype, self.doc.name, item.name, - stock_ledger_entries.get((item.item_code, item.warehouse), []), - item_sales_bom) + if item.item_code in self.stock_items: + buying_amount = get_buying_amount(self.doc.doctype, self.doc.name, + item.name, stock_ledger_entries.get((item.item_code, + item.warehouse), [])) + elif item_sales_bom and item_sales_bom.get(item.item_code): + buying_amount = get_sales_bom_buying_amount(item.item_code, item.warehouse, + self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, + item_sales_bom) - item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 - webnotes.conn.set_value(item.doctype, item.name, "buying_amount", - item.buying_amount) + item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 + webnotes.conn.set_value(item.doctype, item.name, "buying_amount", + item.buying_amount) def check_expense_account(self, item): if item.buying_amount and not item.expense_account: diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.py b/stock/doctype/stock_reconciliation/stock_reconciliation.py index 617ec698c4..dbb0a348c3 100644 --- a/stock/doctype/stock_reconciliation/stock_reconciliation.py +++ b/stock/doctype/stock_reconciliation/stock_reconciliation.py @@ -293,7 +293,7 @@ class DocType(StockController): self.doc.stock_value_difference = 0.0 for d in self.entries: - self.doc.stock_value_difference -= get_buying_amount(d.item_code, self.doc.doctype, self.doc.name, + self.doc.stock_value_difference -= get_buying_amount(self.doc.doctype, self.doc.name, d.voucher_detail_no, stock_ledger_entries.get((d.item_code, d.warehouse), [])) webnotes.conn.set(self.doc, "stock_value_difference", self.doc.stock_value_difference) diff --git a/stock/utils.py b/stock/utils.py index f04b663236..52471e4c6f 100644 --- a/stock/utils.py +++ b/stock/utils.py @@ -164,20 +164,18 @@ def validate_warehouse_user(warehouse): webnotes.throw(_("Not allowed entry in Warehouse") \ + ": " + warehouse, UserNotAllowedForWarehouse) -def get_buying_amount(item_code, voucher_type, voucher_no, voucher_detail_no, - stock_ledger_entries, item_sales_bom=None): - if item_sales_bom and item_sales_bom.get(item_code): - # sales bom item - buying_amount = 0.0 - for bom_item in item_sales_bom[item_code]: - if bom_item.get("parent_detail_docname")==voucher_detail_no: - buying_amount += _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries) - return buying_amount - else: - # doesn't have sales bom - return _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries) +def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no, + stock_ledger_entries, item_sales_bom): + # sales bom item + buying_amount = 0.0 + for bom_item in item_sales_bom[item_code]: + if bom_item.get("parent_detail_docname")==voucher_detail_no: + buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no, + stock_ledger_entries.get((bom_item.item_code, warehouse), [])) + + return buying_amount -def _get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries): +def get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries): # IMP NOTE # stock_ledger_entries should already be filtered by item_code and warehouse and # sorted by posting_date desc, posting_time desc