fix: advance paid amount and ledger entries against SO/PO
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 1a89c45..7970a3e 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -1299,7 +1299,7 @@
if getdate(posting_date) < getdate(self.posting_date):
posting_date = self.posting_date
- dr_or_cr = "credit" if invoice.reference_doctype == "Sales Invoice" else "debit"
+ dr_or_cr = "credit" if invoice.reference_doctype in ["Sales Invoice", "Sales Order"] else "debit"
args_dict["account"] = invoice.account
args_dict[dr_or_cr] = invoice.allocated_amount
args_dict[dr_or_cr + "_in_account_currency"] = invoice.allocated_amount
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index b436d02..0755f2e 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -490,7 +490,9 @@
# For payments with `Advance` in separate account feature enabled, only new ledger entries are posted for each reference.
# No need to cancel/delete payment ledger entries
- if not (voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account):
+ if voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account:
+ doc.make_advance_gl_entries(cancel=1)
+ else:
_delete_pl_entries(voucher_type, voucher_no)
for entry in entries:
@@ -501,14 +503,16 @@
# update ref in advance entry
if voucher_type == "Journal Entry":
- referenced_row = update_reference_in_journal_entry(entry, doc, do_not_save=False)
+ referenced_row, update_advance_paid = update_reference_in_journal_entry(
+ entry, doc, do_not_save=False
+ )
# advance section in sales/purchase invoice and reconciliation tool,both pass on exchange gain/loss
# amount and account in args
# referenced_row is used to deduplicate gain/loss journal
entry.update({"referenced_row": referenced_row})
doc.make_exchange_gain_loss_journal([entry], dimensions_dict)
else:
- referenced_row = update_reference_in_payment_entry(
+ referenced_row, update_advance_paid = update_reference_in_payment_entry(
entry,
doc,
do_not_save=True,
@@ -522,7 +526,7 @@
if voucher_type == "Payment Entry" and doc.book_advance_payments_in_separate_party_account:
# both ledgers must be posted to for `Advance` in separate account feature
- doc.make_advance_gl_entries(cancel=1)
+ # TODO: find a more efficient way post only for the new linked vouchers
doc.make_advance_gl_entries()
else:
gl_map = doc.build_gl_map()
@@ -533,6 +537,10 @@
update_voucher_outstanding(
entry.against_voucher_type, entry.against_voucher, entry.account, entry.party_type, entry.party
)
+ # update advance paid in Advance Receivable/Payable doctypes
+ if update_advance_paid:
+ for t, n in update_advance_paid:
+ frappe.get_doc(t, n).set_total_advance_paid()
frappe.flags.ignore_party_validation = False
@@ -622,11 +630,12 @@
jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0]
# Update Advance Paid in SO/PO since they might be getting unlinked
+ update_advance_paid = []
advance_payment_doctypes = frappe.get_hooks(
"advance_payment_receivable_doctypes"
) + frappe.get_hooks("advance_payment_payable_doctypes")
if jv_detail.get("reference_type") in advance_payment_doctypes:
- frappe.get_doc(jv_detail.reference_type, jv_detail.reference_name).set_total_advance_paid()
+ update_advance_paid.append((jv_detail.reference_type, jv_detail.reference_name))
if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0:
# adjust the unreconciled balance
@@ -675,7 +684,7 @@
if not do_not_save:
journal_entry.save(ignore_permissions=True)
- return new_row.name
+ return new_row.name, update_advance_paid
def update_reference_in_payment_entry(
@@ -694,6 +703,7 @@
"account": d.account,
"dimensions": d.dimensions,
}
+ update_advance_paid = []
if d.voucher_detail_no:
existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0]
@@ -703,9 +713,7 @@
"advance_payment_receivable_doctypes"
) + frappe.get_hooks("advance_payment_payable_doctypes")
if existing_row.get("reference_doctype") in advance_payment_doctypes:
- frappe.get_doc(
- existing_row.reference_doctype, existing_row.reference_name
- ).set_total_advance_paid()
+ update_advance_paid.append((existing_row.reference_doctype, existing_row.reference_name))
if d.allocated_amount <= existing_row.allocated_amount:
existing_row.allocated_amount -= d.allocated_amount
@@ -735,7 +743,7 @@
if not do_not_save:
payment_entry.save(ignore_permissions=True)
- return row
+ return row, update_advance_paid
def cancel_exchange_gain_loss_journal(
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index a3db196..aa3d3e0 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -1864,7 +1864,7 @@
(ple.against_voucher_type == self.doctype)
& (ple.against_voucher_no == self.name)
& (ple.party == party)
- & (ple.docstatus == 1)
+ & (ple.delinked == 0)
& (ple.company == self.company)
)
.run(as_dict=True)