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.

(cherry picked from commit 662ccd467c)
This commit is contained in:
ruthra kumar
2023-07-22 11:18:11 +05:30
committed by Mergify
parent 3d661709fa
commit 14600fa190
3 changed files with 31 additions and 9 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
} }
} }
} }

View File

@@ -184,10 +184,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:
@@ -1510,6 +1517,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,
} }

View File

@@ -823,3 +823,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