diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 91c9a36ee77..99dd4ae675f 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -381,7 +381,16 @@ def get_items_to_be_repost(voucher_type=None, voucher_no=None, doc=None, reposti items_to_be_repost = frappe.db.get_all( "Stock Ledger Entry", filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, - fields=["item_code", "warehouse", "posting_date", "posting_time", "creation", "posting_datetime"], + fields=[ + "item_code", + "warehouse", + # aggregate the non-grouped columns (earliest row per item+warehouse) so the GROUP BY + # is valid on Postgres; a single voucher's entries share posting_date/time per group + {"MIN": "posting_date", "as": "posting_date"}, + {"MIN": "posting_time", "as": "posting_time"}, + {"MIN": "creation", "as": "creation"}, + {"MIN": "posting_datetime", "as": "posting_datetime"}, + ], order_by="creation asc", group_by="item_code, warehouse", )