241
241
date = strftime('%Y-%m-%d')
242
243
# Prepare some value
243
244
account = self.pool.get('account.analytic.account').browse(cr, uid, [account_id], context)[0]
244
245
context.update({'from': 'mass_reallocation'}) # this permits reallocation to be accepted when rewrite analaytic lines
245
correction_journal_ids = self.pool.get('account.analytic.journal').search(cr, uid, [('type', '=', 'correction'), ('is_current_instance', '=', True)])
246
move_prefix = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.instance_id.move_prefix
248
aaj_obj = self.pool.get('account.analytic.journal')
249
correction_journal_ids = aaj_obj.search(cr, uid, [('type', '=', 'correction'), ('is_current_instance', '=', True)])
246
250
correction_journal_id = correction_journal_ids and correction_journal_ids[0] or False
247
251
if not correction_journal_id:
248
252
raise osv.except_osv(_('Error'), _('No analytic journal found for corrections!'))
254
# sequence info from GL journal
255
aj_obj = self.pool.get('account.journal')
256
gl_correction_journal_ids = aj_obj.search(cr, uid, [('type', '=', 'correction'), ('is_current_instance', '=', True)])
257
gl_correction_journal_id = gl_correction_journal_ids and gl_correction_journal_ids[0] or False
258
if not gl_correction_journal_id:
259
raise osv.except_osv(_('Error'), _('No GL journal found for corrections!'))
260
gl_correction_journal_rec = aj_obj.browse(cr, uid, gl_correction_journal_id, context=context)
250
263
for aline in self.browse(cr, uid, ids, context=context):
251
264
if account.category in ['OC', 'DEST']:
255
268
fieldname = 'cost_center_id'
256
269
if account.category == 'DEST':
257
270
fieldname = 'destination_id'
258
# if period is not closed, so override line.
259
if period and period.state not in ['done', 'mission-closed']:
272
# update or reverse ?
273
update = period and period.state not in ['done', 'mission-closed']
274
if aline.journal_id.type == 'hq':
275
# US-773/2: if HQ entry always like period closed fashion
279
# not mission close: override line
260
280
# Update account # Date: UTP-943 speak about original date for non closed periods
261
self.write(cr, uid, [aline.id], {fieldname: account_id, 'date': aline.date,
262
'source_date': aline.source_date or aline.date}, context=context)
282
fieldname: account_id,
284
'source_date': aline.source_date or aline.date,
286
self.write(cr, uid, [aline.id], vals, context=context)
263
287
# else reverse line before recreating them with right values
289
# mission close or + or HQ entry: reverse
291
# compute entry sequence
292
seq_num_ctx = period and {'fiscalyear_id': period.fiscalyear_id.id} or None
293
seqnum = self.pool.get('ir.sequence').get_id(cr, uid, gl_correction_journal_rec.sequence_id.id, context=seq_num_ctx)
294
entry_seq = "%s-%s-%s" % (move_prefix, gl_correction_journal_rec.code, seqnum)
265
296
# First reverse line
266
297
rev_ids = self.pool.get('account.analytic.line').reverse(cr, uid, [aline.id], posting_date=date)
267
298
# UTP-943: Shoud have a correction journal on these lines
279
310
self.pool.get('account.analytic.line').write(cr, uid, cor_ids, {'last_corrected_id': aline.id})
280
311
# finally flag analytic line as reallocated
281
312
self.pool.get('account.analytic.line').write(cr, uid, [aline.id], {'is_reallocated': True})
314
if isinstance(rev_ids, (int, long, )):
316
if isinstance(cor_ids, (int, long, )):
318
for rev_cor_id in rev_ids + cor_ids:
319
cr.execute('update account_analytic_line set entry_sequence = %s where id = %s', (entry_seq, rev_cor_id))
284
322
self.write(cr, uid, [aline.id], {'account_id': account_id}, context=context)