From 1d93d66c30e69bfcb277123462bf822aa3c3c1d4 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 5 Sep 2023 09:59:34 +0530 Subject: [PATCH] refactor: cancel gain/loss JE on multi currency transactions --- .../unreconcile_payments/unreconcile_payments.py | 13 +++++++++---- erpnext/public/js/utils/unreconcile.js | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py index 25f85db71f..4f9fb50d46 100644 --- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py +++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py @@ -8,7 +8,11 @@ from frappe.query_builder import Criterion from frappe.query_builder.functions import Abs, Sum from frappe.utils.data import comma_and -from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries, update_voucher_outstanding +from erpnext.accounts.utils import ( + cancel_exchange_gain_loss_journal, + unlink_ref_doc_from_payment_entries, + update_voucher_outstanding, +) class UnreconcilePayments(Document): @@ -51,12 +55,11 @@ class UnreconcilePayments(Document): self.append("allocations", alloc) def on_submit(self): - # todo: add more granular unlinking - # different amounts for same invoice should be individually unlinkable - + # todo: more granular unreconciliation for alloc in self.allocations: doc = frappe.get_doc(alloc.reference_doctype, alloc.reference_name) unlink_ref_doc_from_payment_entries(doc, self.voucher_no) + cancel_exchange_gain_loss_journal(doc, self.voucher_type, self.voucher_no) update_voucher_outstanding( alloc.reference_doctype, alloc.reference_name, alloc.account, alloc.party_type, alloc.party ) @@ -104,6 +107,7 @@ def get_linked_payments_for_doc( ) .where(Criterion.all(criteria)) .groupby(ple.voucher_no, ple.against_voucher_no) + .having(qb.Field("allocated_amount") > 0) .run(as_dict=True) ) return res @@ -122,6 +126,7 @@ def get_linked_payments_for_doc( ple.against_voucher_type.as_("voucher_type"), ple.against_voucher_no.as_("voucher_no"), Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"), + ple.account_currency, ) .where(Criterion.all(criteria)) .groupby(ple.against_voucher_no) diff --git a/erpnext/public/js/utils/unreconcile.js b/erpnext/public/js/utils/unreconcile.js index cd44f3578b..acc77a64b0 100644 --- a/erpnext/public/js/utils/unreconcile.js +++ b/erpnext/public/js/utils/unreconcile.js @@ -3,6 +3,12 @@ frappe.provide('erpnext.accounts'); erpnext.accounts.unreconcile_payments = { add_unreconcile_btn(frm) { if (frm.doc.docstatus == 1) { + if(((frm.doc.doctype == "Journal Entry") && (frm.doc.voucher_type != "Journal Entry")) + || !["Purchase Invoice", "Sales Invoice", "Journal Entry", "Payment Entry"].includes(frm.doc.doctype) + ) { + return; + } + frappe.call({ "method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_references", "args": {