This question has been flagged
4 Replies
3462 Views

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()


Avatar
Discard
Best Answer

Hi Waleed,

Your code seems to be correct and should be working. I would request you to trace the code as follow:

@api.multi
def button_cancel(self):
for order in self:
            print ("\n\nAAAAAAAAA ", order.advance_payment_ids) # Print it and check if you see any advance payments or not
if order.advance_payment_ids:
raise UserError(_('Unable to cancel purchase order as some advance payments already created.'))
else:
                print ("\n\nBBBBBBBBBBBBBBBBBB::: No Adv Paymnt")
super(Purchase_Order, order).button_cancel() # in super, you should pass order and not self

Try above steps and check that your execution is going in the if statement or in the else.

Also make sure your python (py) file is imported in the __init__.py file.

Let me know what you found.

Avatar
Discard
Author

My code today is working without any changes.. thanks for your input.

Best Answer

Hello @Waleed,

Your procedure looking fine. if you not getting on log then you can try this method on new file or new module i think then it will work.

Avatar
Discard
Author

You are right! My code is working today without any changes.

Best Answer

My opinion:

@api.multi
def button_cancel(self):
if self.advance_payment_ids:
raise UserError(_('Unable to cancel purchase order as some advance payments already created.'))
else:
return super(Purchase_Order, self).button_cancel()
Avatar
Discard
Author

Thanks, i will try you suggestion but I tried to log the error before do a for loop and I got nothing:

@api.multi

def button_cancel(self):

_logger.error("Override button_cancel called")

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()