From 2ec8a0e943705a61febe487b5583b4be1a46073f Mon Sep 17 00:00:00 2001 From: David Date: Sun, 17 Nov 2024 11:44:16 +0100 Subject: [PATCH] fix(migration): to new item_wise_tax_detail --- erpnext/patches.txt | 2 +- ...e_old_item_wise_tax_detail_data_format.py} | 29 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) rename erpnext/patches/v15_0/{migrate_old_item_wise_tax_data_format.py => migrate_old_item_wise_tax_detail_data_format.py} (57%) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 139c64de010..a0757230d5d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -388,4 +388,4 @@ erpnext.patches.v15_0.migrate_to_utm_analytics erpnext.patches.v15_0.update_sub_voucher_type_in_gl_entries erpnext.patches.v15_0.update_task_assignee_email_field_in_asset_maintenance_log erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter -erpnext.patches.v15_0.migrate_old_item_wise_tax_data_format +erpnext.patches.v15_0.migrate_old_item_wise_tax_detail_data_format diff --git a/erpnext/patches/v15_0/migrate_old_item_wise_tax_data_format.py b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py similarity index 57% rename from erpnext/patches/v15_0/migrate_old_item_wise_tax_data_format.py rename to erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py index 633a760fa52..7eb2d5614cf 100644 --- a/erpnext/patches/v15_0/migrate_old_item_wise_tax_data_format.py +++ b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py @@ -7,25 +7,26 @@ from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail def execute(): - # Get all DocTypes that have the 'item_wise_tax_details' field + # Get all DocTypes that have the 'item_wise_tax_detail' field doctypes_with_tax_details = frappe.get_all( - "DocField", filters={"fieldname": "item_wise_tax_details"}, fields=["parent"], pluck="parent" + "DocField", filters={"fieldname": "item_wise_tax_detail"}, fields=["parent"], pluck="parent" ) for doctype in doctypes_with_tax_details: - # Get all documents of this DocType that have data in 'item_wise_tax_details' + migrated_count = 0 # Counter for migrated records per DocType + # Get all documents of this DocType that have data in 'item_wise_tax_detail' docs = frappe.get_all( doctype, - filters={"item_wise_tax_details": ["is", "set"]}, - fields=["name", "item_wise_tax_details"], + filters={"item_wise_tax_detail": ["is", "set"]}, + fields=["name", "item_wise_tax_detail"], ) for doc in docs: - if not doc.item_wise_tax_details: + if not doc.item_wise_tax_detail: continue updated_tax_details = {} needs_update = False - for item, tax_data in json.loads(doc.item_wise_tax_details).items(): + for item, tax_data in json.loads(doc.item_wise_tax_detail).items(): if isinstance(tax_data, list) and len(tax_data) == 2: updated_tax_details[item] = ItemWiseTaxDetail( tax_rate=tax_data[0], @@ -35,6 +36,14 @@ def execute(): net_amount=0.0, ) needs_update = True + # intermediate patch version of the originating PR + elif isinstance(tax_data, list) and len(tax_data) == 3: + updated_tax_details[item] = ItemWiseTaxDetail( + tax_rate=tax_data[0], + tax_amount=tax_data[1], + net_amount=tax_data[2], + ) + needs_update = True elif isinstance(tax_data, str): updated_tax_details[item] = ItemWiseTaxDetail( tax_rate=flt(tax_data), @@ -49,11 +58,11 @@ def execute(): frappe.db.set_value( doctype, doc.name, - "item_wise_tax_details", + "item_wise_tax_detail", json.dumps(updated_tax_details), update_modified=False, ) + migrated_count += 1 # Increment the counter for each migrated record frappe.db.commit() - - print("Migration of old item-wise tax data format completed for all relevant DocTypes.") + print(f"Migrated {migrated_count} records for DocType: {doctype}")