Replace sale state validation by domain on origin

issue11304
review361041002
This commit is contained in:
Cédric Krier
2022-03-26 13:05:32 +01:00
parent e2386700a3
commit 26b7564ed3
3 changed files with 17 additions and 35 deletions
+15 -27
View File
@@ -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)
-6
View File
@@ -9,11 +9,5 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_sale_draft_payment">
<field name="text">You cannot reset to draft sale "%(sale)s" because it has payments.</field>
</record>
<record model="ir.message" id="msg_payment_sale_draft">
<field name="text">You cannot use sale "%(sale)s" on payment "%(payment)s" because it is a draft sale.</field>
</record>
<record model="ir.message" id="msg_payment_sale_cancel">
<field name="text">You cannot use sale "%(sale)s" on payment "%(payment)s" because it is a cancelled sale.</field>
</record>
</data>
</tryton>
+2 -2
View File
@@ -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::