diff --git a/account.py b/account.py index f390269cf5..f378d7ffaa 100644 --- a/account.py +++ b/account.py @@ -3,10 +3,9 @@ import functools from decimal import Decimal -from trytond.i18n import gettext from trytond.model import fields -from trytond.modules.account_payment.exceptions import PaymentValidationError from trytond.pool import Pool, PoolMeta +from trytond.pyson import Eval, If def sale_payment_confirm(func): @@ -29,6 +28,20 @@ def sale_payment_confirm(func): class Payment(metaclass=PoolMeta): __name__ = 'account.payment' + @classmethod + def __setup__(cls): + super().__setup__() + cls.origin.domain['sale.sale'] = [ + If(~Eval('state').in_(['failed', 'succeeded']), + ('state', '!=', 'draft'), + ()), + If(Eval('state') == 'draft', + ('state', '!=', 'cancelled'), + ()), + ] + if 'state' not in cls.origin.depends: + cls.origin.depends.append('state') + @classmethod def _get_origin(cls): return super(Payment, cls)._get_origin() + ['sale.sale'] @@ -60,31 +73,6 @@ class Payment(metaclass=PoolMeta): if currency is not None: self.currency = currency - @classmethod - def validate(cls, payments): - pool = Pool() - Sale = pool.get('sale.sale') - super(Payment, cls).validate(payments) - for payment in payments: - if isinstance(payment.origin, Sale): - payment.check_sale_state() - - def check_sale_state(self): - assert isinstance(self.origin, Pool().get('sale.sale')) - if self.state == 'succeeded': - # Do not prevent succeeding payment - return - if self.state != 'failed' and self.origin.state == 'draft': - raise PaymentValidationError( - gettext('sale_payment.msg_payment_sale_draft', - sale=self.origin.rec_name, - payment=self.rec_name)) - elif self.state == 'draft' and self.origin.state == 'cancelled': - raise PaymentValidationError( - gettext('sale_payment.msg_payment_sale_cancel', - sale=self.origin.rec_name, - payment=self.rec_name)) - @classmethod def create(cls, vlist): payments = super(Payment, cls).create(vlist) diff --git a/message.xml b/message.xml index 5080430ffc..a40e70c1c4 100644 --- a/message.xml +++ b/message.xml @@ -9,11 +9,5 @@ this repository contains the full copyright notices and license terms. --> You cannot reset to draft sale "%(sale)s" because it has payments. - - You cannot use sale "%(sale)s" on payment "%(payment)s" because it is a draft sale. - - - You cannot use sale "%(sale)s" on payment "%(payment)s" because it is a cancelled sale. - diff --git a/tests/scenario_sale_payment.rst b/tests/scenario_sale_payment.rst index 7f70015eed..b02f87c0e9 100644 --- a/tests/scenario_sale_payment.rst +++ b/tests/scenario_sale_payment.rst @@ -137,7 +137,7 @@ Attempt to add a second payment to draft sale:: >>> payment.save() # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - PaymentValidationError: ... + DomainValidationError: ... Cancel the sale:: @@ -156,7 +156,7 @@ Attempt to add a second payment to the cancelled sale:: >>> payment.save() # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - PaymentValidationError: ... + DomainValidationError: ... Revive the sale::