Skip to Content
Menu
This question has been flagged

I have 2 computed (not stored) monetary fields on my crm.lead model. I want the grouped sums to appear, when I group the leads (based on some other fields on the model).

My XML code has:

<field name="my_deal_share" sum="My Deal Share" widget="monetary" options="{'currency_field': 'company_currency'}" />

<field name="my_profit_share" sum="My Profit Share" widget="monetary" options="{'currency_field': 'company_currency'}" />

I have used the following Python code in my model:

    @api.model

    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):

        _logger.info("Entering read_group method for CrmLead")

        _logger.info(f"Domain: {domain}")

        _logger.info(f"Fields requested: {fields}") # Check if ':sum' is present here

        _logger.info(f"Groupby: {groupby}")


        # Call the original read_group first to get the grouped data

        res = super(CrmLead, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)


        _logger.info(f"Original read_group result (first 2 groups): {res[:2]}")


        # Determine which computed fields need summing

        needs_deal_share_sum = 'my_deal_share:sum' in fields

        needs_profit_share_sum = 'my_profit_share:sum' in fields


        _logger.info(f"Needs Deal Share Sum: {needs_deal_share_sum}")

        _logger.info(f"Needs Profit Share Sum: {needs_profit_share_sum}")


        # Only proceed if at least one of the computed fields' sums is requested

        if needs_deal_share_sum or needs_profit_share_sum:

            for group in res:

                # Get the records for this specific group

                group_domain = domain + (group.get(groupby[0] + '_domain') if groupby else [])

                records_in_group = self.search(group_domain)


                _logger.info(f"Processing group: {group.get(groupby[0])}")

                _logger.info(f"Records in group count: {len(records_in_group)}")


                # Calculate and set the sum for my_deal_share if requested

                if needs_deal_share_sum:

                    computed_my_deal_share_sum = sum(rec.my_deal_share for rec in records_in_group)

                    group['my_deal_share'] = computed_my_deal_share_sum

                    _logger.info(f"Computed my_deal_share_sum: {computed_my_deal_share_sum}")


                # Calculate and set the sum for my_profit_share if requested

                if needs_profit_share_sum:

                    computed_my_profit_share_sum = sum(rec.my_profit_share for rec in records_in_group)

                    group['my_profit_share'] = computed_my_profit_share_sum

                    _logger.info(f"Computed my_profit_share_sum: {computed_my_profit_share_sum}")

            _logger.info(f"Final read_group result (first 2 groups) after custom sums: {res[:2]}")


        return res

When I check logs, I see:

2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Deal Share Sum: False

2025-06-20 08:01:42,196 7188 INFO db3 odoo.addons.custom_crm.models.crm_lead: Needs Profit Share Sum: False

Thus, these grouped sums are not calculated.

How can I resolve this?


Odoo v18 Community Edition

Avatar
Discard
Best Answer

Seems like you're looking for something like this, where My Deal Share and My Profit Share are computed, non-stored fields:


Based on the sample data in my database, result (of the core read_group) for a simple group by (grouped by stage_id only) is

[
{
'stage_id': (1, 'New'),
'stage_id_count': 4,
'expected_revenue': 26664.0,
'probability': 0.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 1)]
}, {
'stage_id': (2, 'Qualified'),
'stage_id_count': 2,
'expected_revenue': 13332.0,
'probability': 47.725,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 2)]
}, {
'stage_id': (3, 'Proposition'),
'stage_id_count': 1,
'expected_revenue': 4444.0,
'probability': 0.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 3)]
}, {
'stage_id': (4, 'Won'),
'stage_id_count': 2,
'expected_revenue': 5555.0,
'probability': 100.0,
'__fold': False,
'__domain': ['&', '&', ('type', '=', 'opportunity'), ('user_id', '=', 2), ('stage_id', '=', 4)]
}
]


Note 1: This is not the group by result of the screenshot, as it would just be to excessive. The principle however is exactly the same.



Therefore, you should need to put your focus on the __domain key of each dictionary in the list only, since each grouping-result provides you with the domain relevant to its records already. Using this domain, you now can simply issue your sum'ming function on the resulting record set.

class CrmLead(models.Model):
_inherit = 'crm.lead'

my_deal_share = fields.Monetary(compute='_compute_my_deal_share')
my_profit_share = fields.Monetary(compute='_compute_my_profit_share')
currency_id = fields.Many2one(related='company_id.currency_id')

def _compute_my_deal_share(self):
for rec in self:
rec.my_deal_share = rec.expected_revenue * 0.5 # or, i.e. a rate based on the currently logged in user, it's team, etc...

def _compute_my_profit_share(self):
for rec in self:
rec.my_profit_share = rec.expected_revenue * 0.1

@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
result = super(CrmLead, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
for group in result:
lead_ids = self.search(group.get('__domain'))
group['my_deal_share'] = sum(lead_ids.mapped('my_deal_share'))
group['my_profit_share'] = sum(lead_ids.mapped('my_profit_share'))
return result


Note 2: Please keep in mind that excessive grouping and or large result sets will affect your database's performance. You may want to consider to the get rid of the search in the loop and try to create a map of all relevant leads that then can be filtered instead.


Note 3: It's easier if you provide an installable example in future...

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 25
549
1
Jun 25
695
1
Feb 25
1236
1
Jul 25
411
3
Jul 25
781