# flake8: noqa
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, api


class ResPartner(models.Model):

    _inherit = "res.partner"

    @api.multi
    def _credit_debit_get(self):
        # pylint: disable=E8103
        tables, where_clause, where_params = self.env['account.move.line']._query_get()
        # child_companies = self.env.user.company_id.child_ids
        # TODO habria que mejorarlo y evitar esta consulta adicional
        child_companies = self.env['res.company'].search(
            [('id', 'child_of', self.env.user.company_id.id)])
        where_params = [tuple(self.ids)] + [
            tuple(child_companies.ids)] + where_params
        if where_clause:
            where_clause = 'AND ' + where_clause
        self._cr.execute("""SELECT account_move_line.partner_id, act.type, SUM(account_move_line.amount_residual)
                      FROM account_move_line
                      LEFT JOIN account_account a ON (account_move_line.account_id=a.id)
                      LEFT JOIN account_account_type act ON (a.user_type_id=act.id)
                      WHERE act.type IN ('receivable','payable')
                      AND account_move_line.partner_id IN %s
                      AND account_move_line.reconciled IS FALSE
                      AND account_move_line.company_id IN %s
                      """ + where_clause + """
                      GROUP BY account_move_line.partner_id, act.type
                      """, where_params)
        for pid, type, val in self._cr.fetchall():
            partner = self.browse(pid)
            if type == 'receivable':
                partner.credit = val
            elif type == 'payable':
                partner.debit = -val

@@ -1918,12 +1918,1208 @@
 ebit = -val%0A
+%0A    @api.multi%0A    def _asset_difference_search(self, account_type, operator, operand):%0A        if operator not in ('%3C', '=', '%3E', '%3E=', '%3C='):%0A            return %5B%5D%0A        if type(operand) not in (float, int):%0A            return %5B%5D%0A        sign = 1%0A        if account_type == 'payable':%0A            sign = -1%0A        # We add to filter by childs companies if you are in parent company%0A        child_companies = self.env%5B'res.company'%5D.search(%0A            %5B('id', 'child_of', self.env.user.company_id.id)%5D)%0A        res = self._cr.execute('''%0A            SELECT partner.id%0A            FROM res_partner partner%0A            LEFT JOIN account_move_line aml ON aml.partner_id = partner.id%0A            RIGHT JOIN account_account acc ON aml.account_id = acc.id%0A            WHERE acc.internal_type = %25s%0A              AND NOT acc.deprecated AND acc.company_id IN %25s%0A            GROUP BY partner.id%0A            HAVING %25s * COALESCE(SUM(aml.amount_residual), 0) ''' + operator + ''' %25s''', (%0A            account_type, tuple(child_companies.ids), sign, operand))%0A        res = self._cr.fetchall()%0A        if not res:%0A            return %5B('id', '=', '0')%5D%0A        return %5B('id', 'in', %5Br%5B0%5D for r in res%5D)%5D%0A

Fix to show the partner with balance (#55)