Merge lp:~vauxoo/openobject-addons/account_voucher_refactoring into lp:openobject-addons

Proposed by Javier Duran
Status: Rejected
Rejected by: qdp (OpenERP)
Proposed branch: lp:~vauxoo/openobject-addons/account_voucher_refactoring
Merge into: lp:openobject-addons
Diff against target: 378 lines (+208/-144)
1 file modified
account_voucher/account_voucher.py (+208/-144)
To merge this branch: bzr merge lp:~vauxoo/openobject-addons/account_voucher_refactoring
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+68417@code.launchpad.net

Commit message

[MERGE] account_voucher refactoring

Description of the change

--- Hello we are Checking this merge proposal, can you reject to avoid noise please, we didn't considerate multi currency,

Regards.

Hi,

I think IMHO the action_move_line_create method need refactoring, it is too long and pretty confused, it has over 160 lines also there is a variable that doesn't follow the OpenERP Specific Guidelines (e.g. inv when you talk about voucher). I attached a patch file where this one becomes six smaller methods easier to understand and to use and to improve by others. I hope this can help a little to has a better readable/maintainable code.

Regards,

http://doc.openerp.com/v6.0/contribute/15_guidelines/coding_guidelines_framework.html
http://doc.openerp.com/v6.0/contribute/15_guidelines/coding_guidelines_framework.html#call-your-fish-a-fish
http://doc.openerp.com/v6.0/contribute/15_guidelines/coding_guidelines_framework.html#keep-your-methods-short-simple-when-possible

To post a comment you must log in.
Revision history for this message
Javier Duran (javieredm) wrote :

For example, in Venezuela is a little different to reconcile accounting entries, this improvement allows not having to overwrite all over than 160 lines to change three or four lines, making it simpler and easier.

Regards,

4890. By Javier Duran

[REF] Refactoring the first_move_line_create and voucher_move_line_create method
in account_voucher module

Revision history for this message
Nhomar - Vauxoo (nhomar) wrote :

Hello.

When this merge proposal will be checked, we need this improvement to continue with pour localization works.

From it we will have a lot of refactor to do.

REgards.

Revision history for this message
qdp (OpenERP) (qdp) wrote :

i guess this is a duplicated merge proposal, and i'm rejecting it as you asked in the change description.

thanks

Unmerged revisions

4890. By Javier Duran

[REF] Refactoring the first_move_line_create and voucher_move_line_create method
in account_voucher module

4889. By Javier Duran

[REF] Refactoring the action_move_line_create method in account_voucher module

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account_voucher/account_voucher.py'
2--- account_voucher/account_voucher.py 2011-07-04 13:50:50 +0000
3+++ account_voucher/account_voucher.py 2011-07-26 21:17:31 +0000
4@@ -621,6 +621,187 @@
5 res['account_id'] = account_id
6 return {'value':res}
7
8+ def move_create(self, cr, uid, voucher_brw):
9+ move_obj = self.pool.get('account.move')
10+ seq_obj = self.pool.get('ir.sequence')
11+
12+ if voucher_brw.number:
13+ name = voucher_brw.number
14+ elif voucher_brw.journal_id.sequence_id:
15+ name = seq_obj.get_id(cr, uid, voucher_brw.journal_id.sequence_id.id)
16+ else:
17+ raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !'))
18+ if not voucher_brw.reference:
19+ ref = name.replace('/','')
20+ else:
21+ ref = voucher_brw.reference
22+
23+ move = {
24+ 'name': name,
25+ 'journal_id': voucher_brw.journal_id.id,
26+ 'narration': voucher_brw.narration,
27+ 'date': voucher_brw.date,
28+ 'ref': ref,
29+ 'period_id': voucher_brw.period_id and voucher_brw.period_id.id or False
30+ }
31+ move_id = move_obj.create(cr, uid, move)
32+ return move_id
33+
34+
35+ def first_move_line_get_item(self, cr, uid, voucher, move_id, company_currency,\
36+ current_currency, debit, credit, sign):
37+ return {
38+ 'name': voucher.name or '/',
39+ 'debit': debit,
40+ 'credit': credit,
41+ 'account_id': voucher.account_id.id,
42+ 'move_id': move_id,
43+ 'journal_id': voucher.journal_id.id,
44+ 'period_id': voucher.period_id.id,
45+ 'partner_id': voucher.partner_id.id,
46+ 'currency_id': company_currency <> current_currency and current_currency or False,
47+ 'amount_currency': company_currency <> current_currency and sign * voucher.amount or 0.0,
48+ 'date': voucher.date,
49+ 'date_maturity': voucher.date_due
50+ }
51+
52+
53+ def first_move_line_create(self, cr, uid, voucher_brw, move_id, company_currency, current_currency, context_multi_currency):
54+ move_line_obj = self.pool.get('account.move.line')
55+ currency_obj = self.pool.get('res.currency')
56+
57+ debit = 0.0
58+ credit = 0.0
59+ # TODO: is there any other alternative then the voucher type ??
60+ # -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
61+ if voucher_brw.type in ('purchase', 'payment'):
62+ credit = currency_obj.compute(cr, uid, current_currency, company_currency, voucher_brw.amount, context=context_multi_currency)
63+ elif voucher_brw.type in ('sale', 'receipt'):
64+ debit = currency_obj.compute(cr, uid, current_currency, company_currency, voucher_brw.amount, context=context_multi_currency)
65+ if debit < 0:
66+ credit = -debit
67+ debit = 0.0
68+ if credit < 0:
69+ debit = -credit
70+ credit = 0.0
71+ sign = debit - credit < 0 and -1 or 1
72+ #create the first line of the voucher
73+ move_line = self.first_move_line_get_item(cr, uid, voucher_brw, move_id, company_currency, current_currency, debit, credit, sign)
74+ move_line_id = move_line_obj.create(cr, uid, move_line)
75+ return move_line_id
76+
77+
78+ def voucher_move_line_get_item(self, cr, uid, voucher, line, move_id, company_currency, current_currency):
79+ return {
80+ 'journal_id': voucher.journal_id.id,
81+ 'period_id': voucher.period_id.id,
82+ 'name': line.name and line.name or '/',
83+ 'account_id': line.account_id.id,
84+ 'move_id': move_id,
85+ 'partner_id': voucher.partner_id.id,
86+ 'currency_id': company_currency <> current_currency and current_currency or False,
87+ 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False,
88+ 'quantity': 1,
89+ 'credit': 0.0,
90+ 'debit': 0.0,
91+ 'date': voucher.date
92+ }
93+
94+
95+ def voucher_move_line_create(self, cr, uid, voucher_brw, line_total, move_id,\
96+ company_currency, current_currency, context_multi_currency, context=None):
97+ move_line_obj = self.pool.get('account.move.line')
98+ currency_obj = self.pool.get('res.currency')
99+ tot_line = line_total
100+ rec_lst_ids = []
101+
102+ if context is None:
103+ context = {}
104+
105+ for line in voucher_brw.line_ids:
106+ #create one move line per voucher line where amount is not 0.0
107+ if not line.amount:
108+ continue
109+ #we check if the voucher line is fully paid or not and create a move line to balance the payment and initial invoice if needed
110+ if line.amount == line.amount_unreconciled:
111+ amount = line.move_line_id.amount_residual #residual amount in company currency
112+ else:
113+ amount = currency_obj.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency)
114+ move_line = self.voucher_move_line_get_item(cr, uid, voucher_brw, line, move_id, company_currency, current_currency)
115+ if amount < 0:
116+ amount = -amount
117+ if line.type == 'dr':
118+ line.type = 'cr'
119+ else:
120+ line.type = 'dr'
121+
122+ if (line.type=='dr'):
123+ tot_line += amount
124+ move_line['debit'] = amount
125+ else:
126+ tot_line -= amount
127+ move_line['credit'] = amount
128+
129+ if voucher_brw.tax_id and voucher_brw.type in ('sale', 'purchase'):
130+ move_line.update({
131+ 'account_tax_id': voucher_brw.tax_id.id,
132+ })
133+ if move_line.get('account_tax_id', False):
134+ tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0]
135+ if not (tax_data.base_code_id and tax_data.tax_code_id):
136+ raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name))
137+ sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1
138+ move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0
139+ voucher_line = move_line_obj.create(cr, uid, move_line)
140+ if line.move_line_id.id:
141+ rec_ids = [voucher_line, line.move_line_id.id]
142+ rec_lst_ids.append(rec_ids)
143+
144+ return (tot_line, rec_lst_ids)
145+
146+
147+ def writeoff_move_line_create(self, cr, uid, voucher_brw, line_total, move_id, name,\
148+ company_currency, current_currency, context_multi_currency):
149+ move_line_obj = self.pool.get('account.move.line')
150+ currency_obj = self.pool.get('res.currency')
151+ move_line_id = False
152+
153+ if not currency_obj.is_zero(cr, uid, voucher_brw.currency_id, line_total):
154+ diff = line_total
155+ account_id = False
156+ write_off_name = ''
157+ if voucher_brw.payment_option == 'with_writeoff':
158+ account_id = voucher_brw.writeoff_acc_id.id
159+ write_off_name = voucher_brw.comment
160+ elif voucher_brw.type in ('sale', 'receipt'):
161+ account_id = voucher_brw.partner_id.property_account_receivable.id
162+ else:
163+ account_id = voucher_brw.partner_id.property_account_payable.id
164+ move_line = {
165+ 'name': write_off_name or name,
166+ 'account_id': account_id,
167+ 'move_id': move_id,
168+ 'partner_id': voucher_brw.partner_id.id,
169+ 'date': voucher_brw.date,
170+ 'credit': diff > 0 and diff or 0.0,
171+ 'debit': diff < 0 and -diff or 0.0,
172+ #'amount_currency': company_currency <> current_currency and currency_obj.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0,
173+ #'currency_id': company_currency <> current_currency and current_currency or False,
174+ }
175+ move_line_id = move_line_obj.create(cr, uid, move_line)
176+
177+ return move_line_id
178+
179+
180+ def voucher_reconcile(self, cr, uid, rec_list_ids):
181+ move_line_obj = self.pool.get('account.move.line')
182+ for rec_ids in rec_list_ids:
183+ if len(rec_ids) >= 2:
184+ move_line_obj.reconcile_partial(cr, uid, rec_ids)
185+
186+ return True
187+
188+
189 def action_move_line_create(self, cr, uid, ids, context=None):
190
191 def _get_payment_term_lines(term_id, amount):
192@@ -635,159 +816,42 @@
193 move_line_pool = self.pool.get('account.move.line')
194 currency_pool = self.pool.get('res.currency')
195 tax_obj = self.pool.get('account.tax')
196- seq_obj = self.pool.get('ir.sequence')
197- for inv in self.browse(cr, uid, ids, context=context):
198- if inv.move_id:
199+
200+ for voucher in self.browse(cr, uid, ids, context=context):
201+ if voucher.move_id:
202 continue
203 context_multi_currency = context.copy()
204- context_multi_currency.update({'date': inv.date})
205-
206- if inv.number:
207- name = inv.number
208- elif inv.journal_id.sequence_id:
209- name = seq_obj.get_id(cr, uid, inv.journal_id.sequence_id.id)
210- else:
211- raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !'))
212- if not inv.reference:
213- ref = name.replace('/','')
214- else:
215- ref = inv.reference
216-
217- move = {
218- 'name': name,
219- 'journal_id': inv.journal_id.id,
220- 'narration': inv.narration,
221- 'date': inv.date,
222- 'ref': ref,
223- 'period_id': inv.period_id and inv.period_id.id or False
224- }
225- move_id = move_pool.create(cr, uid, move)
226-
227- #create the first line manually
228- company_currency = inv.journal_id.company_id.currency_id.id
229- current_currency = inv.currency_id.id
230- debit = 0.0
231- credit = 0.0
232- # TODO: is there any other alternative then the voucher type ??
233- # -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
234- if inv.type in ('purchase', 'payment'):
235- credit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency)
236- elif inv.type in ('sale', 'receipt'):
237- debit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency)
238- if debit < 0:
239- credit = -debit
240- debit = 0.0
241- if credit < 0:
242- debit = -credit
243- credit = 0.0
244- sign = debit - credit < 0 and -1 or 1
245- #create the first line of the voucher
246- move_line = {
247- 'name': inv.name or '/',
248- 'debit': debit,
249- 'credit': credit,
250- 'account_id': inv.account_id.id,
251- 'move_id': move_id,
252- 'journal_id': inv.journal_id.id,
253- 'period_id': inv.period_id.id,
254- 'partner_id': inv.partner_id.id,
255- 'currency_id': company_currency <> current_currency and current_currency or False,
256- 'amount_currency': company_currency <> current_currency and sign * inv.amount or 0.0,
257- 'date': inv.date,
258- 'date_maturity': inv.date_due
259- }
260- move_line_pool.create(cr, uid, move_line)
261+ context_multi_currency.update({'date': voucher.date})
262+ company_currency = voucher.journal_id.company_id.currency_id.id
263+ current_currency = voucher.currency_id.id
264+ #create the move
265+ move_id = self.move_create(cr, uid, voucher)
266+ name = move_pool.browse(cr, uid, move_id, context=context).name
267+ #create the first line manually
268+ move_line_id = self.first_move_line_create(cr, uid, voucher, move_id, company_currency, current_currency, context_multi_currency)
269+ mov_line_brw = move_line_pool.browse(cr, uid, move_line_id, context=context)
270+
271 rec_list_ids = []
272- line_total = debit - credit
273- if inv.type == 'sale':
274- line_total = line_total - currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount, context=context_multi_currency)
275- elif inv.type == 'purchase':
276- line_total = line_total + currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount, context=context_multi_currency)
277-
278- for line in inv.line_ids:
279- #create one move line per voucher line where amount is not 0.0
280- if not line.amount:
281- continue
282- #we check if the voucher line is fully paid or not and create a move line to balance the payment and initial invoice if needed
283- if line.amount == line.amount_unreconciled:
284- amount = line.move_line_id.amount_residual #residual amount in company currency
285- else:
286- amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency)
287- move_line = {
288- 'journal_id': inv.journal_id.id,
289- 'period_id': inv.period_id.id,
290- 'name': line.name or '/',
291- 'account_id': line.account_id.id,
292- 'move_id': move_id,
293- 'partner_id': inv.partner_id.id,
294- 'currency_id': company_currency <> current_currency and current_currency or False,
295- 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False,
296- 'quantity': 1,
297- 'credit': 0.0,
298- 'debit': 0.0,
299- 'date': inv.date
300- }
301- if amount < 0:
302- amount = -amount
303- if line.type == 'dr':
304- line.type = 'cr'
305- else:
306- line.type = 'dr'
307- if (line.type=='dr'):
308- line_total += amount
309- move_line['debit'] = amount
310- else:
311- line_total -= amount
312- move_line['credit'] = amount
313-
314- if inv.tax_id and inv.type in ('sale', 'purchase'):
315- move_line.update({
316- 'account_tax_id': inv.tax_id.id,
317- })
318- if move_line.get('account_tax_id', False):
319- tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0]
320- if not (tax_data.base_code_id and tax_data.tax_code_id):
321- raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name))
322- sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1
323- move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0
324- voucher_line = move_line_pool.create(cr, uid, move_line)
325- if line.move_line_id.id:
326- rec_ids = [voucher_line, line.move_line_id.id]
327- rec_list_ids.append(rec_ids)
328-
329- if not currency_pool.is_zero(cr, uid, inv.currency_id, line_total):
330- diff = line_total
331- account_id = False
332- write_off_name = ''
333- if inv.payment_option == 'with_writeoff':
334- account_id = inv.writeoff_acc_id.id
335- write_off_name = inv.comment
336- elif inv.type in ('sale', 'receipt'):
337- account_id = inv.partner_id.property_account_receivable.id
338- else:
339- account_id = inv.partner_id.property_account_payable.id
340- move_line = {
341- 'name': write_off_name or name,
342- 'account_id': account_id,
343- 'move_id': move_id,
344- 'partner_id': inv.partner_id.id,
345- 'date': inv.date,
346- 'credit': diff > 0 and diff or 0.0,
347- 'debit': diff < 0 and -diff or 0.0,
348- #'amount_currency': company_currency <> current_currency and currency_pool.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0,
349- #'currency_id': company_currency <> current_currency and current_currency or False,
350- }
351- move_line_pool.create(cr, uid, move_line)
352- self.write(cr, uid, [inv.id], {
353+ line_total = mov_line_brw.debit - mov_line_brw.credit
354+ if voucher.type == 'sale':
355+ line_total = line_total - currency_pool.compute(cr, uid, voucher.currency_id.id, company_currency, voucher.tax_amount, context=context_multi_currency)
356+ elif voucher.type == 'purchase':
357+ line_total = line_total + currency_pool.compute(cr, uid, voucher.currency_id.id, company_currency, voucher.tax_amount, context=context_multi_currency)
358+
359+ #create one move line per voucher line where amount is not 0.0
360+ line_total, rec_list_ids = self.voucher_move_line_create(cr, uid, voucher, line_total, move_id, company_currency, current_currency, context_multi_currency, context=context)
361+ #create the writeoff line if needed
362+ ml_writeoff_id = self.writeoff_move_line_create(cr, uid, voucher, line_total, move_id, name, company_currency, current_currency, context_multi_currency)
363+ self.write(cr, uid, [voucher.id], {
364 'move_id': move_id,
365 'state': 'posted',
366 'number': name,
367 })
368 if inv.journal_id.entry_posted:
369 move_pool.post(cr, uid, [move_id], context={})
370- for rec_ids in rec_list_ids:
371- if len(rec_ids) >= 2:
372- move_line_pool.reconcile_partial(cr, uid, rec_ids)
373+ #reconcile move line
374+ self.voucher_reconcile(cr, uid, rec_list_ids)
375+
376 return True
377
378 def copy(self, cr, uid, id, default={}, context=None):

Subscribers

People subscribed via source and target branches

to all changes: