# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
from odoo import models, fields, api
from odoo.exceptions import ValidationError
from odoo.tools.translate import _

class SaleOrder(models.Model):
    _inherit = "sale.order"

    @api.model
    def create(self, vals):
        """Remove customer as a follower
        """
        res = super(SaleOrder, self).create(vals)
        res.message_unsubscribe(partner_ids=[res.partner_id.id])
        return res

    @api.multi
    def write(self, vals):
        """Remove customer as a follower
        """
        context = self.env.context or {}
        for rec in self:
            if 'mail_invite_follower_channel_only' not in context:
                rec.message_unsubscribe(partner_ids=[rec.partner_id.id])
        return super(SaleOrder, self).write(vals)


class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    @api.model
    def create(self, vals):
        """Remove customer as a follower
        """
        res = super(AccountInvoice, self).create(vals)
        res.message_unsubscribe(partner_ids=[res.partner_id.id])
        return res

    @api.multi
    def write(self, vals):
        """Remove customer as a follower
        """
        context = self.env.context or {}
        for rec in self:
            if 'mail_invite_follower_channel_only' not in context:
                rec.message_unsubscribe(partner_ids=[rec.partner_id.id])
        return super(AccountInvoice, self).write(vals)
