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.

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