mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-18 06:22:12 +00:00
fix: fetch cost center allocation percentage only from the applicable allocation
(cherry picked from commit 36e5945c66)
This commit is contained in:
@@ -179,50 +179,53 @@ def process_gl_map(gl_map, merge_entries=True, precision=None):
|
|||||||
|
|
||||||
|
|
||||||
def distribute_gl_based_on_cost_center_allocation(gl_map, precision=None):
|
def distribute_gl_based_on_cost_center_allocation(gl_map, precision=None):
|
||||||
cost_center_allocation = get_cost_center_allocation_data(gl_map[0]["company"], gl_map[0]["posting_date"])
|
|
||||||
if not cost_center_allocation:
|
|
||||||
return gl_map
|
|
||||||
|
|
||||||
new_gl_map = []
|
new_gl_map = []
|
||||||
for d in gl_map:
|
for d in gl_map:
|
||||||
cost_center = d.get("cost_center")
|
cost_center = d.get("cost_center")
|
||||||
|
|
||||||
# Validate budget against main cost center
|
# Validate budget against main cost center
|
||||||
validate_expense_against_budget(d, expense_amount=flt(d.debit, precision) - flt(d.credit, precision))
|
validate_expense_against_budget(d, expense_amount=flt(d.debit, precision) - flt(d.credit, precision))
|
||||||
|
cost_center_allocation = get_cost_center_allocation_data(
|
||||||
if cost_center and cost_center_allocation.get(cost_center):
|
gl_map[0]["company"], gl_map[0]["posting_date"], cost_center
|
||||||
for sub_cost_center, percentage in cost_center_allocation.get(cost_center, {}).items():
|
)
|
||||||
gle = copy.deepcopy(d)
|
if not cost_center_allocation:
|
||||||
gle.cost_center = sub_cost_center
|
|
||||||
for field in ("debit", "credit", "debit_in_account_currency", "credit_in_account_currency"):
|
|
||||||
gle[field] = flt(flt(d.get(field)) * percentage / 100, precision)
|
|
||||||
new_gl_map.append(gle)
|
|
||||||
else:
|
|
||||||
new_gl_map.append(d)
|
new_gl_map.append(d)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for sub_cost_center, percentage in cost_center_allocation:
|
||||||
|
gle = copy.deepcopy(d)
|
||||||
|
gle.cost_center = sub_cost_center
|
||||||
|
for field in ("debit", "credit", "debit_in_account_currency", "credit_in_account_currency"):
|
||||||
|
gle[field] = flt(flt(d.get(field)) * percentage / 100, precision)
|
||||||
|
new_gl_map.append(gle)
|
||||||
|
|
||||||
return new_gl_map
|
return new_gl_map
|
||||||
|
|
||||||
|
|
||||||
def get_cost_center_allocation_data(company, posting_date):
|
def get_cost_center_allocation_data(company, posting_date, cost_center):
|
||||||
par = frappe.qb.DocType("Cost Center Allocation")
|
cost_center_allocation = frappe.db.get_value(
|
||||||
child = frappe.qb.DocType("Cost Center Allocation Percentage")
|
"Cost Center Allocation",
|
||||||
|
{
|
||||||
|
"docstatus": 1,
|
||||||
|
"company": company,
|
||||||
|
"valid_from": ("<=", posting_date),
|
||||||
|
"main_cost_center": cost_center,
|
||||||
|
},
|
||||||
|
pluck="name",
|
||||||
|
order_by="valid_from desc",
|
||||||
|
)
|
||||||
|
|
||||||
records = (
|
if not cost_center_allocation:
|
||||||
frappe.qb.from_(par)
|
return []
|
||||||
.inner_join(child)
|
|
||||||
.on(par.name == child.parent)
|
|
||||||
.select(par.main_cost_center, child.cost_center, child.percentage)
|
|
||||||
.where(par.docstatus == 1)
|
|
||||||
.where(par.company == company)
|
|
||||||
.where(par.valid_from <= posting_date)
|
|
||||||
.orderby(par.valid_from, order=frappe.qb.desc)
|
|
||||||
).run(as_dict=True)
|
|
||||||
|
|
||||||
cc_allocation = frappe._dict()
|
records = frappe.db.get_all(
|
||||||
for d in records:
|
"Cost Center Allocation Percentage",
|
||||||
cc_allocation.setdefault(d.main_cost_center, frappe._dict()).setdefault(d.cost_center, d.percentage)
|
{"parent": cost_center_allocation},
|
||||||
|
["cost_center", "percentage"],
|
||||||
|
as_list=True,
|
||||||
|
)
|
||||||
|
|
||||||
return cc_allocation
|
return records
|
||||||
|
|
||||||
|
|
||||||
def merge_similar_entries(gl_map, precision=None):
|
def merge_similar_entries(gl_map, precision=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user