fix: allocation logic on 'Get Outstanding Invoices' btn in PE

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.
This commit is contained in:
ruthra kumar 2023-07-22 11:18:11 +05:30
parent cc36af57bd
commit 662ccd467c
3 changed files with 32 additions and 10 deletions

View File

@ -122,13 +122,10 @@ frappe.ui.form.on('Payment Entry', {
frm.set_query('payment_term', 'references', function(frm, cdt, cdn) { frm.set_query('payment_term', 'references', function(frm, cdt, cdn) {
const child = locals[cdt][cdn]; const child = locals[cdt][cdn];
if (in_list(['Purchase Invoice', 'Sales Invoice'], child.reference_doctype) && child.reference_name) { 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 { return {
query: "erpnext.controllers.queries.get_payment_terms_for_references",
filters: { filters: {
'name': ['in', payment_term_list] 'reference': child.reference_name
} }
} }
} }
@ -1463,4 +1460,4 @@ frappe.ui.form.on('Payment Entry', {
}); });
} }
}, },
}) })

View File

@ -228,10 +228,17 @@ class PaymentEntry(AccountsController):
d = frappe._dict(d) d = frappe._dict(d)
latest_lookup.setdefault((d.voucher_type, d.voucher_no), frappe._dict())[d.payment_term] = d latest_lookup.setdefault((d.voucher_type, d.voucher_no), frappe._dict())[d.payment_term] = d
for d in self.get("references"): for idx, d in enumerate(self.get("references"), start=1):
latest = (latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()).get( latest = latest_lookup.get((d.reference_doctype, d.reference_name)) or frappe._dict()
d.payment_term
) 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 # The reference has already been fully paid
if not latest: if not latest:
@ -1633,6 +1640,9 @@ def split_invoices_based_on_payment_terms(outstanding_invoices, company):
"invoice_amount": flt(d.invoice_amount), "invoice_amount": flt(d.invoice_amount),
"outstanding_amount": flt(d.outstanding_amount), "outstanding_amount": flt(d.outstanding_amount),
"payment_term_outstanding": payment_term_outstanding, "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_amount": payment_term.payment_amount,
"payment_term": payment_term.payment_term, "payment_term": payment_term.payment_term,
"account": d.account, "account": d.account,

View File

@ -874,3 +874,18 @@ def get_fields(doctype, fields=None):
fields.insert(1, meta.title_field.strip()) fields.insert(1, meta.title_field.strip())
return unique(fields) 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