|
@ -1,92 +1,142 @@ |
|
|
|
|
|
import logging |
|
|
|
|
|
import itertools |
|
|
|
|
|
|
|
|
from odoo import models, fields, api, _ |
|
|
from odoo import models, fields, api, _ |
|
|
from odoo.osv.expression import normalize_domain, AND |
|
|
from odoo.osv.expression import normalize_domain, AND |
|
|
|
|
|
from odoo.tools import email_normalize_all |
|
|
|
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model): |
|
|
class ResPartner(models.Model): |
|
|
_inherit = "res.partner" |
|
|
_inherit = "res.partner" |
|
|
|
|
|
|
|
|
tot_sent_survey = fields.Integer("Sent survey count", compute="_count_survey_input") |
|
|
|
|
|
tot_done_survey = fields.Integer( |
|
|
|
|
|
"Completed survey count", compute="_count_survey_input" |
|
|
|
|
|
|
|
|
answer_count = fields.Integer( |
|
|
|
|
|
string="Sent survey count", |
|
|
|
|
|
compute="_compute_answer_count", |
|
|
|
|
|
compute_sudo=True, |
|
|
) |
|
|
) |
|
|
tot_sent_done_survey = fields.Integer( |
|
|
|
|
|
"Completed sent survey count", compute="_count_survey_input" |
|
|
|
|
|
|
|
|
answer_done_count = fields.Integer( |
|
|
|
|
|
string="Completed sent survey count", |
|
|
|
|
|
compute="_compute_answer_count", |
|
|
|
|
|
compute_sudo=True, |
|
|
) |
|
|
) |
|
|
sent_done_ratio = fields.Integer( |
|
|
|
|
|
string="Completed sent survey ratio", compute="_get_sent_done_ratio" |
|
|
|
|
|
|
|
|
answer_done_ratio = fields.Integer( |
|
|
|
|
|
string="Completed sent survey ratio", |
|
|
|
|
|
compute="_compute_answer_count", |
|
|
|
|
|
compute_sudo=True, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
# COMPUTES |
|
|
# COMPUTES |
|
|
|
|
|
|
|
|
def _count_survey_input(self): |
|
|
|
|
|
UserInput = self.env["survey.user_input"] |
|
|
|
|
|
partners_survey = UserInput |
|
|
|
|
|
in_onchange = self.env.context.get("in_onchange", False) |
|
|
|
|
|
origin = in_onchange and self._origin or False |
|
|
|
|
|
if in_onchange: |
|
|
|
|
|
domain = [ |
|
|
|
|
|
("partner_id", "=", self._origin.id), |
|
|
|
|
|
"|", |
|
|
|
|
|
("invite_token", "!=", False), |
|
|
|
|
|
("state", "=", "dones"), |
|
|
|
|
|
] |
|
|
|
|
|
if self.email: |
|
|
|
|
|
domain = ["|", ("email", "=", self.email)] + domain |
|
|
|
|
|
partners_survey = UserInput.search(domain) |
|
|
|
|
|
else: |
|
|
|
|
|
partners_survey = UserInput.search( |
|
|
|
|
|
|
|
|
@api.depends("email") |
|
|
|
|
|
def _compute_answer_count(self): |
|
|
|
|
|
default_vals = { |
|
|
|
|
|
"answer_count": 0, |
|
|
|
|
|
"answer_done_count": 0, |
|
|
|
|
|
"answer_done_ratio": 0, |
|
|
|
|
|
} |
|
|
|
|
|
stats = dict((pid, default_vals) for pid in self.ids) |
|
|
|
|
|
read_group_res = ( |
|
|
|
|
|
self.env["survey.user_input"] |
|
|
|
|
|
.sudo() |
|
|
|
|
|
._read_group( |
|
|
[ |
|
|
[ |
|
|
"|", |
|
|
|
|
|
("partner_id", "in", self.ids), |
|
|
|
|
|
("email", "in", self.filtered("email").mapped("email")), |
|
|
|
|
|
"|", |
|
|
|
|
|
("invite_token", "!=", False), |
|
|
("invite_token", "!=", False), |
|
|
("state", "=", "done"), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
for partner in self: |
|
|
|
|
|
done = partners_survey.filtered( |
|
|
|
|
|
lambda sui: ( |
|
|
|
|
|
sui.partner_id == (origin or partner) |
|
|
|
|
|
or partner.email |
|
|
|
|
|
and sui.email == partner.email |
|
|
|
|
|
) |
|
|
|
|
|
and sui.state == "done" |
|
|
|
|
|
|
|
|
("partner_id", "in", self.ids), |
|
|
|
|
|
], |
|
|
|
|
|
["partner_id", "state"], |
|
|
|
|
|
["partner_id", "state"], |
|
|
|
|
|
lazy=False, |
|
|
) |
|
|
) |
|
|
link = partners_survey.filtered( |
|
|
|
|
|
lambda sui: ( |
|
|
|
|
|
sui.partner_id == (origin or partner) |
|
|
|
|
|
or partner.email |
|
|
|
|
|
and sui.email == partner.email |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_logger.info(read_group_res) |
|
|
|
|
|
for item in read_group_res: |
|
|
|
|
|
stats[item["partner_id"][0]]["answer_count"] += item["__count"] |
|
|
|
|
|
if item["state"] == "done": |
|
|
|
|
|
stats[item["partner_id"][0]]["answer_done_count"] += item["__count"] |
|
|
|
|
|
|
|
|
|
|
|
for partner_stats in stats.values(): |
|
|
|
|
|
partner_stats["answer_done_ratio"] = round( |
|
|
|
|
|
( |
|
|
|
|
|
partner_stats["answer_done_count"] |
|
|
|
|
|
/ (partner_stats["answer_count"] or 1) |
|
|
) |
|
|
) |
|
|
and sui.invite_token |
|
|
|
|
|
|
|
|
* 100, |
|
|
|
|
|
0, |
|
|
) |
|
|
) |
|
|
partner.tot_sent_survey = len(link) |
|
|
|
|
|
partner.tot_done_survey = len(done) |
|
|
|
|
|
partner.tot_sent_done_survey = len(link & done) |
|
|
|
|
|
|
|
|
|
|
|
@api.depends("tot_sent_done_survey", "tot_sent_survey") |
|
|
|
|
|
def _get_sent_done_ratio(self): |
|
|
|
|
|
for survey in self: |
|
|
|
|
|
if survey.tot_sent_survey == 0: |
|
|
|
|
|
survey.sent_done_ratio = 0 |
|
|
|
|
|
else: |
|
|
|
|
|
survey.sent_done_ratio = int( |
|
|
|
|
|
round(100 * survey.tot_sent_done_survey / survey.tot_sent_survey, 0) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for partner in self: |
|
|
|
|
|
partner.update(stats.get(partner._origin.id, default_vals)) |
|
|
|
|
|
|
|
|
|
|
|
# def _count_survey_input(self): |
|
|
|
|
|
# UserInput = self.env["survey.user_input"] |
|
|
|
|
|
# partners_survey = UserInput |
|
|
|
|
|
# in_onchange = self.env.context.get("in_onchange", False) |
|
|
|
|
|
# origin = in_onchange and self._origin or False |
|
|
|
|
|
# if in_onchange: |
|
|
|
|
|
# domain = [ |
|
|
|
|
|
# ("partner_id", "=", self._origin.id), |
|
|
|
|
|
# "|", |
|
|
|
|
|
# ("invite_token", "!=", False), |
|
|
|
|
|
# ("state", "=", "dones"), |
|
|
|
|
|
# ] |
|
|
|
|
|
# if self.email: |
|
|
|
|
|
# domain = ["|", ("email", "=", self.email)] + domain |
|
|
|
|
|
# partners_survey = UserInput.search(domain) |
|
|
|
|
|
# else: |
|
|
|
|
|
# partners_survey = UserInput.search( |
|
|
|
|
|
# [ |
|
|
|
|
|
# "|", |
|
|
|
|
|
# ("partner_id", "in", self.ids), |
|
|
|
|
|
# ("email", "in", self.filtered("email").mapped("email")), |
|
|
|
|
|
# "|", |
|
|
|
|
|
# ("invite_token", "!=", False), |
|
|
|
|
|
# ("state", "=", "done"), |
|
|
|
|
|
# ] |
|
|
|
|
|
# ) |
|
|
|
|
|
# for partner in self: |
|
|
|
|
|
# done = partners_survey.filtered( |
|
|
|
|
|
# lambda sui: ( |
|
|
|
|
|
# sui.partner_id == (origin or partner) |
|
|
|
|
|
# or partner.email |
|
|
|
|
|
# and sui.email == partner.email |
|
|
|
|
|
# ) |
|
|
|
|
|
# and sui.state == "done" |
|
|
|
|
|
# ) |
|
|
|
|
|
# link = partners_survey.filtered( |
|
|
|
|
|
# lambda sui: ( |
|
|
|
|
|
# sui.partner_id == (origin or partner) |
|
|
|
|
|
# or partner.email |
|
|
|
|
|
# and sui.email == partner.email |
|
|
|
|
|
# ) |
|
|
|
|
|
# and sui.invite_token |
|
|
|
|
|
# ) |
|
|
|
|
|
# partner.answer_count = len(link) |
|
|
|
|
|
# partner.answer_done_all = len(done) |
|
|
|
|
|
# partner.answer_done_count = len(link & done) |
|
|
|
|
|
|
|
|
|
|
|
# @api.depends("answer_done_count", "answer_count") |
|
|
|
|
|
# def _get_answer_done_ratio(self): |
|
|
|
|
|
# for survey in self: |
|
|
|
|
|
# if survey.answer_count == 0: |
|
|
|
|
|
# survey.answer_done_ratio = 0 |
|
|
|
|
|
# else: |
|
|
|
|
|
# survey.answer_done_ratio = int( |
|
|
|
|
|
# round(100 * survey.answer_done_count / survey.answer_count, 0) |
|
|
|
|
|
# ) |
|
|
|
|
|
|
|
|
# ACTIONS |
|
|
# ACTIONS |
|
|
|
|
|
|
|
|
def action_survey_user_input(self): |
|
|
def action_survey_user_input(self): |
|
|
self.ensure_one() |
|
|
self.ensure_one() |
|
|
action = self.env.ref("survey.action_survey_user_input").read()[0] |
|
|
|
|
|
|
|
|
action = self.env["ir.actions.act_window"]._for_xml_id( |
|
|
|
|
|
"survey.action_survey_user_input" |
|
|
|
|
|
) |
|
|
action["display_name"] += _(" from {}").format(self.display_name) |
|
|
action["display_name"] += _(" from {}").format(self.display_name) |
|
|
# manage context |
|
|
|
|
|
ctx = dict(self.env.context) |
|
|
|
|
|
link_only = ctx.pop("link_only", False) |
|
|
|
|
|
action["context"] = ctx |
|
|
|
|
|
# manage domain |
|
|
|
|
|
domain = action.get("domain") or [] |
|
|
|
|
|
|
|
|
# manage domain to filter on the good partner |
|
|
|
|
|
domain = action.get("domain") or list() |
|
|
if isinstance(domain, str): |
|
|
if isinstance(domain, str): |
|
|
domain = eval(domain) |
|
|
domain = eval(domain) |
|
|
if len(domain) > 1: |
|
|
if len(domain) > 1: |
|
@ -98,16 +148,6 @@ class ResPartner(models.Model): |
|
|
) |
|
|
) |
|
|
else: |
|
|
else: |
|
|
domain = ["|", ("partner_id", "=", self.id), ("email", "ilike", self.email)] |
|
|
domain = ["|", ("partner_id", "=", self.id), ("email", "ilike", self.email)] |
|
|
if link_only: |
|
|
|
|
|
if len(domain) > 1: |
|
|
|
|
|
domain = AND( |
|
|
|
|
|
[ |
|
|
|
|
|
[("invite_token", "!=", False)], |
|
|
|
|
|
normalize_domain(domain), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
else: |
|
|
|
|
|
domain = [("invite_token", "!=", False)] |
|
|
|
|
|
action["domain"] = domain |
|
|
action["domain"] = domain |
|
|
# return updated action |
|
|
# return updated action |
|
|
return action |
|
|
return action |
|
@ -118,4 +158,4 @@ class ResPartner(models.Model): |
|
|
def onchange_email(self): |
|
|
def onchange_email(self): |
|
|
self.ensure_one() |
|
|
self.ensure_one() |
|
|
if isinstance(self._origin.id, int): |
|
|
if isinstance(self._origin.id, int): |
|
|
self.with_context(in_onchange=True)._count_survey_input() |
|
|
|
|
|
|
|
|
self.with_context(in_onchange=True)._compute_answer_count() |