Enable Dark Mode!
how-to-customize-odoo-periodic-digest-parameters-from-the-backend.jpg
By: Cybrosys Technologies

How to Customize Odoo Periodic Digest Parameters from the Backend

Technical

Odoo Periodic Digest Email is a special inbuilt feature in Odoo that sends automated emails to the Organization's personnel about the company's performance. Using this feature, details regarding any modules working in Odoo can be sent as snapshot emails on a daily basis.

The Digest Email can be enabled through General Settings -> Statistics -> Digest Email.

How to Customize Odoo Periodic Digest Parameters from the Backend-cybrosys

There will be a built-in Digest mail template set by Odoo. Click on Configure Digest Emails.

How to Customize Odoo Periodic Digest Parameters from the Backend-cybrosys

There, we can see some preset keys enabled which are called KPI keys (eg:Connected Users, Revenue, All Sales, etc) listed. These KPIs, Periodicity and the Recipients can be configured as per our needs. Based on the settings done in the template, the digest mails will be sent automatically by Odoo. We can add our own keys from an existing or our own custom module using either Odoo Studio or code.

In order to create new KPI keys through backend codes, 

1. First, create the field which is of type Boolean like kpi_fieldname

2. Set up a compute field saying how to calculate the key’s value and name it like  kpi_fieldname_value.

3. Add these fields in the XML template 

4. Select the KPI(s) from the UI of Periodic Digest Email

To illustrate these steps, let's make a custom module sale_commission_digest, which calculates the commission for Salesperson. 

The commission value is saved in the custom compute field sale_commission of the sale.order model. It is calculated here as the 10% of all the confirmed/locked sale order’s total value for every salesperson.

class ResUsers(models.Model):
    """ Added product information to the sale order """
    _inherit = 'res.users'

    sale_commission = fields.Float(string='Commission',
                               	                   compute='_compute_sale_commission')

    def _compute_sale_commission(self):
        """Computing the value as the fixed 10% of total sales done by the salespersons."""
        for user in self:
            user.sale_commission = sum(self.env['sale.order'].search(
                [('state', 'in', ('sale', 'done')), 
                 ('user_id', '=', user.id)]).mapped('amount_total')) * 0.1

We can set up  KPIs from this module based on the sale_commission field’s value.

For that, let's create the python file digest.py in the same module’s models directory, which inherits the model digest.digest. 

Then for the KPI setup, we need a boolean field and a compute field. Boolean field is to add in the Periodic Digest’s configuration. And the compute field’s value will be calculated and  passed when the digest mails are being sent. Let’s define them.

class Digest(models.Model):
    _inherit = 'digest.digest'

    kpi_commission_allowed = fields.Boolean('Commission Allowed')
    kpi_commission_allowed_value = fields.Float(
           compute='_compute_commission_allowed_value')
And define the compute method as the whole sum of commission allowed for all the salespersons. 
def _compute_commission_allowed_value(self):
    users = self.env['res.users'].search([])
    for digest in self:
        digest.kpi_commission_allowed_value = sum(
                                   users.mapped('sale_commission'))

Next step is to add the boolean field in the digest mail setup form view. For that, under views directory, create an xml file which inherits addon digest module’s view. The snippet can be as

<?xml version='1.0' encoding='utf-8'?>
<odoo>
   <record id="digest_digest_view_form" model="ir.ui.view">
       <field name="name">digest.digest.view.form.inherit.commission</field>
       <field name="model">digest.digest</field>
       <field name="priority">30</field>
       <field name="inherit_id" ref="digest.digest_digest_view_form"/>
       <field name="arch" type="xml">
           <xpath expr="//group[@name='kpis']/group[last()]" position="before">
               <group name="kpi_commission" string="Sales Commission">
                   <field name="kpi_commission_allowed"/>
               </group>
           </xpath>
       </field>
   </record>
</odoo>

After upgrading the module, the Periodic digest page can be seen with the update of our custom field inside it, like

How to Customize Odoo Periodic Digest Parameters from the Backend-cybrosys

Just enable the Commission Allowed field from here and click on ‘SEND NOW’ button on top left to trigger the periodic mail sending function manually. 

Then, go to Settings -> Technical -> Emails to see the mail sent. We can see that the mail body will be having details about our newly added sale commission details.

How to Customize Odoo Periodic Digest Parameters from the Backend-cybrosys

Here, we can see the button ‘Open Report’ for some of the KPI Headers. This is because, for every KPI boolean key we define, we can set the kpi_action if needed. This is done using a compute method _compute_kpis_actions() in built in modules. If actions are set through it, the ‘Open Report’ button will be visible for them. If you need to direct to the Odoo page corresponding to the Commission allowed sale order details, we can super the so-called method and define the action id and menu id. 

def _compute_kpis_actions(self, company, user):
    res = super(Digest, self)._compute_kpis_actions(company, user)
    res['kpi_commission_allowed'] = 'sale.action_quotations_with_onboarding&menu_id=%s' %   self.env.ref('sale.menu_sale_quotations').id
    return res

 Here, the IDs of the already existing action and menu is given so that, whenever we click on the button, it will direct to the mentioned view.

In this way, we can use the built in Odoo Periodic Digest configuration and mail with the data from our custom modules. This can leverage Odoo’s Periodic Digest to automate the sending of valuable summaries and updates to your users, helping them stay informed and engaged with the system’s activities.

To read more about How to Configure Periodic Digest Emails in Odoo 17, refer to our blog How to Configure Periodic Digest Emails in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message