Master reconciliation and statement import ama

ideally, this query and the one from get_account_data_for_manual_reconciliation should be factorized.
I'd suggest to:
*remove the right join with partner (it's just to get the partner name where you already have the partner_id), can be done in post-processing. I don't know if it can slow down the querying or speed it... but it makes the query more simple.
*the condition a.type not in ('payable', 'receivable') can be given in the additional condition under the account_id_condition variable
*so, you should be able to use something like this:
if account: 
    table = 'a'
else
    table = 'p'
query =  """SELECT partner_id, to_char(last_time_entries_checked, 'YYYY-MM-DD') AS last_time_entries_checked, account_id, account_name, account_code FROM (
                    SELECT partner_id, last_time_entries_checked, account_id, account_name, account_code,
                        MAX(max_date) AS max_date,
                        SUM(debit) AS debit,
                        SUM(credit) AS credit
                    FROM (
                        -- The first subrequest is required in order for all lines in a partial reconciliations
                        -- to be considered as ONE debit OR ONE credit (since amount_residual isn't stored in the DB) 
                        SELECT p.id AS partner_id,"""
query += table + """
                            .last_time_entries_checked,
                            a.id AS account_id,
                            a.name AS account_name,
                            a.code AS account_code,
                            MAX(l.create_date) AS max_date,
                            CASE WHEN SUM(l.debit) - SUM(l.credit) > 0 THEN SUM(l.debit) - SUM(l.credit) ELSE 0 END AS debit,
                            CASE WHEN SUM(l.credit) - SUM(l.debit) > 0 THEN SUM(l.credit) - SUM(l.debit) ELSE 0 END AS credit
                        FROM account_move_line l
                        RIGHT JOIN account_account a ON (a.id = l.account_id)
                        WHERE a.reconcile IS TRUE
                        AND l.reconcile_id IS NULL
                        AND l.state <> 'draft'
                        AND l.reconcile_partial_id IS NOT NULL
                        %s
                        GROUP BY l.reconcile_partial_id, a.id
                        UNION
                        SELECT p.id AS partner_id,"""
query += table+"""                            .last_time_entries_checked,
                            a.id AS account_id,
                            a.name AS account_name,
                            a.code AS account_code,
                            MAX(l.create_date) AS max_date,
                            SUM(l.debit) AS debit,
                            SUM(l.credit) AS credit
                        FROM account_move_line l
                        RIGHT JOIN account_account a ON (a.id = l.account_id)
                        WHERE a.reconcile IS TRUE
                        AND l.reconcile_id IS NULL
                        AND l.state <> 'draft'
                        AND reconcile_partial_id IS NULL
                        %s
                        GROUP BY a.id
                    ) AS s 
                    GROUP BY partner_id, last_time_entries_checked, account_id, account_name, account_code
                ) AS s
                WHERE debit > 0
                AND credit > 0
                AND (last_time_entries_checked IS NULL OR max_date > last_time_entries_checked)
                ORDER BY last_time_entries_checked"""