Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions erpnext/accounts/report/purchase_register/purchase_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import frappe
from frappe import _, msgprint
from frappe.model.meta import get_field_precision
from frappe.query_builder.custom import ConstantColumn
from frappe.utils import flt, getdate
from pypika.terms import Bracket, LiteralValue, Order
Expand Down Expand Up @@ -125,17 +126,32 @@ def _execute(filters=None, additional_table_columns=None):
row.update({frappe.scrub(tax_acc): tax_amount})

# total tax, grand total, rounded total & outstanding amount

outstanding_precision = (
get_field_precision(
frappe.get_meta("Purchase Invoice").get_field("outstanding_amount"),
currency=company_currency,
)
or 2
)
row.update(
{
"total_tax": total_tax,
"grand_total": inv.base_grand_total,
"rounded_total": inv.base_rounded_total,
"outstanding_amount": inv.outstanding_amount,
}
)

if inv.doctype == "Purchase Invoice":
row.update({"debit": inv.base_grand_total, "credit": 0.0})
row.update(
{
"debit": inv.base_grand_total,
"credit": 0.0,
"outstanding_amount": flt(
(inv.outstanding_amount * (inv.conversion_rate or 1)), outstanding_precision
),
}
)
Comment on lines +146 to +154
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Please add regression tests for Purchase Register outstanding conversion.

Line 150-Line 152 changes monetary behavior (conversion + rounding). Add coverage for foreign-currency invoices and precision expectations to prevent regressions.

I can help draft concrete fixtures/assertions if you want.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@erpnext/accounts/report/purchase_register/purchase_register.py` around lines
146 - 154, Add a regression test that verifies outstanding_amount conversion and
rounding in purchase_register logic: create foreign-currency Purchase Invoices
with a non-1 conversion_rate and known outstanding_amount, call the function
that builds rows (the code path that executes row.update with "debit", "credit",
"outstanding_amount" in purchase_register.py) and assert that outstanding_amount
equals flt(inv.outstanding_amount * (inv.conversion_rate or 1),
outstanding_precision) and that debit/credit are set as expected; name the test
e.g. test_purchase_register_outstanding_conversion and include fixtures for an
invoice in another currency, a specific conversion_rate, and expected precision
values to lock in rounding behavior.

else:
row.update({"debit": 0.0, "credit": inv.base_grand_total})
data.append(row)
Expand Down Expand Up @@ -395,6 +411,7 @@ def get_invoices(filters, additional_query_columns):
pi.base_rounded_total,
pi.outstanding_amount,
pi.mode_of_payment,
pi.conversion_rate,
)
.where(pi.docstatus == 1)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# MIT License. See license.txt

import frappe
from frappe.utils import add_months, today
from frappe.utils import add_months, flt, today

from erpnext.accounts.report.purchase_register.purchase_register import execute
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
Expand Down Expand Up @@ -67,6 +67,47 @@ def test_purchase_register_ignores_tax_rows_from_other_doctype(self):
self.assertEqual(first_row.total_tax, 100)
self.assertEqual(first_row.grand_total, 1100)

def test_purchase_currency_conversion(self):
usd_creditors = frappe.get_doc(
{
"doctype": "Account",
"account_name": "USD Creditors",
"parent_account": "Accounts Payable - _TC",
"company": "_Test Company",
"account_type": "Payable",
"root_type": "Liability",
"report_type": "Balance Sheet",
"account_currency": "USD",
}
).insert()
foreign_invoice = make_purchase_invoice()
foreign_invoice.db_set("currency", "USD")
foreign_invoice.db_set("conversion_rate", 80)
foreign_invoice.db_set("credit_to", usd_creditors.name)
foreign_invoice.db_set("outstanding_amount", 100.236)
local_invoice = make_purchase_invoice()
local_invoice.db_set("currency", "INR")
local_invoice.db_set("conversion_rate", 1)
local_invoice.db_set("outstanding_amount", 200.456)
columns, data, *_ = execute(frappe._dict({"company": foreign_invoice.company}))
outstanding_precision = 2
for row in data:
if row.get("invoice") == foreign_invoice.name:
expected_value = flt(
foreign_invoice.outstanding_amount * (foreign_invoice.conversion_rate or 1),
outstanding_precision,
)
self.assertEqual(row.get("outstanding_amount"), expected_value)
self.assertEqual(
row.get("outstanding_amount"), round(row.get("outstanding_amount"), outstanding_precision)
)
if row.get("invoice") == local_invoice.name:
expected_value = flt(
local_invoice.outstanding_amount * (local_invoice.conversion_rate or 1),
outstanding_precision,
)
self.assertEqual(row.get("outstanding_amount"), expected_value)

def test_purchase_register_ledger_view(self):
filters = frappe._dict(
company="_Test Company 6",
Expand Down
19 changes: 17 additions & 2 deletions erpnext/accounts/report/sales_register/sales_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,31 @@ def _execute(filters, additional_table_columns=None):

# total tax, grand total, outstanding amount & rounded total

outstanding_precision = (
get_field_precision(
frappe.get_meta("Sales Invoice").get_field("outstanding_amount"),
currency=company_currency,
)
or 2
)
row.update(
{
"tax_total": total_tax,
"grand_total": inv.base_grand_total,
"rounded_total": inv.base_rounded_total,
"outstanding_amount": inv.outstanding_amount,
}
)

if inv.doctype == "Sales Invoice":
row.update({"debit": inv.base_grand_total, "credit": 0.0})
row.update(
{
"debit": inv.base_grand_total,
"credit": 0.0,
"outstanding_amount": flt(
(inv.outstanding_amount * (inv.conversion_rate or 1)), outstanding_precision
),
}
)
Comment on lines +160 to +168
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add regression tests for multi-currency outstanding conversion in Sales Register.

This changes financial output semantics (outstanding_amount now converted and rounded). Please add report-level coverage for at least: foreign-currency invoice, same-currency invoice, and precision assertion on the displayed outstanding value.

If useful, I can draft a focused test matrix and expected values for these scenarios.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@erpnext/accounts/report/sales_register/sales_register.py` around lines 160 -
168, Add regression tests that exercise the Sales Register report's
outstanding_amount conversion and rounding: create one foreign-currency invoice
with conversion_rate != 1 and one same-currency invoice with conversion_rate =
1, set known outstanding_amount and outstanding_precision, run the Sales
Register report (the code path that produces row.update({...
"outstanding_amount": flt((inv.outstanding_amount * (inv.conversion_rate or 1)),
outstanding_precision) })) and assert the returned/displayed outstanding_amount
equals the expected (inv.outstanding_amount * conversion_rate) rounded to
outstanding_precision; include assertions for both invoices and an explicit
precision check to ensure the flt and rounding behavior is correct.

else:
row.update({"debit": 0.0, "credit": inv.base_grand_total})
data.append(row)
Expand Down Expand Up @@ -437,6 +451,7 @@ def get_invoices(filters, additional_query_columns):
si.is_internal_customer,
si.represents_company,
si.company,
si.conversion_rate,
)
.where(si.docstatus == 1)
)
Expand Down
49 changes: 48 additions & 1 deletion erpnext/accounts/report/sales_register/test_sales_register.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import frappe
from frappe.utils import getdate, today
from frappe.utils import flt, getdate, today

from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
from erpnext.accounts.report.sales_register.sales_register import execute
from erpnext.accounts.test.accounts_mixin import AccountsTestMixin
from erpnext.selling.doctype.customer.test_customer import make_customer
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
from erpnext.tests.utils import ERPNextTestSuite

Expand Down Expand Up @@ -212,3 +213,49 @@ def test_journal_with_cost_center_filter(self):
}
result_output = {k: v for k, v in filtered_output[0].items() if k in expected_result}
self.assertDictEqual(result_output, expected_result)

def test_currency_conversion(self):
usd_debtors = frappe.get_doc(
{
"doctype": "Account",
"account_name": "USD Debtors",
"parent_account": "Accounts Receivable - _TC",
"company": "_Test Company",
"account_type": "Receivable",
"root_type": "Asset",
"report_type": "Balance Sheet",
"account_currency": "USD",
}
).insert()
foreign_invoice = create_sales_invoice(
customer="_Test Customer",
currency="USD",
conversion_rate=80,
qty=1,
rate=100,
debit_to=usd_debtors.name,
)
foreign_invoice.db_set("outstanding_amount", 100.236)
make_customer("_Test Customer2")
local_invoice = create_sales_invoice(
customer="_Test Customer2", currency="INR", conversion_rate=1, qty=1, rate=200
)
local_invoice.db_set("outstanding_amount", 200.456)
columns, data, *_ = execute(frappe._dict({"company": foreign_invoice.company}))
outstanding_precision = 2
for row in data:
if row.get("invoice") == foreign_invoice.name:
expected_value = flt(
foreign_invoice.outstanding_amount * (foreign_invoice.conversion_rate or 1),
outstanding_precision,
)
self.assertEqual(row.get("outstanding_amount"), expected_value)
self.assertEqual(
row.get("outstanding_amount"), round(row.get("outstanding_amount"), outstanding_precision)
)
if row.get("invoice") == local_invoice.name:
expected_value = flt(
local_invoice.outstanding_amount * (local_invoice.conversion_rate or 1),
outstanding_precision,
)
self.assertEqual(row.get("outstanding_amount"), expected_value)
Loading