<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>info_recruits</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2024/08/15 14:02:51.713</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/15 14:02:51.713</modified_date>
  </info>
  <notepads>
    <notepad>
      <backgroundcolorblue>251</backgroundcolorblue>
      <backgroundcolorgreen>232</backgroundcolorgreen>
      <backgroundcolorred>201</backgroundcolorred>
      <bordercolorblue>90</bordercolorblue>
      <bordercolorgreen>58</bordercolorgreen>
      <bordercolorred>14</bordercolorred>
      <fontbold>N</fontbold>
      <fontcolorblue>90</fontcolorblue>
      <fontcolorgreen>58</fontcolorgreen>
      <fontcolorred>14</fontcolorred>
      <fontitalic>N</fontitalic>
      <fontname>Segoe UI</fontname>
      <fontsize>9</fontsize>
      <height>394</height>
      <xloc>960</xloc>
      <yloc>16</yloc>
      <note>acquired_citizenship
address_actual
address_place_stay
city_birth
date_info_actual
date_loss_info_actual
died_date
dispensary_registration_code
email
end_date_place_stay
end_date_residence
inn
is_in_prison
medic
medical_requirements
region_birth
residence
right_deferment
snils
source_info_actual
start_date_place_stay
start_date_residence
took_break
type_place_stay</note>
      <width>163</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Select values 4</from>
      <to>Table output</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Select values 4</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Select values 4</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>100</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>age</name>
        <rename>age</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_place</name>
        <rename>birth_place</rename>
        <update>Y</update>
      </value>
      <value>
        <name>contract_service</name>
        <rename>contract_service</rename>
        <update>Y</update>
      </value>
      <value>
        <name>deferment_liberation</name>
        <rename>deferment_liberation</rename>
        <update>Y</update>
      </value>
      <value>
        <name>disability_group</name>
        <rename>disability_group</rename>
        <update>Y</update>
      </value>
      <value>
        <name>driver_license</name>
        <rename>driver_license</rename>
        <update>Y</update>
      </value>
      <value>
        <name>education</name>
        <rename>education</rename>
        <update>Y</update>
      </value>
      <value>
        <name>employment</name>
        <rename>employment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>gender</name>
        <rename>gender</rename>
        <update>Y</update>
      </value>
      <value>
        <name>is_registered</name>
        <rename>is_registered</rename>
        <update>Y</update>
      </value>
      <value>
        <name>is_wanted</name>
        <rename>is_wanted</rename>
        <update>Y</update>
      </value>
      <value>
        <name>issue_date</name>
        <rename>issue_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marital_status</name>
        <rename>marital_status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>mobilization</name>
        <rename>mobilization</rename>
        <update>Y</update>
      </value>
      <value>
        <name>number_children</name>
        <rename>number_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>number_children_18_old</name>
        <rename>number_children_18_old</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_number</name>
        <rename>passport_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_series</name>
        <rename>passport_series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>phone</name>
        <rename>phone</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_registration</name>
        <rename>reason_registration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sports_category</name>
        <rename>sports_category</rename>
        <update>Y</update>
      </value>
      <value>
        <name>tractor_license</name>
        <rename>tractor_license</rename>
        <update>Y</update>
      </value>
      <value>
        <name>update_date</name>
        <rename>update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>urgent_service</name>
        <rename>urgent_service</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values 4</name>
    <type>SelectValues</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <select_unspecified>N</select_unspecified>
      <meta>
        <name>issue_date</name>
        <rename>issue_date</rename>
        <type>Date</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask>yyyy-MM-dd</conversion_mask>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
    </fields>
    <attributes/>
    <GUI>
      <xloc>448</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH extracted_children AS (
    SELECT
        ri.recruit_id,
        jsonb_array_elements_text(ri.info->'svedDeti'->'rebenok') AS child
    FROM
        public.recruits_info ri
    WHERE
        jsonb_typeof(ri.info->'svedDeti'->'rebenok') = 'array'
),
children_birth_dates AS (
    SELECT
        recruit_id,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'den')::int AS day,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'mesyacz')::int AS month,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'-&gt;&gt;'god')::int AS year
    FROM
        extracted_children
),
children_count AS (
    SELECT
        recruit_id,
        COUNT(*) AS children_under_16
    FROM
        children_birth_dates
    WHERE
        AGE(make_date(year, month, day)) &lt; interval '16 years'
    GROUP BY
        recruit_id
),
registration_reasons AS (
    SELECT mrr.value AS reason_registration,
    r.id AS recruit_id
    FROM public.recruits AS r
    JOIN public.military_registration_reason AS mrr
    ON r.registration_reasons::jsonb @> to_jsonb(mrr.code::text)
),

driver_license AS (
    SELECT 
        ri.recruit_id,
        COALESCE(
            string_agg(sved_vodit->>'kategoriya', ', '),
            'Нет информации'
        ) AS driver_license
    FROM 
        public.recruits_info ri
    LEFT JOIN LATERAL 
        jsonb_array_elements(ri.info->'svedVoditUdost'->'voditUdost'->'svedKat') AS sved_vodit 
    ON true
    GROUP BY ri.recruit_id
),

sports_category AS ( -- проверка на наличие спортивного разряда или звания
    SELECT 
    ri.recruit_id,
    CASE
        WHEN 
            -- Условия для "true" (спортивная категория)
            ri.info->'svedSport' ? 'sport'  -- Проверяем, что ключ 'sport' существует
            AND jsonb_typeof(ri.info->'svedSport'->'sport') = 'array'  -- Проверяем, что 'sport' это массив
            AND jsonb_array_length(ri.info->'svedSport'->'sport') > 0  -- И массив не пустой
            AND EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedSport'->'sport') AS sport_elem
                WHERE 
                    (
                        -- Проверяем, что 'sportRazr' существует и срок действия актуален
                        jsonb_typeof(sport_elem->'sportRazr') = 'object' 
                        AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                        AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') > CURRENT_DATE
                    )
                    OR (
                        -- Проверяем, что 'sportZvan' существует и не пусто
                        sport_elem->>'sportZvan' IS NOT NULL 
                        AND sport_elem->>'sportZvan' &lt;&gt; ''
                    )
                    OR (
                        -- Проверяем, что 'sportRazr' существует и срок действия истек
                        jsonb_typeof(sport_elem->'sportRazr') = 'object' 
                        AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                        AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') &lt;= CURRENT_DATE
                    )
            )
            AND ri.info->'svedSport'->>'prOtsSvedSport' IS NULL
        THEN true
        
        WHEN 
            -- Условия для "false" (не является спортивной категорией)
            (
                ri.info->'svedSport'->'sport' IS NULL  -- Если 'sport' = NULL
                OR (
                    ri.info->'svedSport' ? 'sport'  -- Проверяем, что ключ 'sport' существует
                    AND jsonb_typeof(ri.info->'svedSport'->'sport') = 'array'  -- Поле 'sport' должно быть массивом
                    AND jsonb_array_length(ri.info->'svedSport'->'sport') > 0  -- И массив не пустой
                )
            )
            AND EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedSport'->'sport') AS sport_elem
                WHERE 
                    (
                        -- Проверяем 'sportZvan' и если оно не существует или пусто
                        sport_elem->>'sportZvan' IS NULL OR sport_elem->>'sportZvan' = ''
                    )
                    OR (
                        -- Проверяем 'sportRazr' и срок действия истек
                        jsonb_typeof(sport_elem->'sportRazr') = 'object'
                        AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                        AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') > CURRENT_DATE
                        -- И 'sportZvan' также не должно существовать или быть пустым
                        AND (sport_elem->>'sportZvan' IS NULL OR sport_elem->>'sportZvan' = '')
                    )
            )
            AND ri.info->'svedSport'->>'prOtsSvedSport' IS NULL
        THEN false
        
        WHEN 
            -- Условия для "null" (нет информации)
            ri.info->'svedSport'->>'prOtsSvedSport' = '1'
            OR ri.info->'svedSport' IS NULL  -- Проверяем, что 'svedSport' отсутствует
        THEN null
        
        ELSE null  -- Неопределенное значение для всех других случаев
    END AS sports_category
    FROM public.recruits_info ri
),

disability_group AS (
    SELECT
        ri.recruit_id,
        CASE
            WHEN ri.info->'svedInvalid'->'invalid'->>'gruppa' IS NOT NULL 
                AND ri.info->'svedInvalid'->>'prOtsSvedInvalid' IS NULL
                AND (ri.info->'sportRazr'->>'dataSnyat' IS NULL 
                     OR TO_DATE(ri.info->'sportRazr'->>'dataSnyat', 'YYYY-MM-DD') &lt;= CURRENT_DATE)
            THEN ri.info->'svedInvalid'->'invalid'->>'gruppa'
            WHEN ri.info->'svedInvalid'->>'prOtsSvedInvalid' = '1' THEN 'Нет информации об инвалидности'
            ELSE 'Нет информации об инвалидности'
        END AS disability_group
    FROM public.recruits_info ri
),

tractor_license AS (
    SELECT
    ri.recruit_id, 
    CASE
        WHEN ri.info->'svedUdostrTraktMash'->>'udostrTraktMash' IS NOT NULL
        AND TO_DATE(ri.info->'svedUdostrTraktMash'->'udostrTraktMash'->>'dataOkonchUTM', 'YYYY-MM-DD') > CURRENT_DATE
        THEN true
        WHEN ri.info->'svedUdostrTraktMash'->>'udostrTraktMash' IS NOT NULL
        AND TO_DATE(ri.info->'svedUdostrTraktMash'->'udostrTraktMash'->>'dataOkonchUTM', 'YYYY-MM-DD') &lt;= CURRENT_DATE
        THEN false  
        WHEN ri.info->'svedUdostrTraktMash'->>'prOtsSvedUTM' = '1' THEN null
        ELSE null
    END AS tractor_license
    FROM public.recruits_info ri
),

is_wanted AS (
    SELECT
        ri.recruit_id,
        CASE
            WHEN
                jsonb_typeof(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') = 'array' -- проверяем, что 'faktyUgolovPresl' это массив
                AND jsonb_array_length(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') > 0 -- массив не пустой
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') AS criminal_prosecution
                    WHERE
                        criminal_prosecution->>'dataPrekrashh' IS NULL -- дата прекращения отсутствует
                )
                AND ri.info->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' IS NULL
            THEN true

            WHEN 
                jsonb_typeof(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') = 'array' -- проверяем, что 'faktyUgolovPresl' это массив
                AND jsonb_array_length(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') > 0 -- массив не пустой
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedUgolovPresl'->'faktyUgolovPresl') AS criminal_prosecution
                    WHERE
                        criminal_prosecution->>'dataPrekrashh' IS NOT NULL -- дата прекращения не отсутствует
                )
                AND ri.info->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' IS NULL
            THEN false

            WHEN
                ri.info->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' = '1'
            THEN NULL

            ELSE NULL
        END AS criminal_prosecution
    FROM public.recruits_info ri
),

employment AS (
    SELECT
        ri.recruit_id,
        COALESCE(
            CASE
                WHEN (
                    jsonb_typeof(info->'svedVUZ'->'vuz') = 'array' 
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedVUZ'->'vuz') AS vuz
                        WHERE vuz->>'kodStatus' IN ('1', '2', '4')
                    )
                ) OR (
                    jsonb_typeof(info->'svedKolledzh'->'kolledzh') = 'array'
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedKolledzh'->'kolledzh') AS kolledzh
                        WHERE kolledzh->>'kodStatus' IN ('1', '2', '4')
                    )
                ) THEN 'Обучается'
                ELSE NULL
            END ||
            CASE
                WHEN jsonb_typeof(info->'svedTrud'->'trudDeyat') = 'array'
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedTrud'->'trudDeyat') AS trud
                        WHERE trud->>'prAktMestRab' = '1'
                    ) THEN
                        CASE
                            WHEN (
                                jsonb_typeof(info->'svedVUZ'->'vuz') = 'array'
                                AND EXISTS (
                                    SELECT 1
                                    FROM jsonb_array_elements(info->'svedVUZ'->'vuz') AS vuz
                                    WHERE vuz->>'kodStatus' IN ('1', '2', '4')
                                )
                            ) OR (
                                jsonb_typeof(info->'svedKolledzh'->'kolledzh') = 'array'
                                AND EXISTS (
                                    SELECT 1
                                    FROM jsonb_array_elements(info->'svedKolledzh'->'kolledzh') AS kolledzh
                                    WHERE kolledzh->>'kodStatus' IN ('1', '2', '4')
                                )
                            ) THEN ', Работает'
                            ELSE 'Работает'
                        END
                ELSE NULL
            END,
            CASE
                WHEN
                    info->'svedVUZ'->>'prOtsSvedVUZ' = '1' -- Отсутствие сведений о вузе
                    AND info->'svedKolledzh'->>'prOtsSvedKolledzh' = '1' -- Отсутствие сведений о колледже
                    AND info->'svedTrud'->>'prOtsSvedTrud' = '1' -- Отсутствие сведений о работе
                THEN 'Нет информации'
                ELSE 'Нет информации'
            END
        ) AS employment_status
    FROM public.recruits_info ri
)

SELECT 
    r.full_name, 
    r.last_name, 
    r.first_name, 
    r.middle_name, 
    r.gender, 
    r.birth_date,  
    EXTRACT(YEAR FROM AGE(r.birth_date)) AS age,
    r.birth_place,
    r.phone,
    r.conscription AS deferment_liberation,

    CASE 
        WHEN (EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 18 AND 70 AND r.gender = 'MALE' AND 
              (r.conscription IS NULL OR r.conscription = false) AND 
              COALESCE(cc.children_under_16, 0) &lt; 5
          AND r.vu_current_info ->> 'isMilitaryRegistered' = 'true') 
            OR 
            (EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 18 AND 45 AND r.gender = 'FEMALE' AND 
             (r.conscription IS NULL OR r.conscription = false) AND 
             COALESCE(cc.children_under_16, 0) &lt; 5
          AND r.vu_current_info ->> 'isMilitaryRegistered' = 'true') 
        THEN true
        ELSE false
    END AS mobilization,

    CASE 
        WHEN (EXTRACT(YEAR FROM AGE(NOW(), birth_date)) BETWEEN 18 AND 30 
              AND gender = 'MALE' -- мужчины от 18 до 30 лет
              AND (conscription IS NULL OR conscription = false)
          AND r.vu_current_info ->> 'isMilitaryRegistered' = 'true')
        THEN true
        ELSE false
    END AS urgent_service,

    CASE WHEN (EXTRACT(YEAR FROM AGE(NOW(), birth_date)) BETWEEN 18 AND 50 
                AND gender = 'MALE' -- мужчины от 18 до 50 лет
                AND (conscription IS NULL OR conscription = false) -- отсутствие отсрочки
                AND ri.info->'svedSudim'->>'prOtsSvedSudim' = '1' -- признак отсутствия данных о судимости
        AND r.vu_current_info ->> 'isMilitaryRegistered' = 'true')
        THEN true
        ELSE false
    END AS contract_service,

    CASE 
        WHEN r.vu_current_info ->> 'isMilitaryRegistered' = 'true' THEN 'Состоит на воинском учете'
        WHEN r.vu_current_info ->> 'isMilitaryRegistered' = 'false' THEN 'Не состоит на воинском учете'
        ELSE 'Нет информации'
    END AS is_registered,

CASE 
        WHEN (EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' IN ('1', '2', '3', '4', '5')) 
            OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' IN ('1', '2', '3', '4', '5')
            )) THEN 'Высшее'
        WHEN (EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' = '10') 
            OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' = '10'
            )) THEN 'Среднее профессиональное'
        WHEN (EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' IN ('7', '9')) 
            OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' IN ('7', '9')
            )) THEN 'Общее'
        WHEN ((ri.info->'svedRObr'->>'prOtsRObr' = '1' OR ri.info->'svedInObr'->>'prOtsInObr' = '1')) THEN 'Нет данных'
        ELSE 'Не указано'  -- Добавлено условие по умолчанию
    END AS education,
    
    ri.info->'svedSemPolozh'->>'semPolozhNaim' AS marital_status,

    (SELECT COUNT(*) 
     FROM jsonb_array_elements(ri.info->'svedDeti'->'rebenok') AS childs
     WHERE jsonb_typeof(ri.info->'svedDeti'->'rebenok') = 'array'
       AND age(make_date(NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'god', '')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'mesyacz','')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'den','')::int)) &lt; make_interval(years => 18)
    ) AS number_children, 

    (SELECT COUNT(*) 
     FROM jsonb_array_elements(ri.info->'svedDeti'->'rebenok') AS childs
     WHERE jsonb_typeof(ri.info->'svedDeti'->'rebenok') = 'array'
       AND age(make_date(NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'god', '')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'mesyacz','')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'den','')::int)) >= make_interval(years => 18)
    ) AS number_children_18_old, 

    ri.info->'pasportRF'->'aktPasportRF'->>'nomDok' AS passport_number,
    ri.info->'pasportRF'->'aktPasportRF'->>'serDok' AS passport_series,
    ri.info->'svedVoin'->'voin'->>'dataReg' AS issue_date,
    ri.recruit_id,  -- ID рекрута
    r.target_recruitment_id AS recruitment_id,
    rr.reason_registration, -- причина постановки на учет char
    sc.sports_category, -- наличие спортивной категории bool
    dl.driver_license, -- категории водительских удостоверений char 
    dg.disability_group, -- группа инвалидности char
    tl.tractor_license, -- наличие удостоверения тракториста bool
    iw.criminal_prosecution AS is_wanted, -- наличие уголовного преследования bool
    em.employment_status AS employment, -- занятость char
    r.system_update_date AS update_date
FROM 
    public.recruits_info ri
JOIN 
    public.recruits r ON ri.recruit_id = r.id
LEFT JOIN 
    children_count cc ON r.id = cc.recruit_id  -- Присоединение для получения количества детей
JOIN 
    registration_reasons rr ON rr.recruit_id = ri.recruit_id
JOIN
    sports_category sc ON sc.recruit_id = ri.recruit_id
JOIN 
    driver_license dl ON dl.recruit_id = ri.recruit_id
JOIN
    disability_group dg ON dg.recruit_id = ri.recruit_id
JOIN 
    tractor_license tl ON tl.recruit_id = ri.recruit_id
JOIN
    is_wanted iw ON iw.recruit_id = ri.recruit_id
JOIN
    employment em ON em.recruit_id = ri.recruit_id
WHERE 
    r.target_recruitment_id IS NOT NULL 
    AND r.current_recruitment_id IS NOT NULL;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>208</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>1000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
</fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>N</specify_fields>
    <table>citizen</table>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>656</xloc>
      <yloc>64</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
