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
=== modified file 'account/account_move_line.py'
--- account/account_move_line.py 2012-09-06 14:35:17 +0000
+++ account/account_move_line.py 2013-01-29 05:45:27 +0000
@@ -208,10 +208,11 @@
208 return context208 return context
209209
210 def _default_get(self, cr, uid, fields, context=None):210 def _default_get(self, cr, uid, fields, context=None):
211 #default_get should only do the following:
212 # -propose the next amount in debit/credit in order to balance the move
213 # -propose the next account from the journal (default debit/credit account) accordingly
211 if context is None:214 if context is None:
212 context = {}215 context = {}
213 if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
214 context['journal_id'] = context.get('search_default_journal_id')
215 account_obj = self.pool.get('account.account')216 account_obj = self.pool.get('account.account')
216 period_obj = self.pool.get('account.period')217 period_obj = self.pool.get('account.period')
217 journal_obj = self.pool.get('account.journal')218 journal_obj = self.pool.get('account.journal')
@@ -220,131 +221,71 @@
220 fiscal_pos_obj = self.pool.get('account.fiscal.position')221 fiscal_pos_obj = self.pool.get('account.fiscal.position')
221 partner_obj = self.pool.get('res.partner')222 partner_obj = self.pool.get('res.partner')
222 currency_obj = self.pool.get('res.currency')223 currency_obj = self.pool.get('res.currency')
224
225 if not context.get('journal_id', False):
226 context['journal_id'] = context.get('search_default_journal_id', False)
227 if not context.get('period_id', False):
228 context['period_id'] = context.get('search_default_period_id', False)
223 context = self.convert_to_period(cr, uid, context)229 context = self.convert_to_period(cr, uid, context)
230
224 # Compute simple values231 # Compute simple values
225 data = super(account_move_line, self).default_get(cr, uid, fields, context=context)232 data = super(account_move_line, self).default_get(cr, uid, fields, context=context)
226 # Starts: Manual entry from account.move form233 if context.get('journal_id'):
227 if context.get('lines'):234 total = 0.0
228 total_new = context.get('balance', 0.00)235 #in account.move form view, it is not possible to compute total debit and credit using
229 if context['journal']:236 #a browse record. So we must use the context to pass the whole one2many field and compute the total
230 journal_data = journal_obj.browse(cr, uid, context['journal'], context=context)237 if context.get('line_id'):
231 if journal_data.type == 'purchase':238 for move_line_dict in move_obj.resolve_o2m_commands_to_record_dicts(cr, uid, 'line_id', context.get('line_id'), context=context):
232 if total_new > 0:239 data['name'] = data.get('name') or move_line_dict.get('name')
233 account = journal_data.default_credit_account_id240 data['partner_id'] = data.get('partner_id') or move_line_dict.get('partner_id')
234 else:241 total += move_line_dict.get('debit', 0.0) - move_line_dict.get('credit', 0.0)
235 account = journal_data.default_debit_account_id242 elif context.get('period_id'):
236 else:243 #find the date and the ID of the last unbalanced account.move encoded by the current user in that journal and period
237 if total_new > 0:244 move_id = False
238 account = journal_data.default_credit_account_id245 cr.execute('''SELECT move_id, date FROM account_move_line
239 else:246 WHERE journal_id = %s AND period_id = %s AND create_uid = %s AND state = %s
240 account = journal_data.default_debit_account_id247 ORDER BY id DESC limit 1''', (context['journal_id'], context['period_id'], uid, 'draft'))
241 if account and ((not fields) or ('debit' in fields) or ('credit' in fields)) and 'partner_id' in data and (data['partner_id']):248 res = cr.fetchone()
242 part = partner_obj.browse(cr, uid, data['partner_id'], context=context)249 move_id = res and res[0] or False
243 account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)250 data['date'] = res and res[1] or period_obj.browse(cr, uid, context['period_id'], context=context).date_start
244 account = account_obj.browse(cr, uid, account, context=context)251 data['move_id'] = move_id
245 data['account_id'] = account.id252 if move_id:
246253 #if there exist some unbalanced accounting entries that match the journal and the period,
247 s = -total_new254 #we propose to continue the same move by copying the ref, the name, the partner...
248 data['debit'] = s > 0 and s or 0.0255 move = move_obj.browse(cr, uid, move_id, context=context)
249 data['credit'] = s < 0 and -s or 0.0256 data.setdefault('name', move.line_id[-1].name)
250 data = self._default_get_move_form_hook(cr, uid, data)257 for l in move.line_id:
251 return data258 data['partner_id'] = data.get('partner_id') or l.partner_id.id
252 # Ends: Manual entry from account.move form259 data['ref'] = data.get('ref') or l.ref
253 if not 'move_id' in fields: #we are not in manual entry260 total += (l.debit or 0.0) - (l.credit or 0.0)
254 return data261
255 # Compute the current move262 #compute the total of current move
256 move_id = False263 data['debit'] = total < 0 and -total or 0.0
257 partner_id = False264 data['credit'] = total > 0 and total or 0.0
258 if context.get('journal_id', False) and context.get('period_id', False):265 #pick the good account on the journal accordingly if the next proposed line will be a debit or a credit
259 if 'move_id' in fields:266 journal_data = journal_obj.browse(cr, uid, context['journal_id'], context=context)
260 cr.execute('SELECT move_id \267 account = total > 0 and journal_data.default_credit_account_id or journal_data.default_debit_account_id
261 FROM \268 #map the account using the fiscal position of the partner, if needed
262 account_move_line \269 part = data.get('partner_id') and partner_obj.browse(cr, uid, data['partner_id'], context=context) or False
263 WHERE \270 if account and data.get('partner_id'):
264 journal_id = %s and period_id = %s AND create_uid = %s AND state = %s \271 account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
265 ORDER BY id DESC limit 1',272 account = account_obj.browse(cr, uid, account, context=context)
266 (context['journal_id'], context['period_id'], uid, 'draft'))273 data['account_id'] = account and account.id or False
267 res = cr.fetchone()274 #compute the amount in secondary currency of the account, if needed
268 move_id = (res and res[0]) or False275 if account and account.currency_id:
269 if not move_id:276 data['currency_id'] = account.currency_id.id
270 return data277 #set the context for the multi currency change
271 else:278 compute_ctx = context.copy()
272 data['move_id'] = move_id279 compute_ctx.update({
273 if 'date' in fields:280 #the following 2 parameters are used to choose the currency rate, in case where the account
274 cr.execute('SELECT date \281 #doesn't work with an outgoing currency rate method 'at date' but 'average'
275 FROM \282 'res.currency.compute.account': account,
276 account_move_line \283 'res.currency.compute.account_invert': True,
277 WHERE \284 })
278 journal_id = %s AND period_id = %s AND create_uid = %s \285 if data.get('date'):
279 ORDER BY id DESC',286 compute_ctx.update({'date': data['date']})
280 (context['journal_id'], context['period_id'], uid))287 data['amount_currency'] = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], -total, context=compute_ctx)
281 res = cr.fetchone()288 data = self._default_get_move_form_hook(cr, uid, data)
282 if res:
283 data['date'] = res[0]
284 else:
285 period = period_obj.browse(cr, uid, context['period_id'],
286 context=context)
287 data['date'] = period.date_start
288 if not move_id:
289 return data
290 total = 0
291 ref_id = False
292 move = move_obj.browse(cr, uid, move_id, context=context)
293 if 'name' in fields:
294 data.setdefault('name', move.line_id[-1].name)
295 acc1 = False
296 for l in move.line_id:
297 acc1 = l.account_id
298 partner_id = partner_id or l.partner_id.id
299 ref_id = ref_id or l.ref
300 total += (l.debit or 0.0) - (l.credit or 0.0)
301
302 if 'ref' in fields:
303 data['ref'] = ref_id
304 if 'partner_id' in fields:
305 data['partner_id'] = partner_id
306
307 if move.journal_id.type == 'purchase':
308 if total > 0:
309 account = move.journal_id.default_credit_account_id
310 else:
311 account = move.journal_id.default_debit_account_id
312 else:
313 if total > 0:
314 account = move.journal_id.default_credit_account_id
315 else:
316 account = move.journal_id.default_debit_account_id
317 part = partner_id and partner_obj.browse(cr, uid, partner_id) or False
318 # part = False is acceptable for fiscal position.
319 account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
320 if account:
321 account = account_obj.browse(cr, uid, account, context=context)
322
323 if account and ((not fields) or ('debit' in fields) or ('credit' in fields)):
324 data['account_id'] = account.id
325 # Propose the price VAT excluded, the VAT will be added when confirming line
326 if account.tax_ids:
327 taxes = fiscal_pos_obj.map_tax(cr, uid, part and part.property_account_position or False, account.tax_ids)
328 tax = tax_obj.browse(cr, uid, taxes)
329 for t in tax_obj.compute_inv(cr, uid, tax, total, 1):
330 total -= t['amount']
331
332 s = -total
333 data['debit'] = s > 0 and s or 0.0
334 data['credit'] = s < 0 and -s or 0.0
335
336 if account and account.currency_id:
337 data['currency_id'] = account.currency_id.id
338 acc = account
339 if s>0:
340 acc = acc1
341 compute_ctx = context.copy()
342 compute_ctx.update({
343 'res.currency.compute.account': acc,
344 'res.currency.compute.account_invert': True,
345 })
346 v = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], s, context=compute_ctx)
347 data['amount_currency'] = v
348 return data289 return data
349290
350 def on_create_write(self, cr, uid, id, context=None):291 def on_create_write(self, cr, uid, id, context=None):