Merge pull request #38592 from s-aga-r/FIX-STOCK-VARIANCE

fix: consider the `Valuation Method` while picking incorrect SLE
This commit is contained in:
s-aga-r 2023-12-05 18:08:01 +05:30 committed by GitHub
commit 9424bbc01c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,11 @@ def get_columns():
"label": _("Warehouse"), "label": _("Warehouse"),
"options": "Warehouse", "options": "Warehouse",
}, },
{
"fieldname": "valuation_method",
"fieldtype": "Data",
"label": _("Valuation Method"),
},
{ {
"fieldname": "voucher_type", "fieldname": "voucher_type",
"fieldtype": "Link", "fieldtype": "Link",
@ -194,6 +199,7 @@ def get_columns():
def get_data(filters=None): def get_data(filters=None):
filters = frappe._dict(filters or {}) filters = frappe._dict(filters or {})
item_warehouse_map = get_item_warehouse_combinations(filters) item_warehouse_map = get_item_warehouse_combinations(filters)
valuation_method = frappe.db.get_single_value("Stock Settings", "valuation_method")
data = [] data = []
if item_warehouse_map: if item_warehouse_map:
@ -206,8 +212,17 @@ def get_data(filters=None):
continue continue
for row in report_data: for row in report_data:
if has_difference(row, precision, filters.difference_in): if has_difference(
data.append(add_item_warehouse_details(row, item_warehouse)) row, precision, filters.difference_in, item_warehouse.valuation_method or valuation_method
):
row.update(
{
"item_code": item_warehouse.item_code,
"warehouse": item_warehouse.warehouse,
"valuation_method": item_warehouse.valuation_method or valuation_method,
}
)
data.append(row)
break break
return data return data
@ -229,6 +244,7 @@ def get_item_warehouse_combinations(filters: dict = None) -> dict:
.select( .select(
bin.item_code, bin.item_code,
bin.warehouse, bin.warehouse,
item.valuation_method,
) )
.where( .where(
(item.is_stock_item == 1) (item.is_stock_item == 1)
@ -248,37 +264,27 @@ def get_item_warehouse_combinations(filters: dict = None) -> dict:
return query.run(as_dict=1) return query.run(as_dict=1)
def has_difference(row, precision, difference_in): def has_difference(row, precision, difference_in, valuation_method):
has_qty_difference = flt(row.difference_in_qty, precision) or flt(row.fifo_qty_diff, precision) if valuation_method == "Moving Average":
has_value_difference = ( qty_diff = flt(row.difference_in_qty, precision)
flt(row.diff_value_diff, precision) value_diff = flt(row.diff_value_diff, precision)
or flt(row.fifo_value_diff, precision) valuation_diff = flt(row.valuation_diff, precision)
or flt(row.fifo_difference_diff, precision) else:
) qty_diff = flt(row.difference_in_qty, precision) or flt(row.fifo_qty_diff, precision)
has_valuation_difference = flt(row.valuation_diff, precision) or flt( value_diff = (
row.fifo_valuation_diff, precision flt(row.diff_value_diff, precision)
) or flt(row.fifo_value_diff, precision)
or flt(row.fifo_difference_diff, precision)
)
valuation_diff = flt(row.valuation_diff, precision) or flt(row.fifo_valuation_diff, precision)
if difference_in == "Qty" and has_qty_difference: if difference_in == "Qty" and qty_diff:
return True return True
elif difference_in == "Value" and has_value_difference: elif difference_in == "Value" and value_diff:
return True return True
elif difference_in == "Valuation" and has_valuation_difference: elif difference_in == "Valuation" and valuation_diff:
return True return True
elif difference_in not in ["Qty", "Value", "Valuation"] and ( elif difference_in not in ["Qty", "Value", "Valuation"] and (
has_qty_difference or has_value_difference or has_valuation_difference qty_diff or value_diff or valuation_diff
): ):
return True return True
return False
def add_item_warehouse_details(row, item_warehouse):
row.update(
{
"item_code": item_warehouse.item_code,
"warehouse": item_warehouse.warehouse,
}
)
return row