|
| 1 | +# Copyright (c) 2024, Frappe and contributors |
| 2 | +# For license information, please see license.txt |
| 3 | + |
| 4 | +import frappe |
| 5 | +from frappe import _ |
| 6 | +from frappe.custom.doctype.property_setter.property_setter import make_property_setter |
| 7 | +from frappe.model.document import Document |
| 8 | +from frappe.frappeclient import FrappeClient |
| 9 | +from frappe.utils import get_url_to_form |
| 10 | +import json |
| 11 | + |
| 12 | +class ERPNextCRMSettings(Document): |
| 13 | + def validate(self): |
| 14 | + if self.enabled: |
| 15 | + self.validate_if_erpnext_installed() |
| 16 | + self.add_quotation_to_option() |
| 17 | + self.create_custom_fields() |
| 18 | + self.create_crm_form_script() |
| 19 | + |
| 20 | + def validate_if_erpnext_installed(self): |
| 21 | + if self.is_erpnext_in_the_current_site: |
| 22 | + if "erpnext" not in frappe.get_installed_apps(): |
| 23 | + frappe.throw(_("ERPNext is not installed in the current site")) |
| 24 | + |
| 25 | + def add_quotation_to_option(self): |
| 26 | + if self.is_erpnext_in_the_current_site: |
| 27 | + if not frappe.db.exists("Property Setter", {"name": "Quotation-quotation_to-link_filters"}): |
| 28 | + make_property_setter( |
| 29 | + doctype="Quotation", |
| 30 | + fieldname="quotation_to", |
| 31 | + property="link_filters", |
| 32 | + value='[["DocType","name","in", ["Customer", "Lead", "Prospect", "Frappe CRM Deal"]]]', |
| 33 | + property_type="JSON", |
| 34 | + validate_fields_for_doctype=False, |
| 35 | + ) |
| 36 | + |
| 37 | + def create_custom_fields(self): |
| 38 | + if self.is_erpnext_in_the_current_site: |
| 39 | + from erpnext.crm.frappe_crm_api import create_custom_fields_for_frappe_crm |
| 40 | + create_custom_fields_for_frappe_crm() |
| 41 | + else: |
| 42 | + self.create_custom_fields_in_remote_site() |
| 43 | + |
| 44 | + def create_custom_fields_in_remote_site(self): |
| 45 | + client = get_erpnext_site_client(self) |
| 46 | + try: |
| 47 | + client.post_api("erpnext.crm.frappe_crm_api.create_custom_fields_for_frappe_crm") |
| 48 | + except Exception: |
| 49 | + frappe.log_error( |
| 50 | + frappe.get_traceback(), |
| 51 | + f"Error while creating custom field in the remote erpnext site: {self.erpnext_site_url}" |
| 52 | + ) |
| 53 | + frappe.throw("Error while creating custom field in ERPNext, check error log for more details") |
| 54 | + |
| 55 | + def create_crm_form_script(self): |
| 56 | + if not frappe.db.exists("CRM Form Script", "Create Quotation from CRM Deal"): |
| 57 | + script = get_crm_form_script() |
| 58 | + frappe.get_doc({ |
| 59 | + "doctype": "CRM Form Script", |
| 60 | + "name": "Create Quotation from CRM Deal", |
| 61 | + "dt": "CRM Deal", |
| 62 | + "view": "Form", |
| 63 | + "script": script, |
| 64 | + "enabled": 1, |
| 65 | + "is_standard": 1 |
| 66 | + }).insert() |
| 67 | + |
| 68 | +def get_erpnext_site_client(erpnext_crm_settings): |
| 69 | + site_url = erpnext_crm_settings.erpnext_site_url |
| 70 | + api_key = erpnext_crm_settings.api_key |
| 71 | + api_secret = erpnext_crm_settings.api_secret |
| 72 | + |
| 73 | + return FrappeClient( |
| 74 | + site_url, api_key=api_key, api_secret=api_secret |
| 75 | + ) |
| 76 | + |
| 77 | +@frappe.whitelist() |
| 78 | +def get_quotation_url(crm_deal, organization): |
| 79 | + erpnext_crm_settings = frappe.get_single("ERPNext CRM Settings") |
| 80 | + if not erpnext_crm_settings.enabled: |
| 81 | + frappe.throw(_("ERPNext is not integrated with the CRM")) |
| 82 | + |
| 83 | + if erpnext_crm_settings.is_erpnext_in_the_current_site: |
| 84 | + quotation_url = get_url_to_form("Quotation") |
| 85 | + return f"{quotation_url}/new?quotation_to=CRM Deal&crm_deal={crm_deal}&party_name={crm_deal}" |
| 86 | + else: |
| 87 | + site_url = erpnext_crm_settings.get("erpnext_site_url") |
| 88 | + quotation_url = f"{site_url}/app/quotation" |
| 89 | + |
| 90 | + prospect = create_prospect_in_remote_site(crm_deal, erpnext_crm_settings) |
| 91 | + return f"{quotation_url}/new?quotation_to=Prospect&crm_deal={crm_deal}&party_name={prospect}" |
| 92 | + |
| 93 | +def create_prospect_in_remote_site(crm_deal, erpnext_crm_settings): |
| 94 | + try: |
| 95 | + client = get_erpnext_site_client(erpnext_crm_settings) |
| 96 | + doc = frappe.get_doc("CRM Deal", crm_deal) |
| 97 | + contacts = get_contacts(doc) |
| 98 | + return client.post_api("erpnext.crm.frappe_crm_api.create_prospect_against_crm_deal", |
| 99 | + { |
| 100 | + "organization": doc.organization, |
| 101 | + "lead_name": doc.lead_name, |
| 102 | + "no_of_employees": doc.no_of_employees, |
| 103 | + "deal_owner": doc.deal_owner, |
| 104 | + "crm_deal": doc.name, |
| 105 | + "territory": doc.territory, |
| 106 | + "industry": doc.industry, |
| 107 | + "website": doc.website, |
| 108 | + "annual_revenue": doc.annual_revenue, |
| 109 | + "contacts": json.dumps(contacts), |
| 110 | + "erpnext_company": erpnext_crm_settings.erpnext_company |
| 111 | + }, |
| 112 | + ) |
| 113 | + except Exception: |
| 114 | + frappe.log_error( |
| 115 | + frappe.get_traceback(), |
| 116 | + f"Error while creating prospect in remote site: {erpnext_crm_settings.erpnext_site_url}" |
| 117 | + ) |
| 118 | + frappe.throw(_("Error while creating prospect in ERPNext, check error log for more details")) |
| 119 | + |
| 120 | +def get_contacts(doc): |
| 121 | + contacts = [] |
| 122 | + for c in doc.contacts: |
| 123 | + contacts.append({ |
| 124 | + "contact": c.contact, |
| 125 | + "full_name": c.full_name, |
| 126 | + "email": c.email, |
| 127 | + "mobile_no": c.mobile_no, |
| 128 | + "gender": c.gender, |
| 129 | + "is_primary": c.is_primary, |
| 130 | + }) |
| 131 | + return contacts |
| 132 | + |
| 133 | +def create_customer_in_erpnext(doc, method): |
| 134 | + erpnext_crm_settings = frappe.get_single("ERPNext CRM Settings") |
| 135 | + if not erpnext_crm_settings.enabled or doc.status != "Won": |
| 136 | + return |
| 137 | + |
| 138 | + contacts = get_contacts(doc) |
| 139 | + customer = { |
| 140 | + "customer_name": doc.organization, |
| 141 | + "customer_group": "All Customer Groups", |
| 142 | + "customer_type": "Company", |
| 143 | + "territory": doc.territory, |
| 144 | + "default_currency": doc.currency, |
| 145 | + "industry": doc.industry, |
| 146 | + "website": doc.website, |
| 147 | + "crm_deal": doc.name, |
| 148 | + "contacts": json.dumps(contacts), |
| 149 | + } |
| 150 | + if erpnext_crm_settings.is_erpnext_in_the_current_site: |
| 151 | + from erpnext.crm.frappe_crm_api import create_customer |
| 152 | + create_customer(customer) |
| 153 | + else: |
| 154 | + create_customer_in_remote_site(customer, erpnext_crm_settings) |
| 155 | + |
| 156 | +def create_customer_in_remote_site(customer, erpnext_crm_settings): |
| 157 | + client = get_erpnext_site_client(erpnext_crm_settings) |
| 158 | + try: |
| 159 | + client.post_api("erpnext.crm.frappe_crm_api.create_customer", customer) |
| 160 | + except Exception: |
| 161 | + frappe.log_error( |
| 162 | + frappe.get_traceback(), |
| 163 | + "Error while creating customer in remote site" |
| 164 | + ) |
| 165 | + frappe.throw(_("Error while creating customer in ERPNext, check error log for more details")) |
| 166 | + |
| 167 | +def get_crm_form_script(): |
| 168 | + return """ |
| 169 | +function setupForm({ doc, call, $dialog, updateField, createToast }) { |
| 170 | + let actions = []; |
| 171 | + if (!["Lost", "Won"].includes(doc?.status)) { |
| 172 | + actions.push({ |
| 173 | + label: __("Create Quotation"), |
| 174 | + onClick: async () => { |
| 175 | + let quotation_url = await call( |
| 176 | + "crm.fcrm.doctype.erpnext_crm_settings.erpnext_crm_settings.get_quotation_url", |
| 177 | + { |
| 178 | + crm_deal: doc.name, |
| 179 | + organization: doc.organization |
| 180 | + } |
| 181 | + ); |
| 182 | +
|
| 183 | + if (quotation_url) { |
| 184 | + window.open(quotation_url, '_blank'); |
| 185 | + } |
| 186 | + } |
| 187 | + }) |
| 188 | + } |
| 189 | + |
| 190 | + return { |
| 191 | + actions: actions, |
| 192 | + }; |
| 193 | +} |
| 194 | +""" |
0 commit comments