mirror of
https://github.com/frappe/erpnext.git
synced 2026-04-12 11:25:09 +00:00
refactor: validate only for overlapping keys
This commit is contained in:
@@ -34,7 +34,10 @@ class BudgetValidation:
|
||||
):
|
||||
self.budgets.append(frappe.get_doc("Budget", x.name))
|
||||
|
||||
def get_active_budgets(self):
|
||||
def generate_active_budget_keys(self):
|
||||
"""
|
||||
key structure - (dimension_type, dimension, GL account)
|
||||
"""
|
||||
self.active_keys = set()
|
||||
self.get_budget_records()
|
||||
for x in self.budgets:
|
||||
@@ -45,6 +48,9 @@ class BudgetValidation:
|
||||
)
|
||||
|
||||
def generate_doc_dimension_keys(self):
|
||||
"""
|
||||
key structure - (dimension_type, dimension, GL account)
|
||||
"""
|
||||
keys = []
|
||||
for itm in self.doc.items:
|
||||
keys.extend(
|
||||
@@ -56,10 +62,41 @@ class BudgetValidation:
|
||||
)
|
||||
self.item_dimension_keys = set(keys)
|
||||
|
||||
def build_processing_dictionary(self):
|
||||
self.budget_map = frappe._dict()
|
||||
|
||||
for x in self.budgets:
|
||||
budget_against = frappe.scrub(x.budget_against)
|
||||
dimension = x.get(budget_against)
|
||||
for acc in x.accounts:
|
||||
key = (budget_against, dimension, acc.account)
|
||||
if key in self.overlap:
|
||||
self.budget_map[key] = frappe._dict(
|
||||
{"budget_amount": acc.budget_amount, "items_to_process": []}
|
||||
)
|
||||
|
||||
for itm in self.doc.items:
|
||||
for dim in self.dimensions:
|
||||
if itm.get(dim.get("fieldname")):
|
||||
key = (dim.get("fieldname"), itm.get(dim.get("fieldname")), itm.expense_account)
|
||||
|
||||
if key in self.overlap:
|
||||
self.budget_map[key]["items_to_process"].append(itm)
|
||||
|
||||
def validate(self):
|
||||
self.get_active_budgets()
|
||||
self.generate_active_budget_keys()
|
||||
self.generate_doc_dimension_keys()
|
||||
|
||||
print(self.active_keys)
|
||||
print(self.item_dimension_keys)
|
||||
print(self.active_keys & self.item_dimension_keys)
|
||||
self.overlap = self.active_keys & self.item_dimension_keys
|
||||
self.build_processing_dictionary()
|
||||
self.validate_for_overbooking()
|
||||
|
||||
def get_booked_amount(self):
|
||||
pass
|
||||
|
||||
def validate_for_overbooking(self):
|
||||
# Need to fetch historical amount and add them to the current document
|
||||
for k, v in self.budget_map.items():
|
||||
current_amount = sum([x.amount for x in v.items_to_process])
|
||||
self.budget_map[k]["current_amount"] = current_amount
|
||||
print((k, v.get("budget_amount"), current_amount))
|
||||
|
||||
Reference in New Issue
Block a user