From 5d5484805aab9636c4c3129327c621e5dcbc3cd1 Mon Sep 17 00:00:00 2001 From: Ali <010109ali@gmail.com> Date: Mon, 3 Feb 2025 13:02:24 +0400 Subject: [PATCH] Fixed curency exchange bug in payment entry --- jey_erp/public/js/payment_entry_mod.js | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/jey_erp/public/js/payment_entry_mod.js b/jey_erp/public/js/payment_entry_mod.js index 955479f..787fb32 100644 --- a/jey_erp/public/js/payment_entry_mod.js +++ b/jey_erp/public/js/payment_entry_mod.js @@ -47,5 +47,59 @@ frappe.ui.form.on('Payment Entry', { return {}; } }); + }, + + // Переопределение received_amount + received_amount: function (frm) { + if (frm.set_paid_amount_based_on_received_amount) return; + + const company_currency = frappe.get_doc(":Company", frm.doc.company).default_currency; + const from_currency = frm.doc.paid_from_account_currency; + const to_currency = frm.doc.paid_to_account_currency; + + const from_rate = frm.doc.source_exchange_rate || 1; + const to_rate = frm.doc.target_exchange_rate || 1; + + const cross_rate = from_currency === to_currency ? 1 : from_rate / to_rate; + + frm.set_paid_amount_based_on_received_amount = true; + frm.set_value( + "paid_amount", + flt(frm.doc.received_amount / cross_rate, precision("paid_amount")) + ); + frm.set_paid_amount_based_on_received_amount = false; + + frm.set_value("base_received_amount", flt(frm.doc.received_amount * to_rate)); + frm.events.hide_unhide_fields(frm); + frm.trigger("set_difference_amount"); + }, + + // Переопределение paid_amount + paid_amount: function (frm) { + if (frm.set_paid_amount_based_on_received_amount) return; + + const company_currency = frappe.get_doc(":Company", frm.doc.company).default_currency; + const from_currency = frm.doc.paid_from_account_currency; + const to_currency = frm.doc.paid_to_account_currency; + + // Получаем курсы валют относительно базовой валюты компании + const from_rate = frm.doc.source_exchange_rate || 1; + const to_rate = frm.doc.target_exchange_rate || 1; + + // Рассчитываем кросс-курс между валютами списания и зачисления + const cross_rate = from_currency === to_currency ? 1 : from_rate / to_rate; + + // Обновляем Received Amount + frm.set_paid_amount_based_on_received_amount = true; + frm.set_value( + "received_amount", + flt(frm.doc.paid_amount * cross_rate, precision("received_amount")) + ); + frm.set_paid_amount_based_on_received_amount = false; + + // Обновляем base_paid_amount (в базовой валюте компании) + frm.set_value("base_paid_amount", flt(frm.doc.paid_amount * from_rate)); + frm.events.hide_unhide_fields(frm); + frm.trigger("set_difference_amount"); } });