Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

172 строки
4.7 KiB

  1. import frappe
  2. import json
  3. def get_email_accounts(user=None):
  4. if not user:
  5. user = frappe.session.user
  6. email_accounts = []
  7. accounts = frappe.get_all("User Email", filters={ "parent": user },
  8. fields=["email_account", "email_id", "enable_outgoing"],
  9. distinct=True, order_by="idx")
  10. if not accounts:
  11. return {
  12. "email_accounts": [],
  13. "all_accounts": ""
  14. }
  15. all_accounts = ",".join([ account.get("email_account") for account in accounts ])
  16. if len(accounts) > 1:
  17. email_accounts.append({
  18. "email_account": all_accounts,
  19. "email_id": "All Accounts"
  20. })
  21. email_accounts.extend(accounts)
  22. email_accounts.extend([
  23. {
  24. "email_account": "Sent",
  25. "email_id": "Sent Mail"
  26. },
  27. {
  28. "email_account": "Spam",
  29. "email_id": "Spam"
  30. },
  31. {
  32. "email_account": "Trash",
  33. "email_id": "Trash"
  34. }
  35. ])
  36. return {
  37. "email_accounts": email_accounts,
  38. "all_accounts": all_accounts
  39. }
  40. @frappe.whitelist()
  41. def create_email_flag_queue(names, action, flag="(\\Seen)"):
  42. """ create email flag queue to mark email either as read or unread """
  43. class Found(Exception):
  44. pass
  45. if not all([names, action, flag]):
  46. return
  47. for name in json.loads(names or []):
  48. uid, seen_status, email_account = frappe.db.get_value("Communication", name,
  49. ["ifnull(uid, -1)", "ifnull(seen, 0)", "email_account"])
  50. if not uid or uid == -1:
  51. continue
  52. seen = 1 if action == "Read" else 0
  53. # check if states are correct
  54. if (action =='Read' and seen_status == 0) or (action =='Unread' and seen_status == 1):
  55. try:
  56. queue = frappe.db.sql("""select name, action, flag from `tabEmail Flag Queue`
  57. where communication = %(name)s""", {"name":name}, as_dict=True)
  58. for q in queue:
  59. # is same email with same flag
  60. if q.flag == flag:
  61. # to prevent flag local and server states being out of sync
  62. if q.action != action:
  63. frappe.delete_doc("Email Flag Queue", q.name)
  64. raise Found
  65. flag_queue = frappe.get_doc({
  66. "uid": uid,
  67. "flag": flag,
  68. "action": action,
  69. "communication": name,
  70. "doctype": "Email Flag Queue",
  71. "email_account": email_account
  72. })
  73. flag_queue.save(ignore_permissions=True);
  74. frappe.db.set_value("Communication", name, "seen", seen,
  75. update_modified=False)
  76. except Found:
  77. pass
  78. @frappe.whitelist()
  79. def mark_as_trash(communication):
  80. """set email status to trash"""
  81. frappe.db.set_value("Communication", communication, "email_status", "Trash")
  82. @frappe.whitelist()
  83. def mark_as_spam(communication, sender):
  84. """ set email status to spam """
  85. email_rule = frappe.db.get_value("Email Rule", { "email_id": sender })
  86. if not email_rule:
  87. frappe.get_doc({
  88. "doctype": "Email Rule",
  89. "email_id": sender,
  90. "is_spam": 1
  91. }).insert(ignore_permissions=True)
  92. frappe.db.set_value("Communication", communication, "email_status", "Spam")
  93. def link_communication_to_document(doc, reference_doctype, reference_name, ignore_communication_links):
  94. if not ignore_communication_links:
  95. doc.reference_doctype = reference_doctype
  96. doc.reference_name = reference_name
  97. doc.status = "Linked"
  98. doc.save(ignore_permissions=True)
  99. @frappe.whitelist()
  100. def make_issue_from_communication(communication, ignore_communication_links=False):
  101. """ raise a issue from email """
  102. doc = frappe.get_doc("Communication", communication)
  103. issue = frappe.get_doc({
  104. "doctype": "Issue",
  105. "subject": doc.subject,
  106. "raised_by": doc.sender
  107. }).insert(ignore_permissions=True)
  108. link_communication_to_document(doc, "Issue", issue.name, ignore_communication_links)
  109. return issue.name
  110. @frappe.whitelist()
  111. def make_lead_from_communication(communication, ignore_communication_links=False):
  112. """ raise a issue from email """
  113. doc = frappe.get_doc("Communication", communication)
  114. frappe.errprint(doc.sender_full_name)
  115. lead_name = frappe.db.get_value("Lead", {"email_id": doc.sender})
  116. if not lead_name:
  117. lead = frappe.get_doc({
  118. "doctype": "Lead",
  119. "lead_name": doc.sender_full_name,
  120. "email_id": doc.sender
  121. })
  122. lead.flags.ignore_mandatory = True
  123. lead.flags.ignore_permissions = True
  124. lead.insert()
  125. lead_name = lead.name
  126. link_communication_to_document(doc, "Lead", lead_name, ignore_communication_links)
  127. return lead_name
  128. @frappe.whitelist()
  129. def make_opportunity_from_communication(communication, ignore_communication_links=False):
  130. doc = frappe.get_doc("Communication", communication)
  131. lead = doc.reference_name if doc.reference_doctype == "Lead" else None
  132. if not lead:
  133. lead = make_lead_from_communication(communication, ignore_communication_links=True)
  134. enquiry_from = "Lead"
  135. opportunity = frappe.get_doc({
  136. "doctype": "Opportunity",
  137. "enquiry_from": enquiry_from,
  138. "lead": lead
  139. }).insert(ignore_permissions=True)
  140. link_communication_to_document(doc, "Opportunity", opportunity.name, ignore_communication_links)
  141. return opportunity.name