3

I have modified crm_opportunity_report :

  • added a boolean field is_customer = fields.Boolean('Is customer', readonly=True)

  • added a field to the view

    CREATE VIEW crm_opportunity_report AS (
        SELECT
            <...>omitted<...>
            rp.customer as is_customer
        FROM
            "crm_lead" c                        
            LEFT JOIN "res_partner" rp ON rp.id = c.partner_id
            <...>omitted<...>                
        GROUP BY c.id, stage.name, is_customer
    

After this in the report when I click "+" and choose "Is customer" sometimes value Undefined appears as a value for it.

Tried this: COALESCE(rp.customer, FALSE) as is_customer (and the same in Group by) but "Undefined" is still present.

How in Odoo report I can make Undefined mean False so that when it's "Undefined" appears "False"?

Note: when debugging with Firefox I can observe that from the server comes (jsonrpc) data with either "is_customer: false" or "is_customer: true" on items of report. But somehow those items that have "is_customer: false" on the report view are displayed as if "is_customer: Undefined".

Update

Somewhat accidentally I stumbled upon this line

if (value === false) return _t("Undefined");

What might be the reason behind this logic: 'if value is false return "Undefined"'?

Funny part, I can add if (value === true) return _t("Whatever"); :)

  • 1
    Is it possible that some leads in your report do not have a `partner_id` and are this undefined instead of `False`? – travisw Apr 23 '18 at 11:11
  • @travisw That is right, some leads have no partner_id and so they cannot have `is customer` (set to true) because "customer(boolean)" field comes from `res_partner`. Anyway those should have False either because of `COALESCE(rp.customer, FALSE)` . And they have... But in report false values appear as "Undefined" . – Developer Marius Žilėnas Apr 23 '18 at 12:07
  • Can you instead use `COALESCE(rp.customer, 'f')`? As you found, Odoo will translate `False` to "Undefined", but it ought to translate `'f'` to "False" since that's how it stores in the database. – travisw Apr 26 '18 at 02:54
  • @travisw Thank You:). I tried and the result was the same. 'f' and FALSE in Postgresql are both **valid literal values for the "*false*" state** same https://www.postgresql.org/docs/9.1/static/datatype-boolean.html :/ . I also tried to change field's is_customer datatype to fields.Char and it didn't give (with 'f') "False" in Odoo either. This is Odoo 10. Are you referring to Odoo 10 :)? – Developer Marius Žilėnas Apr 26 '18 at 04:43
  • @travisw if I tried adding `fields.Char` for `is_customer` and `CASE rp.customer WHEN TRUE THEN 't' WHEN FALSE THEN 'f' ELSE 'f' END as is_customer` to SQL then I get 'f' and 't' in report. But this is not the same because `Char` is not `Boolean` :/ : in Odoo filter in case with `fields.Char` will appear filter for `Char` and not for `Boolean`. – Developer Marius Žilėnas Apr 26 '18 at 04:58
  • Strange enough: but in crm_opportunity_report there is a field `active = fields.Boolean` and this field has filter as if it is boolean. But my field `is_customer = fields.Boolean` ... appears in filter as if its char. I guess it's because field 'active' in report with current data has `true` for each entry in report's data. While in report's data there are entries with `is_customer` values `false` or `true`. – Developer Marius Žilėnas Apr 26 '18 at 05:09
  • It’s not ideal, but you could create a kind of computed copy/dummy field on `res.partner` that stores a `Char` version of the `Boolean` field. Such as `is_customer_string = fields.Char(‘Is a Customer? (text)’, compute=‘_compute_is_customer_string’)` where the compute method just checks if the field is True and stores as `’t’` or `’f’` depending on that. – travisw Apr 26 '18 at 12:47
  • 1
    @travisw Thank you :). – Developer Marius Žilėnas Apr 27 '18 at 04:28

1 Answers1

2

It’s not ideal, but you could create a kind of computed copy/dummy field on res.partner that stores a Char version of the Boolean field.

Take this for example where the compute method just checks if the field is True and stores as 't' or 'f' depending on that.

is_customer_string = fields.Char('Is a Customer? (text)', compute='_compute_is_customer_string', store=True)

@api.multi
@api.depends('is_customer')
def _compute_is_customer_string(self):
    for partner in self:
        partner.is_customer_string = 't' if partner.is_customer else 'f'

Documentation on computed fields

Note: store=True is probably optional, but if you store the field, then you must use some depends field as a trigger to recompute. If you don't store the computed field, then the depends should be left off.

travisw
  • 2,052
  • 1
  • 14
  • 27
  • Ty. :) In case of store=True will the existing records have this field with value or only those that are created? – Developer Marius Žilėnas Apr 27 '18 at 05:55
  • Assuming your `compute` method works properly (no errors) when you install/upgrade your module, then it will apply to all records, old and new. If for some reason you install/upgrade your module and it fails before completion, then it will have the field stored in the database, but without values. In that case, the easiest way to recompute is to (carefully) drop that column (`ALTER TABLE res_partner DROP COLUMN is_customer_string`) and then re-upgrade your module. – travisw Apr 27 '18 at 06:13
  • Thank you:)! I have another question though: in Odoo pivot reports field values are treated as strings?:) – Developer Marius Žilėnas Apr 27 '18 at 06:15
  • I believe so, but I'm not sure I entirely understand your question – travisw Apr 27 '18 at 06:21
  • In Odoo pivot reports is there any difference if I return all values to report as strings (even if they internally are booleans, integers, etc.)? :) – Developer Marius Žilėnas Apr 27 '18 at 06:27
  • 1
    It's definitely possible Odoo might treat them different if returned as strings. I would see it primarily being an issue for integers and floats since those have mathematical operations done on them during manipulation of the pivot table. I would have to try it though to know for sure. – travisw Apr 27 '18 at 06:33