From ed1cc18ab529c218429e88daef94aaa8df4595af Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Mon, 30 Sep 2019 15:15:52 +0530 Subject: [PATCH] fix: stock ledger report not showing data if the UOM filter has selected (#19179) --- .../stock/report/stock_ledger/stock_ledger.py | 2 +- erpnext/stock/utils.py | 53 +++++++++++-------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/erpnext/stock/report/stock_ledger/stock_ledger.py b/erpnext/stock/report/stock_ledger/stock_ledger.py index d386230687..5bdbca23d9 100644 --- a/erpnext/stock/report/stock_ledger/stock_ledger.py +++ b/erpnext/stock/report/stock_ledger/stock_ledger.py @@ -107,7 +107,7 @@ def get_item_details(items, sl_entries, include_uom): if include_uom: cf_field = ", ucd.conversion_factor" cf_join = "left join `tabUOM Conversion Detail` ucd on ucd.parent=item.name and ucd.uom='%s'" \ - % frappe.db.escape(include_uom) + % (include_uom) res = frappe.db.sql(""" select diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py index 4c663e393f..2ac0bae6da 100644 --- a/erpnext/stock/utils.py +++ b/erpnext/stock/utils.py @@ -253,31 +253,40 @@ def update_included_uom_in_report(columns, result, include_uom, conversion_facto return convertible_cols = {} - for col_idx in reversed(range(0, len(columns))): - col = columns[col_idx] - if isinstance(col, dict) and col.get("convertible") in ['rate', 'qty']: - convertible_cols[col_idx] = col['convertible'] - columns.insert(col_idx+1, col.copy()) - columns[col_idx+1]['fieldname'] += "_alt" - if convertible_cols[col_idx] == 'rate': - columns[col_idx+1]['label'] += " (per {})".format(include_uom) - else: - columns[col_idx+1]['label'] += " ({})".format(include_uom) + + is_dict_obj = False + if isinstance(result[0], dict): + is_dict_obj = True + + convertible_columns = {} + for idx, d in enumerate(columns): + key = d.get("fieldname") if is_dict_obj else idx + if d.get("convertible"): + convertible_columns.setdefault(key, d.get("convertible")) + + # Add new column to show qty/rate as per the selected UOM + columns.insert(idx+1, { + 'label': "{0} (per {1})".format(d.get("label"), include_uom), + 'fieldname': "{0}_{1}".format(d.get("fieldname"), frappe.scrub(include_uom)), + 'fieldtype': 'Currency' if d.get("convertible") == 'rate' else 'Float' + }) for row_idx, row in enumerate(result): - new_row = [] - for col_idx, d in enumerate(row): - new_row.append(d) - if col_idx in convertible_cols: - if conversion_factors[row_idx]: - if convertible_cols[col_idx] == 'rate': - new_row.append(flt(d) * conversion_factors[row_idx]) - else: - new_row.append(flt(d) / conversion_factors[row_idx]) - else: - new_row.append(None) + data = row.items() if is_dict_obj else enumerate(row) + for key, value in data: + if not key in convertible_columns or not conversion_factors[row_idx]: + continue - result[row_idx] = new_row + if convertible_columns.get(key) == 'rate': + new_value = flt(value) * conversion_factors[row_idx] + else: + new_value = flt(value) / conversion_factors[row_idx] + + if not is_dict_obj: + row.insert(key+1, new_value) + else: + new_key = "{0}_{1}".format(key, frappe.scrub(include_uom)) + row[new_key] = new_value def get_available_serial_nos(item_code, warehouse): return frappe.get_all("Serial No", filters = {'item_code': item_code,