I am just a newbie in Odoo. I am creating a custom module for Odoo 11. I want to add a new link in hr.payslip in hr_payroll module . So when admin
I have understood that you want to add a button in the actions section of the form of the model hr.payslip, created by the module hr_payroll.
I see that you are creating a new model named email.payslip. That is not necessary to achieve your purpose, check the following steps:
Modify the __manifest__.py of your module to make it depend on hr_payroll and mail:
'depends': [
'hr_payroll',
'mail',
],
Modify your XML action this way:
Email Payslip
code
if records:
action = records.action_email_payslip_send()
This is to create a button in the actions section of the views of the model hr.payslip. The button will call a Python method of this model, which will be in charge of calling the pop-up to send an email.
Now let's define that method in Python:
class HrPayslip(models.Model):
_inherit = 'hr.payslip'
@api.multi
def action_email_payslip_send(self):
self.ensure_one()
template = self.env.ref(
'your_module.email_template_payslip',
False,
)
compose_form = self.env.ref(
'mail.email_compose_message_wizard_form',
False,
)
ctx = dict(
default_model='hr.payslip',
default_res_id=self.id,
default_use_template=bool(template),
default_template_id=template and template.id or False,
default_composition_mode='comment',
)
return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}
Just replace your_module by the technical name of your module. This method will open the form to send an email, and we are telling it to load by default our custom email template, whose XML ID is email_template_payslip.
Now, we have to define that email template in XML. Create a new folder named data in the root path of your module, put inside the XML file, for example, named email_template_data.xml. Do not forget to add in the data key of your __manifest__.py the line 'data/email_template_data.xml', to tell your module that it must load that XML file content:
Payslip - Send by Email
${(user.email or '')|safe}
${object.company_id.name|safe} Payslip (Ref ${object.name or 'n/a' })
${(object.employee_id.work_email or '')|safe}
${(object.employee_id.user_id.lang or user.lang)}
Hello ${object.employee_id.name},
Here is your payslip from ${object.company_id.name}:
Name: ${object.name}
If you have any question, do not hesitate to contact us.
Thank you for choosing ${object.company_id.name or 'us'}!
In the ctx variable you have every data you added in the Python method. In the object variable, every field of the current hr.payslip record. You can use dot notation to reach any relational field. Check out other email templates to learn more about Mako language.
If you definitely want to use your model email.payslip, you should do almost the same process (depending on what you want exactly) and replace the hr.payslip references by email.payslip ones.
Once you are pretty sure that you are making any modification no more on your email template, you can turn the noupdate attribute to 1, to let Odoo users to customise the email template from the interface without losing their changes in case your module is updated:
...
Once you see the email pop-up and the template loaded OK by default, remember to check these three steps:
force_send could be set to True in the email, in order to not depend on cron jobs.