Фильтр отчетов о продажах Odoo по категориям партнеров - PullRequest
0 голосов
/ 25 апреля 2018

В Sales -> Reports -> Pipeline Я хочу разрешить фильтрацию по res.partner.category.

В Odoo res.partner есть поле category_id

category_id = fields.Many2many('res.partner.category', column1='partner_id',
                                column2='category_id', string='Tags', default=_default_category)

Я пытался копировать

category_id = fields.Many2many('res.partner.category', column1='partner_id',
                                column2='category_id', string='Tags', default=_default_category)` to my crm_opportunity_report (that has inherited crm.opportunity.report) but I get errors.

Попытка добавления поля

category_ids = fields.Many2many(comodel_name='res.partner.category', relation="res_partner_res_partner_category_rel",
                                column1='category_id', column2='partner_id')

и это тоже не удалось.

Как добавить имя категории в качестве фильтра в crm_opportunity_report? Что можно сделать, чтобы разрешить фильтрацию по категориям?

1 Ответ

0 голосов
/ 25 апреля 2018

Вот несколько решений (на основе обсуждения: см. Комментарии по вопросу). Он создает строку ("'Tagname1'; 'Tagname2'; 'Tagname3'; ...") из имен тегов для фильтрации.

             SELECT                        
                        c.id,
                        c.name as name,
                        c.date_deadline,

                        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,

                        COALESCE(rp.customer, FALSE) as is_customer,
                        COALESCE(x.Categories, '') AS Categories

                    FROM
                        "crm_lead" c

                    LEFT JOIN "res_partner" rp ON rp.id = c.partner_id
                    LEFT JOIN "crm_stage" stage ON stage.id = c.stage_id
                    LEFT JOIN
                    (
SELECT rp.id AS partner_id, array_to_string(array_agg(''''||rpc.name||'''' ORDER BY rp.id, rpc.name),';') AS Categories
  FROM res_partner_res_partner_category_rel rpcl 
  JOIN res_partner_category rpc ON rpc.id = rpcl.category_id
  JOIN res_partner rp ON rp.id = rpcl.partner_id  
  GROUP BY rp.id
  ORDER BY rp.id

  ) AS x ON x.partner_id = c.partner_id

                    GROUP BY c.id, stage.name, COALESCE(rp.customer, FALSE), COALESCE(x.Categories, '')
                    ORDER BY c.partner_id
...