diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index ce18d005ebea7059df5c7bafe0524dd69c02e2a5..fff1f962d2179aea2a1f06e79bfdb0876b0287d5 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2808,13 +2808,20 @@ def get_advance_journal_entries( else: q = q.where(journal_acc.debit_in_account_currency > 0) + order_list_query = (journal_acc.reference_type == order_doctype) & ( + (journal_acc.reference_name).isin(order_list) + ) if include_unallocated: - q = q.where((journal_acc.reference_name.isnull()) | (journal_acc.reference_name == "")) - - if order_list: - q = q.where( - (journal_acc.reference_type == order_doctype) & ((journal_acc.reference_name).isin(order_list)) + include_unallocated_query = (journal_acc.reference_name.isnull()) | ( + journal_acc.reference_name == "" ) + if order_list: + q = q.where(include_unallocated_query | order_list_query) + else: + q = q.where(include_unallocated_query) + + elif order_list: + q = q.where(order_list_query) q = q.orderby(journal_entry.posting_date)