'Get statement lines of the specified statements or all unreconciled statement lines and try to automatically reconcile them / find them a partner.
Return ids of statement lines left to reconcile and other data for the reconciliation widget.'

@api.multi
def reconciliation_widget_preprocess(self):

    statements = self
    bsl_obj = self.env['account.bank.statement.line']
    sql_query = 'SELECT    stl.id    \n                                                                                                FROM    account_bank_statement_line    stl        \n                                                                                                WHERE    account_id    IS    NULL    AND    not    exists    (select    1    from    account_move    m    where    m.statement_line_id    =    stl.id)\n                                                                                                                AND    company_id    =    %s\n                                                                '
    params = (self.env.user.company_id.id,)
    if statements:
        sql_query += '    AND    stl.statement_id    IN    %s'
        params += (tuple(statements.ids),)
    sql_query += '    ORDER    BY    stl.id'
    self.env.cr.execute(sql_query, params)
    st_lines_left = self.env['account.bank.statement.line'].browse([line.get('id') for line in self.env.cr.dictfetchall()])
    stl_to_assign_partner = [stl.id for stl in st_lines_left if (not stl.partner_id)]
    refs = list(set([st.name for st in st_lines_left if (not stl.partner_id)]))
    if (st_lines_left and stl_to_assign_partner and refs and st_lines_left[0].journal_id.default_credit_account_id and st_lines_left[0].journal_id.default_debit_account_id):
        sql_query = "SELECT    aml.partner_id,    aml.ref,    stl.id\n                                                                                                                FROM    account_move_line    aml\n                                                                                                                                JOIN    account_account    acc    ON    acc.id    =    aml.account_id\n                                                                                                                                JOIN    account_bank_statement_line    stl    ON    aml.ref    =    stl.name\n                                                                                                                WHERE    (aml.company_id    =    %s    \n                                                                                                                                AND    aml.partner_id    IS    NOT    NULL)    \n                                                                                                                                AND    (\n                                                                                                                                                (aml.statement_id    IS    NULL    AND    aml.account_id    IN    %s)    \n                                                                                                                                                OR    \n                                                                                                                                                (acc.internal_type    IN    ('payable',    'receivable')    AND    aml.reconciled    =    false)\n                                                                                                                                                )\n                                                                                                                                AND    aml.ref    IN    %s\n                                                                                                                                "
        params = (self.env.user.company_id.id, (st_lines_left[0].journal_id.default_credit_account_id.id, st_lines_left[0].journal_id.default_debit_account_id.id), tuple(refs))
        if statements:
            sql_query += 'AND    stl.id    IN    %s'
            params += (tuple(stl_to_assign_partner),)
        self.env.cr.execute(sql_query, params)
        results = self.env.cr.dictfetchall()
        st_line = self.env['account.bank.statement.line']
        for line in results:
            st_line.browse(line.get('id')).write({'partner_id': line.get('partner_id')})
    return {'st_lines_ids': st_lines_left.ids, 'notifications': [], 'statement_name': (((len(statements) == 1) and statements[0].name) or False), 'num_already_reconciled_lines': 0}
