refactor: get_stock_value_on() to get stock value of multiple warehouses at once

This commit is contained in:
s-aga-r
2023-04-25 13:54:36 +05:30
parent 9a37ac6c25
commit e782a054c8
3 changed files with 15 additions and 20 deletions

View File

@@ -1368,10 +1368,7 @@ def get_stock_and_account_balance(account=None, posting_date=None, company=None)
if wh_details.account == account and not wh_details.is_group if wh_details.account == account and not wh_details.is_group
] ]
total_stock_value = 0.0 total_stock_value = get_stock_value_on(related_warehouses, posting_date)
for warehouse in related_warehouses:
value = get_stock_value_on(warehouse, posting_date)
total_stock_value += value
precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency")
return flt(account_balance, precision), flt(total_stock_value, precision), related_warehouses return flt(account_balance, precision), flt(total_stock_value, precision), related_warehouses

View File

@@ -84,7 +84,7 @@ def get_data(report_filters):
closing_date = add_days(from_date, -1) closing_date = add_days(from_date, -1)
for key, stock_data in voucher_wise_dict.items(): for key, stock_data in voucher_wise_dict.items():
prev_stock_value = get_stock_value_on( prev_stock_value = get_stock_value_on(
posting_date=closing_date, item_code=key[0], warehouse=key[1] posting_date=closing_date, item_code=key[0], warehouses=key[1]
) )
for data in stock_data: for data in stock_data:
expected_stock_value = prev_stock_value + data.stock_value_difference expected_stock_value = prev_stock_value + data.stock_value_difference

View File

@@ -9,9 +9,9 @@ import frappe
from frappe import _ from frappe import _
from frappe.query_builder.functions import CombineDatetime, IfNull, Sum from frappe.query_builder.functions import CombineDatetime, IfNull, Sum
from frappe.utils import cstr, flt, get_link_to_form, nowdate, nowtime from frappe.utils import cstr, flt, get_link_to_form, nowdate, nowtime
from pypika.terms import ExistsCriterion
import erpnext import erpnext
from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses
from erpnext.stock.valuation import FIFOValuation, LIFOValuation from erpnext.stock.valuation import FIFOValuation, LIFOValuation
BarcodeScanResult = Dict[str, Optional[str]] BarcodeScanResult = Dict[str, Optional[str]]
@@ -54,7 +54,9 @@ def get_stock_value_from_bin(warehouse=None, item_code=None):
return stock_value return stock_value
def get_stock_value_on(warehouse=None, posting_date=None, item_code=None): def get_stock_value_on(
warehouses: list | str = None, posting_date: str = None, item_code: str = None
) -> float:
if not posting_date: if not posting_date:
posting_date = nowdate() posting_date = nowdate()
@@ -67,20 +69,16 @@ def get_stock_value_on(warehouse=None, posting_date=None, item_code=None):
.orderby(sle.creation, order=frappe.qb.desc) .orderby(sle.creation, order=frappe.qb.desc)
) )
if warehouse: if warehouses:
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"]) if isinstance(warehouses, str):
warehouses = [warehouses]
if is_group: warehouses = set(warehouses)
wh = frappe.qb.DocType("Warehouse") for wh in list(warehouses):
query = query.where( if frappe.db.get_value("Warehouse", wh, "is_group"):
ExistsCriterion( warehouses.update(get_child_warehouses(wh))
frappe.qb.from_(wh)
.select(wh.name) query = query.where(sle.warehouse.isin(warehouses))
.where((wh.name == sle.warehouse) & (wh.lft >= lft) & (wh.rgt <= rgt))
)
)
else:
query = query.where(sle.warehouse == warehouse)
if item_code: if item_code:
query = query.where(sle.item_code == item_code) query = query.where(sle.item_code == item_code)