From b115bf2e2a6a510f761f0a73124bfa915cdfca46 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 21 Feb 2025 15:40:35 +0530 Subject: [PATCH 01/19] refactor: use highest precision for storing exc rate --- erpnext/accounts/doctype/gl_entry/gl_entry.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.json b/erpnext/accounts/doctype/gl_entry/gl_entry.json index c285a33f73e..b438dbbe4ec 100644 --- a/erpnext/accounts/doctype/gl_entry/gl_entry.json +++ b/erpnext/accounts/doctype/gl_entry/gl_entry.json @@ -279,7 +279,8 @@ { "fieldname": "transaction_exchange_rate", "fieldtype": "Float", - "label": "Transaction Exchange Rate" + "label": "Transaction Exchange Rate", + "precision": "9" }, { "fieldname": "debit_in_transaction_currency", @@ -357,7 +358,7 @@ "idx": 1, "in_create": 1, "links": [], - "modified": "2024-08-22 13:03:39.997475", + "modified": "2025-02-21 14:36:49.431166", "modified_by": "Administrator", "module": "Accounts", "name": "GL Entry", From e9af5670334ca8fa45cf42414989abca09cdb45a Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 21 Feb 2025 15:42:01 +0530 Subject: [PATCH 02/19] refactor: set tr currency dr & cr directly on parent document --- .../purchase_invoice/purchase_invoice.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index d996e319a06..551c4350db2 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -873,8 +873,14 @@ class PurchaseInvoice(BuyingController): self.make_payment_gl_entries(gl_entries) self.make_write_off_gl_entry(gl_entries) self.make_gle_for_rounding_adjustment(gl_entries) + self.set_transaction_currency_rate(gl_entries) return gl_entries + def set_transaction_currency_rate(self, gl_entries): + for x in gl_entries: + x["transaction_currency"] = self.currency + x["transaction_exchange_rate"] = self.get("conversion_rate") or 1 + def check_asset_cwip_enabled(self): # Check if there exists any item with cwip accounting enabled in it's asset category for item in self.get("items"): @@ -918,6 +924,7 @@ class PurchaseInvoice(BuyingController): "credit_in_account_currency": base_grand_total if self.party_account_currency == self.company_currency else grand_total, + "credit_in_transaction_currency": grand_total, "against_voucher": against_voucher, "against_voucher_type": self.doctype, "project": self.project, @@ -1056,7 +1063,9 @@ class PurchaseInvoice(BuyingController): # Amount added through landed-cost-voucher if landed_cost_entries: if (item.item_code, item.name) in landed_cost_entries: - for account, amount in landed_cost_entries[(item.item_code, item.name)].items(): + for account, base_amount in landed_cost_entries[ + (item.item_code, item.name) + ].items(): gl_entries.append( self.get_gl_dict( { @@ -1064,8 +1073,8 @@ class PurchaseInvoice(BuyingController): "against": item.expense_account, "cost_center": item.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), - "credit": flt(amount["base_amount"]), - "credit_in_account_currency": flt(amount["amount"]), + "credit": flt(base_amount["base_amount"]), + "credit_in_account_currency": flt(base_amount["amount"]), "project": item.project or self.project, }, item=item, @@ -1101,7 +1110,7 @@ class PurchaseInvoice(BuyingController): else item.deferred_expense_account ) - dummy, amount = self.get_amount_and_base_amount(item, None) + amount, base_amount = self.get_amount_and_base_amount(item, None) if provisional_accounting_for_non_stock_items: self.make_provisional_gl_entry(gl_entries, item) @@ -1112,7 +1121,8 @@ class PurchaseInvoice(BuyingController): { "account": expense_account, "against": self.supplier, - "debit": amount, + "debit": base_amount, + "debit_in_transaction_currency": amount, "cost_center": item.cost_center, "project": item.project or self.project, }, @@ -1338,6 +1348,7 @@ class PurchaseInvoice(BuyingController): dr_or_cr + "_in_account_currency": base_amount if account_currency == self.company_currency else amount, + dr_or_cr + "_in_transaction_currency": amount, "cost_center": tax.cost_center, }, account_currency, @@ -1466,6 +1477,7 @@ class PurchaseInvoice(BuyingController): "debit_in_account_currency": self.base_paid_amount if self.party_account_currency == self.company_currency else self.paid_amount, + "debit_in_transaction_currency": self.paid_amount, "against_voucher": self.return_against if cint(self.is_return) and self.return_against else self.name, @@ -1487,6 +1499,7 @@ class PurchaseInvoice(BuyingController): "credit_in_account_currency": self.base_paid_amount if bank_account_currency == self.company_currency else self.paid_amount, + "credit_in_transaction_currency": self.paid_amount, "cost_center": self.cost_center, }, bank_account_currency, @@ -1511,6 +1524,7 @@ class PurchaseInvoice(BuyingController): "debit_in_account_currency": self.base_write_off_amount if self.party_account_currency == self.company_currency else self.write_off_amount, + "debit_in_transaction_currency": self.write_off_amount, "against_voucher": self.return_against if cint(self.is_return) and self.return_against else self.name, @@ -1531,6 +1545,7 @@ class PurchaseInvoice(BuyingController): "credit_in_account_currency": self.base_write_off_amount if write_off_account_currency == self.company_currency else self.write_off_amount, + "credit_in_transaction_currency": self.write_off_amount, "cost_center": self.cost_center or self.write_off_cost_center, }, item=self, From 7ff3977394c8f2935601a3a753778bd6dd5a5592 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 24 Feb 2025 21:58:59 +0530 Subject: [PATCH 03/19] refactor: handle stocked items --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 551c4350db2..652d45f3ebb 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1051,6 +1051,7 @@ class PurchaseInvoice(BuyingController): "account": item.expense_account, "against": self.supplier, "debit": warehouse_debit_amount, + "debit_in_transaction_currency": item.net_amount, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "cost_center": item.cost_center, "project": item.project or self.project, From ee93ed8c970deff88ef54fdcda2c5607959d29d8 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 25 Feb 2025 14:10:00 +0530 Subject: [PATCH 04/19] refactor: handle stocked items --- .../accounts/doctype/purchase_invoice/purchase_invoice.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 652d45f3ebb..7abf810ab36 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1000,6 +1000,7 @@ class PurchaseInvoice(BuyingController): "project": item.project or self.project, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "debit": warehouse_debit_amount, + "debit_in_transaction_currency": item.net_amount, }, warehouse_account[item.warehouse]["account_currency"], item=item, @@ -1020,6 +1021,7 @@ class PurchaseInvoice(BuyingController): "project": item.project or self.project, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "debit": -1 * flt(credit_amount, item.precision("base_net_amount")), + "debit_in_transaction_currency": item.net_amount, }, warehouse_account[item.from_warehouse]["account_currency"], item=item, @@ -1034,6 +1036,7 @@ class PurchaseInvoice(BuyingController): "account": item.expense_account, "against": self.supplier, "debit": flt(item.base_net_amount, item.precision("base_net_amount")), + "debit_in_transaction_currency": item.net_amount, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "cost_center": item.cost_center, "project": item.project, @@ -1076,6 +1079,7 @@ class PurchaseInvoice(BuyingController): "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(base_amount["base_amount"]), "credit_in_account_currency": flt(base_amount["amount"]), + "credit_in_transaction_currency": item.net_amount, "project": item.project or self.project, }, item=item, @@ -1098,6 +1102,7 @@ class PurchaseInvoice(BuyingController): "project": item.project or self.project, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(item.rm_supp_cost), + "credit_in_transaction_currency": item.net_amount, }, warehouse_account[self.supplier_warehouse]["account_currency"], item=item, @@ -1197,6 +1202,7 @@ class PurchaseInvoice(BuyingController): "account": self.stock_received_but_not_billed, "against": self.supplier, "debit": flt(item.item_tax_amount, item.precision("item_tax_amount")), + "debit_in_transaction_currency": item.net_amount, "remarks": self.remarks or _("Accounting Entry for Stock"), "cost_center": self.cost_center, "project": item.project or self.project, @@ -1316,6 +1322,7 @@ class PurchaseInvoice(BuyingController): "account": cost_of_goods_sold_account, "against": item.expense_account, "debit": stock_adjustment_amt, + "debit_in_transaction_currency": item.net_amount, "remarks": self.get("remarks") or _("Stock Adjustment"), "cost_center": item.cost_center, "project": item.project or self.project, From 3e292ef2cbed31afed297e005e04f021944542fd Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 28 Feb 2025 17:26:19 +0530 Subject: [PATCH 05/19] refactor: set transaction currency dr/cr in sales invoice --- .../doctype/sales_invoice/sales_invoice.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index e326a165d31..b7e6ead7653 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1271,6 +1271,7 @@ class SalesInvoice(SellingController): "debit_in_account_currency": base_grand_total if self.party_account_currency == self.company_currency else grand_total, + "debit_in_transaction_currency": grand_total, "against_voucher": against_voucher, "against_voucher_type": self.doctype, "cost_center": self.cost_center, @@ -1302,6 +1303,9 @@ class SalesInvoice(SellingController): if account_currency == self.company_currency else flt(amount, tax.precision("tax_amount_after_discount_amount")) ), + "credit_in_transaction_currency": flt( + amount, tax.precision("tax_amount_after_discount_amount") + ), "cost_center": tax.cost_center, }, account_currency, @@ -1319,6 +1323,7 @@ class SalesInvoice(SellingController): "against": self.customer, "debit": flt(self.total_taxes_and_charges), "debit_in_account_currency": flt(self.base_total_taxes_and_charges), + "debit_in_transaction_currency": flt(self.total_taxes_and_charges), "cost_center": self.cost_center, }, account_currency, @@ -1417,6 +1422,7 @@ class SalesInvoice(SellingController): if account_currency == self.company_currency else flt(amount, item.precision("net_amount")) ), + "credit_in_transaction_currency": flt(amount, item.precision("net_amount")), "cost_center": item.cost_center, "project": item.project or self.project, }, @@ -1468,6 +1474,7 @@ class SalesInvoice(SellingController): + cstr(self.loyalty_redemption_account) + " for the Loyalty Program", "credit": self.loyalty_amount, + "credit_in_transaction_currency": self.loyalty_amount, "against_voucher": self.return_against if cint(self.is_return) else self.name, "against_voucher_type": self.doctype, "cost_center": self.cost_center, @@ -1482,6 +1489,7 @@ class SalesInvoice(SellingController): "cost_center": self.cost_center or self.loyalty_redemption_cost_center, "against": self.customer, "debit": self.loyalty_amount, + "debit_in_transaction_currency": self.loyalty_amount, "remark": "Loyalty Points redeemed by the customer", }, item=self, @@ -1515,6 +1523,7 @@ class SalesInvoice(SellingController): "credit_in_account_currency": payment_mode.base_amount if self.party_account_currency == self.company_currency else payment_mode.amount, + "credit_in_transaction_currency": payment_mode.amount, "against_voucher": against_voucher, "against_voucher_type": self.doctype, "cost_center": self.cost_center, @@ -1534,6 +1543,7 @@ class SalesInvoice(SellingController): "debit_in_account_currency": payment_mode.base_amount if payment_mode_account_currency == self.company_currency else payment_mode.amount, + "debit_in_transaction_currency": payment_mode.amount, "cost_center": self.cost_center, }, payment_mode_account_currency, @@ -1562,6 +1572,7 @@ class SalesInvoice(SellingController): "debit_in_account_currency": flt(self.base_change_amount) if self.party_account_currency == self.company_currency else flt(self.change_amount), + "debit_in_transaction_currency": flt(self.change_amount), "against_voucher": self.return_against if cint(self.is_return) and self.return_against else self.name, @@ -1577,6 +1588,7 @@ class SalesInvoice(SellingController): "account": self.account_for_change_amount, "against": self.customer, "credit": self.base_change_amount, + "credit_in_transaction_currency": self.change_amount, "cost_center": self.cost_center, }, item=self, @@ -1606,6 +1618,9 @@ class SalesInvoice(SellingController): if self.party_account_currency == self.company_currency else flt(self.write_off_amount, self.precision("write_off_amount")) ), + "credit_in_transaction_currency": flt( + self.write_off_amount, self.precision("write_off_amount") + ), "against_voucher": self.return_against if cint(self.is_return) else self.name, "against_voucher_type": self.doctype, "cost_center": self.cost_center, @@ -1626,6 +1641,9 @@ class SalesInvoice(SellingController): if write_off_account_currency == self.company_currency else flt(self.write_off_amount, self.precision("write_off_amount")) ), + "debit_in_transaction_currency": flt( + self.write_off_amount, self.precision("write_off_amount") + ), "cost_center": self.cost_center or self.write_off_cost_center or default_cost_center, }, write_off_account_currency, @@ -1670,6 +1688,9 @@ class SalesInvoice(SellingController): "credit_in_account_currency": flt( self.rounding_adjustment, self.precision("rounding_adjustment") ), + "credit_in_transaction_currency": flt( + self.rounding_adjustment, self.precision("rounding_adjustment") + ), "credit": flt( self.base_rounding_adjustment, self.precision("base_rounding_adjustment") ), From d1d06885dc647d0ca964850019d32d3091d1de46 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 28 Feb 2025 17:30:25 +0530 Subject: [PATCH 06/19] refactor: move utility method to controller --- .../accounts/doctype/purchase_invoice/purchase_invoice.py | 7 +------ erpnext/accounts/doctype/sales_invoice/sales_invoice.py | 1 + erpnext/controllers/accounts_controller.py | 5 +++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 7abf810ab36..f5ed57b8e24 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -873,14 +873,9 @@ class PurchaseInvoice(BuyingController): self.make_payment_gl_entries(gl_entries) self.make_write_off_gl_entry(gl_entries) self.make_gle_for_rounding_adjustment(gl_entries) - self.set_transaction_currency_rate(gl_entries) + self.set_transaction_currency_and_rate_in_gl_map(gl_entries) return gl_entries - def set_transaction_currency_rate(self, gl_entries): - for x in gl_entries: - x["transaction_currency"] = self.currency - x["transaction_exchange_rate"] = self.get("conversion_rate") or 1 - def check_asset_cwip_enabled(self): # Check if there exists any item with cwip accounting enabled in it's asset category for item in self.get("items"): diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index b7e6ead7653..dd0d52f4fa2 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1238,6 +1238,7 @@ class SalesInvoice(SellingController): self.make_write_off_gl_entry(gl_entries) self.make_gle_for_rounding_adjustment(gl_entries) + self.set_transaction_currency_and_rate_in_gl_map(gl_entries) return gl_entries def make_customer_gl_entry(self, gl_entries): diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 130475b98ee..2a6824e5f19 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -2782,6 +2782,11 @@ class AccountsController(TransactionBase): elif self.doctype == "Payment Entry": self.make_advance_payment_ledger_for_payment() + def set_transaction_currency_and_rate_in_gl_map(self, gl_entries): + for x in gl_entries: + x["transaction_currency"] = self.currency + x["transaction_exchange_rate"] = self.get("conversion_rate") or 1 + @frappe.whitelist() def get_tax_rate(account_head): From 9f3847c0f8b985eb21d29bb4687c1762f467e2ba Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 3 Mar 2025 12:22:35 +0530 Subject: [PATCH 07/19] refactor: handle Journal entries --- .../doctype/journal_entry/journal_entry.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 4c59a58525d..a7a526c46b0 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -1063,14 +1063,15 @@ class JournalEntry(AccountsController): gl_map = [] company_currency = erpnext.get_company_currency(self.company) + self.transaction_currency = company_currency + self.transaction_exchange_rate = 1 if self.multi_currency: for row in self.get("accounts"): if row.account_currency != company_currency: - self.currency = row.account_currency - self.conversion_rate = row.exchange_rate + # Journal assumes the first foregin currency as transaction currency + self.transaction_currency = row.account_currency + self.transaction_exchange_rate = row.exchange_rate break - else: - self.currency = company_currency for d in self.get("accounts"): if d.debit or d.credit or (self.voucher_type == "Exchange Gain Or Loss"): @@ -1095,6 +1096,18 @@ class JournalEntry(AccountsController): "credit_in_account_currency": flt( d.credit_in_account_currency, d.precision("credit_in_account_currency") ), + "transaction_currency": self.transaction_currency, + "transaction_exchange_rate": self.transaction_exchange_rate, + "debit_in_transaction_currency": flt( + d.debit_in_account_currency, d.precision("debit_in_account_currency") + ) + if self.transaction_currency == d.account_currency + else flt(d.debit, d.precision("debit")) / self.transaction_exchange_rate, + "credit_in_transaction_currency": flt( + d.credit_in_account_currency, d.precision("credit_in_account_currency") + ) + if self.transaction_currency == d.account_currency + else flt(d.credit, d.precision("credit")) / self.transaction_exchange_rate, "against_voucher_type": d.reference_type, "against_voucher": d.reference_name, "remarks": remarks, From 5c86e3ce859d9f8baa93bc709fd75abab7849700 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 4 Mar 2025 14:19:38 +0530 Subject: [PATCH 08/19] refactor: handle payment entry --- .../doctype/payment_entry/payment_entry.py | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 46d520e7e34..cb1f6281e94 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1316,10 +1316,15 @@ class PaymentEntry(AccountsController): self.setup_party_account_field() company_currency = erpnext.get_company_currency(self.company) + self.transaction_currency = company_currency + self.transaction_exchange_rate = 1 + if self.paid_from_account_currency != company_currency: - self.currency = self.paid_from_account_currency + self.transaction_currency = self.paid_from_account_currency + self.transaction_exchange_rate = self.source_exchange_rate elif self.paid_to_account_currency != company_currency: - self.currency = self.paid_to_account_currency + self.transaction_currency = self.paid_to_account_currency + self.transaction_exchange_rate = self.target_exchange_rate gl_entries = [] self.add_party_gl_entries(gl_entries) @@ -1400,6 +1405,9 @@ class PaymentEntry(AccountsController): "cost_center": cost_center, dr_or_cr + "_in_account_currency": d.allocated_amount, dr_or_cr: allocated_amount_in_company_currency, + dr_or_cr + "_in_transaction_currency": d.allocated_amount + if self.transaction_currency == self.party_account_currency + else allocated_amount_in_company_currency / self.transaction_exchange_rate, }, item=self, ) @@ -1444,6 +1452,9 @@ class PaymentEntry(AccountsController): "account_currency": self.party_account_currency, "cost_center": self.cost_center, dr_or_cr + "_in_account_currency": self.unallocated_amount, + dr_or_cr + "_in_transaction_currency": self.unallocated_amount + if self.party_account_currency == self.transaction_currency + else base_unallocated_amount / self.transaction_exchange_rate, dr_or_cr: base_unallocated_amount, }, item=self, @@ -1540,9 +1551,16 @@ class PaymentEntry(AccountsController): frappe.db.set_value("Payment Entry Reference", invoice.name, "reconcile_effect_on", posting_date) dr_or_cr, account = self.get_dr_and_account_for_advances(invoice) + base_allocated_amount = self.calculate_base_allocated_amount_for_reference(invoice) args_dict["account"] = account - args_dict[dr_or_cr] = self.calculate_base_allocated_amount_for_reference(invoice) + args_dict[dr_or_cr] = base_allocated_amount args_dict[dr_or_cr + "_in_account_currency"] = invoice.allocated_amount + args_dict[dr_or_cr + "_in_transaction_currency"] = ( + invoice.allocated_amount + if self.party_account_currency == self.transaction_currency + else base_allocated_amount / self.transaction_exchange_rate + ) + args_dict.update( { "against_voucher_type": invoice.reference_doctype, @@ -1560,8 +1578,13 @@ class PaymentEntry(AccountsController): args_dict[dr_or_cr + "_in_account_currency"] = 0 dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" args_dict["account"] = self.party_account - args_dict[dr_or_cr] = self.calculate_base_allocated_amount_for_reference(invoice) + args_dict[dr_or_cr] = base_allocated_amount args_dict[dr_or_cr + "_in_account_currency"] = invoice.allocated_amount + args_dict[dr_or_cr + "_in_transaction_currency"] = ( + invoice.allocated_amount + if self.party_account_currency == self.transaction_currency + else base_allocated_amount / self.transaction_exchange_rate + ) args_dict.update( { "against_voucher_type": "Payment Entry", @@ -1583,6 +1606,9 @@ class PaymentEntry(AccountsController): "account_currency": self.paid_from_account_currency, "against": self.party if self.payment_type == "Pay" else self.paid_to, "credit_in_account_currency": self.paid_amount, + "credit_in_transaction_currency": self.paid_amount + if self.paid_from_account_currency == self.transaction_currency + else self.base_paid_amount / self.transaction_exchange_rate, "credit": self.base_paid_amount, "cost_center": self.cost_center, "post_net_value": True, @@ -1598,6 +1624,9 @@ class PaymentEntry(AccountsController): "account_currency": self.paid_to_account_currency, "against": self.party if self.payment_type == "Receive" else self.paid_from, "debit_in_account_currency": self.received_amount, + "debit_in_transaction_currency": self.received_amount + if self.paid_to_account_currency == self.transaction_currency + else self.base_received_amount / self.transaction_exchange_rate, "debit": self.base_received_amount, "cost_center": self.cost_center, }, @@ -1633,6 +1662,8 @@ class PaymentEntry(AccountsController): dr_or_cr + "_in_account_currency": base_tax_amount if account_currency == self.company_currency else d.tax_amount, + dr_or_cr + "_in_transaction_currency": base_tax_amount + / self.transaction_exchange_rate, "cost_center": d.cost_center, "post_net_value": True, }, @@ -1658,6 +1689,8 @@ class PaymentEntry(AccountsController): rev_dr_or_cr + "_in_account_currency": base_tax_amount if account_currency == self.company_currency else d.tax_amount, + rev_dr_or_cr + "_in_transaction_currency": base_tax_amount + / self.transaction_exchange_rate, "cost_center": self.cost_center, "post_net_value": True, }, @@ -1680,6 +1713,7 @@ class PaymentEntry(AccountsController): "account_currency": account_currency, "against": self.party or self.paid_from, "debit_in_account_currency": d.amount, + "debit_in_transaction_currency": d.amount / self.transaction_exchange_rate, "debit": d.amount, "cost_center": d.cost_center, }, From ceca5b4c72e60bf923bf8c85c7d7be0a15e6f9fc Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 6 Mar 2025 11:35:18 +0530 Subject: [PATCH 09/19] refactor: set transaction currency and rate before gl map --- .../accounts/doctype/payment_entry/payment_entry.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index cb1f6281e94..b4e53b03218 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -1311,10 +1311,7 @@ class PaymentEntry(AccountsController): self.set("remarks", "\n".join(remarks)) - def build_gl_map(self): - if self.payment_type in ("Receive", "Pay") and not self.get("party_account_field"): - self.setup_party_account_field() - + def set_transaction_currency_and_rate(self): company_currency = erpnext.get_company_currency(self.company) self.transaction_currency = company_currency self.transaction_exchange_rate = 1 @@ -1326,6 +1323,11 @@ class PaymentEntry(AccountsController): self.transaction_currency = self.paid_to_account_currency self.transaction_exchange_rate = self.target_exchange_rate + def build_gl_map(self): + if self.payment_type in ("Receive", "Pay") and not self.get("party_account_field"): + self.setup_party_account_field() + self.set_transaction_currency_and_rate() + gl_entries = [] self.add_party_gl_entries(gl_entries) self.add_bank_gl_entries(gl_entries) @@ -1472,6 +1474,7 @@ class PaymentEntry(AccountsController): def make_advance_gl_entries( self, entry: object | dict = None, cancel: bool = 0, update_outstanding: str = "Yes" ): + self.set_transaction_currency_and_rate() gl_entries = [] self.add_advance_gl_entries(gl_entries, entry) From a31770d12282771969feba8c54f2e79a2290e590 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 6 Mar 2025 14:38:18 +0530 Subject: [PATCH 10/19] fix(test): incorrect transaction exchange rate in test case --- erpnext/accounts/doctype/journal_entry/test_journal_entry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/journal_entry/test_journal_entry.py b/erpnext/accounts/doctype/journal_entry/test_journal_entry.py index f81655ca5fd..51718436a5a 100644 --- a/erpnext/accounts/doctype/journal_entry/test_journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/test_journal_entry.py @@ -583,7 +583,7 @@ class TestJournalEntry(IntegrationTestCase): order_by="account", ) expected = [ - {"account": "_Test Bank - _TC", "transaction_exchange_rate": 1.0}, + {"account": "_Test Bank - _TC", "transaction_exchange_rate": 85.0}, {"account": "_Test Receivable USD - _TC", "transaction_exchange_rate": 85.0}, ] self.assertEqual(expected, actual) From 23d465805b8812e7f76ef5031f08fe9a49243b36 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 6 Mar 2025 14:54:20 +0530 Subject: [PATCH 11/19] refactor(test): save first to let the tax table populate --- .../tax_withholding_category/test_tax_withholding_category.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py index 303eeaa72d9..883d33a6dbd 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py @@ -529,7 +529,7 @@ class TestTaxWithholdingCategory(IntegrationTestCase): payment = get_payment_entry(order.doctype, order.name) payment.apply_tax_withholding_amount = 1 payment.tax_withholding_category = "Cumulative Threshold TDS" - payment.submit() + payment.save().submit() self.assertEqual(payment.taxes[0].tax_amount, 4000) def test_multi_category_single_supplier(self): From b348aa3b3716838b1f00f5a8266c8d9b6904a660 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 6 Mar 2025 15:54:14 +0530 Subject: [PATCH 12/19] refactor: isolate to specific doctypes --- erpnext/controllers/accounts_controller.py | 27 +++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 2a6824e5f19..9956d36617b 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1125,20 +1125,19 @@ class AccountsController(TransactionBase): ) # Update details in transaction currency - gl_dict.update( - { - "transaction_currency": self.get("currency") or self.company_currency, - "transaction_exchange_rate": item.get("exchange_rate", 1) - if self.doctype == "Journal Entry" and item - else self.get("conversion_rate", 1), - "debit_in_transaction_currency": self.get_value_in_transaction_currency( - account_currency, gl_dict, "debit" - ), - "credit_in_transaction_currency": self.get_value_in_transaction_currency( - account_currency, gl_dict, "credit" - ), - } - ) + if self.doctype not in ["Purchase Invoice", "Sales Invoice", "Journal Entry", "Payment Entry"]: + gl_dict.update( + { + "transaction_currency": self.get("currency") or self.company_currency, + "transaction_exchange_rate": self.get("conversion_rate", 1), + "debit_in_transaction_currency": self.get_value_in_transaction_currency( + account_currency, gl_dict, "debit" + ), + "credit_in_transaction_currency": self.get_value_in_transaction_currency( + account_currency, gl_dict, "credit" + ), + } + ) if not args.get("against_voucher_type") and self.get("against_voucher_type"): gl_dict.update({"against_voucher_type": self.get("against_voucher_type")}) From bc792c61e9dc07c9ebef4534e9e1e7904adb592f Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 11:40:14 +0530 Subject: [PATCH 13/19] chore: typo --- erpnext/accounts/doctype/journal_entry/journal_entry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index a7a526c46b0..47ee2a8fdeb 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -1068,7 +1068,7 @@ class JournalEntry(AccountsController): if self.multi_currency: for row in self.get("accounts"): if row.account_currency != company_currency: - # Journal assumes the first foregin currency as transaction currency + # Journal assumes the first foreign currency as transaction currency self.transaction_currency = row.account_currency self.transaction_exchange_rate = row.exchange_rate break From 6545467aecac894701b2e62719083d3db195a241 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 13:22:08 +0530 Subject: [PATCH 14/19] fix: incorrect category in list --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index f5ed57b8e24..2f87d8e1906 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -955,7 +955,7 @@ class PurchaseInvoice(BuyingController): valuation_tax_accounts = [ d.account_head for d in self.get("taxes") - if d.category in ("Valuation", "Total and Valuation") + if d.category in ("Valuation", "Valuation and Total") and flt(d.base_tax_amount_after_discount_amount) ] From 7528ef147a0375dc863f2d4d0720f02dbcd71631 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 13:22:20 +0530 Subject: [PATCH 15/19] refactor: convert tax amount using exchange rate --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 2f87d8e1906..948bc050e29 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1197,7 +1197,8 @@ class PurchaseInvoice(BuyingController): "account": self.stock_received_but_not_billed, "against": self.supplier, "debit": flt(item.item_tax_amount, item.precision("item_tax_amount")), - "debit_in_transaction_currency": item.net_amount, + "debit_in_transaction_currency": item.item_tax_amount + / self.conversion_rate, "remarks": self.remarks or _("Accounting Entry for Stock"), "cost_center": self.cost_center, "project": item.project or self.project, From 4cd3f3531c965c318c635ebe2600240c9f9782fd Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 14:43:48 +0530 Subject: [PATCH 16/19] refactor: trx currency dr and cr for tax rows and item rows --- .../purchase_invoice/purchase_invoice.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 948bc050e29..87847802a89 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1049,7 +1049,10 @@ class PurchaseInvoice(BuyingController): "account": item.expense_account, "against": self.supplier, "debit": warehouse_debit_amount, - "debit_in_transaction_currency": item.net_amount, + "debit_in_transaction_currency": flt( + warehouse_debit_amount / self.conversion_rate, + item.precision("net_amount"), + ), "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "cost_center": item.cost_center, "project": item.project or self.project, @@ -1197,8 +1200,10 @@ class PurchaseInvoice(BuyingController): "account": self.stock_received_but_not_billed, "against": self.supplier, "debit": flt(item.item_tax_amount, item.precision("item_tax_amount")), - "debit_in_transaction_currency": item.item_tax_amount - / self.conversion_rate, + "debit_in_transaction_currency": flt( + item.item_tax_amount / self.conversion_rate, + item.precision("item_tax_amount"), + ), "remarks": self.remarks or _("Accounting Entry for Stock"), "cost_center": self.cost_center, "project": item.project or self.project, @@ -1399,6 +1404,10 @@ class PurchaseInvoice(BuyingController): "cost_center": tax.cost_center, "against": self.supplier, "credit": applicable_amount, + "credit_in_transaction_currency": flt( + applicable_amount / self.conversion_rate, + frappe.get_precision("Purchase Invoice Item", "item_tax_amount"), + ), "remarks": self.remarks or _("Accounting Entry for Stock"), }, item=tax, @@ -1417,6 +1426,10 @@ class PurchaseInvoice(BuyingController): "cost_center": tax.cost_center, "against": self.supplier, "credit": valuation_tax[tax.name], + "credit_in_transaction_currency": flt( + valuation_tax[tax.name] / self.conversion_rate, + frappe.get_precision("Purchase Invoice Item", "item_tax_amount"), + ), "remarks": self.remarks or _("Accounting Entry for Stock"), }, item=tax, From 455a55b2ce5dddfc534efae8efb4b58fb50b949e Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 17:00:21 +0530 Subject: [PATCH 17/19] refactor: handle rounding diff for trx currency dr and cr --- erpnext/accounts/general_ledger.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 99dd81b6460..9920040c3a0 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -431,7 +431,7 @@ def process_debit_credit_difference(gl_map): voucher_no = gl_map[0].voucher_no allowance = get_debit_credit_allowance(voucher_type, precision) - debit_credit_diff = get_debit_credit_difference(gl_map, precision) + debit_credit_diff, trx_cur_debit_credit_diff = get_debit_credit_difference(gl_map, precision) if abs(debit_credit_diff) > allowance: if not ( @@ -442,9 +442,9 @@ def process_debit_credit_difference(gl_map): raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no) elif abs(debit_credit_diff) >= (1.0 / (10**precision)): - make_round_off_gle(gl_map, debit_credit_diff, precision) + make_round_off_gle(gl_map, debit_credit_diff, trx_cur_debit_credit_diff, precision) - debit_credit_diff = get_debit_credit_difference(gl_map, precision) + debit_credit_diff, trx_cur_debit_credit_diff = get_debit_credit_difference(gl_map, precision) if abs(debit_credit_diff) > allowance: if not ( voucher_type == "Journal Entry" @@ -456,14 +456,23 @@ def process_debit_credit_difference(gl_map): def get_debit_credit_difference(gl_map, precision): debit_credit_diff = 0.0 + trx_cur_debit_credit_diff = 0 + for entry in gl_map: entry.debit = flt(entry.debit, precision) entry.credit = flt(entry.credit, precision) debit_credit_diff += entry.debit - entry.credit - debit_credit_diff = flt(debit_credit_diff, precision) + entry.debit_in_transaction_currency = flt(entry.debit_in_transaction_currency, precision) + entry.credit_in_transaction_currency = flt(entry.credit_in_transaction_currency, precision) + trx_cur_debit_credit_diff += ( + entry.debit_in_transaction_currency - entry.credit_in_transaction_currency + ) - return debit_credit_diff + debit_credit_diff = flt(debit_credit_diff, precision) + trx_cur_debit_credit_diff = flt(trx_cur_debit_credit_diff, precision) + + return debit_credit_diff, trx_cur_debit_credit_diff def get_debit_credit_allowance(voucher_type, precision): @@ -490,7 +499,7 @@ def has_opening_entries(gl_map: list) -> bool: return False -def make_round_off_gle(gl_map, debit_credit_diff, precision): +def make_round_off_gle(gl_map, debit_credit_diff, trx_cur_debit_credit_diff, precision): round_off_account, round_off_cost_center, round_off_for_opening = get_round_off_account_and_cost_center( gl_map[0].company, gl_map[0].voucher_type, gl_map[0].voucher_no ) @@ -535,6 +544,12 @@ def make_round_off_gle(gl_map, debit_credit_diff, precision): "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0, "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, "credit": debit_credit_diff if debit_credit_diff > 0 else 0, + "debit_in_transaction_currency": abs(trx_cur_debit_credit_diff) + if trx_cur_debit_credit_diff < 0 + else 0, + "credit_in_transaction_currency": trx_cur_debit_credit_diff + if trx_cur_debit_credit_diff > 0 + else 0, "cost_center": round_off_cost_center, "party_type": None, "party": None, From 55d06361232baa64551c90db25d5231f7dd32320 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 7 Mar 2025 17:39:21 +0530 Subject: [PATCH 18/19] test: assert total debit and credit for trx currency --- .../purchase_invoice/test_purchase_invoice.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index 327c69abbce..42dcda6e0ac 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -2656,6 +2656,36 @@ class TestPurchaseInvoice(IntegrationTestCase, StockTestMixin): "Buying Settings", "bill_for_rejected_quantity_in_purchase_invoice", original_value ) + def test_trx_currency_debit_credit_for_high_precision(self): + exc_rate = 0.737517516 + pi = make_purchase_invoice( + currency="USD", conversion_rate=exc_rate, qty=1, rate=2000, do_not_save=True + ) + pi.supplier = "_Test Supplier USD" + pi.save().submit() + + expected = ( + ("_Test Account Cost for Goods Sold - _TC", 1475.04, 0.0, 2000.0, 0.0, "USD", exc_rate), + ("_Test Payable USD - _TC", 0.0, 1475.04, 0.0, 2000.0, "USD", exc_rate), + ) + + actual = frappe.db.get_all( + "GL Entry", + filters={"voucher_no": pi.name}, + fields=[ + "account", + "debit", + "credit", + "debit_in_transaction_currency", + "credit_in_transaction_currency", + "transaction_currency", + "transaction_exchange_rate", + ], + order_by="account", + as_list=1, + ) + self.assertEqual(actual, expected) + def set_advance_flag(company, flag, default_account): frappe.db.set_value( From f1d8feec15595e9529af91827d106e77c1c0354d Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sat, 8 Mar 2025 11:45:12 +0530 Subject: [PATCH 19/19] refactor: internal transfer gl --- erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 87847802a89..92069a7ec8a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1445,6 +1445,7 @@ class PurchaseInvoice(BuyingController): "account": self.unrealized_profit_loss_account, "against": self.supplier, "credit": flt(self.total_taxes_and_charges), + "credit_in_transaction_currency": flt(self.total_taxes_and_charges), "credit_in_account_currency": flt(self.base_total_taxes_and_charges), "cost_center": self.cost_center, },