Merge lp:~camptocamp/banking-addons/bank-statement-reconcile-7.0-fix-related-balance-check into lp:banking-addons/bank-statement-reconcile-70

Proposed by Leonardo Pistone
Status: Merged
Approved by: Yannick Vaucher @ Camptocamp
Approved revision: 136
Merged at revision: 134
Proposed branch: lp:~camptocamp/banking-addons/bank-statement-reconcile-7.0-fix-related-balance-check
Merge into: lp:banking-addons/bank-statement-reconcile-70
Diff against target: 186 lines (+129/-21)
3 files modified
account_statement_ext/__openerp__.py (+1/-1)
account_statement_ext/statement.py (+50/-20)
account_statement_ext/test/test_profile_related_fields.yml (+78/-0)
To merge this branch: bzr merge lp:~camptocamp/banking-addons/bank-statement-reconcile-7.0-fix-related-balance-check
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp code review Approve
Pedro Manuel Baeza code review Approve
Review via email: mp+209723@code.launchpad.net
To post a comment you must log in.
134. By Leonardo Pistone

[fix] account_statement_ext: trigger for a related field

135. By Leonardo Pistone

[fix] other related fields found in the statement

Revision history for this message
Leonardo Pistone (lepistone) wrote :

I added here a very similar fix (with tests) for the other related fields in the same object.

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

Hi, Leonardo, thanks for the patch.

Instead of defining a function _us, you can use lambda for resolving that simple expression:

lambda self, cr, uid, ids, context=None: ids

The other store function is also bad constructed, because it's triggered on 'account.bank.statement', but you have to query 'account.statement.profile' object. This is the correct code (also putting arguments in their usual form):

def _get_statement_from_profile(self, cr, uid, ids, context=None):
    triggered = []
    profile_obj = self.pool['account.statement.profile']
    for profile in profile_obj.browse(cr, uid, ids, context=context):
        triggered += [st.id for st in profile.bank_statement_ids]
    return triggered

Regards.

review: Needs Fixing (code review)
Revision history for this message
Leonardo Pistone (lepistone) wrote :

Thanks for reviewing Pedro.

1. I know we could use lambdas. They both look correct to me, and readability also doesn't change much, IMHO.
2. That is a known weirdness of OpenERP (I put a comment for that reason). If you put a pdb in, you will see that self is the profile, non the statement, even though we are in the statement class.

But none of these points if worth fighting for: let's see if some other reviewer comes up.

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

Hi, Leonardo,

Curious, I don't know about that behaviour. Don't you think then that it's better to rename self to profile_obj or something similar to avoid bad interpretations?

I also see cleaner the lamba expression, but this is a matter of taste, so I'm not going to block the MP for this reason.

Regards.

136. By Leonardo Pistone

[imp] docstring for a function field trigger

Revision history for this message
Leonardo Pistone (lepistone) wrote :

Good point, it's not clear which is the path of least surprise here. The 'self' name is a convention that we could change to profile_obj, but that is weird as well to my eyes.

So I just expanded the docstring to be a bit more informative.

thanks!

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

You have my approval then.

Thanks for your changes.

Regards

review: Approve (code review)
Revision history for this message
Stéphane Bidoul (Acsone) (sbi) wrote :

Hi Leonardo,

A clean we to avoid the weirdness is to put the _get_statement(_from_profile) in the AccountStatementProfile class and reference it like this:

store={
               'account.bank.statement': (_us, ['profile_id'], 10),
               'account.statement.profile': (
                  AccountStatementProfile._get_statement, ['partner_id'], 10),
},

That worked for me in other situations.

Revision history for this message
Leonardo Pistone (lepistone) wrote :

Stéphane,

I understand your point, but that also has the weird point that the method that you need to write some code far away in class A to make a function field in class B.

My opinion is that there are a few ways to do that, but if we name variables and methods properly, and maybe add a docstring, IMO it's good enough.

Thanks!

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

I prefer to use self.pool['account.statement.profile'] in trigger methods even if self is already the instance of 'account.statement.profile' just for clarity and explicitness. But this is correct too, so LGTM.
Thanks

review: Approve (code review)
Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

As Stephane's comment is a just a comment and not a need fixing I'll proceed with merging this one.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account_statement_ext/__openerp__.py'
2--- account_statement_ext/__openerp__.py 2014-03-06 10:48:37 +0000
3+++ account_statement_ext/__openerp__.py 2014-03-07 10:20:51 +0000
4@@ -79,7 +79,7 @@
5 'security/ir.model.access.csv',
6 'security/ir_rule.xml'],
7 'demo_xml': [],
8- 'test': [],
9+ 'test': ['test/test_profile_related_fields.yml'],
10 'installable': True,
11 'images': [],
12 'auto_install': False,
13
14=== modified file 'account_statement_ext/statement.py'
15--- account_statement_ext/statement.py 2014-03-06 10:48:37 +0000
16+++ account_statement_ext/statement.py 2014-03-07 10:20:51 +0000
17@@ -159,6 +159,23 @@
18
19 return profile_ids[0] if profile_ids else False
20
21+ def _get_statement_from_profile(self, cr, uid, profile_ids, context=None):
22+ """Stored function field trigger.
23+
24+ Weirdness warning: we are in the class account.bank.statement, but
25+ when the ORM calls this, self is an account.statement.profile.
26+
27+ Returns a list of account.bank.statement ids to recompute.
28+
29+ """
30+ triggered = []
31+ for profile in self.browse(cr, uid, profile_ids, context=context):
32+ triggered += [st.id for st in profile.bank_statement_ids]
33+ return triggered
34+
35+ def _us(self, cr, uid, ids, context=None):
36+ return ids
37+
38 _columns = {
39 'profile_id': fields.many2one(
40 'account.statement.profile',
41@@ -166,28 +183,41 @@
42 required=True,
43 states={'draft': [('readonly', False)]}),
44 'credit_partner_id': fields.related(
45- 'profile_id',
46- 'partner_id',
47- type='many2one',
48- relation='res.partner',
49- string='Financial Partner',
50- store=True,
51- readonly=True),
52+ 'profile_id',
53+ 'partner_id',
54+ type='many2one',
55+ relation='res.partner',
56+ string='Financial Partner',
57+ store={
58+ 'account.bank.statement': (_us, ['profile_id'], 10),
59+ 'account.statement.profile': (
60+ _get_statement_from_profile, ['partner_id'], 10),
61+ },
62+ readonly=True),
63 'balance_check': fields.related(
64- 'profile_id',
65- 'balance_check',
66- type='boolean',
67- string='Balance check',
68- store=True,
69- readonly=True),
70+ 'profile_id',
71+ 'balance_check',
72+ type='boolean',
73+ string='Balance check',
74+ store={
75+ 'account.bank.statement': (_us, ['profile_id'], 10),
76+ 'account.statement.profile': (
77+ _get_statement_from_profile, ['balance_check'], 10),
78+ },
79+ readonly=True
80+ ),
81 'journal_id': fields.related(
82- 'profile_id',
83- 'journal_id',
84- type='many2one',
85- relation='account.journal',
86- string='Journal',
87- store=True,
88- readonly=True),
89+ 'profile_id',
90+ 'journal_id',
91+ type='many2one',
92+ relation='account.journal',
93+ string='Journal',
94+ store={
95+ 'account.bank.statement': (_us, ['profile_id'], 10),
96+ 'account.statement.profile': (
97+ _get_statement_from_profile, ['journal_id'], 10),
98+ },
99+ readonly=True),
100 'period_id': fields.many2one(
101 'account.period',
102 'Period',
103
104=== added directory 'account_statement_ext/test'
105=== added file 'account_statement_ext/test/test_profile_related_fields.yml'
106--- account_statement_ext/test/test_profile_related_fields.yml 1970-01-01 00:00:00 +0000
107+++ account_statement_ext/test/test_profile_related_fields.yml 2014-03-07 10:20:51 +0000
108@@ -0,0 +1,78 @@
109+-
110+ In order to test the related fields in the profile, I first need to create a profile
111+-
112+ !record {model: account.statement.profile, id: profile_test1}:
113+ name: Bank EUR Profile
114+ journal_id: account.bank_journal
115+ commission_account_id: account.a_expense
116+ company_id: base.main_company
117+ partner_id: base.res_partner_1
118+ journal_id: account.check_journal
119+ balance_check: True
120+-
121+ I create a second profile
122+-
123+ !record {model: account.statement.profile, id: profile_test2}:
124+ name: Second profile
125+ journal_id: account.bank_journal
126+ commission_account_id: account.a_expense
127+ company_id: base.main_company
128+ partner_id: base.res_partner_3
129+ journal_id: account.bank_journal
130+ balance_check: True
131+-
132+ Now I create a statement.
133+-
134+ !record {model: account.bank.statement, id: statement_test1}:
135+ name: Statement 1
136+ profile_id: profile_test1
137+ company_id: base.main_company
138+-
139+ Now I check that the related data come from the profile
140+-
141+ !assert {model: account.bank.statement, id: statement_test1, string: Initial data should come from the profile}:
142+ - balance_check is True
143+ - credit_partner_id.id == _ref('base.res_partner_1')
144+ - journal_id.id == _ref('account.check_journal')
145+-
146+ Now I set the balance flag to False in the profile
147+-
148+ !record {model: account.statement.profile, id: profile_test1}:
149+ balance_check: False
150+-
151+ It should be false in the statement as well.
152+-
153+ !assert {model: account.bank.statement, id: statement_test1, string: We should not check the balance}:
154+ - balance_check is False
155+-
156+ Now I change the journal in the profile
157+-
158+ !record {model: account.statement.profile, id: profile_test1}:
159+ journal_id: account.cash_journal
160+-
161+ The journal should propagate to the statement
162+-
163+ !assert {model: account.bank.statement, id: statement_test1, string: The journal should be updated}:
164+ - journal_id.id == _ref('account.cash_journal')
165+-
166+ Now I change the partner in the profile
167+-
168+ !record {model: account.statement.profile, id: profile_test1}:
169+ partner_id: base.res_partner_2
170+-
171+ The partner should propagate to the statement
172+-
173+ !assert {model: account.bank.statement, id: statement_test1, string: The partner should be updated}:
174+ - credit_partner_id.id == _ref('base.res_partner_2')
175+-
176+ Now I change the profile associated to the statement.
177+-
178+ !record {model: account.bank.statement, id: statement_test1}:
179+ profile_id: profile_test2
180+-
181+ The statement should receive information from the new statement.
182+-
183+ !assert {model: account.bank.statement, id: statement_test1, string: All information should be probagated from the new profile}:
184+ - balance_check is True
185+ - credit_partner_id.id == _ref('base.res_partner_3')
186+ - journal_id.id == _ref('account.bank_journal')

Subscribers

People subscribed via source and target branches