diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b04eeb85a8..c2f14aaacf 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -340,4 +340,5 @@ erpnext.patches.v14_0.rearrange_company_fields erpnext.patches.v14_0.update_leave_notification_template erpnext.patches.v14_0.restore_einvoice_fields erpnext.patches.v13_0.update_sane_transfer_against -erpnext.patches.v12_0.add_company_link_to_einvoice_settings \ No newline at end of file +erpnext.patches.v12_0.add_company_link_to_einvoice_settings +erpnext.patches.v14_0.migrate_cost_center_allocations diff --git a/erpnext/patches/v14_0/migrate_cost_center_allocations.py b/erpnext/patches/v14_0/migrate_cost_center_allocations.py new file mode 100644 index 0000000000..ab8da12412 --- /dev/null +++ b/erpnext/patches/v14_0/migrate_cost_center_allocations.py @@ -0,0 +1,45 @@ +import frappe +from frappe.utils import today + +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() + create_new_cost_center_allocation_records(cc_allocations) + + frappe.delete_doc('DocType', 'Distributed Cost Center', ignore_missing=True) + +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() + + for child_cc, percentage in allocations.items(): + cca.append("allocation_percentages", ({ + "cost_center": child_cc, + "percentage": percentage + })) + + cca.save() + cca.submit() + + +def get_existing_cost_center_allocations(): + par = frappe.qb.DocType("Cost Center") + child = frappe.qb.DocType("Distributed Cost Center") + + records = ( + frappe.qb.from_(par) + .inner_join(child).on(par.name == child.parent) + .select(par.name, child.cost_center, child.percentage_allocation) + .where(par.enable_distributed_cost_center == 1) + ).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 \ No newline at end of file