Skip to Content
Menu
This question has been flagged
2 Replies
5552 Views

I want to create a cron job that will customize attendance module such that the employees who forgot to check out will be checked out automatically at a particular time and at the same time an email would be sent stating "you forgot to checkout, we have checked you out".

I have already created a cron job with auto checkout functionality, that is working perfectly fine. Now I want to send an email as well to those employees.

Here is my code.

```python

class hr_attendance(models.Model):
    _inherit = 'hr.attendance'

    def check_for_incomplete_attendances(self):

        not_checkout = self.env['hr.attendance'].search([('check_out', '=', False)]) 

        for rec in not_checkout:

            date_time = (datetime.now() + timedelta(days=0)).strftime('%Y-%m-%d 18:29:59') 

            rec.check_out = date_time

```

```xml

<odoo>

 <data noupdate = "1">

  <record id="ir_cron_module_auto_checkout" model="ir.cron">

   <field name="name">Auto Checkout</field> 

   <field eval="True" name="auto_checkout"/>

   <field name="interval_number">1</field>

   <field name="nextcall" eval="(DateTime.now() + timedelta(days=0)).strftime('%Y-%m-%d 18:29:59')"/>

   <field name="interval_type">days</field> 

   <field name="numbercall">-1</field> 

   <field eval="True" name="doall" /> 

   <field name="model_id" ref="hr_attendance.model_hr_attendance"/>

   <field name="state">code</field> 

   <field name="code">

    model.check_for_incomplete_attendances()

    </field>

  </record>

 </data>

</odoo>

Avatar
Discard
Author Best Answer

hello Waseem ,

Thankyou for the reply, I am unable to comment on your answer, due to less karma. I wanted to know can I create an xml email template and use it through python code. 

I have created a template, Don't know how to include it in python code.

Here is my template.

```xml

<odoo>

 <data noupdate ="1">

  <record id="email_template" model="mail.template">

   <field name="name">Attendance Reminder Email</field>

   <field name="model_id" ref="hr.model_hr_employee"/>

   <field name="auto_delete" eval="True"/>

   <field name="email_from">${(object.company_id.email}</field>

   <field name="email_to">${object.work_email}</field>

   <field name="subject">Attendance Reminder ${object.name}</field>

   <field name="body_html">

    <![CDATA[

    Hello ${object.name},<br/>

     This is reminder that you didn't sign out today.<br/>

     Best regards...<br/>]]>

   </field>

  </record>

 </data>

</odoo>

```

Avatar
Discard
Best Answer

The first thing that you need to add mail dependency in your manifest file. 

'depends': ['mail'],

and add mail.thread to _inherit as below:

_inherit = ['mail.thread']

and add the below code to check_for_incomplete_attendances method:

template_obj = self.env['mail.mail']
template_data = {
'subject': 'Due Invoice Notification : ' + current_date,
'body_html': message_body,
'email_from': sender,
'email_to': ", ".join(recipients)
}
template_id = template_obj.create(template_data)
template_obj.send(template_id)

In above code, you will create mail template by code and then fill all required data like subject,body_html,...etc.

Avatar
Discard
Related Posts Replies Views Activity
1
Nov 23
3497
0
Aug 22
936
0
Aug 22
851
1
Jul 19
3060
1
Jul 15
13913