fix: incorrect Opening Value in Stock Balance report (backport #34461) (#34622)

* fix: incorrect `Opening Value` in `Stock Balance` report

(cherry picked from commit b04a101c11)

# Conflicts:
#	erpnext/stock/report/stock_balance/stock_balance.py

* chore: `conflicts`

---------

Co-authored-by: s-aga-r <sagarsharma.s312@gmail.com>
This commit is contained in:
mergify[bot]
2023-03-28 21:09:09 +05:30
committed by GitHub
parent 3aab6e6fa8
commit e53a96ae1d

View File

@@ -6,6 +6,7 @@ from operator import itemgetter
import frappe
from frappe import _
from frappe.query_builder.functions import Coalesce
from frappe.utils import cint, date_diff, flt, getdate
from six import iteritems
@@ -276,11 +277,39 @@ def get_stock_ledger_entries(filters, items):
)
def get_opening_vouchers(to_date):
opening_vouchers = {"Stock Entry": [], "Stock Reconciliation": []}
se = frappe.qb.DocType("Stock Entry")
sr = frappe.qb.DocType("Stock Reconciliation")
vouchers_data = (
frappe.qb.from_(
(
frappe.qb.from_(se)
.select(se.name, Coalesce("Stock Entry").as_("voucher_type"))
.where((se.docstatus == 1) & (se.posting_date <= to_date) & (se.is_opening == "Yes"))
)
+ (
frappe.qb.from_(sr)
.select(sr.name, Coalesce("Stock Reconciliation").as_("voucher_type"))
.where((sr.docstatus == 1) & (sr.posting_date <= to_date) & (sr.purpose == "Opening Stock"))
)
).select("voucher_type", "name")
).run(as_dict=True)
if vouchers_data:
for d in vouchers_data:
opening_vouchers[d.voucher_type].append(d.name)
return opening_vouchers
def get_item_warehouse_map(filters, sle):
iwb_map = {}
from_date = getdate(filters.get("from_date"))
to_date = getdate(filters.get("to_date"))
opening_vouchers = get_opening_vouchers(to_date)
float_precision = cint(frappe.db.get_default("float_precision")) or 3
for d in sle:
@@ -309,11 +338,7 @@ def get_item_warehouse_map(filters, sle):
value_diff = flt(d.stock_value_difference)
if d.posting_date < from_date or (
d.posting_date == from_date
and d.voucher_type == "Stock Reconciliation"
and frappe.db.get_value("Stock Reconciliation", d.voucher_no, "purpose") == "Opening Stock"
):
if d.posting_date < from_date or d.voucher_no in opening_vouchers.get(d.voucher_type, []):
qty_dict.opening_qty += qty_diff
qty_dict.opening_val += value_diff