This question has been flagged
2 Replies
10457 Views

Hi,

I am trying to validate credit limit of customer when click on Confirm button in sale.order view

A warning is showed when self.amount_total > partner.credit_limit * 80%, however it is not block the working flow

Here is my source code

@api.one
def show_credit_warning(self, total, credit_limit):
if total > credit_limit * 0.8:
message = _('Message content')
warning_mess = {
'title': _('Credit limit is over 80%!'),
'message': message
}
return {'warning': warning_mess}
return {}

@api.multi
def action_confirm(self):
self.ensure_one()

# Check credit limit
partner = self.partner_id

if self.amount_total > partner.credit_limit:
# Block when over 100%
raise exceptions.ValidationError(
_('Credit Limit Exceeded! You need to increase the credit limit of this customer to proceed'))
else:
# Show warning when over 80% credit limit
# TODO: Not work, display nothing here
self.show_credit_warning(self.amount_total, partner.credit_limit)

res = super(SaleOrder, self).action_confirm()
return res

Thank you for your help!

Avatar
Discard
Best Answer

The warning does not stop the flow unlike the exceptions. It is just a way to notify the user and once user close the warning, the execution flow will continue and I think warning works with onchange only.

If you want to break / stop the flow, use "exceptions" just like you did for "over 100%" condition.

If you just want to notify the user regarding over 80% limit, you can use the wizard popup for that.

1: Call your own method from the "Confirm" button.

2: Check the credit limit in your method, if it is over 100% then raise exception just like you did it already.

3: If it is over 80%, open a wizard popup (return wizard action from the method) to display the message (write your message in the view of the wizard in label or separator tag).

4: Else, call the action_confirm method of the sales order. Like: self.action_confirm()

I hope this will help you to achieve your goal.

Avatar
Discard
Author

Thank you for your answer!

I will try to solve it and update to you soon.

Best Answer

Hi,

Instead of using warning, use exceptions such as validationerror.



warning_mess = {
'title': _('Credit limit is over 80%!'),
'message': message
}
return {'warning': warning_mess}

Instead of above code, use,
raise exceptions.ValidationError(
_('Credit limit is over 80%')

Thanks
Avatar
Discard
Author

I tried it but it stopped the flow immediately.

immediately means ? the exception is raised while clicking the confirm button right