~unifield-team/unifield-wm/us-738

« back to all changes in this revision

Viewing changes to analytic_distribution/analytic_line.py

  • Committer: jf
  • Date: 2015-12-04 08:52:23 UTC
  • mfrom: (2692.1.4 unifield-wm)
  • Revision ID: jfb@tempo-consulting.fr-20151204085223-zbfi2rbtd0fx3cv1
US-773 [FIX] Double analytic correction of HQ entries: keep the initial dates
US-773 [FIX] Mass reallocation: reverse HQ entries
US-773 [FIX] Mass reallocation: reversed entries should have seq number from OD journal
lp:~unifield-team/unifield-wm/us-773

Show diffs side-by-side

added added

removed removed

Lines of Context:
239
239
            return False
240
240
        if not date:
241
241
            date = strftime('%Y-%m-%d')
 
242
 
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
 
247
 
 
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!'))
 
253
 
 
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)
 
261
 
249
262
        # Process lines
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']:
 
271
 
 
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
 
276
                    update = False
 
277
 
 
278
                if update:
 
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)
 
281
                    vals = {
 
282
                        fieldname: account_id,
 
283
                        'date': aline.date,
 
284
                        'source_date': aline.source_date or aline.date,
 
285
                    }
 
286
                    self.write(cr, uid, [aline.id], vals, context=context)
263
287
                # else reverse line before recreating them with right values
264
288
                else:
 
289
                    # mission close or + or HQ entry: reverse
 
290
 
 
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)
 
295
 
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})
 
313
 
 
314
                    if isinstance(rev_ids, (int, long, )):
 
315
                        rev_ids = [rev_ids]
 
316
                    if isinstance(cor_ids, (int, long, )):
 
317
                        cor_ids = [cor_ids]
 
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))
282
320
            else:
283
321
                # Update account
284
322
                self.write(cr, uid, [aline.id], {'account_id': account_id}, context=context)