Merge pull request #34918 from s-aga-r/FIX-ISS-22-23-06298
perf: Journal Entries
This commit is contained in:
commit
b24704740b
@ -1374,10 +1374,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
|
||||||
|
@ -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
|
||||||
|
@ -7,10 +7,11 @@ from typing import Dict, Optional
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.query_builder.functions import CombineDatetime
|
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
|
||||||
|
|
||||||
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]]
|
||||||
@ -53,50 +54,36 @@ 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()
|
||||||
|
|
||||||
values, condition = [posting_date], ""
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
||||||
|
query = (
|
||||||
if warehouse:
|
frappe.qb.from_(sle)
|
||||||
|
.select(IfNull(Sum(sle.stock_value_difference), 0))
|
||||||
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
.where((sle.posting_date <= posting_date) & (sle.is_cancelled == 0))
|
||||||
|
.orderby(CombineDatetime(sle.posting_date, sle.posting_time), order=frappe.qb.desc)
|
||||||
if is_group:
|
.orderby(sle.creation, order=frappe.qb.desc)
|
||||||
values.extend([lft, rgt])
|
|
||||||
condition += "and exists (\
|
|
||||||
select name from `tabWarehouse` wh where wh.name = sle.warehouse\
|
|
||||||
and wh.lft >= %s and wh.rgt <= %s)"
|
|
||||||
|
|
||||||
else:
|
|
||||||
values.append(warehouse)
|
|
||||||
condition += " AND warehouse = %s"
|
|
||||||
|
|
||||||
if item_code:
|
|
||||||
values.append(item_code)
|
|
||||||
condition += " AND item_code = %s"
|
|
||||||
|
|
||||||
stock_ledger_entries = frappe.db.sql(
|
|
||||||
"""
|
|
||||||
SELECT item_code, stock_value, name, warehouse
|
|
||||||
FROM `tabStock Ledger Entry` sle
|
|
||||||
WHERE posting_date <= %s {0}
|
|
||||||
and is_cancelled = 0
|
|
||||||
ORDER BY timestamp(posting_date, posting_time) DESC, creation DESC
|
|
||||||
""".format(
|
|
||||||
condition
|
|
||||||
),
|
|
||||||
values,
|
|
||||||
as_dict=1,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
sle_map = {}
|
if warehouses:
|
||||||
for sle in stock_ledger_entries:
|
if isinstance(warehouses, str):
|
||||||
if not (sle.item_code, sle.warehouse) in sle_map:
|
warehouses = [warehouses]
|
||||||
sle_map[(sle.item_code, sle.warehouse)] = flt(sle.stock_value)
|
|
||||||
|
|
||||||
return sum(sle_map.values())
|
warehouses = set(warehouses)
|
||||||
|
for wh in list(warehouses):
|
||||||
|
if frappe.db.get_value("Warehouse", wh, "is_group"):
|
||||||
|
warehouses.update(get_child_warehouses(wh))
|
||||||
|
|
||||||
|
query = query.where(sle.warehouse.isin(warehouses))
|
||||||
|
|
||||||
|
if item_code:
|
||||||
|
query = query.where(sle.item_code == item_code)
|
||||||
|
|
||||||
|
return query.run(as_list=True)[0][0]
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user