Skip to content

Commit

Permalink
Merge pull request #583 from 8848digital/purchase_order_test_case_issues
Browse files Browse the repository at this point in the history
fix: Advance Payment Issue Fix
  • Loading branch information
tinadn authored Nov 8, 2024
2 parents bad838a + 13a6c30 commit dd14f05
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,21 +1911,22 @@ def get_stock_items(self):
return stock_items

def set_total_advance_paid(self):
ple = frappe.qb.DocType("Payment Ledger Entry")
party = self.customer if self.doctype == "Sales Order" else self.supplier
advance = (
frappe.qb.from_(ple)
.select(ple.account_currency, Abs(Sum(ple.amount_in_account_currency)).as_("amount"))
.where(
(ple.against_voucher_type == self.doctype)
& (ple.against_voucher_no == self.name)
& (ple.party == party)
& (ple.delinked == 0)
& (ple.company == self.company)
)
.groupby(ple.account_currency)
.run(as_dict=True)
)

advance =frappe.db.sql(
f"""SELECT
(ARRAY_AGG(ple.account_currency))[1] as account_currency,
ABS(SUM(ple.amount_in_account_currency)) AS amount
FROM
"tabPayment Ledger Entry" as ple
WHERE
ple.against_voucher_type = '{self.doctype}'
AND ple.against_voucher_no = '{ self.name}'
AND ple.party = '{party}'
AND ple.delinked = 0
AND ple.company = '{self.company}'
"""
,as_dict=True)

if advance:
advance = advance[0]
Expand All @@ -1946,6 +1947,7 @@ def set_total_advance_paid(self):
else:
order_total = self.get("base_rounded_total") or self.base_grand_total
precision = "base_rounded_total" if self.get("base_rounded_total") else "base_grand_total"


formatted_order_total = fmt_money(
order_total, precision=self.precision(precision), currency=advance.account_currency
Expand All @@ -1958,7 +1960,7 @@ def set_total_advance_paid(self):
).format(formatted_advance_paid, self.name, formatted_order_total)
)

frappe.db.set_value(self.doctype, self.name, "advance_paid", advance_paid)
self.db_set("advance_paid", advance_paid)

@property
def company_abbr(self):
Expand Down

0 comments on commit dd14f05

Please sign in to comment.