Skip to content

Commit

Permalink
fix: allocation logic on 'Get Outstanding Invoices' btn in PE
Browse files Browse the repository at this point in the history
1. fixed broken `payment_term` filter in Payment References section
2. Throw error if user fails to select 'Payment Term' for an invoice
with 'Payment Term based allocation' enabled.

(cherry picked from commit 662ccd4)
  • Loading branch information
ruthra-kumar authored and mergify[bot] committed Jul 24, 2023
1 parent 3d66170 commit 14600fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
7 changes: 2 additions & 5 deletions erpnext/accounts/doctype/payment_entry/payment_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ frappe.ui.form.on('Payment Entry', {
frm.set_query('payment_term', 'references', function(frm, cdt, cdn) {
const child = locals[cdt][cdn];
if (in_list(['Purchase Invoice', 'Sales Invoice'], child.reference_doctype) && child.reference_name) {
let payment_term_list = frappe.get_list('Payment Schedule', {'parent': child.reference_name});

payment_term_list = payment_term_list.map(pt => pt.payment_term);

return {
query: "erpnext.controllers.queries.get_payment_terms_for_references",
filters: {
'name': ['in', payment_term_list]
'reference': child.reference_name
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions erpnext/accounts/doctype/payment_entry/payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,17 @@ def validate_allocated_amount_with_latest_data(self):
d = frappe._dict(d)
latest_lookup.setdefault((d.voucher_type, d.voucher_no), frappe._dict())[d.payment_term] = d

for d in self.get("references"):
latest = (latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()).get(
d.payment_term
)
for idx, d in enumerate(self.get("references"), start=1):
latest = latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()

if (d.payment_term is None or d.payment_term == "") and d.payment_term not in latest.keys():
frappe.throw(
_(
"{0} has Payment Term based allocation enabled. Select a Payment Term for Row #{1} in Payment References section"
).format(frappe.bold(d.reference_name), frappe.bold(idx))
)

latest = latest.get(d.payment_term)

# The reference has already been fully paid
if not latest:
Expand Down Expand Up @@ -1510,6 +1517,9 @@ def split_invoices_based_on_payment_terms(outstanding_invoices, company):
"invoice_amount": flt(d.invoice_amount),
"outstanding_amount": flt(d.outstanding_amount),
"payment_term_outstanding": payment_term_outstanding,
"allocated_amount": payment_term_outstanding
if payment_term_outstanding
else d.outstanding_amount,
"payment_amount": payment_term.payment_amount,
"payment_term": payment_term.payment_term,
}
Expand Down
15 changes: 15 additions & 0 deletions erpnext/controllers/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,18 @@ def get_fields(doctype, fields=None):
fields.insert(1, meta.title_field.strip())

return unique(fields)


@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def get_payment_terms_for_references(doctype, txt, searchfield, start, page_len, filters) -> list:
terms = []
if filters:
terms = frappe.db.get_all(
"Payment Schedule",
filters={"parent": filters.get("reference")},
fields=["payment_term"],
limit=page_len,
as_list=1,
)
return terms

0 comments on commit 14600fa

Please sign in to comment.