Merge lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher into lp:openerp-canada

Proposed by Mathieu Benoit
Status: Merged
Merged at revision: 24
Proposed branch: lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher
Merge into: lp:openerp-canada
Diff against target: 169 lines (+68/-27)
1 file modified
l10n_ca_account_check_writing/account_voucher.py (+68/-27)
To merge this branch: bzr merge lp:~savoirfairelinux-openerp/openerp-canada/7.0-bug-missing-amount_in_word-proforma_voucher
Reviewer Review Type Date Requested Status
Sandy Carter (http://www.savoirfairelinux.com) code review, no test Approve
Maxime Chambreuil (http://www.savoirfairelinux.com) Approve
OpenERP Canada Team Pending
Review via email: mp+207570@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Maxime Chambreuil (http://www.savoirfairelinux.com) (max3903) :
review: Approve
Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

l.26, a few comments:
Convention is to use:
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]

l.30-41 you want to use `for id in ids:` to avoid an index error if len(ids) == 0 or missed cases if len(ids) > 1
l.32 Have you considered cases where partner_id is a BrowseNull object?
l.37 Try using context=context for consistency
l.41 forgot to propagate context here

l.8,17,52: you're doing some pep8 fixes but you left the following 13, I would personally not touch any code I am not modifying in order to not screw up bzr annotate, or I would fix all the pep8 errors in a separate commit.
account_voucher.py:33:1: E302 expected 2 blank lines, found 1
account_voucher.py:38:5: F841 local variable 'context' is assigned to but never used
account_voucher.py:41:1: E302 expected 2 blank lines, found 1
account_voucher.py:47:5: F841 local variable 'cents' is assigned to but never used
account_voucher.py:52:9: F841 local variable 'stars' is assigned to but never used
account_voucher.py:53:5: F841 local variable 'AND' is assigned to but never used
account_voucher.py:59:1: E302 expected 2 blank lines, found 1
account_voucher.py:113:17: E123 closing bracket does not match indentation of opening bracket's line
account_voucher.py:115:13: E123 closing bracket does not match indentation of opening bracket's line
account_voucher.py:142:1: E302 expected 2 blank lines, found 1
account_voucher.py:144:1: W293 blank line contains whitespace
account_voucher.py:150:12: E225 missing whitespace around operator
account_voucher.py:156:1: W293 blank line contains whitespace

review: Needs Fixing (code review, no test)
25. By Mathieu Benoit

[FIX] Remove duplicate code and apply convention code.

26. By Mathieu Benoit

[FIX] New fix when add supplier_payments in draft and try to search lang in context.

27. By Mathieu Benoit

[FIX] fix pep8

Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) wrote :

l.124 functions which gets from context should start with the following like you did on l.114
    if context is None:
        context = {}
then, l.128 to l.138 can be simplified to
    # get lang
    supplier_lang = context.get('lang', config.get('lang'))
    if i_id is not None:
        partner_id = self.browse(cr, uid, i_id, context=context).partner_id
        if partner_id:
            supplier_lang = partner_id.lang

l.140 I am really not a fan of context.copy, I have seen it cause hard to debug problems before: try using context=dict(context, lang=supplier_lang) on l.150, it's clearer to read and you don't get stuck with two separate contexts

28. By Mathieu Benoit

[FIX] Simplify onchange_amount about amount_in_word and simplify _get_amount_in_word

Revision history for this message
Sandy Carter (http://www.savoirfairelinux.com) (sandy-carter) :
review: Approve (code review, no test)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'l10n_ca_account_check_writing/account_voucher.py'
2--- l10n_ca_account_check_writing/account_voucher.py 2013-06-11 12:24:41 +0000
3+++ l10n_ca_account_check_writing/account_voucher.py 2014-02-26 17:02:05 +0000
4@@ -21,6 +21,7 @@
5
6 from openerp.osv import orm, fields
7 from openerp.tools.translate import _
8+from openerp.tools import config
9 # OpenERP's built-in routines for converting numbers to words is pretty bad, especially in French
10 # This is why we use the library below. You can get it at:
11 # https://pypi.python.org/pypi/num2words
12@@ -30,6 +31,7 @@
13 # picks them up during .pot generation
14 _("and")
15
16+
17 def custom_translation(s, lang):
18 # OpenERP uses the current stack frame, yes, the *stack frame* to determine which language _()
19 # should translate a string in. If we want to translate a string in another language, such as
20@@ -38,6 +40,7 @@
21 context = {'lang': lang}
22 return _(s)
23
24+
25 def get_amount_line(amount, currency, lang):
26 try:
27 amount_in_word = num2words(int(amount), lang=lang[:2])
28@@ -56,6 +59,7 @@
29 amount_line_fmt = '{amount_in_word} {currency_name} {AND} {cents}/100 {stars}'
30 return amount_line_fmt.format(**vars())
31
32+
33 class account_voucher(orm.Model):
34 _inherit = 'account.voucher'
35
36@@ -63,25 +67,24 @@
37 journal_id, currency_id, ttype, date,
38 payment_rate_currency_id, company_id, context=None):
39 """ Inherited - add amount_in_word and allow_check_writting in returned value dictionary """
40- if not context:
41+ if context is None:
42 context = {}
43+ if isinstance(ids, (int, long)):
44+ ids = [ids]
45 default = super(account_voucher, self).onchange_amount(
46 cr, uid, ids, amount, rate, partner_id, journal_id, currency_id,
47 ttype, date, payment_rate_currency_id, company_id, context=context)
48+
49 if 'value' in default:
50- amount = 'amount' in default['value'] and default['value']['amount'] or amount
51- if ids:
52- supplier_lang = self.browse(cr, uid, ids[0], context=context).partner_id.lang
53- else:
54- # It's a new record and we don't have access to our supplier lang yet
55- supplier_lang = 'en_US'
56- supplier_context = context.copy()
57- # for some calls, such as the currency browse() call, we want to separate our user's
58- # language from our supplier's. That's why we need a separate context.
59- supplier_context['lang'] = supplier_lang
60- currency = self.pool.get('res.currency').browse(cr, uid, currency_id, context=supplier_context)
61- amount_line = get_amount_line(amount, currency, supplier_lang)
62- default['value'].update({'amount_in_word':amount_line})
63+ amount = default['value'].get('amount', amount)
64+ amount_in_word = self._get_amount_in_word(cr,
65+ uid,
66+ currency_id=currency_id,
67+ amount=amount,
68+ context=context)
69+
70+ if amount_in_word is not None:
71+ default['value'].update({'amount_in_word': amount_in_word})
72 if journal_id:
73 allow_check_writing = self.pool.get('account.journal').browse(
74 cr, uid, journal_id, context=context).allow_check_writing
75@@ -90,7 +93,7 @@
76
77 def print_check(self, cr, uid, ids, context=None):
78 if not ids:
79- return {}
80+ return {}
81
82 check_layout_report = {
83 'top': 'account.print.check.top',
84@@ -106,13 +109,48 @@
85 'type': 'ir.actions.report.xml',
86 'report_name': check_layout_report[check_layout],
87 'datas': {
88- 'model': 'account.voucher',
89- 'id': ids and ids[0] or False,
90- 'ids': ids and ids or [],
91- 'report_type': 'pdf'
92- },
93+ 'model': 'account.voucher',
94+ 'id': ids and ids[0] or False,
95+ 'ids': ids and ids or [],
96+ 'report_type': 'pdf'
97+ },
98 'nodestroy': True
99- }
100+ }
101+
102+ def proforma_voucher(self, cr, uid, ids, context=None):
103+ # update all amount in word when perform a voucher
104+ if context is None:
105+ context = {}
106+ if isinstance(ids, (int, long)):
107+ ids = [ids]
108+ for i_id in ids:
109+ amount_in_word = self._get_amount_in_word(cr, uid, i_id, context=context)
110+ self.write(cr, uid, i_id, {'amount_in_word': amount_in_word}, context=context)
111+
112+ return super(account_voucher, self).proforma_voucher(cr, uid, ids, context=context)
113+
114+ def _get_amount_in_word(self, cr, uid, i_id=None, currency_id=None, amount=None, context=None):
115+ if context is None:
116+ context = {}
117+ if amount is None:
118+ amount = self.browse(cr, uid, i_id, context=context).amount
119+
120+ # get lang
121+ supplier_lang = context.get('lang', config.get('lang', None))
122+ if i_id is not None:
123+ partner_id = self.browse(cr, uid, i_id, context=context).partner_id
124+ if partner_id:
125+ supplier_lang = partner_id.lang
126+
127+ context = dict(context, lang=supplier_lang)
128+ if currency_id is None:
129+ if i_id is None:
130+ return None
131+ currency_id = self._get_current_currency(cr, uid, i_id, context=context)
132+
133+ currency = self.pool.get('res.currency').browse(cr, uid, currency_id, context=context)
134+ # get the amount_in_word
135+ return get_amount_line(amount, currency, supplier_lang)
136
137 # By default, the supplier reference number is not so easily accessible from a voucher line because
138 # there's no direct link between the voucher and the invoice. Fortunately, there was this recently
139@@ -120,21 +158,24 @@
140 # https://code.launchpad.net/~elbati/account-payment/adding_account_voucher_supplier_invoice_number_7/+merge/165622
141 # which solves this exact problem and I shamelessely copied that code, which works well.
142
143+
144 class voucher_line(orm.Model):
145 _inherit = 'account.voucher.line'
146-
147+
148 def get_suppl_inv_num(self, cr, uid, move_line_id, context=None):
149 move_line = self.pool.get('account.move.line').browse(cr, uid, move_line_id, context)
150- return (move_line.invoice and move_line.invoice.supplier_invoice_number or '')
151+ return move_line.invoice and move_line.invoice.supplier_invoice_number or ''
152
153 def _get_supplier_invoice_number(self, cr, uid, ids, name, args, context=None):
154- res={}
155+ res = {}
156 for line in self.browse(cr, uid, ids, context):
157 res[line.id] = ''
158 if line.move_line_id:
159- res[line.id] = self.get_suppl_inv_num(cr, uid, line.move_line_id.id, context=context)
160+ res[line.id] = self.get_suppl_inv_num(cr, uid, line.move_line_id.id,
161+ context=context)
162 return res
163-
164+
165 _columns = {
166- 'supplier_invoice_number': fields.function(_get_supplier_invoice_number, type='char', size=64, string="Supplier Invoice Number"),
167+ 'supplier_invoice_number': fields.function(_get_supplier_invoice_number, type='char',
168+ size=64, string="Supplier Invoice Number"),
169 }

Subscribers

People subscribed via source and target branches