61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
import frappe
|
|
from frappe.utils import today
|
|
from frappe import _
|
|
|
|
@frappe.whitelist()
|
|
def add_lead_to_campaign(lead_name, campaign_name, start_date=None):
|
|
if not frappe.db.exists("Lead", lead_name):
|
|
frappe.throw("Lead does not exist")
|
|
|
|
# Check if there's already a scheduled/in-progress campaign for this Lead with same name
|
|
existing = frappe.db.exists("Email Campaign", {
|
|
"recipient": lead_name,
|
|
"campaign_name": campaign_name,
|
|
"email_campaign_for": "Lead",
|
|
"status": ["in", ["Scheduled", "In Progress"]]
|
|
})
|
|
|
|
if existing:
|
|
frappe.throw("This Lead is already in this Email Campaign")
|
|
|
|
doc = frappe.new_doc("Email Campaign")
|
|
doc.update({
|
|
"campaign_name": campaign_name,
|
|
"email_campaign_for": "Lead",
|
|
"recipient": lead_name,
|
|
"start_date": start_date or today(),
|
|
"sender": frappe.session.user # or replace with a specific default sender
|
|
})
|
|
doc.insert(ignore_permissions=True)
|
|
frappe.db.commit()
|
|
|
|
return {"status": "success", "message": "Email Campaign created"}
|
|
|
|
|
|
@frappe.whitelist()
|
|
def add_lead_to_email_group(lead_name, email_group):
|
|
if not lead_name or not email_group:
|
|
return {"status": "error", "message": _("Missing required parameters.")}
|
|
|
|
lead = frappe.get_doc("Lead", lead_name)
|
|
|
|
if not lead.email_id:
|
|
return {"status": "error", "message": _("Lead does not have an email address.")}
|
|
|
|
# Check if already added
|
|
exists = frappe.db.exists(
|
|
"Email Group Member",
|
|
{"email": lead.email_id, "email_group": email_group}
|
|
)
|
|
if exists:
|
|
return {"status": "error", "message": _("Lead is already in this Email Group.")}
|
|
|
|
# Add to email group
|
|
frappe.get_doc({
|
|
"doctype": "Email Group Member",
|
|
"email_group": email_group,
|
|
"email": lead.email_id,
|
|
"status": "Subscribed"
|
|
}).insert(ignore_permissions=True)
|
|
|
|
return {"status": "success"} |