from odoo import fields, models
from odoo.tools import SQL


class AccountMove(models.Model):
    _name = "account.move"
    _inherit = "account.move"

    impacting_cash_basis = fields.Boolean(store=False, search='_search_impacting_cash_basis')

    def _search_impacting_cash_basis(self, operator, value):

        sql = SQL("""(
            WITH moves_with_receivable_payable AS (
                SELECT DISTINCT aml.move_id as id
                FROM account_move_line aml
                JOIN account_account account ON aml.account_id = account.id
                WHERE account.account_type IN ('asset_receivable', 'liability_payable')
            ), 
            reconciled_move_on_receivable_payable AS (
                SELECT DISTINCT aml.move_id as id
                FROM account_partial_reconcile part
                JOIN account_move_line aml ON aml.id = part.debit_move_id OR aml.id = part.credit_move_id
                JOIN account_account account ON aml.account_id = account.id
                WHERE account.account_type IN ('asset_receivable', 'liability_payable')
            )
            SELECT DISTINCT move.id
            FROM account_move move
            LEFT JOIN account_journal journal ON journal.id = move.journal_id
            LEFT JOIN moves_with_receivable_payable move_rp on move_rp.id = move.id
            LEFT JOIN reconciled_move_on_receivable_payable rec_move on rec_move.id = move.id
            WHERE
                journal.type IN ('cash', 'bank')
                OR
                move_rp.id IS NULL
                OR 
                rec_move.id IS NOT NULL
        )""")

        op = 'in' if (operator == '=') ^ (value is False) else 'not in'
        return [('id', op, sql)]
