fix(stock): carry opening balance in Stock Analytics

This commit is contained in:
Krishna Shirsath
2026-10-23 14:13:54 +05:30
parent a689f13ae2
commit 3b4f81ffec
2 changed files with 22 additions and 1 deletions
@@ -13,6 +13,8 @@ from erpnext.accounts.utils import get_fiscal_year
from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter
from erpnext.stock.utils import is_reposting_item_valuation_in_progress
OPENING_KEY = "_opening"
def execute(filters=None):
is_reposting_item_valuation_in_progress()
@@ -160,6 +162,8 @@ def get_periodic_data(entry, filters):
for _start_date, end_date in expected_ranges:
expected_periods.append(get_period(end_date, filters))
range_start = getdate(expected_ranges[0][0]) if expected_ranges else None
periodic_data = {}
for d in entry:
period = get_period(d.posting_date, filters)
@@ -194,6 +198,8 @@ def get_periodic_data(entry, filters):
periodic_data[d.item_code]["balance"][d.warehouse] += value
periodic_data[d.item_code][period][d.warehouse] = periodic_data[d.item_code]["balance"][d.warehouse]
if range_start and getdate(d.posting_date) < range_start:
periodic_data[d.item_code][OPENING_KEY] = periodic_data[d.item_code]["balance"].copy()
return periodic_data
@@ -246,7 +252,7 @@ def get_data(filters):
"uom": item_data.stock_uom,
"brand": item_data.brand,
}
previous_period_value = 0.0
previous_period_value = sum(periodic_data.get(item_data.name, {}).get(OPENING_KEY, {}).values())
for start_date, end_date in ranges:
period = get_period(end_date, filters)
period_data = periodic_data.get(item_data.name, {}).get(period)
@@ -112,3 +112,18 @@ class TestStockAnalyticsReport(FrappeTestCase):
(-10, add_to_date(today, months=5).replace(day=15)),
]
self.assert_single_item_report(movement, [100, 50, 50, 70, 70, 60])
def test_periods_before_the_first_movement_carry_the_opening_balance(self):
self.generate_stock([(10, datetime.date(2026, 4, 15)), (5, datetime.date(2026, 8, 10))])
filters = _dict(
range="Monthly",
from_date=datetime.date(2026, 6, 1),
to_date=datetime.date(2026, 8, 28),
value_quantity="Quantity",
company="_Test Company",
item_code=self.item,
)
cols, data = stock_analytics(filters)
self.compare_analytics_row(frappe._dict(data[0]), cols, [10, 10, 15])