Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
3510 Lượt xem

Hi Odoo Users,

I have tried to use try and except block to raise a warning yet it is not displaying the warning. If I do not use try and except block, the popup warning is displayed but the program cannot continue. 

How do I show popup warning message but let the python program continue on create method via try and except block?

Here is my code:

@api.model
def create(self, vals):
message_test = False
credit = 0
debit = 0
if "line_ids" in vals.keys():
for line in vals['line_ids']:
if line[2]['credit']:
account = self.env['account.account'].browse(line[2]['account_id'])
account_lines = self.env['account.move.line'].search([('account_id', '=', account.id)])
for lines in account_lines:
credit += lines.credit
debit += lines.debit
if account.account_credit_limit:
if (credit + line[2]['credit'] - debit) > account.account_credit_limit:
try:
raise Warning(
_('Overdraft Limit will Exceed .! \n Making this transaction will exceed the '
'Overdraft limit ' 'defined for " %s " account') % account.name)
finally:
continue

result = super(AccountMove, self).create(vals)
return result


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

To display a warning popup in Odoo while allowing the program to continue, utilize the UserError exception from odoo.exceptions by checking conditions and raising it accordingly before continuing with the code execution.


For example, use the following code to solve the issue:

from odoo import models, fields, api, _

from odoo.exceptions import UserError


class AccountMove(models.Model):

    _inherit = 'account.move'


    @api.model

    def create(self, vals):

        credit = 0

        debit = 0

        message_test = False

        if "line_ids" in vals.keys():

            for line in vals['line_ids']:

                if line[2]['credit']:

                    account = self.env['account.account'].browse(line[2]['account_id'])

                    account_lines = self.env['account.move.line'].search([('account_id', '=', account.id)])

                    for lines in account_lines:

                        credit += lines.credit

                        debit += lines.debit

                    if account.account_credit_limit:

                        if (credit + line[2]['credit'] - debit) > account.account_credit_limit:

                            message_test = True

                            warning_message = _(

                                'Overdraft Limit will Exceed! \n Making this transaction will exceed the '

                                'Overdraft limit defined for the account "%s".' % account.name

                            )

        # Raise the warning outside the loop

        if message_test:

            raise UserError(warning_message)


        result = super(AccountMove, self).create(vals)

        return result


Hope it helps.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 1 24
1080
0
thg 10 22
68
0
thg 10 22
7
1
thg 1 24
3695
1
thg 9 21
2136