SELECT
r.id AS id,
r."number" AS registry_number,
to_char(r."date", 'DD.MM.YYYY') AS registry_date,
r."date" AS registry_date_raw,
r.sent AS sent_to_fns_date,
r."year" AS tax_period_year,
r.sample_status AS status,
r.guid AS guid
FROM report.smev_register r
WHERE r.smev_register_type_code = 'УНВ'
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.sample_status <> 'Удалён'
AND (cast(:registry_date_from as date) IS NULL
OR r."date" >= cast(:registry_date_from as date))
AND (cast(:registry_date_to as date) IS NULL
OR r."date" <= cast(:registry_date_to as date))
AND (cast(:registry_number as varchar) IS NULL
OR r."number" = cast(:registry_number as varchar))
AND (cast(:sent_date_from as date) IS NULL
OR r.sent >= cast(:sent_date_from as date))
AND (cast(:sent_date_to as date) IS NULL
OR r.sent <= cast(:sent_date_to as date))
AND (cast(:tax_period_year as integer) IS NULL
OR r."year" = cast(:tax_period_year as integer))
AND (cast(:status as varchar) IS NULL
OR r.sample_status = cast(:status as varchar))