Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

119 рядки
3.4 KiB

  1. import frappe
  2. from frappe.utils.nestedset import get_root_of
  3. from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
  4. get_shopping_cart_settings,
  5. )
  6. from erpnext.e_commerce.shopping_cart.cart import get_debtors_account
  7. def set_default_role(doc, method):
  8. """Set customer, supplier, student, guardian based on email"""
  9. if frappe.flags.setting_role or frappe.flags.in_migrate:
  10. return
  11. roles = frappe.get_roles(doc.name)
  12. contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
  13. if contact_name:
  14. contact = frappe.get_doc("Contact", contact_name)
  15. for link in contact.links:
  16. frappe.flags.setting_role = True
  17. if link.link_doctype == "Customer" and "Customer" not in roles:
  18. doc.add_roles("Customer")
  19. elif link.link_doctype == "Supplier" and "Supplier" not in roles:
  20. doc.add_roles("Supplier")
  21. def create_customer_or_supplier():
  22. """Based on the default Role (Customer, Supplier), create a Customer / Supplier.
  23. Called on_session_creation hook.
  24. """
  25. user = frappe.session.user
  26. if frappe.db.get_value("User", user, "user_type") != "Website User":
  27. return
  28. user_roles = frappe.get_roles()
  29. portal_settings = frappe.get_single("Portal Settings")
  30. default_role = portal_settings.default_role
  31. if default_role not in ["Customer", "Supplier"]:
  32. return
  33. # create customer / supplier if the user has that role
  34. if portal_settings.default_role and portal_settings.default_role in user_roles:
  35. doctype = portal_settings.default_role
  36. else:
  37. doctype = None
  38. if not doctype:
  39. return
  40. if party_exists(doctype, user):
  41. return
  42. party = frappe.new_doc(doctype)
  43. fullname = frappe.utils.get_fullname(user)
  44. if doctype == "Customer":
  45. cart_settings = get_shopping_cart_settings()
  46. if cart_settings.enable_checkout:
  47. debtors_account = get_debtors_account(cart_settings)
  48. else:
  49. debtors_account = ""
  50. party.update(
  51. {
  52. "customer_name": fullname,
  53. "customer_type": "Individual",
  54. "customer_group": cart_settings.default_customer_group,
  55. "territory": get_root_of("Territory"),
  56. }
  57. )
  58. if debtors_account:
  59. party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
  60. else:
  61. party.update(
  62. {
  63. "supplier_name": fullname,
  64. "supplier_group": "All Supplier Groups",
  65. "supplier_type": "Individual",
  66. }
  67. )
  68. party.flags.ignore_mandatory = True
  69. party.insert(ignore_permissions=True)
  70. alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
  71. if party_exists(alternate_doctype, user):
  72. # if user is both customer and supplier, alter fullname to avoid contact name duplication
  73. fullname += "-" + doctype
  74. create_party_contact(doctype, fullname, user, party.name)
  75. return party
  76. def create_party_contact(doctype, fullname, user, party_name):
  77. contact = frappe.new_doc("Contact")
  78. contact.update({"first_name": fullname, "email_id": user})
  79. contact.append("links", dict(link_doctype=doctype, link_name=party_name))
  80. contact.append("email_ids", dict(email_id=user))
  81. contact.flags.ignore_mandatory = True
  82. contact.insert(ignore_permissions=True)
  83. def party_exists(doctype, user):
  84. # check if contact exists against party and if it is linked to the doctype
  85. contact_name = frappe.db.get_value("Contact", {"email_id": user})
  86. if contact_name:
  87. contact = frappe.get_doc("Contact", contact_name)
  88. doctypes = [d.link_doctype for d in contact.links]
  89. return doctype in doctypes
  90. return False