diff --git a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py index 4213d478ce1..0ed30eaee52 100644 --- a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py +++ b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py @@ -601,6 +601,7 @@ def calculate_exchange_rate_using_last_gle(company, account, party_type, party): .select(gl.voucher_type, gl.voucher_no) .where(Criterion.all(conditions)) .orderby(gl.posting_date, order=Order.desc) + .orderby(gl.name, order=Order.desc) .limit(1) .run()[0] ) @@ -615,6 +616,7 @@ def calculate_exchange_rate_using_last_gle(company, account, party_type, party): (gl.voucher_type == voucher_type) & (gl.voucher_no == voucher_no) & (gl.account == account) ) .orderby(gl.posting_date, order=Order.desc) + .orderby(gl.name, order=Order.desc) .limit(1) .run()[0][0] ) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 8785144a8da..61968d603ad 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -895,7 +895,11 @@ class GrossProfitGenerator: if row.cost_center: query = query.where(purchase_invoice_item.cost_center == row.cost_center) - query = query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc).limit(1) + query = ( + query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc) + .orderby(purchase_invoice.name, order=frappe.qb.desc) + .limit(1) + ) last_purchase_rate = query.run() return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0 diff --git a/erpnext/assets/doctype/asset_movement/asset_movement.py b/erpnext/assets/doctype/asset_movement/asset_movement.py index 674be5c65b3..74ac5e55ee3 100644 --- a/erpnext/assets/doctype/asset_movement/asset_movement.py +++ b/erpnext/assets/doctype/asset_movement/asset_movement.py @@ -139,6 +139,7 @@ class AssetMovement(Document): .select(asm_item.target_location, asm_item.to_employee) .where((asm_item.asset == asset) & (asm.company == self.company) & (asm.docstatus == 1)) .orderby(asm.transaction_date, order=frappe.qb.desc) + .orderby(asm.name, order=frappe.qb.desc) .limit(1) .run() ) diff --git a/erpnext/selling/report/inactive_customers/inactive_customers.py b/erpnext/selling/report/inactive_customers/inactive_customers.py index 2dedb346601..1710566b92a 100644 --- a/erpnext/selling/report/inactive_customers/inactive_customers.py +++ b/erpnext/selling/report/inactive_customers/inactive_customers.py @@ -86,6 +86,7 @@ def get_last_sales_amt(customer, doctype): .select(sales_doctype.base_net_total) .where((sales_doctype.customer == customer) & (sales_doctype.docstatus == 1)) .orderby(date_col, order=frappe.qb.desc) + .orderby(sales_doctype.name, order=frappe.qb.desc) .limit(1) ).run()