
from odoo import models


class MailComposeMessage(models.TransientModel):
    _inherit = 'mail.compose.message'

    def send_mail(self, **kwargs):
        for wizard in self:
            if self._context.get('mark_coupon_as_sent') and wizard.model == 'sale.coupon' and wizard.partner_ids:
                self.env[wizard.model].browse(wizard.res_id).state = 'sent'
        return super().send_mail(**kwargs)


from odoo import models


class MailComposeMessage(models.TransientModel):
    _inherit = 'mail.compose.message'

    def send_mail(self, **kwargs):
        for wizard in self:
            if self._context.get('mark_coupon_as_sent') and wizard.model == 'sale.coupon' and wizard.partner_ids:
                # Mark coupon as sent in sudo, as helpdesk users don't have the right to write on coupons
                self.env[wizard.model].sudo().browse(wizard.res_id).state = 'sent'
        return super().send_mail(**kwargs)

Allow helpdesk users to send coupon by email
[IMP] sale_coupon: Allow helpdesk users to send coupon by email

Purpose
=======

Helpdesk users don't have the right to write on a coupon.

When sending a coupon by email, the coupon is marked as 'sent'.

Allow users to send coupons by executing the state change in sudo.

closes odoo/odoo#45091

Taskid: 2179609
Related: odoo/enterprise#8143
Signed-off-by: Yannick Tivisse (yti) <200a91eb0e5cc4726d6a3430713b580138f34298@odoo.com>

  
  from odoo import models
  
  
  class MailComposeMessage(models.TransientModel):
      _inherit = 'mail.compose.message'
  
      def send_mail(self, **kwargs):
          for wizard in self:
              if self._context.get('mark_coupon_as_sent') and wizard.model == 'sale.coupon' and wizard.partner_ids:
+                 # Mark coupon as sent in sudo, as helpdesk users don't have the right to write on coupons
-                 self.env[wizard.model].browse(wizard.res_id).state = 'sent'
+                 self.env[wizard.model].sudo().browse(wizard.res_id).state = 'sent'
          return super().send_mail(**kwargs)
  
Allow helpdesk users to send coupon by email
## Code Before:

from odoo import models


class MailComposeMessage(models.TransientModel):
    _inherit = 'mail.compose.message'

    def send_mail(self, **kwargs):
        for wizard in self:
            if self._context.get('mark_coupon_as_sent') and wizard.model == 'sale.coupon' and wizard.partner_ids:
                self.env[wizard.model].browse(wizard.res_id).state = 'sent'
        return super().send_mail(**kwargs)

## Instruction:
Allow helpdesk users to send coupon by email
## Code After:

from odoo import models


class MailComposeMessage(models.TransientModel):
    _inherit = 'mail.compose.message'

    def send_mail(self, **kwargs):
        for wizard in self:
            if self._context.get('mark_coupon_as_sent') and wizard.model == 'sale.coupon' and wizard.partner_ids:
                # Mark coupon as sent in sudo, as helpdesk users don't have the right to write on coupons
                self.env[wizard.model].sudo().browse(wizard.res_id).state = 'sent'
        return super().send_mail(**kwargs)
