refactor: cancel gain/loss JE on multi currency transactions

This commit is contained in:
ruthra kumar 2023-09-05 09:59:34 +05:30
parent 5dbcf7d2b9
commit 1d93d66c30
2 changed files with 15 additions and 4 deletions

View File

@ -8,7 +8,11 @@ from frappe.query_builder import Criterion
from frappe.query_builder.functions import Abs, Sum from frappe.query_builder.functions import Abs, Sum
from frappe.utils.data import comma_and 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): class UnreconcilePayments(Document):
@ -51,12 +55,11 @@ class UnreconcilePayments(Document):
self.append("allocations", alloc) self.append("allocations", alloc)
def on_submit(self): def on_submit(self):
# todo: add more granular unlinking # todo: more granular unreconciliation
# different amounts for same invoice should be individually unlinkable
for alloc in self.allocations: for alloc in self.allocations:
doc = frappe.get_doc(alloc.reference_doctype, alloc.reference_name) doc = frappe.get_doc(alloc.reference_doctype, alloc.reference_name)
unlink_ref_doc_from_payment_entries(doc, self.voucher_no) 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( update_voucher_outstanding(
alloc.reference_doctype, alloc.reference_name, alloc.account, alloc.party_type, alloc.party 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)) .where(Criterion.all(criteria))
.groupby(ple.voucher_no, ple.against_voucher_no) .groupby(ple.voucher_no, ple.against_voucher_no)
.having(qb.Field("allocated_amount") > 0)
.run(as_dict=True) .run(as_dict=True)
) )
return res return res
@ -122,6 +126,7 @@ def get_linked_payments_for_doc(
ple.against_voucher_type.as_("voucher_type"), ple.against_voucher_type.as_("voucher_type"),
ple.against_voucher_no.as_("voucher_no"), ple.against_voucher_no.as_("voucher_no"),
Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"), Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"),
ple.account_currency,
) )
.where(Criterion.all(criteria)) .where(Criterion.all(criteria))
.groupby(ple.against_voucher_no) .groupby(ple.against_voucher_no)

View File

@ -3,6 +3,12 @@ frappe.provide('erpnext.accounts');
erpnext.accounts.unreconcile_payments = { erpnext.accounts.unreconcile_payments = {
add_unreconcile_btn(frm) { add_unreconcile_btn(frm) {
if (frm.doc.docstatus == 1) { 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({ frappe.call({
"method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_references", "method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_references",
"args": { "args": {