﻿<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rsr:case schemaVersion="1.0" xmlns="http://umms.fms.gov.ru/replication/core" xmlns:rc="http://umms.fms.gov.ru/replication/registration" xmlns:rsr="http://umms.fms.gov.ru/replication/registration/staying">
   <data tag_autodelete="true">
      <information description="Export to Contur" name="Contur" version="2.0"/>
      <variables>
         <variable id="resv_name_id"/>
         <variable id="name_id"/>
         <variable id="hd_id"/>
      </variables>
      <views>
         <view id="documentinfo">SELECT DISTINCT
  rn.resv_name_id,
  hrs_dev.translit(n.first_name) AS first_name_lat,
  NVL(n.xfirst_name, '[отсутствует]') AS first_name_rus,
  hrs_dev.translit(n.last_name) AS last_name_lat,
  NVL(n.xlast_name, '[отсутствует]') AS last_name_rus,
  n.xmiddle_name AS middle_name_rus,
  hrs_dev.translit(n.middle_name) AS middle_name_lat,
  decode(hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'genders', n.gender), 'M', 'M', 'F', 'F') AS gender_code,
  TO_CHAR(n.birth_date, 'dd.MM.yyyy') AS birth_date,
  (SELECT max(name2) FROM hrs_dev.hrs_pm_vcountry_type_list WHERE id = n.nationality) AS nationality_code,
  CASE 
    WHEN n.birth_date &lt;= TO_DATE('31.12.1991', 'DD.MM.YYYY') THEN 'SUN'
    ELSE (SELECT max(name2) FROM hrs_dev.hrs_pm_vcountry_type_list WHERE id = n.birth_country)
  END AS birth_place_code,
  n.birth_place AS birthplace,
  n.birth_place AS birth_city,
  CASE
    WHEN hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type) = '102974'
      AND n.doc_birth_act_no IS NOT NULL
    THEN '3042940374034159473'
    ELSE hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type)
  END AS document_type_code,
  n.doc_type AS document_type_code_id,
  CASE
    WHEN hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type) = '102974'
      AND n.doc_birth_act_no IS NOT NULL
    THEN n.doc_birth_act_no
    ELSE n.doc_no
  END AS document_number,
  CASE
    WHEN hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type) = '102974'
      AND n.doc_birth_act_no IS NOT NULL
    THEN '0'
    ELSE n.doc_series
  END AS document_series,
  n.doc_birth_act_no,
  TO_CHAR(n.doc_issue_date, 'yyyy-MM-dd') AS document_issued,
  TO_CHAR(n.doc_expiry_date, 'yyyy-MM-dd') AS document_validto,
  n.doc_issue_place AS document_authority,
  CASE
    WHEN hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type) = '102974'
      OR TRUNC(MONTHS_BETWEEN(v8_sys_fideliodate, n.birth_date) / 12) &lt; 14
    THEN n.doc_issue_place
    ELSE n.department_code
  END AS contur_authority,
  n.region || ' обл.$$' || n.city || '$$$' || n.street AS living_address,
  (SELECT hrs_dev.hrs_pm_fias_guid(n.region, n.district, n.city, n.street) FROM DUAL) AS fias_living_address,
  n.house,
  n.building,
  n.apartment,
  n.department_code,
  CASE
    WHEN hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', n.doc_type) = '102974'
      OR TRUNC(MONTHS_BETWEEN(v8_sys_fideliodate, n.birth_date) / 12) &lt; 14
    THEN NULL
    ELSE (SELECT max(id)  FROM hrs_dev.HRS_PM_UFMS_AUTHORITIES WHERE code = n.department_code)
  END AS department_code_id,
  vri.room_number,
  n.birth_occupation,
  n.birth_district,
  n.birth_region,
  TO_CHAR(n.addr_date_from, 'yyyy-MM-dd') AS addr_date_from
FROM
  hrs_dev.v_reservation_info vri,
  hrs_dev.hrs_pm_rname rn,
  hrs_dev.hrs_pm_name n
WHERE
  vri.resv_id = {variables.resv_name_id}
  and rn.resv_name_id(+) = vri.resv_id
  and n.name_id = {variables.name_id}
  and rownum = 1
</view>
         <view id="entryinfo">SELECT  
  TO_CHAR(yres_exparrtime, 'yyyy-MM-dd') AS begin_date,
  TO_CHAR(yres_expdeptime, 'yyyy-MM-dd') AS end_date
FROM
  v8live.yres
WHERE
  yres_id = {variables.resv_name_id}
  and rownum = 1
         </view>
         <view id="system">SELECT
  TO_CHAR(systimestamp, 'yyyy-MM-dd') AS current_date,
  TO_CHAR(systimestamp, 'HH24:MI:ss') AS current_time,
  TO_CHAR(systimestamp, 'yyyy-MM-dd') || 'T' ||  TO_CHAR(systimestamp, 'HH24:MI:ss') AS current_long_date,
  TO_CHAR(systimestamp, 'yyyyMMdd') || '_' ||  TO_CHAR(systimestamp, 'HH24MIssff3') AS zip_filename,
  TO_CHAR(systimestamp, 'yyyyMMdd') || '_' ||  TO_CHAR(systimestamp, 'HH24MIss') AS packcage_filename,
  '{variables.resv_name_id}{variables.name_id}_' || TO_CHAR(systimestamp, 'yyyyMMdd') || '_' ||  TO_CHAR(systimestamp, 'HH24MIss') AS xml_filename,
  '{variables.resv_name_id}{variables.name_id}' AS form_uid
FROM
  DUAL
         </view>
         <view id="pms">SELECT
  upper(value) AS pms_name
FROM
  hrs_dev.hrs_pm_station_settings
WHERE
  setting_type_id=1 and station_id = 0
         </view>
         <view id="multiattach">SELECT
   imgs.*,
   decode('{pms.pms_name}',
          'OPERA',
          (SELECT ss.value
              FROM hrs_dev.hrs_pm_settings_types st, hrs_dev.hrs_pm_station_settings ss
              WHERE st.code = 'ATTACHMENTS_PATH' AND ss.setting_type_id = st.id),
          '') || imgs.image_name AS full_attachment
FROM
(
    SELECT P.*, 'passport.' || rownum AS item_name, '103008' AS item_type
    FROM (SELECT image_name, image_type FROM hrs_dev.HRS_PM_IMAGES WHERE client_id = '{variables.name_id}' AND image_name IS NOT NULL AND image_type ='P' ORDER BY insert_date ASC) P
) imgs
         </view>
         <view id="hotelier_info">SELECT
  ud.first_name,
  ud.last_name,
  ud.middle_name,
  decode(hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'genders', ud.gender), 'M', 'M', 'F', 'F') as gender,
  to_char(ud.birth_date, 'dd.MM.yyyy') as birth_date,
  (select max(name2) from hrs_dev.hrs_pm_vcountry_type_list where name2 = ud.nationality) as nationality,
  (select max(name2) from hrs_dev.hrs_pm_vcountry_type_list where name2 = ud.birth_country) as birth_country,
  ud.birth_city,
  (select max(id)  from hrs_dev.HRS_PM_UFMS_DOCTYPES where code = hrs_dev.hrs_pm_vexconvertvalue('pms', 'territory', 'document_categories', ud.document_type)) as document_type,
  ud.document_series,
  ud.document_number,
  (select max(id)  from hrs_dev.HRS_PM_UFMS_AUTHORITIES where code = ud.document_department_code) as document_department_code,
  to_char(ud.document_issued_date, 'dd.MM.yyyy') as document_issued_date,
  to_char(ud.document_valid_from, 'dd.MM.yyyy') as document_valid_from,
  to_char(ud.document_valid_to, 'dd.MM.yyyy') as document_valid_to,
  upper(ud.last_name || ' ' || ud.first_name || ' ' || ud.middle_name) AS full_name
from
  hrs_dev.hrs_pm_user_document ud
where
  ud.hd_id = {variables.hd_id}
  and rownum = 1
         </view>
      </views>
      <output>
         <packcages/>
         <files>
            <file name="{system.xml_filename}.xml" zip_name="{system.zip_filename}_RG.zip"/>
         </files>
      </output>
      <images>
         <image add_as_attachment="false" hrsmultirow="multiattach" name="{multiattach.row.item_name}">{multiattach.row.full_attachment}</image>
      </images>
   </data>
   <number>{system.form_uid}</number>
   <documentPhoto hrsmultirow="multiattach">
      <documentUID>{multiattach.row.image_name}/autodelete-parent</documentUID>
      <type>
         <type>DocumentType</type>
         <element>{multiattach.row.item_type}</element>
      </type>
      <content>{multiattach.row.detached}/autodelete-parent</content>
      <mimeType>image/jpeg</mimeType>
   </documentPhoto>
   <receptionDate>{system.current_date}</receptionDate>
   <rc:declarant>
      <person>
         <lastName>{documentinfo.last_name_rus}</lastName>
         <firstName>{documentinfo.first_name_rus}</firstName>
         <middleName>{documentinfo.middle_name_rus}/autodelete</middleName>
         <gender>
            <type>Gender</type>
            <element>{documentinfo.gender_code}/autodelete-parent</element>
         </gender>
         <birthDate>{documentinfo.birth_date}/autodelete</birthDate>
         <citizenship>
            <type>Citizenship</type>
            <element>{documentinfo.nationality_code}/autodelete-parent</element>
         </citizenship>
         <birthPlace>
            <country>
               <type>Country</type>
               <element>{documentinfo.birth_place_code}/autodelete-parent</element>
            </country>
            <place>{documentinfo.birth_region}/autodelete</place>
            <place2>{documentinfo.birth_district}/autodelete</place2>
            <place3>{documentinfo.birth_city}/autodelete</place3>
            <place4>{documentinfo.birth_occupation}/autodelete</place4>
         </birthPlace>
      </person>
      <document>
         <type>
            <type>reg.personIdentDocType</type>
            <element>{documentinfo.document_type_code}</element>
         </type>
         <series>{documentinfo.document_series}/autodelete</series>
         <number>{documentinfo.document_number}/autodelete-parent</number>
         <authorityOrgan>
            <type>officialOrgan</type>
            <element>{documentinfo.department_code_id}/autodelete-parent</element>
         </authorityOrgan>
          <authority>{documentinfo.contur_authority}/autodelete</authority>
         <issued>{documentinfo.document_issued}/autodelete</issued>
         <validFrom>{documentinfo.document_issued}/autodelete</validFrom>
         <validTo>{documentinfo.document_validto}/autodelete</validTo>
      </document>
      <entered>false</entered>
   </rc:declarant>
   <rc:livingAddress>
      <dateFrom>{documentinfo.addr_date_from}</dateFrom>
      <dateTo>{documentinfo.addr_date_to}/autodelete</dateTo>
      <address>
         <russianAddress>
            <addressObject>
               <type>addressObject</type>
               <element>{documentinfo.fias_living_address}/autodelete-parent</element>
            </addressObject>
            <housing>
               <type>
                  <type>addressObjectType</type>
                  <element>1202</element>
               </type>
               <value>{documentinfo.house}/autodelete-parent</value>
            </housing>
            <housing>
               <type>
                  <type>addressObjectType</type>
                  <element>1203</element>
               </type>
               <value>{documentinfo.building}/autodelete-parent</value>
            </housing>
            <housing>
               <type>
                  <type>addressObjectType</type>
                  <element>1303</element>
               </type>
               <value>{documentinfo.apartment}/autodelete-parent</value>
            </housing>
         </russianAddress>
      </address>
   </rc:livingAddress>
   <rc:stayingPeriod>
      <dateFrom>{entryinfo.begin_date}</dateFrom>
      <dateTo>{entryinfo.end_date}/autodelete</dateTo>
   </rc:stayingPeriod>
   <rc:stayingAddress>
      <addressObject>
         <type>addressObject</type>
         <element>e3ab9bed-d8a5-434d-a227-94c21932a408</element>
      </addressObject>
      <housing>
         <type>
            <type>addressObjectType</type>
            <element>1202</element>
         </type>
         <value>133</value>
      </housing>
      <housing>
         <type>
            <type>addressObjectType</type>
            <element>1303</element>
         </type>
         <value>{documentinfo.room_number}/autodelete-parent</value>
      </housing>
   </rc:stayingAddress>
   <rc:alp>
      <rc:createDate>{entryinfo.begin_date}</rc:createDate>
      <rc:createdBy>ЛИСИНА ЯНА ЮРЬЕВНА</rc:createdBy>
   </rc:alp>
</rsr:case>
