Odoo modules related to surveys
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.

121 lines
4.2 KiB

  1. from odoo import models, fields, api, _
  2. from odoo.osv.expression import normalize_domain, AND
  3. class ResPartner(models.Model):
  4. _inherit = "res.partner"
  5. tot_sent_survey = fields.Integer("Sent survey count", compute="_count_survey_input")
  6. tot_done_survey = fields.Integer(
  7. "Completed survey count", compute="_count_survey_input"
  8. )
  9. tot_sent_done_survey = fields.Integer(
  10. "Completed sent survey count", compute="_count_survey_input"
  11. )
  12. sent_done_ratio = fields.Integer(
  13. string="Completed sent survey ratio", compute="_get_sent_done_ratio"
  14. )
  15. # COMPUTES
  16. def _count_survey_input(self):
  17. UserInput = self.env["survey.user_input"]
  18. partners_survey = UserInput
  19. in_onchange = self.env.context.get("in_onchange", False)
  20. origin = in_onchange and self._origin or False
  21. if in_onchange:
  22. domain = [
  23. ("partner_id", "=", self._origin.id),
  24. "|",
  25. ("invite_token", "not in", [False, None]),
  26. ("state", "=", "dones"),
  27. ]
  28. if self.email:
  29. domain = ["|", ("email", "=", self.email)] + domain
  30. partners_survey = UserInput.search(domain)
  31. else:
  32. partners_survey = UserInput.search(
  33. [
  34. "|",
  35. ("partner_id", "in", self.ids),
  36. ("email", "in", self.filtered("email").mapped("email")),
  37. "|",
  38. ("invite_token", "not in", [False, None]),
  39. ("state", "=", "done"),
  40. ]
  41. )
  42. for partner in self:
  43. done = partners_survey.filtered(
  44. lambda sui: (
  45. sui.partner_id == (origin or partner)
  46. or partner.email
  47. and sui.email == partner.email
  48. )
  49. and sui.state == "done"
  50. )
  51. link = partners_survey.filtered(
  52. lambda sui: (
  53. sui.partner_id == (origin or partner)
  54. or partner.email
  55. and sui.email == partner.email
  56. )
  57. and sui.invite_token
  58. )
  59. partner.tot_sent_survey = len(link)
  60. partner.tot_done_survey = len(done)
  61. partner.tot_sent_done_survey = len(link & done)
  62. @api.depends("tot_sent_done_survey", "tot_sent_survey")
  63. def _get_sent_done_ratio(self):
  64. for survey in self:
  65. if survey.tot_sent_survey == 0:
  66. survey.sent_done_ratio = 0
  67. else:
  68. survey.sent_done_ratio = int(
  69. round(100 * survey.tot_sent_done_survey / survey.tot_sent_survey, 0)
  70. )
  71. # ACTIONS
  72. def action_survey_user_input(self):
  73. self.ensure_one()
  74. action = self.env.ref("survey.action_survey_user_input").read()[0]
  75. action["display_name"] += _(" from {}").format(self.display_name)
  76. # manage context
  77. ctx = dict(self.env.context)
  78. link_only = ctx.pop("link_only", False)
  79. action["context"] = ctx
  80. # manage domain
  81. domain = action.get("domain") or []
  82. if isinstance(domain, str):
  83. domain = eval(domain)
  84. if len(domain) > 1:
  85. domain = AND(
  86. [
  87. ["|", ("partner_id", "=", self.id), ("email", "ilike", self.email)],
  88. normalize_domain(domain),
  89. ]
  90. )
  91. else:
  92. domain = ["|", ("partner_id", "=", self.id), ("email", "ilike", self.email)]
  93. if link_only:
  94. if len(domain) > 1:
  95. domain = AND(
  96. [
  97. [("invite_token", "not in", [False, None])],
  98. normalize_domain(domain),
  99. ]
  100. )
  101. else:
  102. domain = [("invite_token", "not in", [False, None])]
  103. action["domain"] = domain
  104. # return updated action
  105. return action
  106. # ONCHANGES
  107. @api.onchange("email")
  108. def onchange_email(self):
  109. self.ensure_one()
  110. if isinstance(self._origin.id, int):
  111. self.with_context(in_onchange=True)._count_survey_input()