Odoo添加和现有字段到报告

时间:2018-04-10 11:14:04

标签: python report crm odoo

在Odoo中,OpportunityReport中有一份报告addons/crm/report/crm_opportunity_report.py。我知道它需要来自crm.lead的字段。除了我需要的一个字段外,name也位于crm.lead

我会修改现有的OpportunityReport,如下所示:

Index: odoo/addons/crm/report/crm_opportunity_report.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- odoo/addons/crm/report/crm_opportunity_report.py    (date 1523367742000)
+++ odoo/addons/crm/report/crm_opportunity_report.py    (date 1523367742000)
@@ -13,6 +13,7 @@
     _description = "CRM Opportunity Analysis"
     _rec_name = 'date_deadline'

+    name = fields.Char('Name', readonly=True)
     date_deadline = fields.Date('Expected Closing', readonly=True)
     create_date = fields.Datetime('Creation Date', readonly=True)
     opening_date = fields.Datetime('Assignation Date', readonly=True)
@@ -54,6 +55,7 @@
             CREATE VIEW crm_opportunity_report AS (
                 SELECT
                     c.id,
+                    c.name,
                     c.date_deadline,

                     c.date_open as opening_date,

但是AFAIK我不应该破坏db中关于此报告的现有自定义。我该如何更新该报告?我需要创建一个模块吗?

如何在此报告中添加字段name?我会:首先创建一个自定义报告(带有一个新模块),继承此报告,然后使用该新报告,但在Python中是新手,Odoo不知道如何做到这一点。

我从哪里开始? : - )

1 个答案:

答案 0 :(得分:1)

您需要继承视图表&#39; crm.opportunity.report&#39;

请创建一个新模块并添加crm in depends。

然后在youre .py文件中添加此代码

from odoo import fields, models, tools

from ..models import crm_stage

class OpportunityReport(models.Model):
    """ CRM Opportunity Analysis """

    _inherit = "crm.opportunity.report"


    name = fields.Char('Name', readonly=True)


def init(self):
        tools.drop_view_if_exists(self._cr, 'crm_opportunity_report')
        self._cr.execute("""
            CREATE VIEW crm_opportunity_report AS (
                SELECT
                    c.id,
                    c.date_deadline,
                    c.name as name,

                    c.date_open as opening_date,
                    c.date_closed as date_closed,
                    c.date_last_stage_update as date_last_stage_update,

                    c.user_id,
                    c.probability,
                    c.stage_id,
                    stage.name as stage_name,
                    c.type,
                    c.company_id,
                    c.priority,
                    c.team_id,
                    (SELECT COUNT(*)
                     FROM mail_message m
                     WHERE m.model = 'crm.lead' and m.res_id = c.id) as nbr_activities,
                    c.active,
                    c.campaign_id,
                    c.source_id,
                    c.medium_id,
                    c.partner_id,
                    c.city,
                    c.country_id,
                    c.planned_revenue as total_revenue,
                    c.planned_revenue*(c.probability/100) as expected_revenue,
                    c.create_date as create_date,
                    extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
                    abs(extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24)) as  delay_expected,
                    extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open,
                    c.lost_reason,
                    c.date_conversion as date_conversion
                FROM
                    "crm_lead" c
                LEFT JOIN "crm_stage" stage
                ON stage.id = c.stage_id
                GROUP BY c.id, stage.name
            )""")

如果您有任何疑问,请通知。