中国本土应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.8KB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from lxml import etree
  4. from odoo import api, fields, models, _, Command, tools
  5. _logger = logging.getLogger(__name__)
  6. WECOM_USER_MAPPING_ODOO_EMPLOYEE = {
  7. "UserID": "wecom_userid", # 成员UserID
  8. "Name": "name", # 成员名称;
  9. "Department": "department_ids", # 成员部门列表,仅返回该应用有查看权限的部门id
  10. "MainDepartment": "department_id", # 主部门
  11. "IsLeaderInDept": "", # 表示所在部门是否为上级,0-否,1-是,顺序与Department字段的部门逐一对应
  12. "DirectLeader": "", # 直属上级
  13. "Mobile": "mobile_phone", # 手机号码
  14. "Position": "job_title", # 职位信息
  15. "Gender": "gender", # 企微性别:0表示未定义,1表示男性,2表示女性;odoo性别:male为男性,female为女性,other为其他
  16. "Email": "work_email", # 邮箱;
  17. "Status": "active", # 激活状态:1=已激活 2=已禁用 4=未激活 已激活代表已激活企业微信或已关注微工作台(原企业号)5=成员退出
  18. "Avatar": "avatar", # 头像url。注:如果要获取小图将url最后的”/0”改成”/100”即可。
  19. "Alias": "alias", # 成员别名
  20. "Telephone": "work_phone", # 座机;
  21. "Address": "work_location", # 地址;
  22. "ExtAttr": {
  23. "Type": "", # 扩展属性类型: 0-本文 1-网页
  24. "Text": "", # 文本属性类型,扩展属性类型为0时填写
  25. "Value": "", # 文本属性内容
  26. "Web": "", # 网页类型属性,扩展属性类型为1时填写
  27. "Title": "", # 网页的展示标题
  28. "Url": "", # 网页的url
  29. }, # 扩展属性;
  30. }
  31. class HrEmployeePrivate(models.Model):
  32. _inherit = "hr.employee"
  33. _order = "wecom_user_order"
  34. # ----------------------------------------------------------------------------------
  35. # 开发人员注意:hr模块中
  36. # hr.employee.work_email = res.users.email
  37. # hr.employee.private_email = res.partner.email
  38. # ----------------------------------------------------------------------------------
  39. # base 模块中
  40. # res.user.email = res.partner.email
  41. # res.user.private_email = res.partner.email
  42. # ------------------------------------------
  43. # hr.employee.create() 方法中 创建hr.employee.work_email时会将 res.users.email更新到hr.employee.work_email
  44. # res.users.write() 方法中 更新res.users.email时会将 res.users.email更新到hr.employee.work_email
  45. # ------------------------------------------
  46. # 故重写了 将 related='address_home_id.email'去掉,并添加 store 属性
  47. # ----------------------------------------------------------------------------------
  48. # private_email = fields.Char(string="Private Email", groups="hr.group_hr_user",store=True,)
  49. is_wecom_organization = fields.Boolean(
  50. related="company_id.is_wecom_organization", readonly=False
  51. )
  52. wecom_user = fields.Many2one('wecom.user',required=True)
  53. wecom_user_info = fields.Text(string="WeCom user info", readonly=True, default="{}")
  54. wecom_userid = fields.Char(string="WeCom User Id", related="wecom_user.userid",)
  55. wecom_openid = fields.Char(string="WeCom Open Userid", related="wecom_user.open_userid",)
  56. alias = fields.Char(string="Alias", readonly=True,)
  57. english_name = fields.Char(string="English Name", readonly=True,)
  58. department_ids = fields.Many2many(
  59. "hr.department", string="Multiple departments", readonly=True,
  60. )
  61. use_system_avatar = fields.Boolean(readonly=True, default=True)
  62. avatar = fields.Char(string="Avatar")
  63. qr_code = fields.Char(
  64. string="Personal QR code",
  65. readonly=True,
  66. )
  67. wecom_user_order = fields.Char("WeCom user sort", default="0", readonly=True,)
  68. is_wecom_user = fields.Boolean(
  69. string="WeCom employees", readonly=True, default=False,
  70. )
  71. def unbind_wecom_member(self):
  72. """
  73. 解除绑定企业微信成员
  74. """
  75. self.write(
  76. {"is_wecom_user": False, "wecom_userid": None, "qr_code": None,}
  77. )
  78. if self.user_id:
  79. # 关联了User
  80. self.user_id.write(
  81. {"is_wecom_user": False, "wecom_userid": None, "qr_code": None,}
  82. )
  83. # ------------------------------------------------------------
  84. # 从员工生成用户
  85. # ------------------------------------------------------------
  86. def create_user_from_employee(self):
  87. """
  88. 从员工生成用户
  89. :return:
  90. """
  91. send_mail = self.env.context.get("send_mail")
  92. send_message = self.env.context.get("send_message")
  93. if send_mail is None:
  94. send_mail = True
  95. if send_message is None:
  96. send_message = True
  97. for employee in self:
  98. params = {}
  99. if employee.wecom_openid is False:
  100. employee.get_wecom_openid()
  101. try:
  102. res_user_id = self.env["res.users"]._get_or_create_user_by_wecom_userid(
  103. employee, send_mail, send_message
  104. )
  105. except Exception as e:
  106. message = _(
  107. "Failed to copy employee [%s] as system user, reason:%s"
  108. ) % (employee.name, repr(e),)
  109. _logger.warning(message)
  110. params = {
  111. "title": _("Fail"),
  112. "message": message,
  113. "sticky": True, # 延时关闭
  114. "className": "bg-danger",
  115. "type": "danger",
  116. }
  117. else:
  118. message = _("Successfully copied employee [%s] as system user") % (
  119. employee.name
  120. )
  121. params = {
  122. "title": _("Success"),
  123. "message": message,
  124. "sticky": False, # 延时关闭
  125. "className": "bg-success",
  126. "type": "success",
  127. "next": {"type": "ir.actions.client", "tag": "reload",}, # 刷新窗体
  128. }
  129. finally:
  130. action = {
  131. "type": "ir.actions.client",
  132. "tag": "display_notification",
  133. "params": params,
  134. }
  135. return action
  136. # ------------------------------------------------------------
  137. # 从 模型 wecom.user 同步员工
  138. # ------------------------------------------------------------
  139. @api.model
  140. def sync_wecom_user(self):
  141. """
  142. 同步企微成员
  143. """
  144. if self.env.context.get('company_id'):
  145. company = self.env['res.company'].browse(self.env.context['company_id'])
  146. print("1",company)
  147. else:
  148. company = self.env.company
  149. print("2",company)
  150. # if not company:
  151. # company = self.env.company
  152. return {}
上海开阖软件有限公司 沪ICP备12045867号-1