This question has been flagged
3 Replies
5837 Views

Right now I'm trying to call  programmatically, from  another  model,  the action 'action_invoice_sent'  that exists in the invoice details  page  as  a  button  called  'Send  &  Print'  (I'm not able to post images).

I call the action as a method: 'invoice_object.action_invoice_sent()' but nothing happens. Does anyone knows how to send invoices programmatically?

Thanks


Avatar
Discard
Best Answer

Hello Joao,

The function action_invoice_sent  return  an  action  in  dictionary  format.  You have to return the result of this function to perform the action.

def  my_action(self):    
    action = invoice_object.action_invoice_sent()
    return  action

Otherwise, if you want to perform sending the Invoice directly (without opening the wizard), so you will have to create account.invoice.send object with the following context values:



def action_send_invoice_directly(self):
    # Search for your invoice_object
    invoice_object = ...

template = self.env.ref('account.email_template_edi_invoice', raise_if_not_found=False)
lang = False

if template:
lang = template._render_lang(invoice_object.ids)[invoice_object.id]

if not lang:
lang = get_lang(self.env).code

ctx = dict(
mark_invoice_as_sent=True,
active_ids=invoice_object.ids, # Use ids and not id (it has to be a list)
custom_layout="mail.mail_notification_paynow",
model_description=invoice_object.with_context(lang=lang).type_name,
force_email=True,
default_res_model='account.move',
default_use_template=bool(template),
)
values = {
'model': 'account.move',
'res_id': invoice_object.id,
'template_id': template and template.id or False,
'composition_mode': 'comment',
}

wizard = self.env['account.invoice.send'].with_context(ctx).create(values)
    wizard._compute_composition_mode()
wizard.onchange_template_id()
wizard.onchange_is_email()

    # If you want to send without printing the invoice use this function

    wizard._send_email()

# If you want to send and print use this function
    # wizard.send_and_print_action()

Hope my answer helps you.

Avatar
Discard
Author

hi Mahamed,
in this case I want it to run without returning it, since it's an automated processing of invoices. Is this possible?

Hey again,
Yes, It is possible but in a different way. In this cas you can't use the function action_invoice_sent because this function will not send the invoice, it will open the Wizard "account.invoice.send" then this wizard has it own function the send the invoice (_send_email). I'll update my response according to your needs.

Author

it makes sense. In that case, how can I send the invoice?

Dear Joao,
I've updated my answer.
Good luck.

Author

wow, that is so great! looked all over the internet to find something like that. Than you so much.

One doubt: what is the "self.ids" in this line?
lang = template._render_lang(self.ids)[self.id]

Author

it's almost working. The invoice is marked as sent, but the email is not actually sent (it's not even on emails list that failed on odoo settings, so it's not an SMTP configuration problem)
I tried to look into the system's 'account.move.send' model and didn't find what could be the possible cause.

Author

found the solution, 3 methods must be called before send:

wizard = self.env['account.invoice.send'].with_context(ctx).create(values)

wizard._compute_composition_mode()
wizard.onchange_template_id()
wizard.onchange_is_email()

# If you want to send without printing the invoice use this function
wizard._send_email()

Author

now, the only thing that is not working is the invoice 'move_to_sent' is not set as true.

Dear Joao,
for this line: lang = template._render_lang(self.ids)[self.id],
I did a mistake and i've updated the code.
_render_lang: Given some record ids, return the lang for each record based on lang field of template or through specific context-based key.
This line is added to ensure that the email will be sent in the correct language.

Good catch Joao!

found the solution, 3 methods must be called before send:

wizard = self.env['account.invoice.send'].with_context(ctx).create(values)

wizard._compute_composition_mode()
wizard.onchange_template_id()
wizard.onchange_is_email()

Dear Joao, could you please add a positif vote on my solution. :)

Author

added a positive vote :)

also could you change your post to reflect the corrections we discussed here? Just to help someone in the future that stumbles upon this post.

Also, in this line 'model_description=self.with_context(lang=lang).type_name,' self is actually invoice_object? At least I had to use it

Also, I had to create a composer instance:
composer = http.request.env['mail.compose.message'].create({
'composition_mode': 'comment'
})

then use it in the values:
values = {
'model': 'account.move',
'is_email': True,
'res_id': invoice_object.id,
'template_id': template and template.id or False,
'composition_mode': 'comment',
'composer_id': composer.id,
'invoice_ids': invoice_object.ids,
}

Thank you for the positive vote :)
Yes, you were right, it should be invoice_object.
Updates are made on my response.
Thank you for your feedback, it was like a teamwork.

Author

Hi Mohamed

thank you so much for your help :) yes it felt like team work. With this, now anyone can implement the same

Hi Mohammad,
The above code fits to my requirements, but the only problem is it is generating 2 emails.
1. from user to customer
2.from user to user itself (which is not required)
Can you please help me ?
I am using odoo 16 enterprise. Thanks in advance

Best Answer

I think that using the wizard (which will generate some data in temp tables) and composer (which is not used anyway) it's a little bit too complex (and actually slower)

I'd use something like this:

def action_send_invoice(self):
# one 'account.move' object
invoice = self.env['account.move'].search([], limit=1)

template = self.env.ref('account.email_template_edi_invoice')

if not(invoice and template):
return False

res = template.send_mail(invoice.id, force_send=True, notif_layout='mail.mail_notification_paynow')

# or:
res = invoice.with_context(force_send=True).message_post_with_template(template.id, email_layout_xmlid='mail.mail_notification_paynow')

if res:
invoice.is_move_sent = True

return res

Attachments and partner language will be automatically taken care of, but you have to manually set the invoice as sent if success

Avatar
Discard
Best Answer

Excellent. I congratulate you, of someone out of shape trying to catch the thread of programming again

Avatar
Discard