From 162f7d1b50c175e85e118f15140b55e039084210 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 1 Jul 2019 01:09:58 +0530 Subject: [PATCH] auto email and new communication linked to email campaign setup --- .../email_campaign/email_campaign.json | 17 ++++++-- .../doctype/email_campaign/email_campaign.py | 42 ++++++++++++++++++- erpnext/hooks.py | 1 + 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/erpnext/crm/doctype/email_campaign/email_campaign.json b/erpnext/crm/doctype/email_campaign/email_campaign.json index 49b3c0643da..d7113f6b4e7 100644 --- a/erpnext/crm/doctype/email_campaign/email_campaign.json +++ b/erpnext/crm/doctype/email_campaign/email_campaign.json @@ -13,7 +13,8 @@ "status", "email_schedule_section", "email_schedule", - "naming_series" + "naming_series", + "amended_from" ], "fields": [ { @@ -42,7 +43,7 @@ "fieldname": "status", "fieldtype": "Select", "label": "Status", - "options": "\nStarted\nIn Progress\nCompleted" + "options": "\nDraft\nSubmitted\nStarted\nIn Progress\nCompleted" }, { "fieldname": "column_break_4", @@ -72,9 +73,19 @@ "label": "Naming Series", "options": "MAIL-CAMP-.YYYY.-", "reqd": 1 + }, + { + "fieldname": "amended_from", + "fieldtype": "Link", + "label": "Amended From", + "no_copy": 1, + "options": "Email Campaign", + "print_hide": 1, + "read_only": 1 } ], - "modified": "2019-06-30 16:23:00.696185", + "is_submittable": 1, + "modified": "2019-06-30 23:00:24.765312", "modified_by": "Administrator", "module": "CRM", "name": "Email Campaign", diff --git a/erpnext/crm/doctype/email_campaign/email_campaign.py b/erpnext/crm/doctype/email_campaign/email_campaign.py index c3e8f9f5593..82ee6a6cb4a 100644 --- a/erpnext/crm/doctype/email_campaign/email_campaign.py +++ b/erpnext/crm/doctype/email_campaign/email_campaign.py @@ -5,12 +5,14 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import getdate, add_days +from frappe.utils import getdate, add_days, nowdate from frappe.model.document import Document +from frappe.email.inbox import link_communication_to_document class EmailCampaign(Document): def validate(self): self.validate_dates() + self.validate_lead() def validate_dates(self): campaign = frappe.get_doc("Campaign", self.campaign_name) @@ -25,4 +27,40 @@ class EmailCampaign(Document): no_of_days += entry.send_after_days email_schedule_end_date = add_days(getdate(self.start_date), no_of_days) if campaign.to_date and getdate(email_schedule_end_date) > getdate(campaign.to_date): - frappe.throw(_("Email Schedule cannot extend Campaign End Date")) + frappe.throw(_("Email Schedule cannot extend Campaign End Date")) + + def validate_lead(self): + lead = frappe.get_doc("Lead", self.lead) + if not lead.get("email_id"): + frappe.throw(_("Please set email id for lead communication")) + + def send(self): + lead = frappe.get_doc("Lead", self.get("lead")) + email_schedule = frappe.get_doc("Campaign Email Schedule", self.get("email_schedule")) + email_template = frappe.get_doc("Email Template", email_schedule.name) + frappe.sendmail( + recipients = lead.get("email_id"), + sender = lead.get("lead_owner"), + subject = email_template.get("subject"), + message = email_template.get("response"), + reference_doctype = self.doctype, + reference_name = self.name + ) + + def on_submit(self): + """Create a new communication linked to the campaign if not created""" + if not frappe.db.sql("select subject from tabCommunication where reference_name = %s", self.name): + doc = frappe.new_doc("Communication") + doc.subject = "Email Campaign Communication: " + self.name + link_communication_to_document(doc, "Email Campaign", self.name, ignore_communication_links = False) + +@frappe.whitelist() +def send_email_to_leads(): + email_campaigns = frappe.get_all("Email Campaign", filters = { 'start_date': ("<=", nowdate()) }) + for campaign in email_campaigns: + email_campaign = frappe.get_doc("Email Campaign", campaign.name) + for entry in email_campaign.get("email_schedule"): + scheduled_date = add_days(email_campaign.get('start_date'), entry.get('send_after_days')) + if(scheduled_date == nowdate()): + email_campaign.send() +# send_email_to_leads() diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 6ce75bbac36..1466d243ad6 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -266,6 +266,7 @@ scheduler_events = { "erpnext.projects.doctype.project.project.send_project_status_email_to_users", "erpnext.quality_management.doctype.quality_review.quality_review.review", "erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status", + "erpnext.crm.doctype.email_campaign.email_campaign.send_email_to_leads" ], "daily_long": [ "erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms"