如何通过Odoo 11表单的操作下拉菜单中的按钮发送电子邮件? [英] How to send an email from a button located in the action dropdown of an Odoo 11 form?

查看:69
本文介绍了如何通过Odoo 11表单的操作下拉菜单中的按钮发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是Odoo的新手.我正在为Odoo 11创建一个自定义模块.我想在hr_payroll模块的hr.payslip中添加一个新链接.因此,当管理员将导航到单个薪水单时,在操作中我想添加一个名为 Email Payslip 的新选项.单击此按钮后,它将向员工发送电子邮件.

为此,我将自定义模块命名为电子邮件工资单.

代码如下:

初始化 .py

from . import models

清单 .py

{
    'name': 'Email Payslip',
    'summary': """This module will send email with payslip""",
    'version': '10.0.1.0.0',
    'description': """This module will send email with payslip""",
    'author': 'Test',
    'company': 'test',
    'website': 'https://test.com',
    'category': 'Tools',
    'depends': ['base'],
    'license': 'AGPL-3',
    'data': [
        'views/email_payslip.xml',
    ],
    'demo': [],
    'installable': True,
    'auto_install': False,
}

模型 init .py

from . import email_payslip

模型email_payslip.py

import babel
from datetime import date, datetime, time
from dateutil.relativedelta import relativedelta
from pytz import timezone

from odoo import api, fields, models, tools, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError, ValidationError

class EmailPayslip(models.Model):
    #print 'sdabhd'
    _name = 'email.payslip'
    name = fields.Char(string="Title", required=True)
    description = 'Email Payslip'

EmailPayslip()

查看email_payslip.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<act_window id="email_payslip" src_model="hr.payslip" res_model="hr.payslip.line"  name="Email Payslip"/>
</odoo>

上面的代码显示了操作中的电子邮件工资单菜单,但是当我单击链接时,它显示了员工工资单记录.

那么有人可以在这里帮助我吗?什么是实现这一目标的正确方法?任何帮助和建议将不胜感激.

这是我到目前为止所得到的:

解决方案

我了解您想在模块hr_payroll创建的模型hr.payslip形式的操作部分中添加按钮. /p>

我看到您正在创建一个名为email.payslip的新模型.不必达到目的,请检查以下步骤:

修改模块的__manifest__.py以使其依赖于hr_payrollmail:

'depends': [
    'hr_payroll',
    'mail',
],

以这种方式修改XML操作:

<record id="action_email_payslip" model="ir.actions.server">
    <field name="name">Email Payslip</field>
    <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="state">code</field>
    <field name="code">
if records:
    action = records.action_email_payslip_send()
    </field>
</record>

这是在模型hr.payslip的视图的操作"部分中创建一个按钮.该按钮将调用此模型的Python方法,该方法负责调用弹出窗口以发送电子邮件.

现在让我们在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,
        }

只需将your_module替换为模块的技术名称.此方法将打开表单以发送电子邮件,并且我们告诉它默认情况下加载我们的自定义电子邮件模板,该模板的XML ID为email_template_payslip.

现在,我们必须以XML定义该电子邮件模板.在模块的根路径中创建一个名为data的新文件夹,并将其放置在XML文件中,例如,名为email_template_data.xml.不要忘记在__manifest__.pydata键中添加行'data/email_template_data.xml',以告诉您的模块它必须加载该XML文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="0">

        <record id="email_template_payslip" model="mail.template">
            <field name="name">Payslip - Send by Email</field>
            <field name="email_from">${(user.email or '')|safe}</field>                
            <field name="subject">${object.company_id.name|safe} Payslip (Ref ${object.name or 'n/a' })</field>
            <field name="email_to">${(object.employee_id.work_email or '')|safe}</field>
            <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
            <field name="auto_delete" eval="True"/>
            <field name="lang">${(object.employee_id.user_id.lang or user.lang)}</field>
            <field name="body_html"><![CDATA[
<div style="font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">

    <p>Hello ${object.employee_id.name},</p>

    <p>Here is your payslip from ${object.company_id.name}: </p>

    <p style="border-left: 1px solid #8e0000; margin-left: 30px;">
       &nbsp;&nbsp;Name: <strong>${object.name}</strong><br />
    </p>
    <p>If you have any question, do not hesitate to contact us.</p>
    <p>Thank you for choosing ${object.company_id.name or 'us'}!</p>
    <br/>
    <br/>
</div>
            ]]></field>
        </record>

</odoo>

ctx变量中,您具有在Python方法中添加的所有数据.在object变量中,当前hr.payslip记录的每个字段.您可以使用点表示法来到达任何关系字段.查看其他电子邮件模板,以了解有关Mako语言的更多信息.

如果您确实要使用模型email.payslip,则应该执行几乎相同的过程(具体取决于您要的是什么),并将hr.payslip引用替换为email.payslip引用.

一旦确定您将不再对电子邮件模板进行任何修改,则可以将noupdate属性设置为1,以使Odoo用户可以从界面自定义电子邮件模板而不会丢失其更改,以防万一.您的模块已更新:

<odoo noupdate="1">
    ...
</odoo>

一旦您看到电子邮件弹出窗口,并且默认情况下模板加载成功,请记住检查以下三个步骤:

  1. 当前工资单记录的员工的工作电子邮件必须填写(因为它是电子邮件的目的地).
  2. 您必须已经配置了外发邮件服务器.
  3. 检查cron任务 Mail:Email Queue Manager .它必须处于活动状态并且每分钟运行一次(如果您最多希望在一分钟内发送电子邮件),或者只需单击手动运行.另外,电子邮件中的参数force_send可以设置为 True ,以便不依赖于cron作业.

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 will navigate to an individual Payslip, in the action I want to add a new option called Email Payslip. When this is clicked, it will send an email to the employee.

So to achieve this I have made my custom module named as email payslip.

The code is like this:

init.py

from . import models

manifest.py

{
    'name': 'Email Payslip',
    'summary': """This module will send email with payslip""",
    'version': '10.0.1.0.0',
    'description': """This module will send email with payslip""",
    'author': 'Test',
    'company': 'test',
    'website': 'https://test.com',
    'category': 'Tools',
    'depends': ['base'],
    'license': 'AGPL-3',
    'data': [
        'views/email_payslip.xml',
    ],
    'demo': [],
    'installable': True,
    'auto_install': False,
}

Models init.py

from . import email_payslip

Models email_payslip.py

import babel
from datetime import date, datetime, time
from dateutil.relativedelta import relativedelta
from pytz import timezone

from odoo import api, fields, models, tools, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError, ValidationError

class EmailPayslip(models.Model):
    #print 'sdabhd'
    _name = 'email.payslip'
    name = fields.Char(string="Title", required=True)
    description = 'Email Payslip'

EmailPayslip()

Views email_payslip.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<act_window id="email_payslip" src_model="hr.payslip" res_model="hr.payslip.line"  name="Email Payslip"/>
</odoo>

The above code shows the email payslip menu in the action but when I am clicking on the link it is showing the employee payslip record.

So can someone help me here? What would be the right approach to achieve this? Any help and suggestions will be really appreciated.

This is what I have got so far:

解决方案

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:

<record id="action_email_payslip" model="ir.actions.server">
    <field name="name">Email Payslip</field>
    <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="state">code</field>
    <field name="code">
if records:
    action = records.action_email_payslip_send()
    </field>
</record>

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:

<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="0">

        <record id="email_template_payslip" model="mail.template">
            <field name="name">Payslip - Send by Email</field>
            <field name="email_from">${(user.email or '')|safe}</field>                
            <field name="subject">${object.company_id.name|safe} Payslip (Ref ${object.name or 'n/a' })</field>
            <field name="email_to">${(object.employee_id.work_email or '')|safe}</field>
            <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
            <field name="auto_delete" eval="True"/>
            <field name="lang">${(object.employee_id.user_id.lang or user.lang)}</field>
            <field name="body_html"><![CDATA[
<div style="font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">

    <p>Hello ${object.employee_id.name},</p>

    <p>Here is your payslip from ${object.company_id.name}: </p>

    <p style="border-left: 1px solid #8e0000; margin-left: 30px;">
       &nbsp;&nbsp;Name: <strong>${object.name}</strong><br />
    </p>
    <p>If you have any question, do not hesitate to contact us.</p>
    <p>Thank you for choosing ${object.company_id.name or 'us'}!</p>
    <br/>
    <br/>
</div>
            ]]></field>
        </record>

</odoo>

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:

<odoo noupdate="1">
    ...
</odoo>

Once you see the email pop-up and the template loaded OK by default, remember to check these three steps:

  1. The work email of the employee of the current payslip record must be filled in (since it is the destination of the email).
  2. You must have configured your outgoing mail server.
  3. Check the cron task Mail: Email Queue Manager. It must be active and running each minute (if you want to send the email in one minute at the most), or just click on Run Manually. Also the parameter force_send could be set to True in the email, in order to not depend on cron jobs.

这篇关于如何通过Odoo 11表单的操作下拉菜单中的按钮发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆