Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to generate an automatic mail alert to employee if an expirydate is reached, my code is not working?

By
Odoo
on 6/3/15, 12:37 AM 644 views

I have a custom field visa_expiry,emirates_id_expiry, passport_expiry,insurance_id_expiry in hr.employee model .I want to notify the employee  and manager  by an automatic email before 5 days from the expiry_date of   regarding the expiry details .How its possible?need a help My current code is not working and scheduler also,

I am trying to implement the logic in def send_expiry_email(self, cr, uid, ids=None, context=None): this function

class hr_employee(osv.osv):

    

    _inherit = 'hr.employee'

    

    _columns = {

            'passport_expiry':fields.date('Passport expiry'),

                       'visa_expiry':fields.date('Visa Expiry'),

            'emirates_id_expiry':fields.date('Emirates ID Expiry'),

            'join_date':fields.date('Join Date'),

              'insurance_id_expiry':fields.date('Insurance Expiry'),

                    }

    

    def send_expiry_email(self, cr, uid, ids=None, context=None):

        print "Inside Cron Job @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

        day_obj=self.read(cr,uid,ids,['passport_expiry','visa_expiry','emirates_id_expiry','insurance_id_expiry',context=context)

        today = datetime.date.today()

        dateformat="%Y-%m-%d"

        passport_data=datetime.strptime(day_obj[0]['passport_expiry'],dateformat)

        visa_data=datetime.strptime(day_obj[0]['visa_expiry'],dateformat)

        emirates_data=datetime.strptime(day_obj[0]['emirates_id_expiry'],dateformat)

        insurance_data=datetime.strptime(day_obj[0]['insurance_id_expiry'],dateformat)

        

        timedelta = today-passport_data

        diff_passport=timedelta.days

        

        

        timedelta_visa = today-visa_data

        diff_visa=timedelta_visa.days

        

        

        timedelta_emirates = today-emirates_data

        diff_emirates=timedelta_emirates.days

        

        timedelta_insurance = today-insurance_data

        diff_insurance=timedelta_insurance.days

        

        

        

        partner_obj = self.pool.get('hr.employee')

        mail_mail = self.pool.get('mail.mail')

        mail_ids = []

#         today = datetime.datetime.now()    

#         today_month_day = '%-' + today.strftime('%m') + '-' + today.strftime('%d')

        if diff_passport<=5 or diff_visa<=5 or diff_emirates<=5 or diff_insurance<=5:

            par_id = partner_obj.search(cr,uid,[('active','=',True)])

            if par_id!=[]:

                try:

                    for val in partner_obj.browse(cr, uid, par_id):

                        email_from = val.work_email

                        name = val.name                

                        subject = "Expiry Details Alert"

                        body = _("Hello %s,\n" %(name))                 

                        body += _("\tPlease Update Your Details \n")                     

                        footer = _("Kind regards.\n")         

                        footer += _("%s\n\n"%val.company_id.name)

                        mail_ids.append(mail_mail.create(cr, uid, {

                            'email_to': email_from,

                            'subject': subject,

                            'body_html': '<pre><span class="inner-pre" style="font-size: 15px">%s<br>%s</span></pre>' %(body, footer)

                         }, context=context))

                        mail_mail.send(cr, uid, mail_ids, context=context)            

                except Exception, e:

                    print "Exception", e    

        return True


---------------------------------XML---------------------------


<record forcecreate="True" id="ir_cron_employee_expiry_info" model="ir.cron">

            <field name="name">Employee Document Expiry scheduler</field>

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

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

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

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

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

            <field eval="'hr.employee'" name="model"/>

            <field eval="'send_expiry_email'" name="function"/>

            <field eval="'()'" name="args"/>

        </record>

    

    

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/3/15, 12:37 AM
Seen: 644 times
Last updated: 6/4/15, 8:32 AM