2022-01-31 07:55:44 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import today
|
|
|
|
|
2022-01-31 12:37:04 +00:00
|
|
|
|
2022-01-31 07:55:44 +00:00
|
|
|
def execute():
|
|
|
|
for dt in ("cost_center_allocation", "cost_center_allocation_percentage"):
|
|
|
|
frappe.reload_doc("accounts", "doctype", dt)
|
|
|
|
|
|
|
|
cc_allocations = get_existing_cost_center_allocations()
|
2022-01-31 10:28:40 +00:00
|
|
|
if cc_allocations:
|
|
|
|
create_new_cost_center_allocation_records(cc_allocations)
|
2022-01-31 07:55:44 +00:00
|
|
|
|
|
|
|
frappe.delete_doc("DocType", "Distributed Cost Center", ignore_missing=True)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-01-31 07:55:44 +00:00
|
|
|
|
|
|
|
def create_new_cost_center_allocation_records(cc_allocations):
|
|
|
|
for main_cc, allocations in cc_allocations.items():
|
|
|
|
cca = frappe.new_doc("Cost Center Allocation")
|
|
|
|
cca.main_cost_center = main_cc
|
|
|
|
cca.valid_from = today()
|
2023-01-27 06:38:18 +00:00
|
|
|
cca._skip_from_date_validation = True
|
2022-01-31 07:55:44 +00:00
|
|
|
|
|
|
|
for child_cc, percentage in allocations.items():
|
|
|
|
cca.append("allocation_percentages", ({"cost_center": child_cc, "percentage": percentage}))
|
2023-01-27 06:38:18 +00:00
|
|
|
|
2022-01-31 07:55:44 +00:00
|
|
|
cca.save()
|
|
|
|
cca.submit()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-01-31 07:55:44 +00:00
|
|
|
def get_existing_cost_center_allocations():
|
2022-02-01 16:56:03 +00:00
|
|
|
if not frappe.db.exists("DocType", "Distributed Cost Center"):
|
2022-01-31 10:28:40 +00:00
|
|
|
return
|
|
|
|
|
2022-01-31 07:55:44 +00:00
|
|
|
par = frappe.qb.DocType("Cost Center")
|
|
|
|
child = frappe.qb.DocType("Distributed Cost Center")
|
|
|
|
|
|
|
|
records = (
|
|
|
|
frappe.qb.from_(par)
|
2022-01-31 11:54:50 +00:00
|
|
|
.inner_join(child)
|
|
|
|
.on(par.name == child.parent)
|
|
|
|
.select(par.name, child.cost_center, child.percentage_allocation)
|
|
|
|
.where(par.enable_distributed_cost_center == 1)
|
2022-01-31 07:55:44 +00:00
|
|
|
).run(as_dict=True)
|
|
|
|
|
|
|
|
cc_allocations = frappe._dict()
|
|
|
|
for d in records:
|
|
|
|
cc_allocations.setdefault(d.name, frappe._dict()).setdefault(
|
|
|
|
d.cost_center, d.percentage_allocation
|
|
|
|
)
|
|
|
|
|
|
|
|
return cc_allocations
|