Use reference field for statement line relation

issue6854
review367971002
This commit is contained in:
Cédric Krier committed 2022-02-23 00:01:17 +01:00
1 parent 243a278a64
commit ab017699b7
8 files changed
+42 -116

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Use reference field for statement line relation
* Add support for Python 3.10
* Remove support for Python 3.6
+32 -57
View File
@@ -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:
-25
View File
@@ -1,25 +0,0 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tryton>
<data depends="account_statement">
<record model="ir.ui.view" id="statement_line_view_form">
<field name="model">account.statement.line</field>
<field name="inherit"
ref="account_statement.statement_line_view_form"/>
<field name="name">statement_line_form</field>
</record>
<record model="ir.ui.view" id="statement_line_view_tree">
<field name="model">account.statement.line</field>
<field name="inherit"
ref="account_statement.statement_line_view_tree"/>
<field name="name">statement_line_tree</field>
</record>
<record model="ir.ui.view" id="statement_line_view_tree_editable">
<field name="model">account.statement.line</field>
<field name="inherit"
ref="account_statement.statement_line_view_tree_editable"/>
<field name="name">statement_line_tree</field>
</record>
</data>
</tryton>
@@ -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::
+7 -7
View File
@@ -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
-1
View File
@@ -8,4 +8,3 @@ extras_depend:
account_statement_rule
xml:
payment.xml
statement.xml
-12
View File
@@ -1,12 +0,0 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/form/field[@name='invoice']" position="after">
<newline/>
<label name="payment"/>
<field name="payment"/>
<label name="payment_group"/>
<field name="payment_group"/>
</xpath>
</data>
-9
View File
@@ -1,9 +0,0 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/tree/field[@name='invoice']" position="after">
<field name="payment"/>
<field name="payment_group"/>
</xpath>
</data>