<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">



    <changeSet id="0001" author="ilyin">
        <comment>ERVU-193</comment>
        <sql>
			DROP VIEW IF EXISTS actualization.view_applications_status;
			DROP TABLE IF EXISTS actualization.applications_status;
			
			CREATE TABLE IF NOT EXISTS actualization.applications_received_from_epgu
            (
            applications_received_from_epgu_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_conv_info_records_from_easu_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_all bigint NOT NULL DEFAULT 0,
            count_executor_appointed bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_expired bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_on_time bigint NOT NULL DEFAULT 0,
            count_registered bigint NOT NULL DEFAULT 0,
            count_registration_refusal bigint NOT NULL DEFAULT 0,
            count_refusal_provide_service bigint NOT NULL DEFAULT 0,
            count_provided bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS actualization.applications_received_from_epgu  OWNER to ervu_business_metrics;

            COMMENT ON TABLE actualization.applications_received_from_epgu IS 'Актуализация. Заявления, поступившие через ЕПГУ';
			
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_executor_appointed IS 'Назначен исполнитель';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_executor_not_appointed_expired IS 'Не назначен исполнитель свыше допустимых сроков (просрочено)';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_executor_not_appointed_on_time IS 'Не назначен исполнитель в пределах допустимых сроков';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_registered IS 'Зарегистрировано заявлений';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_registration_refusal IS 'Получен отказ в регистрации';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_refusal_provide_service IS 'Получен отказ в предоставлении услуги по заявлению';
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_provided IS 'Услуга по заявлению оказана';
			
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_date ON actualization.applications_received_from_epgu (info_date);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr ON actualization.applications_received_from_epgu (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr_date ON actualization.applications_received_from_epgu (recruitment_id, info_date);
			
			CREATE OR REPLACE VIEW actualization.view_applications_received_from_epgu AS
			SELECT
			applications_received_from_epgu_id,
			round(count_executor_appointed::numeric * 100 / count_all) AS percent_executor_appointed,
			round(count_executor_not_appointed_expired::numeric * 100 / count_all) AS percent_executor_not_appointed_expired,
			round(count_executor_not_appointed_on_time::numeric * 100 / count_all) AS percent_executor_not_appointed_on_time,
			round(count_registered::numeric * 100 / count_all) AS percent_registered,
			round(count_registration_refusal::numeric * 100 / count_all) AS percent_registration_refusal,
			round(count_refusal_provide_service::numeric * 100 / count_all) AS percent_refusal_provide_service,
			round(count_provided::numeric * 100 / count_all) AS percent_provided			
			FROM actualization.applications_received_from_epgu;


			DROP VIEW IF EXISTS deregistration.view_applications_received_from_epgu;
			DROP TABLE IF EXISTS deregistration.applications_received_from_epgu;
			
			CREATE TABLE IF NOT EXISTS deregistration.applications_received_from_epgu
            (
            applications_received_from_epgu_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_conv_info_records_from_easu_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_all bigint NOT NULL DEFAULT 0,
            count_executor_appointed bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_expired bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_on_time bigint NOT NULL DEFAULT 0,
            count_registered bigint NOT NULL DEFAULT 0,
            count_registration_refusal bigint NOT NULL DEFAULT 0,
            count_refusal_provide_service bigint NOT NULL DEFAULT 0,
            count_provided bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS deregistration.applications_received_from_epgu  OWNER to ervu_business_metrics;

            COMMENT ON TABLE deregistration.applications_received_from_epgu IS 'Снятие с учета. Заявления, поступившие через ЕПГУ';
			
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_executor_appointed IS 'Назначен исполнитель';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_executor_not_appointed_expired IS 'Не назначен исполнитель свыше допустимых сроков (просрочено)';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_executor_not_appointed_on_time IS 'Не назначен исполнитель в пределах допустимых сроков';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_registered IS 'Зарегистрировано заявлений';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_registration_refusal IS 'Получен отказ в регистрации';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_refusal_provide_service IS 'Получен отказ в предоставлении услуги по заявлению';
			COMMENT ON COLUMN deregistration.applications_received_from_epgu.count_provided IS 'Услуга по заявлению оказана';
			
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_date ON deregistration.applications_received_from_epgu (info_date);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr ON deregistration.applications_received_from_epgu (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr_date ON deregistration.applications_received_from_epgu (recruitment_id, info_date);
			
			CREATE OR REPLACE VIEW deregistration.view_applications_received_from_epgu AS
			SELECT
			applications_received_from_epgu_id,
			round(count_executor_appointed::numeric * 100 / count_all) AS percent_executor_appointed,
			round(count_executor_not_appointed_expired::numeric * 100 / count_all) AS percent_executor_not_appointed_expired,
			round(count_executor_not_appointed_on_time::numeric * 100 / count_all) AS percent_executor_not_appointed_on_time,
			round(count_registered::numeric * 100 / count_all) AS percent_registered,
			round(count_registration_refusal::numeric * 100 / count_all) AS percent_registration_refusal,
			round(count_refusal_provide_service::numeric * 100 / count_all) AS percent_refusal_provide_service,
			round(count_provided::numeric * 100 / count_all) AS percent_provided			
			FROM deregistration.applications_received_from_epgu;



			DROP VIEW IF EXISTS registration_change_address.view_applications_received_from_epgu;
			DROP TABLE IF EXISTS registration_change_address.applications_received_from_epgu;
			
			CREATE TABLE IF NOT EXISTS registration_change_address.applications_received_from_epgu
            (
            applications_received_from_epgu_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_conv_info_records_from_easu_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_all bigint NOT NULL DEFAULT 0,
            count_executor_appointed bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_expired bigint NOT NULL DEFAULT 0,
            count_executor_not_appointed_on_time bigint NOT NULL DEFAULT 0,
            count_registered bigint NOT NULL DEFAULT 0,
            count_registration_refusal bigint NOT NULL DEFAULT 0,
            count_refusal_provide_service bigint NOT NULL DEFAULT 0,
            count_provided bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS registration_change_address.applications_received_from_epgu  OWNER to ervu_business_metrics;

            COMMENT ON TABLE registration_change_address.applications_received_from_epgu IS 'Актуализация. Заявления, поступившие через ЕПГУ';
			
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_executor_appointed IS 'Назначен исполнитель';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_executor_not_appointed_expired IS 'Не назначен исполнитель свыше допустимых сроков (просрочено)';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_executor_not_appointed_on_time IS 'Не назначен исполнитель в пределах допустимых сроков';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_registered IS 'Зарегистрировано заявлений';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_registration_refusal IS 'Получен отказ в регистрации';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_refusal_provide_service IS 'Получен отказ в предоставлении услуги по заявлению';
			COMMENT ON COLUMN registration_change_address.applications_received_from_epgu.count_provided IS 'Услуга по заявлению оказана';
			
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_date ON registration_change_address.applications_received_from_epgu (info_date);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr ON registration_change_address.applications_received_from_epgu (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_applications_received_from_epgu_recr_date ON registration_change_address.applications_received_from_epgu (recruitment_id, info_date);
			
			CREATE OR REPLACE VIEW registration_change_address.view_applications_received_from_epgu AS
			SELECT
			applications_received_from_epgu_id,
			round(count_executor_appointed::numeric * 100 / count_all) AS percent_executor_appointed,
			round(count_executor_not_appointed_expired::numeric * 100 / count_all) AS percent_executor_not_appointed_expired,
			round(count_executor_not_appointed_on_time::numeric * 100 / count_all) AS percent_executor_not_appointed_on_time,
			round(count_registered::numeric * 100 / count_all) AS percent_registered,
			round(count_registration_refusal::numeric * 100 / count_all) AS percent_registration_refusal,
			round(count_refusal_provide_service::numeric * 100 / count_all) AS percent_refusal_provide_service,
			round(count_provided::numeric * 100 / count_all) AS percent_provided			
			FROM registration_change_address.applications_received_from_epgu;
			
			
			ALTER TABLE summonses_list.formed_summonses ADD COLUMN IF NOT EXISTS count_refused BIGINT NOT NULL DEFAULT 0;
			ALTER TABLE summonses_list.formed_summonses ADD COLUMN IF NOT EXISTS count_auto_7_days BIGINT NOT NULL DEFAULT 0;
			
			COMMENT ON COLUMN summonses_list.formed_summonses.count_refused IS 'отклоненные ВК';
			COMMENT ON COLUMN summonses_list.formed_summonses.count_auto_7_days IS 'врученные автоматически через 7 дней';
			
			DROP VIEW IF EXISTS summonses_list.view_formed_summonses;
			CREATE OR REPLACE VIEW summonses_list.view_formed_summonses AS
			SELECT
			formed_summonses_id,
			count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days AS count_all,
			round(count_formed::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_formed,
			round(count_signed::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_signed,
			round(count_sent::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_sent,
			round(count_delivered_on_hands::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_delivered_on_hands,
			round(count_delivered_by_letter::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_delivered_by_letter,
			round(count_refused::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_refused,
			round(count_auto_7_days::numeric * 100 / (count_formed + count_signed + count_sent + count_delivered_on_hands + count_delivered_by_letter + count_refused + count_auto_7_days)) AS percent_auto_7_days
			FROM summonses_list.formed_summonses;
			
			
			
			CREATE TABLE IF NOT EXISTS registration_change_address.imposition_tmp_measures
            (
            imposition_tmp_measures_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_criminal_administrative_liability_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_refused_in_second bigint NOT NULL DEFAULT 0,
            count_accepted_in_second bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS registration_change_address.imposition_tmp_measures OWNER to ervu_business_metrics;

            COMMENT ON TABLE registration_change_address.imposition_tmp_measures IS 'Постановка на воинский учет при смене адреса. Временные меры, введенные в первом ВК';

            CREATE INDEX IF NOT EXISTS idx_imposition_tmp_measures_date ON registration_change_address.imposition_tmp_measures (info_date);
            CREATE INDEX IF NOT EXISTS idx_imposition_tmp_measures_recr ON registration_change_address.imposition_tmp_measures (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_imposition_tmp_measures_recr_date ON registration_change_address.imposition_tmp_measures (recruitment_id, info_date);
			
			COMMENT ON COLUMN registration_change_address.imposition_tmp_measures.count_refused_in_second IS 'снято мер во 2 ВК';
			COMMENT ON COLUMN registration_change_address.imposition_tmp_measures.count_accepted_in_second IS 'подтверждено мер во 2 ВК';
			
			CREATE OR REPLACE VIEW registration_change_address.view_imposition_tmp_measures AS
			SELECT
			imposition_tmp_measures_id,
			count_refused_in_second + count_accepted_in_second AS count_all,
			round(count_refused_in_second::numeric * 100 / (count_refused_in_second + count_accepted_in_second)) AS percent_refused_in_second,
			round(count_accepted_in_second::numeric * 100 / (count_refused_in_second + count_accepted_in_second)) AS percent_accepted_in_second
			FROM registration_change_address.imposition_tmp_measures;
			
			COMMENT ON TABLE actualization.change_data_from_gir_vu IS 'Актуализация. Изменения сведений из ГИР ВУ в разрезе записей';
			
			CREATE TABLE IF NOT EXISTS actualization.change_data_from_gir_vu_citizen
            (
            change_data_from_gir_vu_citizen_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_criminal_administrative_liability_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_renaming bigint NOT NULL DEFAULT 0,
            count_change_birth_date bigint NOT NULL DEFAULT 0,
            count_change_birth_address bigint NOT NULL DEFAULT 0,
            count_change_passport bigint NOT NULL DEFAULT 0,
            count_reg_address bigint NOT NULL DEFAULT 0,
            count_place_of_stay_with_reg bigint NOT NULL DEFAULT 0,
            count_place_of_stay_without_reg bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS actualization.change_data_from_gir_vu_citizen OWNER to ervu_business_metrics;

            COMMENT ON TABLE actualization.change_data_from_gir_vu_citizen IS 'Актуализация. Изменения сведений из ГИР ВУ';
			
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_renaming IS 'перемена ФИО';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_change_birth_date IS 'изменение ДР';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_change_birth_address IS 'изменение места рождения';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_change_passport IS 'изменение паспортных данных';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_reg_address IS 'изменение адреса регистрации по месту жит-ва';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_place_of_stay_with_reg IS 'изменение адреса регистрации по месту пребывания';
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_place_of_stay_without_reg IS 'адрес места пребывания';

            CREATE INDEX IF NOT EXISTS idx_change_data_from_gir_vu_citizen_date ON actualization.change_data_from_gir_vu_citizen (info_date);
            CREATE INDEX IF NOT EXISTS idx_change_data_from_gir_vu_citizen_recr ON actualization.change_data_from_gir_vu_citizen (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_change_data_from_gir_vu_citizen_recr_date ON actualization.change_data_from_gir_vu_citizen (recruitment_id, info_date);
			
			
			CREATE OR REPLACE VIEW actualization.view_change_data_from_gir_vu_citizen AS
			SELECT
			change_data_from_gir_vu_citizen_id,
			count_all,
			round(count_renaming::numeric * 100 / count_all) AS percent_renaming,
			round(count_change_birth_date::numeric * 100 / count_all) AS percent_change_birth_date,
			round(count_change_birth_address::numeric * 100 / count_all) AS percent_change_birth_address,
			round(count_change_passport::numeric * 100 / count_all) AS percent_change_passport,
			round(count_reg_address::numeric * 100 / count_all) AS percent_reg_address,
			round(count_place_of_stay_with_reg::numeric * 100 / count_all) AS percent_place_of_stay_with_reg,
			round(count_place_of_stay_without_reg::numeric * 100 / count_all) AS percent_place_of_stay_without_reg
			FROM 
			(SELECT
			count_renaming + count_change_birth_date + count_change_birth_address + count_change_passport + count_reg_address + count_place_of_stay_with_reg + count_place_of_stay_without_reg AS count_all,
			*
			FROM actualization.change_data_from_gir_vu_citizen)t;
			
			DROP TABLE IF EXISTS admin_indicators.entry_analysis;
			DROP TABLE IF EXISTS admin_indicators.last_entry_time_by_days;
			DROP TABLE IF EXISTS admin_indicators.last_entry_time_by_hours;
			DROP TABLE IF EXISTS admin_indicators.ip_adresses;
			DROP TABLE IF EXISTS admin_indicators.feedback_analysis;
			DROP TABLE IF EXISTS admin_indicators.user_account_analysis;
			DROP TABLE IF EXISTS admin_indicators.report_analysis;
			DROP VIEW IF EXISTS admin_indicators.view_event_msg_jour;
			DROP TABLE IF EXISTS admin_indicators.event_msg_jour;
			
			
			DROP VIEW IF EXISTS actualization.view_citizen_records;
			DROP TABLE IF EXISTS actualization.citizen_records;
			
			CREATE TABLE IF NOT EXISTS actualization.change_data_personal_visit
            (
            change_data_personal_visit_id bigserial NOT NULL PRIMARY KEY,
            recruitment_id varchar(36) NOT NULL constraint fk_conv_info_records_from_easu_recruitment_id references metrics.recruitment,
            update_date timestamp without time zone NOT NULL DEFAULT now(),
            info_date date NOT NULL,
            count_edited bigint NOT NULL DEFAULT 0,
            count_for_sign bigint NOT NULL DEFAULT 0,
            count_signed bigint NOT NULL DEFAULT 0,
            count_refused bigint NOT NULL DEFAULT 0
            )
            WITH (OIDS = FALSE);
            ALTER TABLE IF EXISTS actualization.change_data_personal_visit  OWNER to ervu_business_metrics;

            COMMENT ON TABLE actualization.change_data_personal_visit IS 'Актуализация. Изменение сведений при личной явке';

            CREATE INDEX IF NOT EXISTS idx_change_data_personal_visit_date ON actualization.change_data_personal_visit (info_date);
            CREATE INDEX IF NOT EXISTS idx_change_data_personal_visit_recr ON actualization.change_data_personal_visit (recruitment_id);
            CREATE INDEX IF NOT EXISTS idx_change_data_personal_visit_recr_date ON actualization.change_data_personal_visit (recruitment_id, info_date);
			
			CREATE OR REPLACE VIEW actualization.view_change_data_personal_visit AS
			SELECT
			change_data_personal_visit_id,
			count_all,
			round(count_edited::numeric * 100 / count_all) AS percent_edited,
			round(count_for_sign::numeric * 100 / count_all) AS percent_for_sign,
			round(count_signed::numeric * 100 / count_all) AS percent_signed,
			round(count_refused::numeric * 100 / count_all) AS percent_refused
			FROM
			(SELECT
			count_edited + count_for_sign + count_signed + count_refused AS count_all,
			*
			FROM actualization.change_data_personal_visit)t;
			
			ALTER TABLE summonses_list.recruit_office_visit ADD COLUMN IF NOT EXISTS count_hand_over_not_need BIGINT NOT NULL DEFAULT 0;
			
			DROP VIEW IF EXISTS summonses_list.view_recruit_office_visit;
			CREATE OR REPLACE VIEW summonses_list.view_recruit_office_visit AS
			SELECT
			recruit_office_visit_id,
			count_all,
			round(count_visit::numeric * 100 / count_all) AS percent_visit,
			round(count_not_visit::numeric * 100 / count_all) AS percent_not_visit,
			round(count_valid_reason::numeric * 100 / count_all) AS percent_valid_reason,
			round(count_not_need::numeric * 100 / count_all) AS percent_not_need,
			round(count_hand_over_not_need::numeric * 100 / count_all) AS percent_hand_over_not_need
			FROM
			(SELECT
			count_visit + count_not_visit + count_valid_reason + count_not_need + count_hand_over_not_need AS count_all,
			*
			FROM summonses_list.recruit_office_visit)t;
			
			ALTER TABLE deregistration.solutions ADD COLUMN IF NOT EXISTS count_accepted_canceled BIGINT NOT NULL DEFAULT 0;
			ALTER TABLE deregistration.solutions ADD COLUMN IF NOT EXISTS count_refused_canceled BIGINT NOT NULL DEFAULT 0;
			COMMENT ON COLUMN deregistration.solutions.count_accepted_canceled IS 'решений о снятии отклонено';
			COMMENT ON COLUMN deregistration.solutions.count_refused_canceled IS 'решений об отказе снятии отклонено';
			
			DROP VIEW IF EXISTS deregistration.view_solutions;
			CREATE OR REPLACE VIEW deregistration.view_solutions AS
			SELECT
			solutions_id,
			count_accepted_all,
			count_refused_all,
			round(count_accepted_signed::numeric * 100 / count_accepted_all) AS percent_accepted_signed,
			round(count_accepted_for_sign::numeric * 100 / count_accepted_all) AS percent_accepted_for_sign,
			round(count_accepted_canceled::numeric * 100 / count_accepted_all) AS percent_accepted_canceled,
			round(count_refused_signed::numeric * 100 / count_refused_all) AS percent_refused_signed,
			round(count_refused_for_sign::numeric * 100 / count_refused_all) AS percent_refused_for_sign,
			round(count_refused_canceled::numeric * 100 / count_refused_all) AS percent_refused_canceled
			FROM
			(SELECT
			count_accepted_signed + count_accepted_for_sign + count_accepted_canceled AS count_accepted_all,
			count_refused_signed + count_refused_for_sign + count_refused_canceled AS count_refused_all,
			*
			FROM deregistration.solutions)t;
			
			DROP VIEW IF EXISTS journal_log.view_dictionary_update;
			CREATE OR REPLACE VIEW journal_log.view_dictionary_update AS
			SELECT
			dictionary_update_id,
			count_all,
			count_all - count_dictionary_1 AS count_without_1,
			count_all - count_dictionary_2 AS count_without_2,
			count_all - count_dictionary_3 AS count_without_3,
			count_all - count_dictionary_4 AS count_without_4,
			round(count_dictionary_1::numeric * 100 / count_all) AS percent_dictionary_1,
			round(count_dictionary_2::numeric * 100 / count_all) AS percent_dictionary_2,
			round(count_dictionary_3::numeric * 100 / count_all) AS percent_dictionary_3,
			round(count_dictionary_4::numeric * 100 / count_all) AS percent_dictionary_4
			FROM
			(SELECT
			count_dictionary_1 + count_dictionary_2 + count_dictionary_3 + count_dictionary_4 AS count_all,
			*
			FROM journal_log.dictionary_update)t;
			
			DROP VIEW IF EXISTS journal_log.view_summonses_list_data_exchange;
			CREATE OR REPLACE VIEW journal_log.view_summonses_list_data_exchange AS
			SELECT
			summonses_list_data_exchange_id,
			count_all,
			round(count_entry_system::numeric * 100 / count_all) AS percent_entry_system,
			round(count_entry_system_error::numeric * 100 / count_entry_system) AS percent_entry_system_error,
			count_all - count_entry_system AS count_without_entry_system,
			round(count_request_extract::numeric * 100 / count_all) AS percent_request_extract,
			round(count_request_extract_error::numeric * 100 / count_request_extract) AS percent_request_extract_error,
			count_all - count_request_extract AS count_without_request_extract
			FROM
			(SELECT
			count_entry_system + count_request_extract AS count_all,
			*
			FROM journal_log.summonses_list_data_exchange)t;
			
			ALTER TABLE journal_log.epgu_notice_sent ADD COLUMN IF NOT EXISTS notice_name_1 character varying NOT NULL;
			ALTER TABLE journal_log.epgu_notice_sent ADD COLUMN IF NOT EXISTS notice_name_2 character varying NOT NULL;
			ALTER TABLE journal_log.epgu_notice_sent ADD COLUMN IF NOT EXISTS notice_name_3 character varying NOT NULL;
			ALTER TABLE journal_log.epgu_notice_sent ADD COLUMN IF NOT EXISTS notice_name_4 character varying NOT NULL;
			
			DROP VIEW IF EXISTS journal_log.view_epgu_notice_sent;
			CREATE OR REPLACE VIEW journal_log.view_epgu_notice_sent AS
			SELECT
			epgu_notice_sent_id,
			count_all,
			round(count_notice_1::numeric * 100 / count_all) AS percent_notice_1,
			round(count_notice_2::numeric * 100 / count_all) AS percent_notice_2,
			round(count_notice_3::numeric * 100 / count_all) AS percent_notice_3,
			round(count_notice_4::numeric * 100 / count_all) AS percent_notice_4,
			count_all - count_notice_1 AS count_without_notice_1,
			count_all - count_notice_2 AS count_without_notice_2,
			count_all - count_notice_3 AS count_without_notice_3,
			count_all - count_notice_4 AS count_without_notice_4
			FROM
			(SELECT
			count_notice_1 + count_notice_2 + count_notice_3 + count_notice_4 AS count_all,
			*
			FROM journal_log.epgu_notice_sent)t;
        </sql>
    </changeSet>
	
    <changeSet id="0002" author="ilyin">
        <comment>ERVU-193_2</comment>
        <sql>
		ALTER TABLE init_registration_info.applications_received_from_epgu ADD COLUMN IF NOT EXISTS info_age varchar;
		UPDATE init_registration_info.applications_received_from_epgu SET info_age = '17_YEARS' WHERE info_age IS NULL;
		ALTER TABLE init_registration_info.applications_received_from_epgu DROP CONSTRAINT IF EXISTS chk_info_age;
		ALTER TABLE init_registration_info.applications_received_from_epgu ADD CONSTRAINT chk_info_age CHECK (info_age::text = ANY (ARRAY['17_YEARS'::character varying::text, '18_YEARS'::character varying::text]));
		ALTER TABLE init_registration_info.applications_received_from_epgu ALTER COLUMN info_age SET NOT NULL;
        </sql>
    </changeSet>
	
</databaseChangeLog>