Skip to content

Commit

Permalink
fix: balance quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed May 23, 2023
1 parent d9979b2 commit 545b2d3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def create_closing_stock_balance_entries(self):
"warehouse_type": self.warehouse_type,
"include_uom": self.include_uom,
"ignore_closing_balance": 1,
"show_variant_attributes": 1,
"show_stock_ageing_data": 1,
}
)
)
Expand Down
3 changes: 2 additions & 1 deletion erpnext/stock/report/stock_balance/stock_balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ frappe.query_reports["Stock Balance"] = {
{
"fieldname": 'ignore_closing_balance',
"label": __('Ignore Closing Balance'),
"fieldtype": 'Check'
"fieldtype": 'Check',
"default": 1
},
],

Expand Down
71 changes: 36 additions & 35 deletions erpnext/stock/report/stock_balance/stock_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def set_company_currency(self) -> None:
def run(self):
self.float_precision = cint(frappe.db.get_default("float_precision")) or 3

self.inventory_dimensions = get_inventory_dimension_fields()
self.inventory_dimensions = self.get_inventory_dimension_fields()
self.prepare_opening_data_from_closing_balance()
self.prepare_stock_ledger_entries()
self.prepare_new_data()
Expand Down Expand Up @@ -110,20 +110,26 @@ def prepare_new_data(self):
report_data.update(variant_data)

if self.filters.get("show_stock_ageing_data"):
fifo_queue = item_wise_fifo_queue[(report_data.item_code, report_data.warehouse)].get(
"fifo_queue"
)
opening_fifo_queue = self.get_opening_fifo_queue(report_data) or []

fifo_queue = []
if fifo_queue := item_wise_fifo_queue.get((report_data.item_code, report_data.warehouse)):
fifo_queue = fifo_queue.get("fifo_queue")

stock_ageing_data = {"average_age": 0, "earliest_age": 0, "latest_age": 0}
if fifo_queue:
fifo_queue = sorted(filter(_func, fifo_queue), key=_func)
opening_fifo_queue.extend(fifo_queue)

stock_ageing_data = {"average_age": 0, "earliest_age": 0, "latest_age": 0}
if opening_fifo_queue:
fifo_queue = sorted(filter(_func, opening_fifo_queue), key=_func)
if not fifo_queue:
continue

to_date = self.to_date
stock_ageing_data["average_age"] = get_average_age(fifo_queue, to_date)
stock_ageing_data["earliest_age"] = date_diff(to_date, fifo_queue[0][1])
stock_ageing_data["latest_age"] = date_diff(to_date, fifo_queue[-1][1])
stock_ageing_data["fifo_queue"] = fifo_queue

report_data.update(stock_ageing_data)

Expand All @@ -146,11 +152,6 @@ def get_item_warehouse_map(self):
if group_by_key not in item_warehouse_map:
self.initialize_data(item_warehouse_map, group_by_key, entry)

qty_dict = item_warehouse_map[group_by_key]
qty_dict.val_rate = entry.valuation_rate
qty_dict.bal_qty += flt(qty_dict.opening_qty)
qty_dict.bal_val += flt(qty_dict.opening_val)

item_warehouse_map = filter_items_with_no_transactions(
item_warehouse_map, self.float_precision, self.inventory_dimensions
)
Expand Down Expand Up @@ -186,10 +187,6 @@ def prepare_item_warehouse_map(self, item_warehouse_map, entry, group_by_key):
qty_dict.out_qty += abs(qty_diff)
qty_dict.out_val += abs(value_diff)

# if self.opening_data and group_by_key in self.opening_data:
# qty_diff += flt(qty_dict.opening_qty)
# value_diff += flt(qty_dict.opening_val)

qty_dict.val_rate = entry.valuation_rate
qty_dict.bal_qty += qty_diff
qty_dict.bal_val += value_diff
Expand All @@ -208,12 +205,13 @@ def initialize_data(self, item_warehouse_map, group_by_key, entry):
"item_name": entry.item_name,
"opening_qty": opening_data.get("bal_qty") or 0.0,
"opening_val": opening_data.get("bal_val") or 0.0,
"opening_fifo_queue": opening_data.get("fifo_queue") or [],
"in_qty": 0.0,
"in_val": 0.0,
"out_qty": 0.0,
"out_val": 0.0,
"bal_qty": 0.0,
"bal_val": 0.0,
"bal_qty": opening_data.get("bal_qty") or 0.0,
"bal_val": opening_data.get("bal_val") or 0.0,
"val_rate": 0.0,
}
)
Expand Down Expand Up @@ -294,7 +292,7 @@ def prepare_stock_ledger_entries(self):
self.sle_entries = query.run(as_dict=True)

def apply_inventory_dimensions_filters(self, query, sle) -> str:
inventory_dimension_fields = get_inventory_dimension_fields()
inventory_dimension_fields = self.get_inventory_dimension_fields()
if inventory_dimension_fields:
for fieldname in inventory_dimension_fields:
query = query.select(fieldname)
Expand Down Expand Up @@ -437,20 +435,6 @@ def get_columns(self):
"convertible": "rate",
"options": "currency",
},
{
"label": _("Reorder Level"),
"fieldname": "reorder_level",
"fieldtype": "Float",
"width": 80,
"convertible": "qty",
},
{
"label": _("Reorder Qty"),
"fieldname": "reorder_qty",
"fieldtype": "Float",
"width": 80,
"convertible": "qty",
},
{
"label": _("Company"),
"fieldname": "company",
Expand Down Expand Up @@ -556,9 +540,17 @@ def get_opening_vouchers(self):

return opening_vouchers

@staticmethod
def get_inventory_dimension_fields():
return [dimension.fieldname for dimension in get_inventory_dimensions()]

@staticmethod
def get_opening_fifo_queue(report_data):
opening_fifo_queue = report_data.get("opening_fifo_queue") or []
for row in opening_fifo_queue:
row[1] = getdate(row[1])

def get_inventory_dimension_fields():
return [dimension.fieldname for dimension in get_inventory_dimensions()]
return opening_fifo_queue


def filter_items_with_no_transactions(iwb_map, float_precision: float, inventory_dimensions: list):
Expand All @@ -571,7 +563,16 @@ def filter_items_with_no_transactions(iwb_map, float_precision: float, inventory
if key in inventory_dimensions:
continue

if key in ["item_code", "warehouse", "item_name", "item_group", "projecy", "stock_uom"]:
if key in [
"item_code",
"warehouse",
"item_name",
"item_group",
"projecy",
"stock_uom",
"company",
"opening_fifo_queue",
]:
continue

val = flt(val, float_precision)
Expand Down

0 comments on commit 545b2d3

Please sign in to comment.