fix: Updation of dunning on PE cancellation

This commit is contained in:
marination 2023-06-15 20:04:54 +05:30
parent 254bab33da
commit c32113918e
2 changed files with 21 additions and 15 deletions

View File

@ -75,16 +75,23 @@ def resolve_dunning(doc, state):
when a Payment Entry is submitted. when a Payment Entry is submitted.
""" """
for reference in doc.references: for reference in doc.references:
# Consider partial and full payments # Consider partial and full payments:
if ( # Submitting full payment: outstanding_amount will be 0
reference.reference_doctype == "Sales Invoice" # Submitting 1st partial payment: outstanding_amount will be the pending installment
and reference.outstanding_amount < reference.total_amount # Cancelling full payment: outstanding_amount will revert to total amount
): # Cancelling last partial payment: outstanding_amount will revert to pending amount
unresolved_dunnings = get_unresolved_dunnings(reference.reference_name) submit_condition = reference.outstanding_amount < reference.total_amount
cancel_condition = reference.outstanding_amount <= reference.total_amount
for dunning_name in unresolved_dunnings: if reference.reference_doctype == "Sales Invoice" and (
submit_condition if doc.docstatus == 1 else cancel_condition
):
state = "Resolved" if doc.docstatus == 2 else "Unresolved"
dunnings = get_linked_dunnings_as_per_state(reference.reference_name, state)
for dunning in dunnings:
resolve = True resolve = True
dunning = frappe.get_doc("Dunning", dunning_name) dunning = frappe.get_doc("Dunning", dunning.get("name"))
for overdue_payment in dunning.overdue_payments: for overdue_payment in dunning.overdue_payments:
outstanding_inv = frappe.get_value( outstanding_inv = frappe.get_value(
"Sales Invoice", overdue_payment.sales_invoice, "outstanding_amount" "Sales Invoice", overdue_payment.sales_invoice, "outstanding_amount"
@ -92,15 +99,13 @@ def resolve_dunning(doc, state):
outstanding_ps = frappe.get_value( outstanding_ps = frappe.get_value(
"Payment Schedule", overdue_payment.payment_schedule, "outstanding" "Payment Schedule", overdue_payment.payment_schedule, "outstanding"
) )
if outstanding_ps > 0 and outstanding_inv > 0: resolve = False if (outstanding_ps > 0 and outstanding_inv > 0) else True
resolve = False
if resolve: dunning.status = "Resolved" if resolve else "Unresolved"
dunning.status = "Resolved"
dunning.save() dunning.save()
def get_unresolved_dunnings(sales_invoice): def get_linked_dunnings_as_per_state(sales_invoice, state):
dunning = frappe.qb.DocType("Dunning") dunning = frappe.qb.DocType("Dunning")
overdue_payment = frappe.qb.DocType("Overdue Payment") overdue_payment = frappe.qb.DocType("Overdue Payment")
@ -110,7 +115,7 @@ def get_unresolved_dunnings(sales_invoice):
.on(overdue_payment.parent == dunning.name) .on(overdue_payment.parent == dunning.name)
.select(dunning.name) .select(dunning.name)
.where( .where(
(dunning.status != "Resolved") (dunning.status == state)
& (dunning.docstatus != 2) & (dunning.docstatus != 2)
& (overdue_payment.sales_invoice == sales_invoice) & (overdue_payment.sales_invoice == sales_invoice)
) )

View File

@ -334,6 +334,7 @@ doc_events = {
"erpnext.accounts.doctype.payment_request.payment_request.update_payment_req_status", "erpnext.accounts.doctype.payment_request.payment_request.update_payment_req_status",
"erpnext.accounts.doctype.dunning.dunning.resolve_dunning", "erpnext.accounts.doctype.dunning.dunning.resolve_dunning",
], ],
"on_cancel": ["erpnext.accounts.doctype.dunning.dunning.resolve_dunning"],
"on_trash": "erpnext.regional.check_deletion_permission", "on_trash": "erpnext.regional.check_deletion_permission",
}, },
"Address": { "Address": {