← Back to team overview

c2c-oerpscenario team mailing list archive

[Bug 878286] [NEW] [V6.0.2]Wrong analytic account calculation

 

Public bug reported:

When you have several child analytic account. All the analytic accounts
between the root one and the last child are not well calculated.

Hereunder the proposal for correction :

file : analytic/analytic.py
replace the two functions in the file by this :
    def _compute_level_tree(self, cr, uid, ids, child_ids, account_root_ids, res, field_names, context=None):
        def recursive_computation(account_id, res):
            currency_obj = self.pool.get('res.currency')
            account = self.browse(cr, uid, account_id)
            for son in account.child_ids:
                res = recursive_computation(son.id, res)
                for field in field_names:
                    if account.currency_id.id == son.currency_id.id or field=='quantity':
                        res[account.id][field] += res[son.id][field]
                    else:
                        res[account.id][field] += currency_obj.compute(cr, uid, son.currency_id.id, account.currency_id.id, res[son.id][field], context=context)
            return res
        for account in self.browse(cr, uid, ids, context=context):
            if account.id in account_root_ids:
                res = recursive_computation(account.id, res)
        return res

    def _debit_credit_bal_qtty(self, cr, uid, ids, name, arg, context=None):
        res = {}
        if context is None:
            context = {}
        child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
        account_root_ids = tuple(self.search(cr, uid, [('parent_id', '=', '')]))
        for i in child_ids:
            res[i] =  {}
            for n in name:
                res[i][n] = 0.0

        if not child_ids:
            return res

        where_date = ''
        where_clause_args = [tuple(child_ids)]
        if context.get('from_date', False):
            where_date += " AND l.date >= %s"
            where_clause_args  += [context['from_date']]
        if context.get('to_date', False):
            where_date += " AND l.date <= %s"
            where_clause_args += [context['to_date']]
        cr.execute("""
              SELECT DISTINCT a.id,
                     sum(
                         CASE WHEN l.amount > 0
                         THEN l.amount
                         ELSE 0.0
                         END
                          ) as debit,
                     sum(
                         CASE WHEN l.amount < 0
                         THEN -l.amount
                         ELSE 0.0
                         END
                          ) as credit,
                     COALESCE(SUM(l.amount),0) AS balance,
                     COALESCE(SUM(l.unit_amount),0) AS quantity
              FROM account_analytic_account a
                  LEFT JOIN account_analytic_line l ON (a.id = l.account_id)
              WHERE a.id IN %s
              """ + where_date + """
              GROUP BY a.id""", where_clause_args)
        for ac_id, debit, credit, balance, quantity in cr.fetchall():
			res[ac_id] = {'debit': debit, 'credit': credit, 'balance': balance, 'quantity': quantity}
        return self._compute_level_tree(cr, uid, ids, child_ids, account_root_ids, res, ['debit', 'credit', 'balance', 'quantity'], context)

** Affects: openobject-addons
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of C2C
OERPScenario, which is subscribed to OpenERP Project Group.
https://bugs.launchpad.net/bugs/878286

Title:
  [V6.0.2]Wrong analytic account calculation

Status in OpenERP Addons (modules):
  New

Bug description:
  When you have several child analytic account. All the analytic
  accounts between the root one and the last child are not well
  calculated.

  Hereunder the proposal for correction :

  file : analytic/analytic.py
  replace the two functions in the file by this :
      def _compute_level_tree(self, cr, uid, ids, child_ids, account_root_ids, res, field_names, context=None):
          def recursive_computation(account_id, res):
              currency_obj = self.pool.get('res.currency')
              account = self.browse(cr, uid, account_id)
              for son in account.child_ids:
                  res = recursive_computation(son.id, res)
                  for field in field_names:
                      if account.currency_id.id == son.currency_id.id or field=='quantity':
                          res[account.id][field] += res[son.id][field]
                      else:
                          res[account.id][field] += currency_obj.compute(cr, uid, son.currency_id.id, account.currency_id.id, res[son.id][field], context=context)
              return res
          for account in self.browse(cr, uid, ids, context=context):
              if account.id in account_root_ids:
                  res = recursive_computation(account.id, res)
          return res

      def _debit_credit_bal_qtty(self, cr, uid, ids, name, arg, context=None):
          res = {}
          if context is None:
              context = {}
          child_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
          account_root_ids = tuple(self.search(cr, uid, [('parent_id', '=', '')]))
          for i in child_ids:
              res[i] =  {}
              for n in name:
                  res[i][n] = 0.0

          if not child_ids:
              return res

          where_date = ''
          where_clause_args = [tuple(child_ids)]
          if context.get('from_date', False):
              where_date += " AND l.date >= %s"
              where_clause_args  += [context['from_date']]
          if context.get('to_date', False):
              where_date += " AND l.date <= %s"
              where_clause_args += [context['to_date']]
          cr.execute("""
                SELECT DISTINCT a.id,
                       sum(
                           CASE WHEN l.amount > 0
                           THEN l.amount
                           ELSE 0.0
                           END
                            ) as debit,
                       sum(
                           CASE WHEN l.amount < 0
                           THEN -l.amount
                           ELSE 0.0
                           END
                            ) as credit,
                       COALESCE(SUM(l.amount),0) AS balance,
                       COALESCE(SUM(l.unit_amount),0) AS quantity
                FROM account_analytic_account a
                    LEFT JOIN account_analytic_line l ON (a.id = l.account_id)
                WHERE a.id IN %s
                """ + where_date + """
                GROUP BY a.id""", where_clause_args)
          for ac_id, debit, credit, balance, quantity in cr.fetchall():
  			res[ac_id] = {'debit': debit, 'credit': credit, 'balance': balance, 'quantity': quantity}
          return self._compute_level_tree(cr, uid, ids, child_ids, account_root_ids, res, ['debit', 'credit', 'balance', 'quantity'], context)

To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-addons/+bug/878286/+subscriptions


Follow ups

References