mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-24 21:52:21 +01:00
perf: Optimzed code for merging similar gl entries
This commit is contained in:
@@ -127,7 +127,7 @@ class GLEntry(Document):
|
|||||||
frappe.throw(msg, title=_("Missing Cost Center"))
|
frappe.throw(msg, title=_("Missing Cost Center"))
|
||||||
|
|
||||||
def validate_dimensions_for_pl_and_bs(self):
|
def validate_dimensions_for_pl_and_bs(self):
|
||||||
account_type = frappe.db.get_value("Account", self.account, "report_type")
|
account_type = frappe.get_cached_value("Account", self.account, "report_type")
|
||||||
|
|
||||||
for dimension in get_checks_for_pl_and_bs_accounts():
|
for dimension in get_checks_for_pl_and_bs_accounts():
|
||||||
if (
|
if (
|
||||||
@@ -252,7 +252,7 @@ class GLEntry(Document):
|
|||||||
|
|
||||||
def validate_balance_type(account, adv_adj=False):
|
def validate_balance_type(account, adv_adj=False):
|
||||||
if not adv_adj and account:
|
if not adv_adj and account:
|
||||||
balance_must_be = frappe.db.get_value("Account", account, "balance_must_be")
|
balance_must_be = frappe.get_cached_value("Account", account, "balance_must_be")
|
||||||
if balance_must_be:
|
if balance_must_be:
|
||||||
balance = frappe.db.sql(
|
balance = frappe.db.sql(
|
||||||
"""select sum(debit) - sum(credit)
|
"""select sum(debit) - sum(credit)
|
||||||
|
|||||||
@@ -234,11 +234,13 @@ def get_cost_center_allocation_data(company, posting_date):
|
|||||||
def merge_similar_entries(gl_map, precision=None):
|
def merge_similar_entries(gl_map, precision=None):
|
||||||
merged_gl_map = []
|
merged_gl_map = []
|
||||||
accounting_dimensions = get_accounting_dimensions()
|
accounting_dimensions = get_accounting_dimensions()
|
||||||
|
merge_properties = get_merge_properties(accounting_dimensions)
|
||||||
|
|
||||||
for entry in gl_map:
|
for entry in gl_map:
|
||||||
|
entry.merge_key = get_merge_key(entry, merge_properties)
|
||||||
# if there is already an entry in this account then just add it
|
# if there is already an entry in this account then just add it
|
||||||
# to that entry
|
# to that entry
|
||||||
same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions)
|
same_head = check_if_in_list(entry, merged_gl_map)
|
||||||
if same_head:
|
if same_head:
|
||||||
same_head.debit = flt(same_head.debit) + flt(entry.debit)
|
same_head.debit = flt(same_head.debit) + flt(entry.debit)
|
||||||
same_head.debit_in_account_currency = flt(same_head.debit_in_account_currency) + flt(
|
same_head.debit_in_account_currency = flt(same_head.debit_in_account_currency) + flt(
|
||||||
@@ -272,37 +274,34 @@ def merge_similar_entries(gl_map, precision=None):
|
|||||||
|
|
||||||
return merged_gl_map
|
return merged_gl_map
|
||||||
|
|
||||||
|
def get_merge_properties(dimensions=None):
|
||||||
def check_if_in_list(gle, gl_map, dimensions=None):
|
merge_properties = [
|
||||||
account_head_fieldnames = [
|
"account",
|
||||||
"voucher_detail_no",
|
|
||||||
"party",
|
|
||||||
"against_voucher",
|
|
||||||
"cost_center",
|
"cost_center",
|
||||||
"against_voucher_type",
|
"party",
|
||||||
"party_type",
|
"party_type",
|
||||||
|
"voucher_detail_no",
|
||||||
|
"against_voucher",
|
||||||
|
"against_voucher_type",
|
||||||
"project",
|
"project",
|
||||||
"finance_book",
|
"finance_book",
|
||||||
]
|
]
|
||||||
|
|
||||||
if dimensions:
|
if dimensions:
|
||||||
account_head_fieldnames = account_head_fieldnames + dimensions
|
merge_properties.extend(dimensions)
|
||||||
|
return merge_properties
|
||||||
|
|
||||||
|
def get_merge_key(entry, merge_properties):
|
||||||
|
merge_key = []
|
||||||
|
for fieldname in merge_properties:
|
||||||
|
merge_key.append(entry.get(fieldname, ''))
|
||||||
|
|
||||||
|
return tuple(merge_key)
|
||||||
|
|
||||||
|
def check_if_in_list(gle, gl_map):
|
||||||
for e in gl_map:
|
for e in gl_map:
|
||||||
same_head = True
|
if e.merge_key == gle.merge_key:
|
||||||
if e.account != gle.account:
|
|
||||||
same_head = False
|
|
||||||
continue
|
|
||||||
|
|
||||||
for fieldname in account_head_fieldnames:
|
|
||||||
if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)):
|
|
||||||
same_head = False
|
|
||||||
break
|
|
||||||
|
|
||||||
if same_head:
|
|
||||||
return e
|
return e
|
||||||
|
|
||||||
|
|
||||||
def toggle_debit_credit_if_negative(gl_map):
|
def toggle_debit_credit_if_negative(gl_map):
|
||||||
for entry in gl_map:
|
for entry in gl_map:
|
||||||
# toggle debit, credit if negative entry
|
# toggle debit, credit if negative entry
|
||||||
|
|||||||
Reference in New Issue
Block a user