refactor(gl): give SI and PI their own precision-loss GL entry method

Remove the doctype-branching make_precision_loss_gl_entry from
exchange_gain_loss.py (and its accounts_controller wrapper); add a
dedicated method to each of SalesInvoiceGLComposer and
PurchaseInvoiceGLComposer. The SI variant now passes 'Sales Invoice'
as the round-off voucher type (output-equivalent) and the throwaway
return value no longer shadows the gettext _ helper.
This commit is contained in:
Nabin Hait
2026-06-03 12:45:58 +05:30
parent d8760b76a8
commit cfed16ab6c
4 changed files with 60 additions and 36 deletions

View File

@@ -32,7 +32,7 @@ class PurchaseInvoiceGLComposer(BaseGLComposer):
self.make_supplier_gl_entry(gl_entries)
self.make_item_gl_entries(gl_entries)
doc.make_precision_loss_gl_entry(gl_entries)
self.make_precision_loss_gl_entry(gl_entries)
self.make_tax_gl_entries(gl_entries)
self.make_internal_transfer_gl_entries(gl_entries)
@@ -48,6 +48,35 @@ class PurchaseInvoiceGLComposer(BaseGLComposer):
doc.set_gl_entry_for_purchase_expense(gl_entries)
return gl_entries
def make_precision_loss_gl_entry(self, gl_entries):
doc = self.doc
(
round_off_account,
round_off_cost_center,
_round_off_for_opening,
) = get_round_off_account_and_cost_center(
doc.company, "Purchase Invoice", doc.name, doc.use_company_roundoff_cost_center
)
precision_loss = doc.get("base_net_total") - flt(
doc.get("net_total") * doc.conversion_rate, doc.precision("net_total")
)
if precision_loss:
gl_entries.append(
doc.get_gl_dict(
{
"account": round_off_account,
"against": doc.supplier,
"credit": precision_loss,
"cost_center": round_off_cost_center
if doc.use_company_roundoff_cost_center
else doc.cost_center or round_off_cost_center,
"remarks": _("Net total calculation precision loss"),
}
)
)
def make_supplier_gl_entry(self, gl_entries):
doc = self.doc
grand_total = (

View File

@@ -39,7 +39,7 @@ class SalesInvoiceGLComposer(BaseGLComposer):
if not (doc.is_return and disable_sdbnb_in_sr):
self.stock_delivered_but_not_billed_gl_entries(gl_entries)
doc.make_precision_loss_gl_entry(gl_entries)
self.make_precision_loss_gl_entry(gl_entries)
tax_service.make_discount_gl_entries(gl_entries)
gl_entries = make_regional_gl_entries(gl_entries, doc)
@@ -56,6 +56,35 @@ class SalesInvoiceGLComposer(BaseGLComposer):
doc.set_transaction_currency_and_rate_in_gl_map(gl_entries)
return gl_entries
def make_precision_loss_gl_entry(self, gl_entries):
doc = self.doc
(
round_off_account,
round_off_cost_center,
_round_off_for_opening,
) = get_round_off_account_and_cost_center(
doc.company, "Sales Invoice", doc.name, doc.use_company_roundoff_cost_center
)
precision_loss = doc.get("base_net_total") - flt(
doc.get("net_total") * doc.conversion_rate, doc.precision("net_total")
)
if precision_loss:
gl_entries.append(
doc.get_gl_dict(
{
"account": round_off_account,
"against": doc.customer,
"debit": precision_loss,
"cost_center": round_off_cost_center
if doc.use_company_roundoff_cost_center
else doc.cost_center or round_off_cost_center,
"remarks": _("Net total calculation precision loss"),
}
)
)
def stock_delivered_but_not_billed_gl_entries(self, gl_entries):
doc = self.doc
if doc.update_stock or not cint(erpnext.is_perpetual_inventory_enabled(doc.company)):

View File

@@ -8,38 +8,9 @@ from frappe import _, qb
from frappe.utils import flt, get_link_to_form
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
from erpnext.accounts.general_ledger import get_round_off_account_and_cost_center
from erpnext.accounts.utils import create_gain_loss_journal, get_currency_precision
def make_precision_loss_gl_entry(doc, gl_entries: list) -> None:
round_off_account, round_off_cost_center, _ = get_round_off_account_and_cost_center(
doc.company, "Purchase Invoice", doc.name, doc.use_company_roundoff_cost_center
)
precision_loss = doc.get("base_net_total") - flt(
doc.get("net_total") * doc.conversion_rate, doc.precision("net_total")
)
credit_or_debit = "credit" if doc.doctype == "Purchase Invoice" else "debit"
against = doc.supplier if doc.doctype == "Purchase Invoice" else doc.customer
if precision_loss:
gl_entries.append(
doc.get_gl_dict(
{
"account": round_off_account,
"against": against,
credit_or_debit: precision_loss,
"cost_center": round_off_cost_center
if doc.use_company_roundoff_cost_center
else doc.cost_center or round_off_cost_center,
"remarks": _("Net total calculation precision loss"),
}
)
)
def gain_loss_journal_already_booked(
gain_loss_account: str,
exc_gain_loss: float,

View File

@@ -1043,11 +1043,6 @@ class AccountsController(TransactionBase):
set_advance_gain_or_loss(self)
def make_precision_loss_gl_entry(self, gl_entries):
from erpnext.accounts.services.exchange_gain_loss import make_precision_loss_gl_entry
make_precision_loss_gl_entry(self, gl_entries)
def gain_loss_journal_already_booked(
self, gain_loss_account, exc_gain_loss, ref2_dt, ref2_dn, ref2_detail_no
) -> bool: