I'm trying to override button_cancel function in purchase order model to apply a validation but the method is not executed.
import logging
from odoo import fields, models, api, _
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class Purchase_Order(models.Model):
_inherit = 'purchase.order'
advance_payment_ids = fields.One2many(
'purchase.order.advance.payment', 'purchase_id', 'Advance payments',
ondelete='cascade', copy=False,
readonly=True, states={'purchase': [('readonly', False)]})
@api.multi
def button_cancel(self):
for order in self:
if order.advance_payment_ids:
raise UserError(_('Unable to cancel purchase order as some advance payments already created.'))
else:
super(Purchase_Order, self).button_cancel()