diff --git a/CHANGELOG b/CHANGELOG index 9591b49f9c..fe7112e5b1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Use reference field for statement line relation * Add support for Python 3.10 * Remove support for Python 3.6 diff --git a/statement.py b/statement.py index bb72e7f724..01c666e578 100644 --- a/statement.py +++ b/statement.py @@ -4,7 +4,7 @@ from collections import defaultdict from trytond.model import fields from trytond.pool import Pool, PoolMeta -from trytond.pyson import Bool, Eval, If +from trytond.pyson import Eval from trytond.transaction import Transaction @@ -70,83 +70,58 @@ class Statement(metaclass=PoolMeta): class StatementLine(metaclass=PoolMeta): __name__ = 'account.statement.line' - payment = fields.Many2One('account.payment', 'Payment', - domain=[ - ('company', '=', Eval('company', -1)), - If(Bool(Eval('party')), [('party', '=', Eval('party'))], []), - ('state', 'in', ['processing', 'succeeded', 'failed']), - ('currency', '=', Eval('currency', -1)), - ('kind', '=', - If(Eval('amount', 0) > 0, 'receivable', - If(Eval('amount', 0) < 0, 'payable', ''))), - ], - states={ - 'invisible': Bool(Eval('payment_group')) | Bool(Eval('invoice')), - 'readonly': Eval('statement_state') != 'draft', - }, - search_order=[('amount', 'ASC'), ('state', 'ASC')], - search_context={ - 'amount_order': Eval('amount', 0), - }, - depends=['company', 'party', 'currency', 'amount', 'statement_state']) - payment_group = fields.Many2One( - 'account.payment.group', "Payment Group", - domain=[ - ('company', '=', Eval('company', -1)), - ('currency', '=', Eval('currency', -1)), - ], - states={ - 'invisible': Bool(Eval('payment')) | Bool(Eval('invoice')), - 'readonly': Eval('statement_state') != 'draft', - }, - depends=['company', 'currency', 'statement_state']) @classmethod def __setup__(cls): super(StatementLine, cls).__setup__() - invoice_invisible = Bool(Eval('payment')) | Bool(Eval('payment_group')) - if 'invisible' in cls.invoice.states: - cls.invoice.states['invisible'] |= invoice_invisible - else: - cls.invoice.states['invisible'] = invoice_invisible + cls.related_to.domain['account.payment.group'] = [ + ('company', '=', Eval('company', -1)), + ('currency', '=', Eval('currency', -1)) + ] + for fname in ['company', 'currency']: + if fname not in cls.related_to.depends: + cls.related_to.depends.append(fname) @classmethod - def copy(cls, lines, default=None): - if default is None: - default = {} - else: - default = default.copy() - default.setdefault('payment', None) - default.setdefault('payment_group', None) - return super(StatementLine, cls).copy(lines, default=default) + def _get_relations(cls): + return super()._get_relations() + ['account.payment.group'] - @fields.depends('payment', 'party', 'account', - 'statement', '_parent_statement.journal') - def on_change_payment(self): + @property + @fields.depends('related_to') + def payment_group(self): + pool = Pool() + PaymentGroup = pool.get('account.payment.group') + related_to = getattr(self, 'related_to', None) + if isinstance(related_to, PaymentGroup) and related_to.id >= 0: + return related_to + + @payment_group.setter + def payment_group(self, value): + self.related_to = value + + @fields.depends(methods=['payment', 'payment_group']) + def on_change_related_to(self): + super().on_change_related_to() if self.payment: - if not self.party: - self.party = self.payment.party clearing_account = self.payment.journal.clearing_account - if clearing_account and self.account != clearing_account: + if clearing_account: self.account = clearing_account - - @fields.depends('payment_group', 'account') - def on_change_payment_group(self): if self.payment_group: self.party = None clearing_account = self.payment_group.journal.clearing_account - if clearing_account and self.account != clearing_account: + if clearing_account: self.account = clearing_account - @fields.depends('party', 'payment') + @fields.depends('party', methods=['payment']) def on_change_party(self): super(StatementLine, self).on_change_party() if self.payment: if self.payment.party != self.party: self.payment = None - self.payment_group = None + if self.party: + self.payment_group = None - @fields.depends('account', 'payment', 'payment_group') + @fields.depends('account', methods=['payment', 'payment_group']) def on_change_account(self): super(StatementLine, self).on_change_account() if self.payment: diff --git a/statement.xml b/statement.xml deleted file mode 100644 index eb1532c320..0000000000 --- a/statement.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - account.statement.line - - statement_line_form - - - account.statement.line - - statement_line_tree - - - account.statement.line - - statement_line_tree - - - diff --git a/tests/scenario_account_negative_payment_clearing.rst b/tests/scenario_account_negative_payment_clearing.rst index 3e124e31e3..fc3b718219 100644 --- a/tests/scenario_account_negative_payment_clearing.rst +++ b/tests/scenario_account_negative_payment_clearing.rst @@ -12,13 +12,11 @@ Imports:: ... get_company >>> from trytond.modules.account.tests.tools import create_fiscalyear, \ ... create_chart, get_accounts - >>> from trytond.modules.account_invoice.tests.tools import \ - ... set_fiscalyear_invoice_sequences >>> today = datetime.date.today() Activate modules:: - >>> config = activate_modules(['account_payment_clearing', 'account_statement']) + >>> config = activate_modules(['account_payment_clearing']) Create company:: @@ -27,8 +25,7 @@ Create company:: Create fiscal year:: - >>> fiscalyear = set_fiscalyear_invoice_sequences( - ... create_fiscalyear(company)) + >>> fiscalyear = create_fiscalyear(company) >>> fiscalyear.click('create_period') Create chart of accounts:: diff --git a/tests/scenario_account_payment_clearing.rst b/tests/scenario_account_payment_clearing.rst index 6fe7532c3b..007209a14e 100644 --- a/tests/scenario_account_payment_clearing.rst +++ b/tests/scenario_account_payment_clearing.rst @@ -248,7 +248,7 @@ Create a line for the payment:: >>> line = statement.lines.new(date=today) >>> line.amount = Decimal('-50.00') - >>> line.payment = payment + >>> line.related_to = payment >>> line.party == supplier True >>> line.account == bank_clearing @@ -257,17 +257,17 @@ Create a line for the payment:: Remove the party must remove payment:: >>> line.party = None - >>> line.payment + >>> line.related_to - >>> line.payment = payment + >>> line.related_to = payment Change account must remove payment:: >>> line.account = receivable - >>> line.payment + >>> line.related_to >>> line.account = None - >>> line.payment = payment + >>> line.related_to = payment Validate statement:: @@ -291,7 +291,7 @@ Create a statement that reimburse the payment group:: ... end_balance=Decimal('0.00'), ... ) >>> line = statement.lines.new(date=today) - >>> line.payment_group = payment.group + >>> line.related_to = payment.group >>> line.account == bank_clearing True >>> line.amount = Decimal('50.00') @@ -436,7 +436,7 @@ Create statement for the payment:: ... end_balance=Decimal('-50.00')) >>> line = statement.lines.new(date=yesterday) >>> line.amount = Decimal('-50.00') - >>> line.payment = payment + >>> line.related_to = payment >>> line.party == supplier True >>> line.account == bank_clearing diff --git a/tryton.cfg b/tryton.cfg index a2202561eb..018fd20aa5 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -8,4 +8,3 @@ extras_depend: account_statement_rule xml: payment.xml - statement.xml diff --git a/view/statement_line_form.xml b/view/statement_line_form.xml deleted file mode 100644 index 1e18e17c58..0000000000 --- a/view/statement_line_form.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - diff --git a/view/statement_line_tree.xml b/view/statement_line_tree.xml deleted file mode 100644 index 4e97148043..0000000000 --- a/view/statement_line_tree.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - -