|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import pytz |
| 4 | +from dateutil.relativedelta import relativedelta |
| 5 | + |
| 6 | +from odoo import _, fields, models |
| 7 | +from odoo.exceptions import AccessError |
| 8 | + |
| 9 | + |
| 10 | +class Digest(models.Model): |
| 11 | + _inherit = "digest.digest" |
| 12 | + |
| 13 | + kpi2_sale_activity_report = fields.Boolean("Sale Activity Report") |
| 14 | + |
| 15 | + def _compute_sale_activity_report_value(self, user_id): |
| 16 | + self.ensure_one() |
| 17 | + start, end, company = self._get_kpi_compute_parameters() |
| 18 | + return self.env["mail.activity"].search_count( |
| 19 | + [ |
| 20 | + ("date_deadline", ">=", start), |
| 21 | + ("date_deadline", "<", end), |
| 22 | + ("user_id", "=", user_id), |
| 23 | + ] |
| 24 | + ) |
| 25 | + |
| 26 | + def _compute_sale_activity_report_timeframes(self, company): |
| 27 | + start_datetime = datetime.utcnow() |
| 28 | + tz_name = company.resource_calendar_id.tz |
| 29 | + if tz_name: |
| 30 | + start_datetime = pytz.timezone(tz_name).localize(start_datetime) |
| 31 | + return [ |
| 32 | + ( |
| 33 | + _("Future activities"), |
| 34 | + start_datetime, |
| 35 | + start_datetime + relativedelta(days=1000), |
| 36 | + ), |
| 37 | + ( |
| 38 | + _("Overdue activities"), |
| 39 | + start_datetime + relativedelta(days=-1000), |
| 40 | + start_datetime, |
| 41 | + ), |
| 42 | + ] |
| 43 | + |
| 44 | + def _compute_kpis(self, company, user): |
| 45 | + res = super()._compute_kpis(company, user) |
| 46 | + |
| 47 | + if not self.kpi2_sale_activity_report: |
| 48 | + return res |
| 49 | + |
| 50 | + self.env.cr.execute( |
| 51 | + """ |
| 52 | +SELECT array_agg(DISTINCT user_id) |
| 53 | +FROM mail_activity |
| 54 | +WHERE create_date >= NOW() - INTERVAL '1 YEAR' |
| 55 | + """ |
| 56 | + ) |
| 57 | + |
| 58 | + sales_user_ids = self.env.cr.fetchone()[0] or [] |
| 59 | + |
| 60 | + if not sales_user_ids: |
| 61 | + return res |
| 62 | + |
| 63 | + invalid_fields = [] |
| 64 | + kpis = [ |
| 65 | + dict( |
| 66 | + kpi_name=f"kpi_sale_activity_report_{user_id}", |
| 67 | + kpi_fullname=self.env["res.users"].browse(user_id).name, |
| 68 | + kpi_action=False, |
| 69 | + kpi_col1=dict(), |
| 70 | + kpi_col2=dict(), |
| 71 | + kpi_col3=dict(), |
| 72 | + kpi_user_id=user_id, |
| 73 | + ) |
| 74 | + for user_id in sales_user_ids |
| 75 | + ] |
| 76 | + |
| 77 | + for col_index, (tf_name, start_datetime, end_datetime) in enumerate( |
| 78 | + self._compute_sale_activity_report_timeframes(company) |
| 79 | + ): |
| 80 | + digest = ( |
| 81 | + self.with_context( |
| 82 | + start_datetime=start_datetime, end_datetime=end_datetime |
| 83 | + ) |
| 84 | + .with_user(user) |
| 85 | + .with_company(company) |
| 86 | + ) |
| 87 | + for index, user_id in enumerate(sales_user_ids): |
| 88 | + kpi_values = kpis[index] |
| 89 | + kpi_values[ |
| 90 | + "kpi_action" |
| 91 | + ] = f"bacula2_crm.user_mail_activity_action&active_id={user_id}" |
| 92 | + try: |
| 93 | + compute_value = digest._compute_sale_activity_report_value(user_id) |
| 94 | + except AccessError: |
| 95 | + invalid_fields.append(user_id) |
| 96 | + continue |
| 97 | + kpi_values["kpi_col%s" % (col_index + 1)].update( |
| 98 | + { |
| 99 | + "value": compute_value, |
| 100 | + "margin": 0, |
| 101 | + "col_subtitle": tf_name, |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + return res + [kpi for kpi in kpis if kpi["kpi_user_id"] not in invalid_fields] |
0 commit comments