Odoo 10 how to check if purchase order "Sent by email" has been completed or not
2 answers
You can do it using the following simple way.
Step 1: Add one boolean field to the PO Model and update the context with the following method.
from odoo import fields,models,api
class purchase_order(models.Model):
_inherit="purchase.order"
sent_po_via_email=fields.Boolean("Sent PO Via Email",default=False,copy=False)
@api.multi
def action_rfq_send(self):
'''
This function opens a window to compose an email, with the edi purchase template message loaded by default
'''
self.ensure_one()
ctx = dict(self.env.context or {})
ir_model_data = self.env['ir.model.data']
try:
if self.env.context.get('send_rfq', False):
template_id = ir_model_data.get_object_reference('purchase', 'email_template_edi_purchase')[1]
else:
ctx.update({'sent_po_via_email':True})
template_id = ir_model_data.get_object_reference('purchase', 'email_template_edi_purchase_done')[1]
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx.update({
'default_model': 'purchase.order',
'default_res_id': self.ids[0],
'default_use_template': bool(template_id),
'default_template_id': template_id,
'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,
}
we override the action_rfq_send method and check if the user is submitting a ref, then update the context ctx.update ({'sent_po_via_email': True}) .
Step 2: Inherit the send_mail method of mail.compose.message.
class MailComposeMessage(models.TransientModel):
_inherit = 'mail.compose.message'
@api.multi
def send_mail(self, auto_commit=False):
context = self._context
if context.get('default_model') == 'purchase.order' and \
context.get('default_res_id') and context.get('sent_po_via_email'):
po_order = self.env['purchase.order'].browse(context['default_res_id'])
po_order.sent_po_via_email = True
return super(MailComposeMessage, self).send_mail(auto_commit=auto_commit)
In the above method, we checked if the user is submitting the PO by email and then check the box True.
We used a simple context to identify the process and based on the context , write the value in order .
This might help you.
+1
source to share