2

In Odoo 11 I want two different action menu with two different functionality.

In the hr payroll I wanted to add email payslip link thats why I used this code to add the email payslip action menu

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <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"> 
            action = records.action_email_payslip_send() 
            </field>
    </record>
    </data>
</odoo>

But this one is adding the action menu in the employees row as well. In the employee rows I want a different action menu. So can some one tell me how to achieve that?

NewUser
  • 12,713
  • 39
  • 142
  • 236
  • I do not understand very well. Do you say that this action (*Email Payslip*) is being shown in `hr.payslip` views and in `hr.employee` views too? – forvas May 13 '19 at 08:47
  • Actually I wanted to send email from the payroll with the selected employees. But the menu what I have added it's sending email for single employee – NewUser May 13 '19 at 08:50
  • I want to show the menu in the hr.payslip form view not in the hr.payslip list view. Hope you understood that. – NewUser May 13 '19 at 09:06
  • @forvas is there any other way where I can send email to the selected employees from the same action button? For now when I am selecting multiple employees and trying to send it showing error like `File "/usr/lib/python3/dist-packages/odoo/models.py", line 4393, in ensure_one raise ValueError("Expected singleton: %s" % self) ValueError: : "Expected singleton: hr.payslip(1, 3, 4)" while evaluating 'action = records.action_email_payslip_send()'` – NewUser May 13 '19 at 09:30
  • Ok, so do you need to make the action button work also for tree view, not only form view, don't you? If you select in the tree view several payslips and click on *Email Payslip*, you want to send an email for each employee of the selected payslip? If you do not want employees to see other employees' payslips in the email, you will have to change all the functionality. – forvas May 13 '19 at 09:43
  • Yes for form view its absolutely fine as it is now. Its sending the email with the attachment for the individual. But in the tree view I want to send their payslip attached i the email and yes no one can see others payslip. Can you share some reference link or any help for that? – NewUser May 13 '19 at 09:53
  • @forvas any help on this? – NewUser May 13 '19 at 10:25
  • So, did the answer finally work for you after the last update? – forvas May 16 '19 at 12:25
  • Is there any possibility where a model would be shown just like we were doing previously when a single payslip is selected. – NewUser May 16 '19 at 12:39
  • Do you mean that you want to show the email compose message pop-up with the template loaded for multiple users? – forvas May 16 '19 at 13:38
  • Nope. I just wanted to to show the pop-up with email compose message when single user has been selected. For the multiple users it is fine what you have answered. – NewUser May 16 '19 at 13:52

1 Answers1

2

Ok, last change and I give up. I hope this is exactly what you want. Using the code of the answer I gave you in How to send an email from a button located in the action dropdown of an Odoo 11 form?, just replace the Python method by this one:

@api.multi
def action_email_payslip_send(self):
    template = self.env.ref(
        'your_module_name.email_template_payslip',
        False,
    )
    compose_form = self.env.ref(
        'mail.email_compose_message_wizard_form',
        False,
    )
    ctx = dict(
        default_model='hr.payslip',
        default_use_template=bool(template),
        default_template_id=template and template.id or False,
    )
    if len(self) == 1:
        ctx.update({
            'default_composition_mode': 'comment',
            'default_res_id': self.ensure_one().id,
        })
    else:
        ctx.update({
            'default_composition_mode': 'mass_mail',
            'active_ids': self.ids,
        })
    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,
    }

This will open you the email compose message pop-up you like even when you are selecting several payslips (in this case the preview will not replace the Mako variables).

I know it is difficult at the beginning, but as @EasyOdoo commented, you have to get ideas from the answers and investigate about it, that way you will be able to make smaller and more accurate questions and get good responses easily.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • This one is showing blank page. Also isn't there any way to make sending individual email with attachment what we were doing when user selects one employee and sends the email? – NewUser May 13 '19 at 10:59
  • any suggestions or reference – NewUser May 14 '19 at 07:37
  • I need time after work to try this method and realise why it returns a blank page as you wrote me. – forvas May 14 '19 at 08:10
  • I have tested the code. Things are working but there is one issue. Lets say I have selected 10 employees. Then it is sending the same email to each user 10 times. – NewUser May 15 '19 at 12:15
  • You mean that this happens only when you have selected 10 payslips of the same employee, in that case he will receive 10 emails, one per payslip. If you select 10 payslips, each one belonging to a different employee, each employee will receive only one email. – forvas May 15 '19 at 12:22
  • Nope. For now I have selected 3 employees from the employee payslip. Each employee have one payslip. But this one is sending 3 emails to each employee, although each employee has only one payslip. – NewUser May 15 '19 at 13:03
  • Hey wait... another thing is when I am going to other employee payslip and trying to send email from there but its sending email to my email id. Is there any relation that I have set my email id in the email configuration. – NewUser May 15 '19 at 13:10
  • A copy-paste error. Replace `self.env.user.id` by `payslip.id` as I did in my new edit. – forvas May 15 '19 at 13:30
  • @NewUser all you have to do is look where the error is an fix it, just fix forvas code he don't have to write a correct code the most important thing is the idea if it's sending 3 for each employee then you need to fix one tiny line in the code to fix this behavior – Charif DZ May 16 '19 at 19:59
  • @NewUser I have modified the whole answer. Take a look and let me know if it was what you were looking for. – forvas May 21 '19 at 10:44
  • @forvas really thank you for this answer. Actually I had already fixed the issue by myself. Only thing I was looking for showing for a confirmation alert when selecting multiple users before sending the email. Once user confirms it will send the emails. let me know if you can help that in this answer or should I ask another question for that? – NewUser May 21 '19 at 11:03
  • 1
    @NewUser that is another different question, as you say you should create a new post asking how to show a confirmation pop-up after clicking in a server action button. I think XML `confirm` attribute only works for ` – forvas May 21 '19 at 11:12
  • 1
    @forvas I have made upvoted and accepted your answer. Thanks for the help. – NewUser May 21 '19 at 11:18
  • @forvas I have an issue . Can you help me out? Here is the url https://stackoverflow.com/questions/56544393/odoo-11-store-attachments-to-other-db – NewUser Jun 11 '19 at 13:04