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.

54 lines
2.4 KiB

  1. from odoo import models, fields, api
  2. class ResPartner(models.Model):
  3. _inherit = 'res.partner'
  4. tot_sent_survey = fields.Integer("Sent survey count", compute="_count_survey_input")
  5. tot_comp_survey = fields.Integer("Completed survey count", compute="_count_survey_input")
  6. tot_sent_comp_survey = fields.Integer("Completed sent survey count", compute="_count_survey_input")
  7. sent_comp_ratio = fields.Integer(string="Completed sent survey ratio", compute="_get_sent_comp_ratio")
  8. # COMPUTES
  9. @api.multi
  10. def _count_survey_input(self):
  11. UserInput = self.env['survey.user_input']
  12. partners_survey = UserInput
  13. in_onchange = self.env.in_onchange
  14. origin = in_onchange and self._origin or False
  15. if in_onchange:
  16. domain = [
  17. ('partner_id', '=', self._origin.id),
  18. '|', ('type', '=', 'link'),
  19. ('state', '=', 'dones'),
  20. ]
  21. if self.email:
  22. domain = ['|', ('email', '=', self.email)] + domain
  23. partners_survey = UserInput.search(domain)
  24. else:
  25. partners_survey = UserInput.search([
  26. '|', ('partner_id', 'in', self.ids),
  27. ('email', 'in', self.filtered('email').mapped('email')),
  28. '|', ('type', '=', 'link'),
  29. ('state', '=', 'done'),
  30. ])
  31. for partner in self:
  32. done = partners_survey.filtered(lambda sui: (sui.partner_id == (origin or partner) or partner.email and sui.email == partner.email) and sui.state == 'done')
  33. link = partners_survey.filtered(lambda sui: (sui.partner_id == (origin or partner) or partner.email and sui.email == partner.email) and sui.type == 'link')
  34. partner.tot_sent_survey = len(link)
  35. partner.tot_comp_survey = len(done)
  36. partner.tot_sent_comp_survey = len(link & done)
  37. @api.depends('tot_sent_comp_survey', 'tot_sent_survey')
  38. def _get_sent_comp_ratio(self):
  39. for survey in self:
  40. if survey.tot_sent_survey == 0:
  41. survey.sent_comp_ratio = 0
  42. else:
  43. survey.sent_comp_ratio = int(round(100 * survey.tot_sent_comp_survey / survey.tot_sent_survey, 0))
  44. @api.onchange('email')
  45. def onchange_email(self):
  46. if isinstance(self._origin.id, int):
  47. self._count_survey_input()