blob: c8b03e678bc29274f7ce89ed287668b5ae40d057 [file] [log] [blame]
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05301import frappe
Faris Ansari38ac7f72019-10-09 11:41:33 +05302from frappe.utils.nestedset import get_root_of
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05303
marinationeef9cf12021-02-16 18:45:36 +05304from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
Chillar Anand915b3432021-09-02 16:44:59 +05305 get_shopping_cart_settings,
6)
marination9fb61ef2022-02-01 00:39:14 +05307from erpnext.e_commerce.shopping_cart.cart import get_debtors_account
Chillar Anand915b3432021-09-02 16:44:59 +05308
9
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053010def set_default_role(doc, method):
Ankush Menat494bd9e2022-03-28 18:52:46 +053011 """Set customer, supplier, student, guardian based on email"""
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053012 if frappe.flags.setting_role or frappe.flags.in_migrate:
13 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053014
15 roles = frappe.get_roles(doc.name)
16
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053018 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 contact = frappe.get_doc("Contact", contact_name)
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053020 for link in contact.links:
21 frappe.flags.setting_role = True
Ankush Menat494bd9e2022-03-28 18:52:46 +053022 if link.link_doctype == "Customer" and "Customer" not in roles:
23 doc.add_roles("Customer")
24 elif link.link_doctype == "Supplier" and "Supplier" not in roles:
25 doc.add_roles("Supplier")
Ankush Menat494bd9e2022-03-28 18:52:46 +053026
Faris Ansari38ac7f72019-10-09 11:41:33 +053027
28def create_customer_or_supplier():
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 """Based on the default Role (Customer, Supplier), create a Customer / Supplier.
Faris Ansari38ac7f72019-10-09 11:41:33 +053030 Called on_session_creation hook.
Ankush Menat494bd9e2022-03-28 18:52:46 +053031 """
Faris Ansari38ac7f72019-10-09 11:41:33 +053032 user = frappe.session.user
33
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 if frappe.db.get_value("User", user, "user_type") != "Website User":
Faris Ansari38ac7f72019-10-09 11:41:33 +053035 return
36
37 user_roles = frappe.get_roles()
Ankush Menat494bd9e2022-03-28 18:52:46 +053038 portal_settings = frappe.get_single("Portal Settings")
Faris Ansari38ac7f72019-10-09 11:41:33 +053039 default_role = portal_settings.default_role
40
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 if default_role not in ["Customer", "Supplier"]:
Faris Ansari38ac7f72019-10-09 11:41:33 +053042 return
43
44 # create customer / supplier if the user has that role
45 if portal_settings.default_role and portal_settings.default_role in user_roles:
46 doctype = portal_settings.default_role
47 else:
48 doctype = None
49
50 if not doctype:
51 return
52
53 if party_exists(doctype, user):
54 return
55
56 party = frappe.new_doc(doctype)
57 fullname = frappe.utils.get_fullname(user)
58
Ankush Menat494bd9e2022-03-28 18:52:46 +053059 if doctype == "Customer":
Faris Ansari38ac7f72019-10-09 11:41:33 +053060 cart_settings = get_shopping_cart_settings()
61
62 if cart_settings.enable_checkout:
63 debtors_account = get_debtors_account(cart_settings)
64 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053065 debtors_account = ""
Faris Ansari38ac7f72019-10-09 11:41:33 +053066
Ankush Menat494bd9e2022-03-28 18:52:46 +053067 party.update(
68 {
69 "customer_name": fullname,
70 "customer_type": "Individual",
71 "customer_group": cart_settings.default_customer_group,
72 "territory": get_root_of("Territory"),
73 }
74 )
Faris Ansari38ac7f72019-10-09 11:41:33 +053075
76 if debtors_account:
Ankush Menat494bd9e2022-03-28 18:52:46 +053077 party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
Faris Ansari38ac7f72019-10-09 11:41:33 +053078 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053079 party.update(
80 {
81 "supplier_name": fullname,
82 "supplier_group": "All Supplier Groups",
83 "supplier_type": "Individual",
84 }
85 )
Faris Ansari38ac7f72019-10-09 11:41:33 +053086
87 party.flags.ignore_mandatory = True
88 party.insert(ignore_permissions=True)
89
Marica3994aa82020-07-22 19:49:54 +053090 alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
91
92 if party_exists(alternate_doctype, user):
93 # if user is both customer and supplier, alter fullname to avoid contact name duplication
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 fullname += "-" + doctype
Marica3994aa82020-07-22 19:49:54 +053095
96 create_party_contact(doctype, fullname, user, party.name)
97
98 return party
99
Ankush Menat494bd9e2022-03-28 18:52:46 +0530100
Marica3994aa82020-07-22 19:49:54 +0530101def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +0530102 contact = frappe.new_doc("Contact")
Ankush Menat494bd9e2022-03-28 18:52:46 +0530103 contact.update({"first_name": fullname, "email_id": user})
104 contact.append("links", dict(link_doctype=doctype, link_name=party_name))
kunhi15e3b7f2022-12-13 12:41:56 +0400105 contact.append("email_ids", dict(email_id=user, is_primary=True))
Faris Ansari38ac7f72019-10-09 11:41:33 +0530106 contact.flags.ignore_mandatory = True
107 contact.insert(ignore_permissions=True)
108
Ankush Menat494bd9e2022-03-28 18:52:46 +0530109
Faris Ansari38ac7f72019-10-09 11:41:33 +0530110def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +0530111 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +0530112 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +0530113 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 contact = frappe.get_doc("Contact", contact_name)
Faris Ansari38ac7f72019-10-09 11:41:33 +0530115 doctypes = [d.link_doctype for d in contact.links]
116 return doctype in doctypes
117
118 return False