fix: Stock Analytics Report must consider warehouse during calculation (#26908)

* fix: Stock Analytics Report must consider warehouse during calculation

* fix: Brand filter in Stock Analytics

(cherry picked from commit 703b081172981833bcf9a1fc5c86517817c3ff32)
This commit is contained in:
Marica 2021-08-12 10:31:01 +05:30 committed by frappe-pr-bot
parent fd1c47ef77
commit 08e4026456
2 changed files with 39 additions and 8 deletions

View File

@ -114,14 +114,41 @@ def get_period(posting_date, filters):
def get_periodic_data(entry, filters):
"""Structured as:
Item 1
- Balance (updated and carried forward):
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
- Jun 2021 (sum of warehouse quantities used in report)
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
- Jul 2021 (sum of warehouse quantities used in report)
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
Item 2
- Balance (updated and carried forward):
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
- Jun 2021 (sum of warehouse quantities used in report)
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
- Jul 2021 (sum of warehouse quantities used in report)
- Warehouse A : bal_qty/value
- Warehouse B : bal_qty/value
"""
periodic_data = {}
for d in entry:
period = get_period(d.posting_date, filters)
bal_qty = 0
# if period against item does not exist yet, instantiate it
# insert existing balance dict against period, and add/subtract to it
if periodic_data.get(d.item_code) and not periodic_data.get(d.item_code).get(period):
periodic_data[d.item_code][period] = periodic_data[d.item_code]['balance']
if d.voucher_type == "Stock Reconciliation":
if periodic_data.get(d.item_code):
bal_qty = periodic_data[d.item_code]["balance"]
if periodic_data.get(d.item_code) and periodic_data.get(d.item_code).get('balance').get(d.warehouse):
bal_qty = periodic_data[d.item_code]['balance'][d.warehouse]
qty_diff = d.qty_after_transaction - bal_qty
else:
@ -132,12 +159,12 @@ def get_periodic_data(entry, filters):
else:
value = d.stock_value_difference
periodic_data.setdefault(d.item_code, {}).setdefault(period, 0.0)
periodic_data.setdefault(d.item_code, {}).setdefault("balance", 0.0)
periodic_data[d.item_code]["balance"] += value
periodic_data[d.item_code][period] = periodic_data[d.item_code]["balance"]
# period-warehouse wise balance
periodic_data.setdefault(d.item_code, {}).setdefault('balance', {}).setdefault(d.warehouse, 0.0)
periodic_data.setdefault(d.item_code, {}).setdefault(period, {}).setdefault(d.warehouse, 0.0)
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]
return periodic_data
@ -160,7 +187,8 @@ def get_data(filters):
total = 0
for dummy, end_date in ranges:
period = get_period(end_date, filters)
amount = flt(periodic_data.get(item_data.name, {}).get(period))
period_data = periodic_data.get(item_data.name, {}).get(period)
amount = sum(period_data.values()) if period_data else 0
row[scrub(period)] = amount
total += amount
row["total"] = total

View File

@ -235,12 +235,15 @@ def filter_items_with_no_transactions(iwb_map, float_precision):
return iwb_map
def get_items(filters):
"Get items based on item code, item group or brand."
conditions = []
if filters.get("item_code"):
conditions.append("item.name=%(item_code)s")
else:
if filters.get("item_group"):
conditions.append(get_item_group_condition(filters.get("item_group")))
if filters.get("brand"): # used in stock analytics report
conditions.append("item.brand=%(brand)s")
items = []
if conditions: