AND (
:contract_number IS NULL
OR :contract_number = ''
OR contract.number ILIKE '%' || replace(:contract_number, chr(92), chr(92) || chr(92)) || '%' ESCAPE chr(92)
)
AND (
:client_full_name IS NULL
OR :client_full_name = ''
OR (
coalesce(apdi.last_name, '') || ' ' ||
coalesce(apdi.first_name, '') || ' ' ||
coalesce(apdi.middle_name, '')
) ILIKE '%' || :client_full_name || '%'
)
AND (
:contract_activity_type IS NULL
OR :contract_activity_type = ''
OR sat.service_type = cast(:contract_activity_type AS ourpension.service_type)
)
AND (
:account_number IS NULL
OR :account_number = ''
OR sh.number ILIKE '%' || :account_number || '%'
)
AND (
:created_at_from IS NULL
OR :created_at_from = ''
OR td.version >= to_date(:created_at_from, 'YYYY-MM-DD')
)
AND (
:created_at_to IS NULL
OR :created_at_to = ''
OR td.version < to_date(:created_at_to, 'YYYY-MM-DD') + interval '1 day'
)