brotherton-erpnext/erpnext/patches/v14_0/single_to_multi_dunning.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.5 KiB
Python
Raw Normal View History

2021-10-07 19:05:35 +02:00
import frappe
2021-10-12 17:48:54 +02:00
2021-10-07 19:05:35 +02:00
from erpnext.accounts.general_ledger import make_reverse_gl_entries
2021-10-12 17:48:54 +02:00
2021-10-07 19:05:35 +02:00
def execute():
frappe.reload_doc("accounts", "doctype", "overdue_payment")
frappe.reload_doc("accounts", "doctype", "dunning")
2021-11-12 23:24:08 +01:00
all_dunnings = frappe.get_all("Dunning", filters={"docstatus": ("!=", 2)}, pluck="name")
2021-10-07 19:05:35 +02:00
for dunning_name in all_dunnings:
dunning = frappe.get_doc("Dunning", dunning_name)
if not dunning.sales_invoice:
# nothing we can do
continue
if dunning.overdue_payments:
# something's already here, doesn't need patching
continue
payment_schedules = frappe.get_all(
"Payment Schedule",
2021-10-07 19:05:35 +02:00
filters={"parent": dunning.sales_invoice},
fields=[
"parent as sales_invoice",
"name as payment_schedule",
"payment_term",
"due_date",
"invoice_portion",
"payment_amount",
# at the time of creating this dunning, the full amount was outstanding
"payment_amount as outstanding",
"'0' as paid_amount",
"discounted_amount",
],
2021-10-07 19:05:35 +02:00
)
dunning.extend("overdue_payments", payment_schedules)
dunning.validate()
dunning.flags.ignore_validate_update_after_submit = True
dunning.save()
if dunning.status != "Resolved":
2021-10-12 17:48:54 +02:00
# With the new logic, dunning amount gets recorded as additional income
2021-10-07 19:05:35 +02:00
# at time of payment. We don't want to record the dunning amount twice,
# so we reverse previous GL Entries that recorded the dunning amount at
# time of submission of the Dunning.
make_reverse_gl_entries(voucher_type="Dunning", voucher_no=dunning.name)