def get_customer(silent=False): """ silent: Return customer if exists else return nothing. Dont throw error. """ user = frappe.session.user contact_name = get_contact_name(user) customer = None if contact_name: contact = frappe.get_doc("Contact", contact_name) for link in contact.links: if link.link_doctype == "Customer": customer = link.link_name break if customer: return frappe.db.get_value("Customer", customer) elif silent: return None else: # should not reach here unless via an API frappe.throw(_( "You are not a verified customer yet. Please contact us to proceed." ), exc=UnverifiedReviewer)
def get_contacts(email_strings: List[str], auto_create_contact=False) -> List[str]: email_addrs = get_emails(email_strings) contacts = [] for email in email_addrs: email = get_email_without_link(email) contact_name = get_contact_name(email) if not contact_name and email and auto_create_contact: email_parts = email.split("@") first_name = frappe.unscrub(email_parts[0]) try: contact_name = ("{0}-{1}".format(first_name, email_parts[1]) if first_name == "Contact" else first_name) contact = frappe.get_doc({ "doctype": "Contact", "first_name": contact_name, "name": contact_name }) contact.add_email(email_id=email, is_primary=True) contact.insert(ignore_permissions=True) contact_name = contact.name except Exception: traceback = frappe.get_traceback() frappe.log_error(traceback) if contact_name: contacts.append(contact_name) return contacts
def get_contacts(email_strings): email_addrs = [] for email_string in email_strings: if email_string: for email in email_string.split(","): parsed_email = parseaddr(email)[1] if parsed_email: email_addrs.append(parsed_email) contacts = [] for email in email_addrs: email = get_email_without_link(email) contact_name = get_contact_name(email) if not contact_name: contact = frappe.get_doc({ "doctype": "Contact", "first_name": frappe.unscrub(email.split("@")[0]), }) contact.add_email(email) contact.insert(ignore_permissions=True) contact_name = contact.name contacts.append(contact_name) return contacts
def get_party(user=None): if not user: user = frappe.session.user contact_name = frappe.db.get_value("Contact", {"user": user}) if not contact_name: contact_name = get_contact_name(user) party = None if contact_name: contact = frappe.get_doc('Contact', contact_name) if contact.links: party_doctype = contact.links[0].link_doctype party = contact.links[0].link_name cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: frappe.local.flags.redirect_location = "/contact" raise frappe.Redirect customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({"first_name": fullname}) contact.add_email(user, is_primary=True) contact.append('links', dict(link_doctype='Customer', link_name=customer.name)) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def test_user_data_creation(self): user_data = json.loads(get_user_data('*****@*****.**')) contact_name = get_contact_name('*****@*****.**') expected_data = { 'Contact': frappe.get_all('Contact', {"name": contact_name}, ["*"]) } expected_data = json.loads(json.dumps(expected_data, default=str)) self.assertEqual({'Contact': user_data['Contact']}, expected_data)
def create_contact(user, ignore_links=False, ignore_mandatory=False): from frappe.contacts.doctype.contact.contact import get_contact_name if user.name in ["Administrator", "Guest"]: return contact_name = get_contact_name(user.email) if not contact_name: contact = frappe.get_doc( { "doctype": "Contact", "first_name": user.first_name, "last_name": user.last_name, "user": user.name, "gender": user.gender, } ) if user.email: contact.add_email(user.email, is_primary=True) if user.phone: contact.add_phone(user.phone, is_primary_phone=True) if user.mobile_no: contact.add_phone(user.mobile_no, is_primary_mobile_no=True) contact.insert( ignore_permissions=True, ignore_links=ignore_links, ignore_mandatory=ignore_mandatory ) else: contact = frappe.get_doc("Contact", contact_name) contact.first_name = user.first_name contact.last_name = user.last_name contact.gender = user.gender # Add mobile number if phone does not exists in contact if user.phone and not any(new_contact.phone == user.phone for new_contact in contact.phone_nos): # Set primary phone if there is no primary phone number contact.add_phone( user.phone, is_primary_phone=not any( new_contact.is_primary_phone == 1 for new_contact in contact.phone_nos ), ) # Add mobile number if mobile does not exists in contact if user.mobile_no and not any( new_contact.phone == user.mobile_no for new_contact in contact.phone_nos ): # Set primary mobile if there is no primary mobile number contact.add_phone( user.mobile_no, is_primary_mobile_no=not any( new_contact.is_primary_mobile_no == 1 for new_contact in contact.phone_nos ), ) contact.save(ignore_permissions=True)
def check_if_user_is_customer(user=None): from frappe.contacts.doctype.contact.contact import get_contact_name if not user: user = frappe.session.user contact_name = get_contact_name(user) customer = None if contact_name: contact = frappe.get_doc('Contact', contact_name) for link in contact.links: if link.link_doctype == "Customer": customer = link.link_name break return True if customer else False
def get_contacts(email_strings): email_addrs = [] for email_string in email_strings: if email_string: for email in email_string.split(","): parsed_email = parseaddr(email)[1] if parsed_email: email_addrs.append(parsed_email) contacts = [] for email in email_addrs: email = get_email_without_link(email) contact_name = get_contact_name(email) if not contact_name and email and cint(frappe.db.get_single_value("System Settings", \ "create_contacts_from_incoming_emails", True)): email_parts = email.split("@") first_name = frappe.unscrub(email_parts[0]) try: contact_name = '{0}-{1}'.format( first_name, email_parts[1]) if first_name == 'Contact' else first_name contact = frappe.get_doc({ "doctype": "Contact", "first_name": contact_name, "name": contact_name }) contact.add_email(email_id=email, is_primary=True) contact.insert(ignore_permissions=True) contact_name = contact.name except Exception: traceback = frappe.get_traceback() frappe.log_error(traceback) if contact_name: contacts.append(contact_name) return contacts
def create_contact(user, ignore_links=False, ignore_mandatory=False): from frappe.contacts.doctype.contact.contact import get_contact_name if user.name in ["Administrator", "Guest"]: return if not get_contact_name(user.email): contact = frappe.get_doc({ "doctype": "Contact", "first_name": user.first_name, "last_name": user.last_name, "user": user.name, "gender": user.gender, }) if user.email: contact.add_email(user.email, is_primary=True) if user.phone: contact.add_phone(user.phone, is_primary_phone=True) if user.mobile_no: contact.add_phone(user.mobile_no, is_primary_mobile_no=True) contact.insert(ignore_permissions=True, ignore_links=ignore_links, ignore_mandatory=ignore_mandatory)
def get_contacts(email_strings): email_addrs = [] for email_string in email_strings: if email_string: result = getaddresses([email_string]) for email in result: email_addrs.append(email[1]) contacts = [] for email in email_addrs: email = get_email_without_link(email) contact_name = get_contact_name(email) if not contact_name and email: email_parts = email.split("@") first_name = frappe.unscrub(email_parts[0]) try: contact_name = '{0}-{1}'.format(first_name, email_parts[1]) if first_name == 'Contact' else first_name contact = frappe.get_doc({ "doctype": "Contact", "first_name": contact_name, "name": contact_name }) contact.add_email(email_id=email, is_primary=True) contact.insert(ignore_permissions=True) contact_name = contact.name except Exception: traceback = frappe.get_traceback() frappe.log_error(traceback) if contact_name: contacts.append(contact_name) return contacts
def test_user_data_creation(self): user_data = json.loads(get_user_data("*****@*****.**")) contact_name = get_contact_name("*****@*****.**") expected_data = {"Contact": frappe.get_all("Contact", {"name": contact_name}, ["*"])} expected_data = json.loads(json.dumps(expected_data, default=str)) self.assertEqual({"Contact": user_data["Contact"]}, expected_data)
def get_party(user=None): if not user: user = frappe.session.user contact_name = get_contact_name(user) party = None # Order For feature. Sets current customer's primary contact for this session. if "order_for" in frappe.session.data: customer_name = frappe.session.data.order_for.get("customer_name") if customer_name and "customer_primary_contact_name" in frappe.session.data.order_for: contact_name = frappe.session.data.order_for.customer_primary_contact_name # Hook: Allows overriding the contact person used by the shopping cart # # Signature: # override_shopping_cart_get_party_contact(contact_name) # # Args: # contact_name: The contact name that will be used to fetch a party # # Returns: # Hook expects a string or None to override the contact_name hooks = frappe.get_hooks("override_shopping_cart_get_party_contact") or [] for method in hooks: contact_name = frappe.call(method, contact_name=contact_name) or contact_name if contact_name: contact = frappe.get_doc('Contact', contact_name) if contact.links: party_doctype, party = _get_customer_or_lead(contact) cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: frappe.local.flags.redirect_location = "/contact" raise frappe.Redirect fullname = get_fullname(user) contact = frappe.db.get_value("Contact Email", {"email_id": user}, "parent") customer = frappe.new_doc("Customer") customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory"), "customer_primary_contact": contact }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.customer_primary_contact = contact customer.flags.ignore_mandatory = True customer.save(ignore_permissions=True) if not contact: contact = frappe.new_doc("Contact") contact.update({ "first_name": fullname, "email_ids": [{ "email_id": user, "is_primary": 1 }] }) else: contact = frappe.get_doc("Contact", contact) contact.append('links', dict(link_doctype='Customer', link_name=customer.name)) contact.flags.ignore_mandatory = True contact.save(ignore_permissions=True) if not customer.customer_primary_contact: customer.customer_primary_contact = contact.name return customer