Merge lp:~openerp-dev/openobject-addons/6.1-opw-584449-ksa into lp:openobject-addons/6.1

Proposed by Kirti Savalia(OpenERP)
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-addons/6.1-opw-584449-ksa
Merge into: lp:openobject-addons/6.1
Diff against target: 211 lines (+65/-124)
1 file modified
account/account_move_line.py (+65/-124)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/6.1-opw-584449-ksa
Reviewer Review Type Date Requested Status
Naresh(OpenERP) Pending
Review via email: mp+145309@code.launchpad.net

Description of the change

Hello,

"[BACKPORT] when new entry created in jounral item need to set by default jounral realted account"

Code is back-ported from 7.0

Thanks
KSA

To post a comment you must log in.

Unmerged revisions

7137. By Kirti Savalia(OpenERP)

[FIX]:(backport)when new entry created in jounral item need to set by default jounral realted account

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account/account_move_line.py'
2--- account/account_move_line.py 2012-09-06 14:35:17 +0000
3+++ account/account_move_line.py 2013-01-29 05:45:27 +0000
4@@ -208,10 +208,11 @@
5 return context
6
7 def _default_get(self, cr, uid, fields, context=None):
8+ #default_get should only do the following:
9+ # -propose the next amount in debit/credit in order to balance the move
10+ # -propose the next account from the journal (default debit/credit account) accordingly
11 if context is None:
12 context = {}
13- if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
14- context['journal_id'] = context.get('search_default_journal_id')
15 account_obj = self.pool.get('account.account')
16 period_obj = self.pool.get('account.period')
17 journal_obj = self.pool.get('account.journal')
18@@ -220,131 +221,71 @@
19 fiscal_pos_obj = self.pool.get('account.fiscal.position')
20 partner_obj = self.pool.get('res.partner')
21 currency_obj = self.pool.get('res.currency')
22+
23+ if not context.get('journal_id', False):
24+ context['journal_id'] = context.get('search_default_journal_id', False)
25+ if not context.get('period_id', False):
26+ context['period_id'] = context.get('search_default_period_id', False)
27 context = self.convert_to_period(cr, uid, context)
28+
29 # Compute simple values
30 data = super(account_move_line, self).default_get(cr, uid, fields, context=context)
31- # Starts: Manual entry from account.move form
32- if context.get('lines'):
33- total_new = context.get('balance', 0.00)
34- if context['journal']:
35- journal_data = journal_obj.browse(cr, uid, context['journal'], context=context)
36- if journal_data.type == 'purchase':
37- if total_new > 0:
38- account = journal_data.default_credit_account_id
39- else:
40- account = journal_data.default_debit_account_id
41- else:
42- if total_new > 0:
43- account = journal_data.default_credit_account_id
44- else:
45- account = journal_data.default_debit_account_id
46- if account and ((not fields) or ('debit' in fields) or ('credit' in fields)) and 'partner_id' in data and (data['partner_id']):
47- part = partner_obj.browse(cr, uid, data['partner_id'], context=context)
48- account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
49- account = account_obj.browse(cr, uid, account, context=context)
50- data['account_id'] = account.id
51-
52- s = -total_new
53- data['debit'] = s > 0 and s or 0.0
54- data['credit'] = s < 0 and -s or 0.0
55- data = self._default_get_move_form_hook(cr, uid, data)
56- return data
57- # Ends: Manual entry from account.move form
58- if not 'move_id' in fields: #we are not in manual entry
59- return data
60- # Compute the current move
61- move_id = False
62- partner_id = False
63- if context.get('journal_id', False) and context.get('period_id', False):
64- if 'move_id' in fields:
65- cr.execute('SELECT move_id \
66- FROM \
67- account_move_line \
68- WHERE \
69- journal_id = %s and period_id = %s AND create_uid = %s AND state = %s \
70- ORDER BY id DESC limit 1',
71- (context['journal_id'], context['period_id'], uid, 'draft'))
72- res = cr.fetchone()
73- move_id = (res and res[0]) or False
74- if not move_id:
75- return data
76- else:
77- data['move_id'] = move_id
78- if 'date' in fields:
79- cr.execute('SELECT date \
80- FROM \
81- account_move_line \
82- WHERE \
83- journal_id = %s AND period_id = %s AND create_uid = %s \
84- ORDER BY id DESC',
85- (context['journal_id'], context['period_id'], uid))
86- res = cr.fetchone()
87- if res:
88- data['date'] = res[0]
89- else:
90- period = period_obj.browse(cr, uid, context['period_id'],
91- context=context)
92- data['date'] = period.date_start
93- if not move_id:
94- return data
95- total = 0
96- ref_id = False
97- move = move_obj.browse(cr, uid, move_id, context=context)
98- if 'name' in fields:
99- data.setdefault('name', move.line_id[-1].name)
100- acc1 = False
101- for l in move.line_id:
102- acc1 = l.account_id
103- partner_id = partner_id or l.partner_id.id
104- ref_id = ref_id or l.ref
105- total += (l.debit or 0.0) - (l.credit or 0.0)
106-
107- if 'ref' in fields:
108- data['ref'] = ref_id
109- if 'partner_id' in fields:
110- data['partner_id'] = partner_id
111-
112- if move.journal_id.type == 'purchase':
113- if total > 0:
114- account = move.journal_id.default_credit_account_id
115- else:
116- account = move.journal_id.default_debit_account_id
117- else:
118- if total > 0:
119- account = move.journal_id.default_credit_account_id
120- else:
121- account = move.journal_id.default_debit_account_id
122- part = partner_id and partner_obj.browse(cr, uid, partner_id) or False
123- # part = False is acceptable for fiscal position.
124- account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
125- if account:
126- account = account_obj.browse(cr, uid, account, context=context)
127-
128- if account and ((not fields) or ('debit' in fields) or ('credit' in fields)):
129- data['account_id'] = account.id
130- # Propose the price VAT excluded, the VAT will be added when confirming line
131- if account.tax_ids:
132- taxes = fiscal_pos_obj.map_tax(cr, uid, part and part.property_account_position or False, account.tax_ids)
133- tax = tax_obj.browse(cr, uid, taxes)
134- for t in tax_obj.compute_inv(cr, uid, tax, total, 1):
135- total -= t['amount']
136-
137- s = -total
138- data['debit'] = s > 0 and s or 0.0
139- data['credit'] = s < 0 and -s or 0.0
140-
141- if account and account.currency_id:
142- data['currency_id'] = account.currency_id.id
143- acc = account
144- if s>0:
145- acc = acc1
146- compute_ctx = context.copy()
147- compute_ctx.update({
148- 'res.currency.compute.account': acc,
149- 'res.currency.compute.account_invert': True,
150- })
151- v = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], s, context=compute_ctx)
152- data['amount_currency'] = v
153+ if context.get('journal_id'):
154+ total = 0.0
155+ #in account.move form view, it is not possible to compute total debit and credit using
156+ #a browse record. So we must use the context to pass the whole one2many field and compute the total
157+ if context.get('line_id'):
158+ for move_line_dict in move_obj.resolve_o2m_commands_to_record_dicts(cr, uid, 'line_id', context.get('line_id'), context=context):
159+ data['name'] = data.get('name') or move_line_dict.get('name')
160+ data['partner_id'] = data.get('partner_id') or move_line_dict.get('partner_id')
161+ total += move_line_dict.get('debit', 0.0) - move_line_dict.get('credit', 0.0)
162+ elif context.get('period_id'):
163+ #find the date and the ID of the last unbalanced account.move encoded by the current user in that journal and period
164+ move_id = False
165+ cr.execute('''SELECT move_id, date FROM account_move_line
166+ WHERE journal_id = %s AND period_id = %s AND create_uid = %s AND state = %s
167+ ORDER BY id DESC limit 1''', (context['journal_id'], context['period_id'], uid, 'draft'))
168+ res = cr.fetchone()
169+ move_id = res and res[0] or False
170+ data['date'] = res and res[1] or period_obj.browse(cr, uid, context['period_id'], context=context).date_start
171+ data['move_id'] = move_id
172+ if move_id:
173+ #if there exist some unbalanced accounting entries that match the journal and the period,
174+ #we propose to continue the same move by copying the ref, the name, the partner...
175+ move = move_obj.browse(cr, uid, move_id, context=context)
176+ data.setdefault('name', move.line_id[-1].name)
177+ for l in move.line_id:
178+ data['partner_id'] = data.get('partner_id') or l.partner_id.id
179+ data['ref'] = data.get('ref') or l.ref
180+ total += (l.debit or 0.0) - (l.credit or 0.0)
181+
182+ #compute the total of current move
183+ data['debit'] = total < 0 and -total or 0.0
184+ data['credit'] = total > 0 and total or 0.0
185+ #pick the good account on the journal accordingly if the next proposed line will be a debit or a credit
186+ journal_data = journal_obj.browse(cr, uid, context['journal_id'], context=context)
187+ account = total > 0 and journal_data.default_credit_account_id or journal_data.default_debit_account_id
188+ #map the account using the fiscal position of the partner, if needed
189+ part = data.get('partner_id') and partner_obj.browse(cr, uid, data['partner_id'], context=context) or False
190+ if account and data.get('partner_id'):
191+ account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
192+ account = account_obj.browse(cr, uid, account, context=context)
193+ data['account_id'] = account and account.id or False
194+ #compute the amount in secondary currency of the account, if needed
195+ if account and account.currency_id:
196+ data['currency_id'] = account.currency_id.id
197+ #set the context for the multi currency change
198+ compute_ctx = context.copy()
199+ compute_ctx.update({
200+ #the following 2 parameters are used to choose the currency rate, in case where the account
201+ #doesn't work with an outgoing currency rate method 'at date' but 'average'
202+ 'res.currency.compute.account': account,
203+ 'res.currency.compute.account_invert': True,
204+ })
205+ if data.get('date'):
206+ compute_ctx.update({'date': data['date']})
207+ data['amount_currency'] = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], -total, context=compute_ctx)
208+ data = self._default_get_move_form_hook(cr, uid, data)
209 return data
210
211 def on_create_write(self, cr, uid, id, context=None):