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.

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