Skip to content

Commit

Permalink
Merge pull request #38257 from ruthra-kumar/optimize_ple_outstanding_…
Browse files Browse the repository at this point in the history
…query

refactor: optmize outstanding amount query
  • Loading branch information
ruthra-kumar authored Nov 23, 2023
2 parents 0ca7527 + 8b04c1d commit efd31a4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions erpnext/accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,28 @@ def query_for_outstanding(self):
Table("outstanding").amount_in_account_currency >= self.max_outstanding
)

if self.limit and self.get_invoices:
outstanding_vouchers = (
qb.from_(ple)
.select(
ple.against_voucher_no.as_("voucher_no"),
Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"),
)
.where(ple.delinked == 0)
.where(Criterion.all(filter_on_against_voucher_no))
.where(Criterion.all(self.common_filter))
.groupby(ple.against_voucher_type, ple.against_voucher_no, ple.party_type, ple.party)
.orderby(ple.posting_date, ple.voucher_no)
.having(qb.Field("amount_in_account_currency") > 0)
.limit(self.limit)
.run()
)
if outstanding_vouchers:
filter_on_voucher_no.append(ple.voucher_no.isin([x[0] for x in outstanding_vouchers]))
filter_on_against_voucher_no.append(
ple.against_voucher_no.isin([x[0] for x in outstanding_vouchers])
)

# build query for voucher amount
query_voucher_amount = (
qb.from_(ple)
Expand Down

0 comments on commit efd31a4

Please sign in to comment.