AND (
cast(:registry_date_from as date) IS NOT NULL
OR cast(:registry_date_to as date) IS NOT NULL
OR cast(:registry_number as varchar) IS NOT NULL
OR cast(:sent_date_from as date) IS NOT NULL
OR cast(:sent_date_to as date) IS NOT NULL
OR cast(:tax_period_year as integer) IS NOT NULL
OR cast(:status as varchar) IS NOT NULL
)
AND r.status <> 'DELETED'
AND (cast(:registry_date_from as date) IS NULL
OR r.registry_date >= cast(:registry_date_from as date))
AND (cast(:registry_date_to as date) IS NULL
OR r.registry_date <= cast(:registry_date_to as date))
AND (cast(:registry_number as varchar) IS NULL
OR r.registry_number = cast(:registry_number as varchar))
AND (cast(:sent_date_from as date) IS NULL
OR r.sent_to_fns_date >= cast(:sent_date_from as date))
AND (cast(:sent_date_to as date) IS NULL
OR r.sent_to_fns_date <= cast(:sent_date_to as date))
AND (cast(:tax_period_year as integer) IS NULL
OR r.tax_period_year = cast(:tax_period_year as integer))
AND (cast(:status as varchar) IS NULL
OR r.status = cast(:status as varchar))