Skip to content

Commit

Permalink
fix: incorrect Opening Value in Stock Balance report (backport #3…
Browse files Browse the repository at this point in the history
…4461) (#34622)

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

(cherry picked from commit b04a101)

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

* chore: `conflicts`

---------

Co-authored-by: s-aga-r <sagarsharma.s312@gmail.com>
  • Loading branch information
mergify[bot] and s-aga-r committed Mar 28, 2023
1 parent 3aab6e6 commit e53a96a
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions erpnext/stock/report/stock_balance/stock_balance.py
Expand Up @@ -6,6 +6,7 @@

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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e53a96a

Please sign in to comment.