GongYingLian/erpnext/patches/v15_0/migrate_to_utm_analytics.py
zhuangxujie 01bc837fd4
Some checks failed
Server (Postgres) / Build & reinstall (setup) (push) Has been cancelled
Server (Postgres) / Python Unit Tests (1) (push) Has been cancelled
Server (Postgres) / Python Unit Tests (2) (push) Has been cancelled
Server (Postgres) / Python Unit Tests (3) (push) Has been cancelled
Server (Postgres) / Python Unit Tests (4) (push) Has been cancelled
Server (Mariadb) / Build & reinstall (setup) (push) Has been cancelled
Server (Mariadb) / Python Unit Tests (1) (push) Has been cancelled
Server (Mariadb) / Python Unit Tests (2) (push) Has been cancelled
Server (Mariadb) / Python Unit Tests (3) (push) Has been cancelled
Server (Mariadb) / Python Unit Tests (4) (push) Has been cancelled
Server (Mariadb) / Coverage Wrap Up (push) Has been cancelled
Lock threads / lock (push) Has been cancelled
Regenerate POT file (translatable strings) / Regenerate POT file (develop) (push) Has been cancelled
Regenerate POT file (translatable strings) / Regenerate POT file (version-16-hotfix) (push) Has been cancelled
Sync translations to hotfix branches / Trigger sync → version-16-hotfix (push) Has been cancelled
Create weekly release pull requests / Release (15) (push) Has been cancelled
Create weekly release pull requests / Release (16) (push) Has been cancelled
first commit
2026-07-23 16:31:28 +08:00

67 lines
2.0 KiB
Python

import click
import frappe
from frappe.query_builder.functions import Coalesce
from erpnext.setup.install import create_marketing_campaign_custom_fields
def execute():
"""
Remove Lead Source doctype and use UTM Source Instead
Ensure that for each Campaign, a UTM Campaign is also set
"""
if not frappe.db.exists("DocType", "Lead Source") or not frappe.db.exists("DocType", "UTM Source"):
return
ls = frappe.qb.DocType("Lead Source")
ms = frappe.qb.DocType("UTM Source")
# Fetch all Lead Sources
if lead_sources := frappe.qb.from_(ls).select(ls.source_name, ls.details).run(as_dict=True):
# Prepare the insert query with IGNORE
insert_query = frappe.qb.into(ms).ignore().columns(ms.name, ms.description)
# Add values for each Lead Source
for source in lead_sources:
insert_query = insert_query.insert(source.source_name, Coalesce(source.details, ""))
# Execute the insert query
insert_query.run()
frappe.delete_doc("DocType", "Lead Source", ignore_missing=True)
campaign = frappe.qb.DocType("Campaign")
create_marketing_campaign_custom_fields()
marketing_campaign = frappe.qb.DocType("UTM Campaign")
# Fetch all Campaigns
if campaigns := (
frappe.qb.from_(campaign).select(campaign.campaign_name, campaign.description).run(as_dict=True)
):
# Prepare the insert query with IGNORE
insert_query = (
frappe.qb.into(marketing_campaign)
.ignore()
.columns(
marketing_campaign.name,
marketing_campaign.campaign_description,
marketing_campaign.crm_campaign,
)
)
# Add values for each Campaign
for camp in campaigns:
insert_query = insert_query.insert(
camp.campaign_name, Coalesce(camp.description, ""), camp.campaign_name
)
# Execute the insert query
insert_query.run()
click.secho(
f"Inserted {len(lead_sources)} Lead Sources into UTM Sources and deleted Lead Source.\n"
f"Inserted {len(campaigns)} Campaigns into UTM Campaigns.\n"
"You can also make use of the new UTM Medium for analytics, now.",
fg="green",
)