Merge pull request #48413 from ljain112/fix-ple

perf: multiple fixes related to ple and gle
This commit is contained in:
ruthra kumar
2025-07-28 16:11:31 +05:30
committed by GitHub
3 changed files with 48 additions and 55 deletions

View File

@@ -18,7 +18,7 @@ from erpnext.accounts.party import (
validate_party_frozen_disabled, validate_party_frozen_disabled,
validate_party_gle_currency, validate_party_gle_currency,
) )
from erpnext.accounts.utils import get_account_currency, get_fiscal_year from erpnext.accounts.utils import OUTSTANDING_DOCTYPES, get_account_currency, get_fiscal_year
from erpnext.exceptions import InvalidAccountCurrency from erpnext.exceptions import InvalidAccountCurrency
exclude_from_linked_with = True exclude_from_linked_with = True
@@ -224,26 +224,23 @@ class GLEntry(Document):
def validate_account_details(self, adv_adj): def validate_account_details(self, adv_adj):
"""Account must be ledger, active and not freezed""" """Account must be ledger, active and not freezed"""
ret = frappe.db.sql( account = frappe.get_cached_value(
"""select is_group, docstatus, company "Account", self.account, fieldname=["is_group", "docstatus", "company"], as_dict=True
from tabAccount where name=%s""", )
self.account,
as_dict=1,
)[0]
if ret.is_group == 1: if account.is_group == 1:
frappe.throw( frappe.throw(
_( _(
"""{0} {1}: Account {2} is a Group Account and group accounts cannot be used in transactions""" """{0} {1}: Account {2} is a Group Account and group accounts cannot be used in transactions"""
).format(self.voucher_type, self.voucher_no, self.account) ).format(self.voucher_type, self.voucher_no, self.account)
) )
if ret.docstatus == 2: if account.docstatus == 2:
frappe.throw( frappe.throw(
_("{0} {1}: Account {2} is inactive").format(self.voucher_type, self.voucher_no, self.account) _("{0} {1}: Account {2} is inactive").format(self.voucher_type, self.voucher_no, self.account)
) )
if ret.company != self.company: if account.company != self.company:
frappe.throw( frappe.throw(
_("{0} {1}: Account {2} does not belong to Company {3}").format( _("{0} {1}: Account {2} does not belong to Company {3}").format(
self.voucher_type, self.voucher_no, self.account, self.company self.voucher_type, self.voucher_no, self.account, self.company
@@ -385,7 +382,7 @@ def update_outstanding_amt(
) )
) )
if against_voucher_type in ["Sales Invoice", "Purchase Invoice", "Fees"]: if against_voucher_type in OUTSTANDING_DOCTYPES:
ref_doc = frappe.get_doc(against_voucher_type, against_voucher) ref_doc = frappe.get_doc(against_voucher_type, against_voucher)
# Didn't use db_set for optimization purpose # Didn't use db_set for optimization purpose

View File

@@ -16,7 +16,7 @@ from erpnext.accounts.doctype.gl_entry.gl_entry import (
validate_balance_type, validate_balance_type,
validate_frozen_account, validate_frozen_account,
) )
from erpnext.accounts.utils import update_voucher_outstanding from erpnext.accounts.utils import OUTSTANDING_DOCTYPES, update_voucher_outstanding
from erpnext.exceptions import InvalidAccountDimensionError, MandatoryAccountDimensionError from erpnext.exceptions import InvalidAccountDimensionError, MandatoryAccountDimensionError
@@ -51,38 +51,36 @@ class PaymentLedgerEntry(Document):
# end: auto-generated types # end: auto-generated types
def validate_account(self): def validate_account(self):
valid_account = frappe.db.get_list( account = frappe.get_cached_value(
"Account", "Account", self.account, fieldname=["account_type", "company"], as_dict=True
"name",
filters={"name": self.account, "account_type": self.account_type, "company": self.company},
ignore_permissions=True,
) )
if not valid_account:
if account.company != self.company:
frappe.throw(_("{0} account is not of company {1}").format(self.account, self.company))
if account.account_type != self.account_type:
frappe.throw(_("{0} account is not of type {1}").format(self.account, self.account_type)) frappe.throw(_("{0} account is not of type {1}").format(self.account, self.account_type))
def validate_account_details(self): def validate_account_details(self):
"""Account must be ledger, active and not freezed""" """Account must be ledger, active and not freezed"""
ret = frappe.db.sql( account = frappe.get_cached_value(
"""select is_group, docstatus, company "Account", self.account, fieldname=["is_group", "docstatus", "company"], as_dict=True
from tabAccount where name=%s""", )
self.account,
as_dict=1,
)[0]
if ret.is_group == 1: if account.is_group == 1:
frappe.throw( frappe.throw(
_( _(
"""{0} {1}: Account {2} is a Group Account and group accounts cannot be used in transactions""" """{0} {1}: Account {2} is a Group Account and group accounts cannot be used in transactions"""
).format(self.voucher_type, self.voucher_no, self.account) ).format(self.voucher_type, self.voucher_no, self.account)
) )
if ret.docstatus == 2: if account.docstatus == 2:
frappe.throw( frappe.throw(
_("{0} {1}: Account {2} is inactive").format(self.voucher_type, self.voucher_no, self.account) _("{0} {1}: Account {2} is inactive").format(self.voucher_type, self.voucher_no, self.account)
) )
if ret.company != self.company: if account.company != self.company:
frappe.throw( frappe.throw(
_("{0} {1}: Account {2} does not belong to Company {3}").format( _("{0} {1}: Account {2} does not belong to Company {3}").format(
self.voucher_type, self.voucher_no, self.account, self.company self.voucher_type, self.voucher_no, self.account, self.company
@@ -170,7 +168,7 @@ class PaymentLedgerEntry(Document):
# update outstanding amount # update outstanding amount
if ( if (
self.against_voucher_type in ["Journal Entry", "Sales Invoice", "Purchase Invoice", "Fees"] self.against_voucher_type in OUTSTANDING_DOCTYPES
and self.flags.update_outstanding == "Yes" and self.flags.update_outstanding == "Yes"
and not frappe.flags.is_reverse_depr_entry and not frappe.flags.is_reverse_depr_entry
): ):

View File

@@ -50,6 +50,7 @@ class PaymentEntryUnlinkError(frappe.ValidationError):
GL_REPOSTING_CHUNK = 100 GL_REPOSTING_CHUNK = 100
OUTSTANDING_DOCTYPES = frozenset(["Sales Invoice", "Purchase Invoice", "Fees"])
@frappe.whitelist() @frappe.whitelist()
@@ -1893,45 +1894,42 @@ def create_payment_ledger_entry(
def update_voucher_outstanding(voucher_type, voucher_no, account, party_type, party): def update_voucher_outstanding(voucher_type, voucher_no, account, party_type, party):
if not (voucher_type in OUTSTANDING_DOCTYPES and party_type and party):
return
ple = frappe.qb.DocType("Payment Ledger Entry") ple = frappe.qb.DocType("Payment Ledger Entry")
vouchers = [frappe._dict({"voucher_type": voucher_type, "voucher_no": voucher_no})] vouchers = [frappe._dict({"voucher_type": voucher_type, "voucher_no": voucher_no})]
common_filter = [] common_filter = []
common_filter.append(ple.party_type == party_type)
common_filter.append(ple.party == party)
if account: if account:
common_filter.append(ple.account == account) common_filter.append(ple.account == account)
if party_type:
common_filter.append(ple.party_type == party_type)
if party:
common_filter.append(ple.party == party)
ple_query = QueryPaymentLedger() ple_query = QueryPaymentLedger()
# on cancellation outstanding can be an empty list # on cancellation outstanding can be an empty list
voucher_outstanding = ple_query.get_voucher_outstandings(vouchers, common_filter=common_filter) voucher_outstanding = ple_query.get_voucher_outstandings(vouchers, common_filter=common_filter)
if ( if not voucher_outstanding:
voucher_type in ["Sales Invoice", "Purchase Invoice", "Fees"] return
and party_type
and party
and voucher_outstanding
):
outstanding = voucher_outstanding[0]
ref_doc = frappe.get_lazy_doc(voucher_type, voucher_no)
outstanding_amount = flt(
outstanding["outstanding_in_account_currency"], ref_doc.precision("outstanding_amount")
)
# Didn't use db_set for optimisation purpose outstanding = voucher_outstanding[0]
ref_doc.outstanding_amount = outstanding_amount ref_doc = frappe.get_lazy_doc(voucher_type, voucher_no)
frappe.db.set_value( outstanding_amount = flt(
voucher_type, outstanding["outstanding_in_account_currency"], ref_doc.precision("outstanding_amount")
voucher_no, )
"outstanding_amount",
outstanding_amount,
)
ref_doc.set_status(update=True) # Didn't use db_set for optimisation purpose
ref_doc.notify_update() ref_doc.outstanding_amount = outstanding_amount
frappe.db.set_value(
voucher_type,
voucher_no,
"outstanding_amount",
outstanding_amount,
)
ref_doc.set_status(update=True)
ref_doc.notify_update()
def delink_original_entry(pl_entry, partial_cancel=False): def delink_original_entry(pl_entry, partial_cancel=False):