params
WITH q AS (
SELECT DISTINCT ON (c.number)
atc.id AS id,
c.id AS contract_id,
i.id AS fl_id
FROM ourpension.contract c
LEFT JOIN ourpension.individual i
ON i.id = c.individual_id
JOIN ourpension.sharer sh
ON c.id = sh.contract_id
LEFT JOIN ourpension.application_termination_contract atc
ON atc.sharer_id = sh.id
LEFT JOIN ourpension.uv_activity_type uat
ON uat.activity_eng_type = c.service_type
AND uat.client_type = c.client_type
WHERE atc."date" >=
CASE
WHEN :start_date_app = 'now()'
THEN current_date
WHEN :start_date_app = 'now() - interval 24 hours'
THEN current_date - interval '1 day'
ELSE to_date(:start_date_app, 'YYYY.MM.DD')
END
AND atc."date" <
CASE
WHEN :end_date_app = 'now()'
THEN current_date
WHEN :end_date_app = 'now() - interval 24 hours'
THEN current_date - interval '1 day'
ELSE to_date(:end_date_app, 'YYYY.MM.DD')
END + interval '1 day'
AND i.first_name IS NOT NULL
AND (
:isChecked = false
OR NOT EXISTS (
SELECT 1
FROM ourpension.year_tax_deduction td
WHERE td.application_termination_contract_id = atc.id
AND td.type IN ('RECEIVED', 'NOT_RECEIVED')
)
)
AND (
:contract_activity_type IS NULL
OR :contract_activity_type = ''
OR :contract_activity_type = 'ALL'
OR uat.activity_rus_type = :contract_activity_type
)
)
SELECT
q.id,
q.contract_id,
q.fl_id
FROM q
WHERE EXISTS (
SELECT 1
FROM tools.selection s
WHERE s.row_id = q.id
AND s.username = :username
AND s.code = :query_table_code
)
#set ( $l = $db.find($params) )
{
"list": [
#if ($l)
#foreach ( $t in $l )
{
"id": "$t.id",
"contract_id": "$t.contract_id",
"fl_id": "$t.fl_id"
}#if($foreach.hasNext),#end
#end
#end
]
}