<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://mycompany.com/mynamespace"
    xmlns:js="urn:the-xml-files:xslt-csharp"
    xmlns:ntd='http://www.ntd.ru/ntd/'>

  <xsl:output method="text" />

  <xsl:template match="text()"/>

  <!--<xsl:template match="root/Dictionaries">
    DECLARE @dbid int

    <xsl:for-each select="Dictionary">
      exec @dbid = sp_GenerateIdForEntity 'DICTIONARY_BASE', 'PrimaryKey', 1
      INSERT INTO AKUZ.T_DICTIONARY_BASE (DictionaryID, Code, Name, DateAdd, type_)
      VALUES(@dbid, <xsl:value-of select="@Code" />, '<xsl:value-of select="@Name" />', GETDATE(), <xsl:value-of select="@Type" />)
    </xsl:for-each>
    GO
  </xsl:template>-->

  <xsl:template match="root/InsuranceCompanies">
    <xsl:for-each select="InsuranceCompany">
      -- Страх.организация
      create or replace function TempFunc () returns void as $$
      declare
      _orgid uuid;
      _type integer;
      begin
      _orgid := '<xsl:value-of select="@OrganizationID"/>';
      _type := (select EntityID FROM AKUZ.T_ENTITIES
      WHERE EntName='INSURANCE_COMPANY' and Schema='AKUZ');
      INSERT INTO AKUZ.T_ORGANIZATION
      (OrganizationID, Name, ShortName, type_)
      VALUES (_orgID, '<xsl:value-of select="Name"/>', '<xsl:value-of select="ShortName"/>', _type);

      INSERT INTO AKUZ.T_INSURANCE_COMPANY (OrganizationID, Code, PolicyTemplate)
      VALUES (_orgID, '<xsl:value-of select="@Code"/>', <xsl:choose>
        <xsl:when test="string(@PolicyTemplate)">
          '<xsl:value-of select="@PolicyTemplate"/>'
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>);
      end;$$ LANGUAGE plpgsql;
      select TempFunc();
    </xsl:for-each>
  </xsl:template>

  <!-- V009_RESULT: V009.xml -->
  <xsl:template match="packet[(zglv[type='Rezult'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="RezultZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V009 "<zap>" template -->
  <xsl:template name="RezultZap">
    do $$
    begin
    if not exists(select 1 from AKUZ.T_V009_RESULT where IDRMP = <xsl:value-of select="IDRMP" />) then
    INSERT INTO AKUZ.T_V009_RESULT (IDRMP, RMPNAME, DL_USLOV, DATEBEG, DATEEND)
    values(
    <xsl:value-of select="IDRMP" />
    , '<xsl:value-of select="RMPNAME" />'
    , <xsl:value-of select="DL_USLOV" />
    , to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')
    , <xsl:if test="DATEEND != ''">
      to_date('<xsl:value-of select="DATEEND" />', 'dd.mm.yyyy')
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    );
    else
    update AKUZ.T_V009_RESULT
    set RMPNAME = '<xsl:value-of select="RMPNAME" />'
    , DL_USLOV = <xsl:value-of select="DL_USLOV" />
    , DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')
    , DATEEND = <xsl:if test="DATEEND != ''">
      to_date('<xsl:value-of select="DATEEND" />', 'dd.mm.yyyy')
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    where IDRMP = <xsl:value-of select="IDRMP" />;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V020_STRUCTURE_BED: V020.xml -->
  <xsl:template match="packet[zglv[type='KoPr']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="KoPrZap" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="KoPrZap">
    DO $$
    BEGIN
    IF NOT EXISTS(SELECT 1 FROM AKUZ.T_V020_STRUCTURE_BED WHERE IDK_PR = <xsl:value-of select="IDK_PR" />) THEN
    INSERT INTO AKUZ.T_V020_STRUCTURE_BED (IDK_PR, K_PRNAME, DATEBEG, DATEEND)
    VALUES(
    <xsl:value-of select="IDK_PR" />,
    '<xsl:value-of select="K_PRNAME" />',
    to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'),
    <xsl:choose>
      <xsl:when test="DATEEND != ''">
        to_date('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V020_STRUCTURE_BED SET
    K_PRNAME = '<xsl:value-of select="K_PRNAME" />',
    DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'),
    DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        to_date('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    WHERE IDK_PR = <xsl:value-of select="IDK_PR" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V021_SPECIALITY: V021.xml -->

  <xsl:template match="packet[(zglv[type='MedSpec'])]">
    <xsl:for-each select="zap">
      <!-- insert or update V021_SPECIALITY -->
      <xsl:call-template name="MedSpecZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V021 "<zap>" template -->

  <xsl:template name="MedSpecZap" >
    do
    $$
    begin
    if not exists(select * from AKUZ.T_V021_SPECIALITY  where ID = <xsl:value-of select="IDSPEC" /> and DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')::timestamp)
    then
    INSERT INTO AKUZ.T_V021_SPECIALITY
    (id, name, DateBegin, DateEnd, V021SpecialityID, POSTNAME, IDPOST_MZ)
    VALUES
    (
    <xsl:value-of select="IDSPEC" />
    , '<xsl:value-of select="SPECNAME" />'
    , to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')::timestamp
    , <xsl:choose>
      <xsl:when test="string(DATEEND)">
        <xsl:text>to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'dd.mm.yyyy')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , gen_random_uuid()
    , <xsl:choose>
      <xsl:when test="string(POSTNAME)">
        '<xsl:value-of select="POSTNAME"/>'
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="string(IDPOST_MZ)">
        <xsl:value-of select="IDPOST_MZ"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    else
    update AKUZ.T_V021_SPECIALITY
    set name = '<xsl:value-of select="SPECNAME" />'
    , DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')::timestamp
    , DateEnd = <xsl:choose>
      <xsl:when test="string(DATEEND)">
        <xsl:text>to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'dd.mm.yyyy')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , POSTNAME = <xsl:choose>
      <xsl:when test="string(POSTNAME)">
        '<xsl:value-of select="POSTNAME"/>'
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , IDPOST_MZ = <xsl:choose>
      <xsl:when test="string(IDPOST_MZ)">
        <xsl:value-of select="IDPOST_MZ"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where Id = '<xsl:value-of select="IDSPEC" />' and DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'dd.mm.yyyy')::timestamp;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V015_SPECIALITY: V015.xml -->
  <!--<xsl:template match="packet[(zglv[@type='MedSpec'])]">
    <xsl:for-each select="zap">
      -->
  <!-- insert or update V015_SPECIALITY -->
  <!--
      <xsl:call-template name="MedSpecZap" />
    </xsl:for-each>
  </xsl:template>
  -->
  <!-- V015 "<zap>" template -->
  <!--
  <xsl:template name="MedSpecZap" >
    if not exists(select * from [AKUZ].[T_V015_SPECIALITY] s where s.[RECID] = <xsl:value-of select="@RECID" />)
    INSERT INTO [AKUZ].[T_V015_SPECIALITY] ([RECID] ,[CODE] ,[NAME] ,[HIGH] ,[OKSO] ,[DATEBEG] ,[DATEEND])
    SELECT <xsl:value-of select="@RECID" />
    , <xsl:value-of select="@CODE" />
    , LEFT('<xsl:value-of select="@NAME" />', 256)
    , <xsl:if test="@HIGH != ''">
      '<xsl:value-of select="@HIGH" />'
    </xsl:if><xsl:if test="@HIGH = ''">NULL</xsl:if>
    , <xsl:if test="@OKSO != ''">
      '<xsl:value-of select="@OKSO" />'
    </xsl:if><xsl:if test="@OKSO = ''">NULL</xsl:if>
    , CONVERT(datetime, '<xsl:value-of select="@DATEBEG" />', 104)
    , <xsl:if test="@DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="@DATEEND" />', 104)
    </xsl:if><xsl:if test="@DATEEND = ''">NULL</xsl:if>
    else
    update s
    set s.[CODE] = <xsl:value-of select="@CODE" />
    , s.[NAME] = LEFT('<xsl:value-of select="@NAME" />', 256)
    , s.[HIGH] = <xsl:if test="@HIGH != ''">
      '<xsl:value-of select="@HIGH" />'
    </xsl:if><xsl:if test="@HIGH = ''">NULL</xsl:if>
    , s.[OKSO] = <xsl:if test="@OKSO != ''">
      '<xsl:value-of select="@OKSO" />'
    </xsl:if><xsl:if test="@OKSO = ''">NULL</xsl:if>
    , s.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="@DATEBEG" />', 104)
    , s.[DATEEND] = <xsl:if test="@DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="@DATEEND" />', 104)
    </xsl:if><xsl:if test="@DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_V015_SPECIALITY] s
    where s.[RECID] = <xsl:value-of select="@RECID" />
    GO
  </xsl:template>
  -->

  <!-- V016_DISP_TYPE: V016.xml -->
  <xsl:template match="packet[(zglv[type='DispT'])]">
    <xsl:for-each select="zap">
      <!-- insert or update V016_DISP_TYPE -->
      <xsl:call-template name="DispT" />
    </xsl:for-each>
  </xsl:template>
  <!-- V016 "<zap>" template -->
  <xsl:template name="DispT">
    DO $$
    BEGIN
    IF NOT EXISTS(
    SELECT 1 FROM AKUZ.T_V016_DISP_TYPE WHERE IDDT = LEFT('<xsl:value-of select="IDDT" />', 3) AND DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    ) THEN
    INSERT INTO AKUZ.T_V016_DISP_TYPE (IDDT, DTNAME, DATEBEG, DATEEND, DTRULE)
    VALUES(
    LEFT('<xsl:value-of select="IDDT" />', 3)
    , LEFT('<xsl:value-of select="DTNAME" />', 256)
    , to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>; </xsl:text>
    </xsl:for-each>'
    );
    ELSE
    UPDATE AKUZ.T_V016_DISP_TYPE SET
    DTNAME = LEFT('<xsl:value-of select="DTNAME" />', 256)
    , DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DTRULE = '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>; </xsl:text>
    </xsl:for-each>'
    WHERE IDDT = LEFT('<xsl:value-of select="IDDT" />', 3) AND DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V017_DISP_RESULT: V017.xml -->
  <xsl:template match="packet[zglv[type='DispR']]">
    <xsl:for-each select="zap">
      <!-- insert or update V017_DISP_RESULT -->
      <xsl:call-template name="DispR" />
    </xsl:for-each>
  </xsl:template>
  <!-- V017 "<zap>" template -->
  <xsl:template name="DispR">
    DO $$
    BEGIN
    IF NOT EXISTS(SELECT 1 FROM AKUZ.T_V017_DISP_RESULT WHERE IDDR = <xsl:value-of select="IDDR" />) THEN
    INSERT INTO AKUZ.T_V017_DISP_RESULT (IDDR, DRNAME, DATEBEG, DATEEND)
    VALUES(
    <xsl:value-of select="IDDR" />
    , LEFT('<xsl:value-of select="DRNAME" />', 256)
    , to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V017_DISP_RESULT SET
    DRNAME = LEFT('<xsl:value-of select="DRNAME" />', 256)
    , DATEBEG = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text> to_date('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    WHERE IDDR = <xsl:value-of select="IDDR" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- VMP_SERVICE: V019.xml elements -->
  <xsl:template match="packet[(zglv[type='HMet'])]">

    <!-- Create MedicalServiceType -->

    DO $$
    BEGIN
    IF NOT EXISTS (
    SELECT 1 FROM akuz.t_medical_service_type ms WHERE ms.medicalservicetypeid = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    ) THEN
    INSERT INTO akuz.t_medical_service_type (medicalservicetypeid, name)
    SELECT '642809B3-0C46-4673-8E97-76A6D60C14F8', 'ВМП(V019)';
    END IF;
    END $$;

    <xsl:for-each select="zap">

      <!-- clear MedicalServiceMkbLinks -->
      DELETE FROM akuz.t_medical_service_mkb_link msml
      USING akuz.t_medical_service ms
      WHERE msml.medicalservice = ms.medicalserviceid
      and ms.type_ = 1113 and ms.medicalservicetype = '642809B3-0C46-4673-8E97-76A6D60C14F8'
      and ms.code = '<xsl:value-of select="IDHM" />'
      and ms.economiccode = '<xsl:value-of select="HVID" />'
      and (ms.PatientModelCode = <xsl:choose>
        <xsl:when test="IDMODP != ''">
          <xsl:value-of select="IDMODP" />
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose> or ms.PatientModelCode is null)
      and ms.DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY');
      --pgsql:go
    </xsl:for-each>

    <xsl:for-each select="zap">

      <!-- insert or update MedicalService -->

      <xsl:call-template name="HMetZapElements" />

      <!-- insert new MedicalServiceMkbLinks -->
      <xsl:call-template name="HMetDiagnosisElements" />

    </xsl:for-each>
  </xsl:template>

  <!-- V019 "<zap>" template -->
  <xsl:template name="HMetZapElements" >
    do $$
    begin
    if not exists(select 1 from AKUZ.T_MEDICAL_SERVICE ms where ms.type_ = 1113 and ms.MedicalServiceType = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.Code = '<xsl:value-of select="IDHM" />'
    and ms.EconomicCode = '<xsl:value-of select="HVID" />'
    and (ms.PatientModelCode = <xsl:choose>
      <xsl:when test="IDMODP != ''">
        <xsl:value-of select="IDMODP" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose> or ms.PatientModelCode is null)
    and ms.DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'))
    then
    insert into akuz.t_medical_service (Code, Name, NameFull, Profile, MedicalServiceID, type_, MedicalServiceType, ShortName, PatientModelCode, EconomicCode, InternalCode, PayType, DateBegin, DateEnd, IsCommonService)
    values (
    '<xsl:value-of select="IDHM" />'
    , LEFT('<xsl:value-of select="HMNAME" />', 256)
    , LEFT('<xsl:value-of select="HMNAME" />', 256)
    , 3
    , gen_random_uuid()
    , 1113
    , '642809B3-0C46-4673-8E97-76A6D60C14F8'
    , LEFT('<xsl:value-of select="HMNAME" />', 24)
    , <xsl:choose>
      <xsl:when test="IDMODP != ''">
        <xsl:value-of select="IDMODP" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
    , '<xsl:value-of select="HVID" />'
    , '<xsl:value-of select="HGR" />'
    , 1
    , to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    , <xsl:if test="DATEEND != ''">
      <xsl:text>to_date('</xsl:text>
      <xsl:value-of select="DATEEND" />
      <xsl:text>', 'DD.MM.YYYY')</xsl:text>
    </xsl:if>
    <xsl:if test="DATEEND = ''">
      <xsl:text>NULL</xsl:text>
    </xsl:if>
    , false
    );
    else
    update akuz.t_medical_service ms
    set Name = LEFT('<xsl:value-of select="HMNAME" />', 256)
    , NameFull = LEFT('<xsl:value-of select="HMNAME" />', 256)
    , ShortName = LEFT('<xsl:value-of select="HMNAME" />', 24)
    , PatientModelCode = <xsl:choose>
      <xsl:when test="IDMODP != ''">
        <xsl:value-of select="IDMODP" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
    , InternalCode = '<xsl:value-of select="HGR" />'
    , DateEnd = <xsl:if test="DATEEND != ''">
      <xsl:text>to_date('</xsl:text>
      <xsl:value-of select="DATEEND" />
      <xsl:text>', 'DD.MM.YYYY')</xsl:text>
    </xsl:if>
    <xsl:if test="DATEEND = ''">
      <xsl:text>NULL</xsl:text>
    </xsl:if>
    where ms.type_ = 1113 and ms.MedicalServiceType = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.Code = '<xsl:value-of select="IDHM" />'
    and ms.EconomicCode = '<xsl:value-of select="HVID" />'
    and (ms.PatientModelCode = <xsl:choose>
      <xsl:when test="IDMODP != ''">
        <xsl:value-of select="IDMODP" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose> or ms.PatientModelCode is null)
    and ms.DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY');
    end if;
    end
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V019 "DIAG" template -->
  <xsl:template name="HMetDiagnosisElements">
    INSERT INTO AKUZ.T_MEDICAL_SERVICE_MKB_LINK (MedicalServiceMkbLinkID, MedicalService, MkbCode)
    SELECT
    gen_random_uuid() as MedicalServiceMkbLinkID
    , ms.MedicalServiceID
    , MkbCode as MkbCode
    from AKUZ.T_MEDICAL_SERVICE ms
    cross join lateral unnest(string_to_array(replace(replace('<xsl:value-of select="DIAG" />',' ',''),',',';'), ';')) as MkbCode
    where ms.type_ = 1113 and ms.MedicalServiceType = '642809B3-0C46-4673-8E97-76A6D60C14F8'
    and ms.Code = '<xsl:value-of select="IDHM" />'
    and ms.EconomicCode = '<xsl:value-of select="HVID" />'
    and ms.DateBegin = to_date('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    and not exists
    (
    select 1 MedicalServiceMkbLinkID
    from AKUZ.T_MEDICAL_SERVICE_MKB_LINK msl
    where ms.MedicalServiceID = msl.MedicalService
    and MkbCode = msl.MkbCode
    );
    --pgsql:go
  </xsl:template>

  <!-- V002_PROFILE: V002.xml -->
  <xsl:template match="packet[zglv[type='ProfOt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="ProfOtZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V002 "<zap>" template -->
  <xsl:template name="ProfOtZap">
    do $$
    begin
    if not exists(select 1 from AKUZ.T_V002_PROFILE where Id = <xsl:value-of select="IDPR"/> and DateBegin = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V002_PROFILE  (V002ProfileID, Id, Name, IS_IDENT, PARENT_ID, DateBegin, DateEnd)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="IDPR" />
    , '<xsl:value-of select="PRNAME" />'
    , <xsl:choose>
      <xsl:when test="IS_IDENT != ''">
        <xsl:value-of select="IS_IDENT"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="PARENT_ID != ''">
        <xsl:value-of select="PARENT_ID"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    else
    UPDATE AKUZ.T_V002_PROFILE SET
    Name = '<xsl:value-of select="PRNAME" />'
    IS_IDENT = <xsl:choose>
    <xsl:when test="IS_IDENT != ''">
      <xsl:value-of select="IS_IDENT"/>
    </xsl:when>
    <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    PARENT_ID = <xsl:choose>
    <xsl:when test="PARENT_ID != ''">
      <xsl:value-of select="PARENT_ID"/>
    </xsl:when>
    <xsl:otherwise>NULL</xsl:otherwise>
  </xsl:choose>
    , DateEnd = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where Id = <xsl:value-of select="IDPR"/> and DateBegin = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V025_KPC: V025.xml -->
  <xsl:template match="packet[zglv[type='KPC']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="KPCZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V025 "<zap>" template -->
  <xsl:template name="KPCZap">
    DO $$
    DECLARE
    V025KpcID integer;
    BEGIN
    IF NOT EXISTS (SELECT 1 FROM akuz.t_v025_kpc WHERE idpc = '<xsl:value-of select="IDPC" />' AND datebeg = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'))
    THEN
    V025KpcID := sp_generateidforentity('AKUZ.V025_KPC');
    INSERT INTO akuz.t_v025_kpc (V025KpcID, idpc, n_pc, datebeg, dateend)
    VALUES (
    V025KpcID,
    '<xsl:value-of select="IDPC" />',
    '<xsl:value-of select="N_PC" />',
    TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'),
    <xsl:choose>
      <xsl:when test="DATEEND != ''">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE akuz.t_v025_kpc
    SET n_pc = '<xsl:value-of select="N_PC" />',
    dateend = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    WHERE idpc = '<xsl:value-of select="IDPC" />'
    AND datebeg = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY');
    END IF;
    END $$;
    --pgsql:go
  </xsl:template>

  <!-- V011_DRUG: V011.xml -->
  <xsl:template match="book[(entries/zglv[type='V011_Drugs'])]">
    <xsl:for-each select="entry">
      <xsl:call-template name="Drugs" />
    </xsl:for-each>
  </xsl:template>

  <!-- V011 "<zap>" template -->
  <xsl:template name="Drugs">
    INSERT INTO AKUZ.T_V011_DRUG (ID, S_NUM_REG, DATE_REG, DATE_END_REG, S_ORG_NAME, S_ORG_COUNTRY,
    S_TORG_NAME, S_MNN, S_FORMS, S_PROD_PHASE, S_DOCS, S_FARM_GROUP, S_CODES)
    SELECT
    <xsl:value-of select="ID" />,
    SUBSTRING('<xsl:value-of select="S_NUM_REG" />', 1, 50),
    TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'),
    <xsl:choose>
      <xsl:when test="DATEEND != ''">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>,
    SUBSTRING('<xsl:value-of select="S_ORG_NAME" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_ORG_COUNTRY" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_TORG_NAME" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_MNN" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_FORMS" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_PROD_PHASE" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_DOCS" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_FARM_GROUP" />', 1, 255),
    SUBSTRING('<xsl:value-of select="S_CODES" />', 1, 255)
    ON CONFLICT (ID) DO UPDATE SET
    S_NUM_REG = EXCLUDED.S_NUM_REG,
    DATE_REG = EXCLUDED.DATE_REG,
    DATE_END_REG = EXCLUDED.DATE_END_REG,
    S_ORG_NAME = EXCLUDED.S_ORG_NAME,
    S_ORG_COUNTRY = EXCLUDED.S_ORG_COUNTRY,
    S_TORG_NAME = EXCLUDED.S_TORG_NAME,
    S_MNN = EXCLUDED.S_MNN,
    S_FORMS = EXCLUDED.S_FORMS,
    S_PROD_PHASE = EXCLUDED.S_PROD_PHASE,
    S_DOCS = EXCLUDED.S_DOCS,
    S_FARM_GROUP = EXCLUDED.S_FARM_GROUP,
    S_CODES = EXCLUDED.S_CODES;
    --pgsql:go
  </xsl:template>

  <!-- V024_DopKr: V024.xml -->
  <xsl:template match="packet[zglv[type='DopKr']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="DopKr" />
    </xsl:for-each>
  </xsl:template>

  <!-- V024 "<zap>" template -->
  <xsl:template name="DopKr">
    do $$
    begin
    if not exists(select * from AKUZ.T_V024 where IDDKK = '<xsl:value-of select="IDDKK"/>' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V024 (V024DoprKrID, IDDKK, DKKNAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , SUBSTRING('<xsl:value-of select="IDDKK" />', 1, 255)
    , <xsl:choose>
      <xsl:when test="DKKNAME != '' and string-length(DKKNAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DKKNAME" />
        <xsl:text>', 1, 500)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    else
    UPDATE AKUZ.T_V024 SET
    DKKNAME = <xsl:choose>
      <xsl:when test="DKKNAME != '' and string-length(DKKNAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DKKNAME" />
        <xsl:text>', 1, 500)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where IDDKK = '<xsl:value-of select="IDDKK"/>' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')::timestamp;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V028_RefType: V028.xml -->
  <xsl:template match="packet[zglv[type='NAPR_V']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="NAPR_Vzap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V028 "<zap>" template -->
  <xsl:template name="NAPR_Vzap">
    do $$
    begin
    IF NOT EXISTS (select 1 from AKUZ.T_V028_REFERRAL_TYPE where IDVN = <xsl:value-of select="IDVN" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V028_REFERRAL_TYPE (IDVN, N_VN, DATEBEG, DATEEND)
    values(
    <xsl:value-of select="IDVN" />
    , <xsl:choose>
      <xsl:when test="N_VN != '' and string-length(N_VN) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="N_VN" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    else
    UPDATE AKUZ.T_V028_REFERRAL_TYPE SET
    N_VN = <xsl:choose>
      <xsl:when test="N_VN != '' and string-length(N_VN) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="N_VN" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where IDVN = <xsl:value-of select="IDVN" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V029_ResMet: V029.xml -->
  <xsl:template match="packet[zglv[type='MET_ISSL']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="MET_ISSLzap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V029 "<zap>" template -->
  <xsl:template name="MET_ISSLzap">
    do $$
    begin
    IF NOT EXISTS (select 1 from AKUZ.T_V029_RESEARCH_METHOD where IDMET = <xsl:value-of select="IDMET" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V029_RESEARCH_METHOD (IDMET, N_MET, DATEBEG, DATEEND)
    VALUES(
    <xsl:value-of select="IDMET" />
    , SUBSTRING('<xsl:value-of select="N_MET" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    else
    UPDATE AKUZ.T_V029_RESEARCH_METHOD SET
    N_MET = SUBSTRING('<xsl:value-of select="N_MET" />', 1, 255)
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where IDMET = <xsl:value-of select="IDMET" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')::timestamp;
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V031_GroupDrug: V031.xml -->
  <xsl:template match="packet[zglv[type='GroupDrug']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="GroupDrugZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V031 "<zap>" template -->
  <xsl:template name="GroupDrugZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_V031_GROUP_DRUG where DrugGroupCode = <xsl:value-of select="DrugGroupCode" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V031_GROUP_DRUG (V031GroupDrugID, DrugGroupCode, ManIndMNN, DrugGroup, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="DrugGroupCode" />
    , <xsl:choose>
      <xsl:when test="ManIndMNN != '' and string-length(ManIndMNN) &gt; 0">
        <xsl:value-of select="ManIndMNN" />
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , SUBSTRING('<xsl:value-of select="DrugGroup" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V031_GROUP_DRUG SET
    DrugGroupCode = <xsl:value-of select="DrugGroupCode" />
    , ManIndMNN = <xsl:choose>
      <xsl:when test="ManIndMNN != '' and string-length(ManIndMNN) &gt; 0">
        <xsl:value-of select="ManIndMNN" />
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DrugGroup = '<xsl:value-of select="DrugGroup" />'
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where DrugGroupCode = <xsl:value-of select="DrugGroupCode" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V032_DrugGrReg: V032.xml -->
  <xsl:template match="packet[zglv[type='DrugGrReg']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="DrugGrRegZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V032 "<zap>" template -->
  <xsl:template name="DrugGrRegZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_V032_DRUG_GR_REG
    where ScheDrugGrCd = '<xsl:value-of select="ScheDrugGrCd" />' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) then
    INSERT INTO AKUZ.T_V032_DRUG_GR_REG (V032DrugGrRegID, ScheDrugGrCd, Name, SchemCode, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , SUBSTRING('<xsl:value-of select="ScheDrugGrCd" />', 1, 255)
    , SUBSTRING('<xsl:value-of select="Name" />', 1, 255)
    , SUBSTRING('<xsl:value-of select="SchemCode" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')::timestamp
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V032_DRUG_GR_REG SET
    ScheDrugGrCd = SUBSTRING('<xsl:value-of select="ScheDrugGrCd" />', 1, 255)
    , Name = SUBSTRING('<xsl:value-of select="Name" />', 1, 255)
    , SchemCode =SUBSTRING('<xsl:value-of select="SchemCode" />', 1, 255)
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')::timestamp
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ScheDrugGrCd = '<xsl:value-of select="ScheDrugGrCd" />' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V033_SchemCodDrug: V033.xml -->
  <xsl:template match="packet[zglv[type='SchemCodDrug']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="SchemCodDrugZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V033 "<zap>" template -->
  <xsl:template name="SchemCodDrugZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_V033_SCHEM_COD_DRUG
    where ScheDrugGrCd = '<xsl:value-of select="ScheDrugGrCd" />'
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    and DrugCode = <xsl:choose>
      <xsl:when test="DrugCode != '' and string-length(DrugCode) &gt; 0">
        <xsl:text>'</xsl:text>
        <xsl:value-of select="DrugCode" />
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>) then
    INSERT INTO AKUZ.T_V033_SCHEM_COD_DRUG (V033SchemCodDrugID, ScheDrugGrCd, DrugCode, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , '<xsl:value-of select="ScheDrugGrCd" />'
    , <xsl:choose>
      <xsl:when test="DrugCode != '' and string-length(DrugCode) &gt; 0">
        <xsl:text>'</xsl:text>
        <xsl:value-of select="DrugCode" />
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V033_SCHEM_COD_DRUG SET
    ScheDrugGrCd = '<xsl:value-of select="ScheDrugGrCd" />'
    , DrugCode = <xsl:choose>
      <xsl:when test="DrugCode != '' and string-length(DrugCode) &gt; 0">
        <xsl:text>'</xsl:text>
        <xsl:value-of select="DrugCode" />
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ScheDrugGrCd = '<xsl:value-of select="ScheDrugGrCd" />' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- V022_ModPac: V022.xml -->
  <xsl:template match="packet[zglv[type='ModPac']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="ModPacZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- V022 "<zap>" template -->
  <xsl:template name="ModPacZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_V022_PATIENT_MODEL where IDMPAC = <xsl:value-of select="IDMPAC" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />','DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_V022_PATIENT_MODEL (V022PatientModelID, IDMPAC, MPACNAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="IDMPAC" />
    , <xsl:choose>
    <xsl:when test="normalize-space(MPACNAME) != ''">
      '<xsl:value-of select="MPACNAME"/>'
    </xsl:when>
    <xsl:otherwise>''</xsl:otherwise>
  </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_V022_PATIENT_MODEL SET
    MPACNAME = <xsl:choose>
    <xsl:when test="normalize-space(MPACNAME) != ''">
      '<xsl:value-of select="MPACNAME"/>'
    </xsl:when>
    <xsl:otherwise>''</xsl:otherwise>
  </xsl:choose>
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where IDMPAC = <xsl:value-of select="IDMPAC" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />','DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N001_OnkPrOt: N001.xml -->
  <xsl:template match="packet[zglv[type='OnkPrOt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkPrOtZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N001 "<zap>" template -->
  <xsl:template name="OnkPrOtZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N001_OnkPrOt where ID_PrOt = <xsl:value-of select="ID_PrOt" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />','DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N001_OnkPrOt (N001ID, ID_PrOt, PrOt_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_PrOt" />
    , '<xsl:value-of select="PrOt_NAME" />'
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N001_OnkPrOt SET
    PrOt_NAME = '<xsl:value-of select="PrOt_NAME" />'
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_PrOt = <xsl:value-of select="ID_PrOt" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />','DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N002_OnkStad: N002.xml -->
  <xsl:template match="packet[zglv[type='OnkStad']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkStadZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N002 "<zap>" template -->
  <xsl:template name="OnkStadZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N002_OnkStad where ID_St = <xsl:value-of select="ID_St" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N002_OnkStad (N002ID, ID_St, DS_St, KOD_St, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_St" />
    , <xsl:choose>
      <xsl:when test="DS_St != '' and string-length(DS_St) &gt; 0">
        <xsl:text>'</xsl:text>
        <xsl:value-of select="DS_St" />
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , '<xsl:value-of select="KOD_St" />'
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N002_OnkStad SET
    DS_St = <xsl:choose>
      <xsl:when test="DS_St != '' and string-length(DS_St) &gt; 0">
        <xsl:text>'</xsl:text>
        <xsl:value-of select="DS_St" />
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , KOD_St = '<xsl:value-of select="KOD_St" />'
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_St = <xsl:value-of select="ID_St" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N003_OnkT: N003.xml -->
  <xsl:template match="packet[zglv[type='OnkT']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkTZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N003 "<zap>" template -->
  <xsl:template name="OnkTZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N003_OnkT
    where ID_T = <xsl:value-of select="ID_T" />
    and DS_T = <xsl:choose>
      <xsl:when test="DS_T != '' and string-length(DS_T) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_T" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    and KOD_T = SUBSTRING('<xsl:value-of select="KOD_T" />', 1, 255)
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N003_OnkT (N003ID, ID_T, DS_T, KOD_T, T_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_T" />
    , <xsl:choose>
      <xsl:when test="DS_T != '' and string-length(DS_T) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_T" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , SUBSTRING('<xsl:value-of select="KOD_T" />', 1, 255)
    , <xsl:choose>
      <xsl:when test="T_NAME != '' and string-length(T_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="T_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N003_OnkT SET
    DS_T = <xsl:choose>
      <xsl:when test="DS_T != '' and string-length(DS_T) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_T" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , KOD_T = SUBSTRING('<xsl:value-of select="KOD_T" />', 1, 255)
    , T_NAME = <xsl:choose>
      <xsl:when test="T_NAME != '' and string-length(T_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="T_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_T = <xsl:value-of select="ID_T" />
    and DS_T = <xsl:choose>
      <xsl:when test="DS_T != '' and string-length(DS_T) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_T" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    and KOD_T = SUBSTRING('<xsl:value-of select="KOD_T" />', 1, 255)
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N004_OnkN: N004.xml -->
  <xsl:template match="packet[zglv[type='OnkN']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkNZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N004 "<zap>" template -->
  <xsl:template name="OnkNZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N004_OnkN
    where ID_N = <xsl:value-of select="ID_N" />
    and KOD_N = SUBSTRING('<xsl:value-of select="KOD_N" />', 1, 5)
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N004_OnkN (N004ID, ID_N, DS_N, KOD_N, N_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_N" />
    , <xsl:choose>
      <xsl:when test="DS_N != '' and string-length(DS_N) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_N" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , SUBSTRING('<xsl:value-of select="KOD_N" />', 1, 5)
    , <xsl:choose>
      <xsl:when test="N_NAME != '' and string-length(N_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="N_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N004_OnkN SET
    DS_N = <xsl:choose>
      <xsl:when test="DS_N != '' and string-length(DS_N) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_N" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , KOD_N = SUBSTRING('<xsl:value-of select="KOD_N" />', 1, 5)
    , N_NAME = <xsl:choose>
      <xsl:when test="N_NAME != '' and string-length(N_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="N_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_N = <xsl:value-of select="ID_N" />
    and KOD_N = SUBSTRING('<xsl:value-of select="KOD_N" />', 1, 5)
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N005_OnkM: N005.xml -->
  <xsl:template match="packet[zglv[type='OnkM']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N005 "<zap>" template -->
  <xsl:template name="OnkMZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N005_OnkM
    where ID_M = <xsl:value-of select="ID_M" />
    and DS_M = <xsl:choose>
      <xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_M" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N005_OnkM (N005ID, ID_M, DS_M, KOD_M, M_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    ,<xsl:value-of select="ID_M" />
    , <xsl:choose>
      <xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_M" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , SUBSTRING('<xsl:value-of select="KOD_M" />', 1, 5)
    , <xsl:choose>
      <xsl:when test="M_NAME != '' and string-length(M_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="M_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N005_OnkM SET
    DS_M = <xsl:choose>
      <xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_M" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , KOD_M = SUBSTRING('<xsl:value-of select="KOD_M" />', 1, 5)
    , M_NAME = <xsl:choose>
      <xsl:when test="M_NAME != '' and string-length(M_NAME) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="M_NAME" />
        <xsl:text>', 1, 255)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_M = <xsl:value-of select="ID_M" />
    and DS_M = <xsl:choose>
      <xsl:when test="DS_M != '' and string-length(DS_M) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="DS_M" />
        <xsl:text>', 1, 5)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N006_OnkTNM: N006.xml -->
  <xsl:template match="packet[zglv[type='OnkTNM']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkTNMZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N006 "<zap>" template -->
  <xsl:template name="OnkTNMZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N006_OnkTNM where ID_gr = <xsl:value-of select="ID_gr" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N006_OnkTNM (N006ID, ID_gr, DS_gr, ID_St, ID_T, ID_N, ID_M, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_gr" />
    , SUBSTRING('<xsl:value-of select="DS_gr" />', 1, 5)
    , <xsl:value-of select="ID_St" />
    , <xsl:value-of select="ID_T" />
    , <xsl:value-of select="ID_N" />
    , <xsl:value-of select="ID_M" />
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    UPDATE AKUZ.T_N006_OnkTNM SET
    DS_gr = SUBSTRING('<xsl:value-of select="DS_gr" />', 1, 5)
    , ID_St = <xsl:value-of select="ID_St" />
    , ID_T = <xsl:value-of select="ID_T" />
    , ID_N = <xsl:value-of select="ID_N" />
    , ID_M = <xsl:value-of select="ID_M" />
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_gr = <xsl:value-of select="ID_gr" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N007_OnkMrf: N007.xml -->
  <xsl:template match="packet[zglv[type='OnkMrf']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrfZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N007 "<zap>" template -->
  <xsl:template name="OnkMrfZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N007_OnkMrf where ID_Mrf = <xsl:value-of select="ID_Mrf" />) THEN
    INSERT INTO AKUZ.T_N007_OnkMrf (N007ID, ID_Mrf, Mrf_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_Mrf" />
    , SUBSTRING('<xsl:value-of select="Mrf_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N007_OnkMrf SET
    Mrf_NAME = SUBSTRING('<xsl:value-of select="Mrf_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_Mrf = <xsl:value-of select="ID_Mrf" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N008_OnkMrfRt: N008.xml -->
  <xsl:template match="packet[zglv[type='OnkMrfRt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrfRtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N008 "<zap>" template -->
  <xsl:template name="OnkMrfRtZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N008_OnkMrfRt where ID_R_M = <xsl:value-of select="ID_R_M" />) THEN
    INSERT INTO AKUZ.T_N008_OnkMrfRt (N008ID, ID_R_M, ID_Mrf, R_M_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_R_M" />
    , <xsl:value-of select="ID_Mrf" />
    , SUBSTRING('<xsl:value-of select="R_M_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N008_OnkMrfRt SET
    ID_Mrf = <xsl:value-of select="ID_Mrf" />
    , R_M_NAME = SUBSTRING('<xsl:value-of select="R_M_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != ''">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_R_M = <xsl:value-of select="ID_R_M" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N009_OnkMrtDS: N009.xml -->
  <xsl:template match="packet[zglv[type='OnkMrtDS']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrtDSZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N009 "<zap>" template -->
  <xsl:template name="OnkMrtDSZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N009_OnkMrtDS where ID_M_D = <xsl:value-of select="ID_M_D" />) THEN
    INSERT INTO AKUZ.T_N009_OnkMrtDS (N009ID, ID_M_D, DS_Mrf, ID_Mrf, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_M_D" />
    , SUBSTRING('<xsl:value-of select="DS_Mrf" />', 1, 5)
    , <xsl:value-of select="ID_Mrf" />
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N009_OnkMrtDS SET
    DS_Mrf = SUBSTRING('<xsl:value-of select="DS_Mrf" />', 1, 5)
    , ID_Mrf = <xsl:value-of select="ID_Mrf" />
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_M_D = <xsl:value-of select="ID_M_D" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N010_OnkIgh: N010.xml -->
  <xsl:template match="packet[zglv[type='OnkIgh']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N010 "<zap>" template -->
  <xsl:template name="OnkIghZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N010_OnkIgh where ID_Igh = <xsl:value-of select="ID_Igh" />) THEN
    INSERT INTO AKUZ.T_N010_OnkIgh (N010ID, ID_Igh, KOD_Igh, Igh_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_Igh" />
    , SUBSTRING('<xsl:value-of select="KOD_Igh" />', 1, 255)
    , SUBSTRING('<xsl:value-of select="Igh_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N010_OnkIgh SET
    KOD_Igh = SUBSTRING('<xsl:value-of select="KOD_Igh" />', 1, 255)
    , Igh_NAME = SUBSTRING('<xsl:value-of select="Igh_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_Igh = <xsl:value-of select="ID_Igh" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N011_OnkIghRt: N011.xml -->
  <xsl:template match="packet[zglv[type='OnkIghRt']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghRtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N011 "<zap>" template -->
  <xsl:template name="OnkIghRtZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N011_OnkIghRt where ID_R_I = <xsl:value-of select="ID_R_I" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N011_OnkIghRt (N011ID, ID_R_I, ID_Igh, KOD_R_I, R_I_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_R_I" />
    , <xsl:value-of select="ID_Igh" />
    , SUBSTRING('<xsl:value-of select="KOD_R_I" />', 1, 255)
    , SUBSTRING('<xsl:value-of select="R_I_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N011_OnkIghRt SET
    ID_Igh = <xsl:value-of select="ID_Igh" />
    , KOD_R_I = SUBSTRING('<xsl:value-of select="KOD_R_I" />', 1, 255)
    , R_I_NAME = SUBSTRING('<xsl:value-of select="R_I_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_R_I = <xsl:value-of select="ID_R_I" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N012_OnkIghDS: N012.xml -->
  <xsl:template match="packet[zglv[type='OnkIghDS']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghDSZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N012 "<zap>" template -->
  <xsl:template name="OnkIghDSZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N012_OnkIghDS where ID_I_D = <xsl:value-of select="ID_I_D" />) THEN
    INSERT INTO AKUZ.T_N012_OnkIghDS (N012ID, ID_I_D, ID_Igh, DS_Igh, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_I_D" />
    , <xsl:value-of select="ID_Igh" />
    , SUBSTRING('<xsl:value-of select="DS_Igh" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N012_OnkIghDS SET
    ID_Igh = <xsl:value-of select="ID_Igh" />
    , DS_Igh = SUBSTRING('<xsl:value-of select="DS_Igh" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_I_D = <xsl:value-of select="ID_I_D" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N013_OnkLech: N013.xml -->
  <xsl:template match="packet[zglv[type='OnkLech']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLechZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N013 "<zap>" template -->
  <xsl:template name="OnkLechZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N013_OnkLech where ID_TLech = <xsl:value-of select="ID_TLech" />) THEN
    INSERT INTO AKUZ.T_N013_OnkLech (N013ID, ID_TLech, TLech_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_TLech" />
    , SUBSTRING('<xsl:value-of select="TLech_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N013_OnkLech SET
    TLech_NAME = SUBSTRING('<xsl:value-of select="TLech_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_TLech = <xsl:value-of select="ID_TLech" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N014_OnkHir: N014.xml -->
  <xsl:template match="packet[zglv[type='OnkHir']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkHirZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N014 "<zap>" template -->
  <xsl:template name="OnkHirZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N014_OnkHir where ID_THir = <xsl:value-of select="ID_THir" />) THEN
    INSERT INTO AKUZ.T_N014_OnkHir (N014ID, ID_THir, THir_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_THir" />
    , SUBSTRING('<xsl:value-of select="THir_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N014_OnkHir SET
    THir_NAME = SUBSTRING('<xsl:value-of select="THir_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_THir = <xsl:value-of select="ID_THir" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N015_OnkLek_L: N015.xml -->
  <xsl:template match="packet[zglv[type='OnkLek_L']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLek_LZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N015 "<zap>" template -->
  <xsl:template name="OnkLek_LZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N015_OnkLek_L where ID_TLek_L = <xsl:value-of select="ID_TLek_L" />) THEN
    INSERT INTO AKUZ.T_N015_OnkLek_L (N015ID, ID_TLek_L, TLek_NAME_L, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_TLek_L" />
    , SUBSTRING('<xsl:value-of select="TLek_NAME_L" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N015_OnkLek_L SET
    TLek_NAME_L = SUBSTRING('<xsl:value-of select="TLek_NAME_L" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_TLek_L = <xsl:value-of select="ID_TLek_L" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N016_OnkLek_V: N016.xml -->
  <xsl:template match="packet[zglv[type='OnkLek_V']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLek_VZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N016 "<zap>" template -->
  <xsl:template name="OnkLek_VZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N016_OnkLek_V where ID_TLek_V = <xsl:value-of select="ID_TLek_V" />) THEN
    INSERT INTO AKUZ.T_N016_OnkLek_V (N016ID, ID_TLek_V, TLek_NAME_V, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_TLek_V" />
    , SUBSTRING('<xsl:value-of select="TLek_NAME_V" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N016_OnkLek_V SET
    TLek_NAME_V = SUBSTRING('<xsl:value-of select="TLek_NAME_V" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_TLek_V = <xsl:value-of select="ID_TLek_V" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N017_OnkLuch: N017.xml -->
  <xsl:template match="packet[zglv[type='OnkLuch']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLuchZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N017 "<zap>" template -->
  <xsl:template name="OnkLuchZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N017_OnkLuch where ID_TLuch = <xsl:value-of select="ID_TLuch" />) THEN
    INSERT INTO AKUZ.T_N017_OnkLuch (N017ID, ID_TLuch, TLuch_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_TLuch" />
    , SUBSTRING('<xsl:value-of select="TLuch_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N017_OnkLuch SET
    TLuch_NAME = SUBSTRING('<xsl:value-of select="TLuch_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_TLuch = <xsl:value-of select="ID_TLuch" />;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N018_OnkReas: N018.xml -->
  <xsl:template match="packet[zglv[type='OnkReas']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkReasZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N018 "<zap>" template -->
  <xsl:template name="OnkReasZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N018_OnkReas where ID_REAS = <xsl:value-of select="ID_REAS" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N018_OnkReas (N018ID, ID_REAS, REAS_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_REAS" />
    , SUBSTRING('<xsl:value-of select="REAS_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N018_OnkReas SET
    REAS_NAME = SUBSTRING('<xsl:value-of select="REAS_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_REAS = <xsl:value-of select="ID_REAS" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N019_OnkCons: N019.xml -->
  <xsl:template match="packet[zglv[type='OnkCons']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkConsZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- N019 "<zap>" template -->
  <xsl:template name="OnkConsZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N019_OnkCons where ID_CONS = <xsl:value-of select="ID_CONS" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N019_OnkCons (N019ID, ID_CONS, CONS_NAME, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_CONS" />
    , SUBSTRING('<xsl:value-of select="CONS_NAME" />', 1, 255)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N019_OnkCons SET
    CONS_NAME = SUBSTRING('<xsl:value-of select="CONS_NAME" />', 1, 255)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_CONS = <xsl:value-of select="ID_CONS" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N021_OnkLpsh: N021.xml -->
  <xsl:template match="packet[zglv[type='OnkLpsh']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLpshZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N021 "<zap>" template -->
  <xsl:template name="OnkLpshZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N021_OnkLpsh where ID_ZAP = <xsl:value-of select="ID_ZAP" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N021_OnkLpsh (N021ID, ID_ZAP, CODE_SH, ID_LEKP, DATEBEG, DATEEND, ID_LEKP_EXT, LEKP_EXT)
    VALUES(
    gen_random_uuid()
    , <xsl:value-of select="ID_ZAP" />
    , SUBSTRING('<xsl:value-of select="CODE_SH" />', 1, 10)
    , SUBSTRING('<xsl:value-of select="ID_LEKP" />', 1, 6)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="ID_LEKP_EXT != '' and string-length(ID_LEKP_EXT) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="ID_LEKP_EXT" />
        <xsl:text>', 1, 25)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="LEKP_EXT != '' and string-length(LEKP_EXT) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="LEKP_EXT" />
        <xsl:text>', 1, 256)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N021_OnkLpsh SET
    CODE_SH = SUBSTRING('<xsl:value-of select="CODE_SH" />', 1, 10)
    , ID_LEKP = SUBSTRING('<xsl:value-of select="ID_LEKP" />', 1, 6)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , ID_LEKP_EXT = <xsl:choose>
      <xsl:when test="ID_LEKP_EXT != '' and string-length(ID_LEKP_EXT) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="ID_LEKP_EXT" />
        <xsl:text>', 1, 25)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , LEKP_EXT = <xsl:choose>
      <xsl:when test="LEKP_EXT != '' and string-length(LEKP_EXT) &gt; 0">
        <xsl:text>SUBSTRING('</xsl:text>
        <xsl:value-of select="LEKP_EXT" />
        <xsl:text>', 1, 256)</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_ZAP = <xsl:value-of select="ID_ZAP" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- N020_OnkLekp: N020.xml -->
  <xsl:template match="packet[zglv[type='OnkLekp']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLekpZap" />
    </xsl:for-each>
  </xsl:template>

  <!-- N020 "<zap>" template -->
  <xsl:template name="OnkLekpZap">
    DO $$
    BEGIN
    IF NOT EXISTS (select 1 from AKUZ.T_N020_OnkLekp where ID_LEKP = SUBSTRING('<xsl:value-of select="ID_LEKP" />', 1, 6) and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp) THEN
    INSERT INTO AKUZ.T_N020_OnkLekp (N020ID, ID_LEKP, MNN, DATEBEG, DATEEND)
    VALUES(
    gen_random_uuid()
    , SUBSTRING('<xsl:value-of select="ID_LEKP" />', 1, 6)
    , SUBSTRING('<xsl:value-of select="MNN" />', 1, 300)
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp
    , <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    );
    ELSE
    UPDATE AKUZ.T_N020_OnkLekp SET
    MNN = SUBSTRING('<xsl:value-of select="MNN" />', 1, 300)
    , DATEEND = <xsl:choose>
      <xsl:when test="DATEEND != '' and string-length(DATEEND) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="DATEEND" />
        <xsl:text>', 'DD.MM.YYYY')::timestamp</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    where ID_LEKP = SUBSTRING('<xsl:value-of select="ID_LEKP" />', 1, 6) and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')::timestamp;
    END IF;
    END;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- CouncilGoals: N019.xml -->
  <xsl:template match="packet[(zglv[type='N019CouncilGoals'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="N019CouncilGoals" />
    </xsl:for-each>
  </xsl:template>

  <!-- N019 "<zap>" template -->
  <xsl:template name="N019CouncilGoals">
    INSERT INTO AKUZ.T_COUNCIL_GOAL (CouncilGoalID, Code, Name, Type, DateBegin, DateEnd)
    SELECT
    gen_random_uuid(),
    <xsl:value-of select="ID_CONS" />,
    SUBSTRING('<xsl:value-of select="CONS_NAME" />', 1, 255),
    1,
    TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY'),
    <xsl:choose>
      <xsl:when test="DATEEND != ''">
        TO_DATE('<xsl:value-of select="DATEEND" />', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    ON CONFLICT (Code, Type) WHERE Type = 1 DO UPDATE SET
    <xsl:if test="CONS_NAME != ''">Name = EXCLUDED.Name,</xsl:if>
    DateEnd = EXCLUDED.DateEnd;
  </xsl:template>

  <!-- F014_RESULT_DENY_REASON: F014.xml -->
  <xsl:template match="packet[(zglv[type='OplOtk'])]">
    <xsl:for-each select="zap">
      <!-- insert or update F014_RESULT_DENY_REASON -->
      <xsl:call-template name="OplOtkZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- F014_RESULT_DENY_REASON "<zap>" template -->
  <xsl:template name="OplOtkZap" >
    create extension if not exists "uuid-ossp";
    do $$
    begin
    if not exists(select * from AKUZ.T_F014_DENY_REASON  where Kod = <xsl:value-of select="Kod" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')) then
    insert into AKUZ.T_F014_DENY_REASON (F014DenyReasonID, Kod, Naim, DATEBEG, DATEEND, Osn, Komment, IDVID, KodPG)
    values(
    uuid_generate_v4()
    , <xsl:value-of select="Kod" />
    , '<xsl:value-of select="Naim" />'
    , TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    , <xsl:if test="DATEEND != ''">
      <xsl:text>TO_DATE('</xsl:text>
      <xsl:value-of select="DATEEND" />
      <xsl:text>', 'DD.MM.YYYY')</xsl:text>
    </xsl:if>
    <xsl:if test="DATEEND = ''">NULL</xsl:if>
    , '<xsl:value-of select="Osn" />'
    , '<xsl:value-of select="Komment" />'
    , <xsl:if test="IDVID != ''">
      <xsl:value-of select="IDVID" />
    </xsl:if>
    <xsl:if test="IDVID = ''">NULL</xsl:if>
    , <xsl:if test="KodPG != ''">
      '<xsl:value-of select="KodPG" />'
    </xsl:if>
    <xsl:if test="KodPG = ''">NULL</xsl:if>
    );
    else
    update AKUZ.T_F014_DENY_REASON
    set
    Kod = <xsl:value-of select="Kod" />
    , Naim = '<xsl:value-of select="Naim" />'
    , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY')
    , DATEEND = <xsl:if test="DATEEND != ''">
      <xsl:text>TO_DATE('</xsl:text>
      <xsl:value-of select="DATEEND" />
      <xsl:text>', 'DD.MM.YYYY')</xsl:text>
    </xsl:if>
    <xsl:if test="DATEEND = ''">NULL</xsl:if>
    , Osn = '<xsl:value-of select="Osn" />'
    , Komment = '<xsl:value-of select="Komment" />'
    where Kod = <xsl:value-of select="Kod" /> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG" />', 'DD.MM.YYYY');
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- F002 INSURER: F002.xml -->
  <xsl:template match="PACKET[insCompany]">
    <xsl:for-each select="insCompany">
      <!-- insert or update F002 INSURER -->
      <xsl:call-template name="InsCompany"/>
    </xsl:for-each>
  </xsl:template>
  <!-- F002 INSURER "<insCompany>" template -->
  <xsl:template name="InsCompany">
    create extension if not exists "uuid-ossp";
    do $$
    begin
    if not exists(select * from AKUZ.T_INSURER where SmoCod = '<xsl:value-of select="smocod"/>' and Gk_start = TO_DATE('<xsl:value-of select="insInclude/d_begin"/>', 'DD.MM.YYYY')) then
    INSERT INTO AKUZ.T_INSURER
    (InsurerID, OKATO, SmoCod, Name, ShortName, INN, OGRN, KPP, IndexLegal, AddressLegal,
    IndexReal, Address, OKOPF, DirectorLastName, DirectorFirstName, DirectorFatherName, Phone, Fax, <!--hot_line,--> EMail, SAIT,
    N_DOC, Date_b, Date_e,<!-- D_TERM,--> ORG, Gk_start, Gk_end, NAME_E, NAL_P, <!--YEAR_WORK,--> DUVED,
    KOL_ZL, Date_red)
    values(
    <!-- D_TERM, YEAR_WORK, hot_line,  -->
    uuid_generate_v4()
    , '<xsl:value-of select="TF_OKATO"/>'
    , '<xsl:value-of select="smocod"/>'
    , '<xsl:value-of select="nam_smop"/>'
    , '<xsl:value-of select="nam_smok"/>'
    , '<xsl:value-of select="INN"/>'
    , '<xsl:value-of select="Ogrn"/>'
    , '<xsl:value-of select="KPP"/>'
    , '<xsl:value-of select="jurAddress/INDEX_J"/>'
    , '<xsl:value-of select="jurAddress/ADDR_J"/>'
    , '<xsl:value-of select="pstAddress/INDEX_F"/>'
    , '<xsl:value-of select="pstAddress/addr_f"/>'
    , '<xsl:value-of select="OKOPF"/>'
    , '<xsl:value-of select="fam_ruk"/>'
    , '<xsl:value-of select="im_ruk"/>'
    , '<xsl:value-of select="ot_ruk"/>'
    , '<xsl:value-of select="Phone"/>'
    , '<xsl:value-of select="Fax"/>'
    <!--, '<xsl:value-of select="hot_line"/>'-->
    , '<xsl:value-of select="e_mail"/>'
    , '<xsl:value-of select="WWW"/>'
    , '<xsl:value-of select="licenziy/N_DOC"/>'
    , TO_DATE('<xsl:value-of select="insInclude/d_begin"/>', 'DD.MM.YYYY')
    , <xsl:choose>
      <xsl:when test="insInclude/d_end != '' and string-length(insInclude/d_end) &gt; 0">
        TO_DATE('<xsl:value-of select="insInclude/d_end"/>', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    <!--, TO_DATE('<xsl:value-of select="licenziy/D_TERM"/>', 'DD.MM.YYYY')-->
    , <xsl:value-of select="ORG" />
    , TO_DATE('<xsl:value-of select="licenziy/D_START"/>', 'DD.MM.YYYY')
    , <xsl:choose>
      <xsl:when test="licenziy/DATE_E != '' and string-length(licenziy/DATE_E) &gt; 0">
        <xsl:text>TO_DATE('</xsl:text>
        <xsl:value-of select="licenziy/DATE_E"/>
        <xsl:text>', 'DD.MM.YYYY')</xsl:text>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="insInclude/NAME_E != '' and string-length(insInclude/NAME_E) &gt; 0">
        <xsl:value-of select="insInclude/NAME_E"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , <xsl:choose>
      <xsl:when test="insInclude/NAL_P != '' and string-length(insInclude/NAL_P) &gt; 0">
        <xsl:value-of select="insInclude/NAL_P"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    <!--, '<xsl:value-of select="insAdvice/YEAR_WORK"/>'-->
    , TO_DATE('<xsl:value-of select="insAdvice/DUVED"/>', 'DD.MM.YYYY')
    , <xsl:choose>
      <xsl:when test="insAdvice/KOL_ZL != '' and string-length(insAdvice/KOL_ZL) &gt; 0">
        <xsl:value-of select="insAdvice/KOL_ZL"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , TO_DATE('<xsl:value-of select="D_EDIT"/>', 'DD.MM.YYYY')
    );
    else
    update AKUZ.T_INSURER
    set
    OKATO = '<xsl:value-of select="TF_OKATO"/>'
    , SmoCod = '<xsl:value-of select="smocod"/>'
    , Name = '<xsl:value-of select="nam_smop"/>'
    , ShortName = '<xsl:value-of select="nam_smok"/>'
    , OGRN = '<xsl:value-of select="Ogrn"/>'
    , KPP = '<xsl:value-of select="KPP"/>'
    , IndexLegal = '<xsl:value-of select="jurAddress/INDEX_J"/>'
    , AddressLegal = '<xsl:value-of select="jurAddress/ADDR_J"/>'
    , IndexReal = '<xsl:value-of select="pstAddress/INDEX_F"/>'
    , Address = '<xsl:value-of select="pstAddress/addr_f"/>'
    , OKOPF = '<xsl:value-of select="OKOPF"/>'
    , DirectorLastName = '<xsl:value-of select="fam_ruk"/>'
    , DirectorFirstName = '<xsl:value-of select="im_ruk"/>'
    , DirectorFatherName = '<xsl:value-of select="ot_ruk"/>'
    , Phone = '<xsl:value-of select="Phone"/>'
    , Fax = '<xsl:value-of select="Fax"/>'
    <!--, hot_line = '<xsl:value-of select="hot_line"/>'-->
    , EMail = '<xsl:value-of select="e_mail"/>'
    , SAIT = '<xsl:value-of select="WWW"/>'
    , N_DOC = '<xsl:value-of select="licenziy/N_DOC"/>'
    , Date_b = TO_DATE('<xsl:value-of select="insInclude/d_begin" />', 'DD.MM.YYYY')
    , Date_e = <xsl:choose>
      <xsl:when test="insInclude/d_end != '' and string-length(insInclude/d_end) &gt; 0">
        TO_DATE('<xsl:value-of select="insInclude/d_end"/>', 'DD.MM.YYYY')
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    <!--, D_TERM = TO_DATE('<xsl:value-of select="licenziy/D_TERM"/>', 'DD.MM.YYYY')-->
    , ORG = <xsl:value-of select="ORG" />
    , Gk_start = TO_DATE('<xsl:value-of select="licenziy/D_START"/>', 'DD.MM.YYYY')
    , Gk_end = TO_DATE('<xsl:value-of select="licenziy/DATE_E"/>', 'DD.MM.YYYY')
    , NAME_E = <xsl:choose>
      <xsl:when test="insInclude/NAME_E != '' and string-length(insInclude/NAME_E) &gt; 0">
        <xsl:value-of select="insInclude/NAME_E"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , NAL_P = <xsl:choose>
      <xsl:when test="insInclude/NAL_P != '' and string-length(insInclude/NAL_P) &gt; 0">
        <xsl:value-of select="insInclude/NAL_P"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    <!--, YEAR_WORK = '<xsl:value-of select="insAdvice/YEAR_WORK"/>'-->
    , DUVED = TO_DATE('<xsl:value-of select="insAdvice/DUVED"/>', 'DD.MM.YYYY')
    , KOL_ZL = <xsl:choose>
      <xsl:when test="insAdvice/KOL_ZL != '' and string-length(insAdvice/KOL_ZL) &gt; 0">
        <xsl:value-of select="insAdvice/KOL_ZL"/>
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>
    , Date_red = TO_DATE('<xsl:value-of select="D_EDIT"/>', 'DD.MM.YYYY')
    where SmoCod = '<xsl:value-of select="smocod"/>' and Gk_start = TO_DATE('<xsl:value-of select="insInclude/d_begin"/>', 'DD.MM.YYYY');
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>

  <!-- F032 TRMO: F032.xml -->
  <xsl:template match="packet[(zglv[starts-with(type, 'TRMO-')])]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        INSERT INTO AKUZ.T_F032_TRMO (F032TrmoID, UIDMO, IDMO, MCOD, OKTMO_P, SUBJ, D_BEGIN, D_BEGIN_OMS, D_END, NAME_E, OSP, PARENT_IDMO
        , PARENT_UIDMO, VID_MO, OID_MO, OID_SPMO, NAM_MOP, NAM_MOK, INN, KPP, OGRN, JURADDRESS_INDEX, JURADDRESS_ADDRESS, GAR_ADDRESS
        , OKFS, VEDPRI, PHONE, FAX, EMAIL, DATEBEG, DATEEND)
        VALUES
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
            <xsl:text>,&#10;</xsl:text>
          </xsl:if>
          <xsl:text>(</xsl:text>
          <xsl:text>uuid_generate_v4(),</xsl:text>
          '<xsl:value-of select="UIDMO"/>',
          '<xsl:value-of select="IDMO"/>',
          '<xsl:value-of select="MCOD"/>',
          '<xsl:value-of select="OKTMO_P"/>',
          '<xsl:value-of select="SUBJ"/>',
          TO_DATE('<xsl:value-of select="D_BEGIN"/>', 'DD.MM.YYYY'),
          TO_DATE('<xsl:value-of select="D_BEGIN_OMS"/>', 'DD.MM.YYYY'),
          <xsl:choose>
            <xsl:when test="normalize-space(D_END) != ''">
              <xsl:text>TO_DATE('</xsl:text>
              <xsl:value-of select="D_END"/>
              <xsl:text>', 'DD.MM.YYYY')</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          <xsl:choose>
            <xsl:when test="normalize-space(NAME_E) != ''">
              <xsl:value-of select="NAME_E"/>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          <xsl:value-of select="OSP"/>,
          '<xsl:value-of select="PARENT_IDMO"/>',
          '<xsl:value-of select="PARENT_UIDMO"/>',
          '<xsl:value-of select="VID_MO"/>',
          <xsl:choose>
            <xsl:when test="normalize-space(OID_MO) != ''">
              '<xsl:value-of select="OID_MO"/>'
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>,
          '<xsl:value-of select="OID_SPMO"/>',
          '<xsl:value-of select="NAM_MOP"/>',
          '<xsl:value-of select="NAM_MOK"/>',
          '<xsl:value-of select="INN"/>',
          '<xsl:value-of select="KPP"/>',
          '<xsl:value-of select="OGRN"/>',
          '<xsl:value-of select="JURADDRESS_INDEX"/>',
          '<xsl:value-of select="JURADDRESS_ADDRESS"/>',
          '<xsl:value-of select="GAR_ADDRESS"/>',
          '<xsl:value-of select="OKFS"/>',
          '<xsl:value-of select="VEDPRI"/>',
          '<xsl:value-of select="PHONE"/>',
          '<xsl:value-of select="FAX"/>',
          '<xsl:value-of select="EMAIL"/>',
          TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY'),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>TO_DATE('</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 'DD.MM.YYYY')</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:for-each>
        ON CONFLICT (UIDMO, DATEBEG)
        DO UPDATE SET
        IDMO = EXCLUDED.IDMO,
        MCOD = EXCLUDED.MCOD,
        OKTMO_P = EXCLUDED.OKTMO_P,
        SUBJ = EXCLUDED.SUBJ,
        D_BEGIN = EXCLUDED.D_BEGIN,
        D_BEGIN_OMS = EXCLUDED.D_BEGIN_OMS,
        D_END = EXCLUDED.D_END,
        NAME_E = EXCLUDED.NAME_E,
        OSP = EXCLUDED.OSP,
        PARENT_IDMO = EXCLUDED.PARENT_IDMO,
        PARENT_UIDMO = EXCLUDED.PARENT_UIDMO,
        VID_MO = EXCLUDED.VID_MO,
        OID_MO = EXCLUDED.OID_MO,
        OID_SPMO = EXCLUDED.OID_SPMO,
        NAM_MOP = EXCLUDED.NAM_MOP,
        NAM_MOK = EXCLUDED.NAM_MOK,
        INN = EXCLUDED.INN,
        KPP = EXCLUDED.KPP,
        OGRN = EXCLUDED.OGRN,
        JURADDRESS_INDEX = EXCLUDED.JURADDRESS_INDEX,
        JURADDRESS_ADDRESS = EXCLUDED.JURADDRESS_ADDRESS,
        GAR_ADDRESS = EXCLUDED.GAR_ADDRESS,
        OKFS = EXCLUDED.OKFS,
        VEDPRI = EXCLUDED.VEDPRI,
        PHONE = EXCLUDED.PHONE,
        FAX = EXCLUDED.FAX,
        EMAIL = EXCLUDED.EMAIL,
        DATEEND = EXCLUDED.DATEEND;
        --pgsql:go
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <!-- F033 SPMO: F033.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'SPMO-')]]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        INSERT INTO AKUZ.T_F033_SPMO (F033SpmoID, UIDSPMO, IDSPMO, NAM_SPMO, NAM_SK_SPMO, PHONE, OSP, VID_SPMO, OID_SPMO, DATEBEG, DATEEND)
        VALUES
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
            <xsl:text>,&#10;</xsl:text>
          </xsl:if>
          <xsl:text>(</xsl:text>
          <xsl:text>uuid_generate_v4(),</xsl:text>
          '<xsl:value-of select="UIDSPMO"/>',
          '<xsl:value-of select="IDSPMO"/>',
          '<xsl:value-of select="NAM_SPMO"/>',
          '<xsl:value-of select="NAM_SK_SPMO"/>',
          '<xsl:value-of select="PHONE"/>',
          <xsl:value-of select="OSP"/>,
          '<xsl:value-of select="VID_SPMO"/>',
          '<xsl:value-of select="OID_SPMO"/>',
          TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY'),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>TO_DATE('</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 'DD.MM.YYYY')</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:for-each>
        ON CONFLICT (UIDSPMO, DATEBEG)
        DO UPDATE SET
        IDSPMO = EXCLUDED.IDSPMO,
        NAM_SPMO = EXCLUDED.NAM_SPMO,
        NAM_SK_SPMO = EXCLUDED.NAM_SK_SPMO,
        PHONE = EXCLUDED.PHONE,
        OSP = EXCLUDED.OSP,
        VID_SPMO = EXCLUDED.VID_SPMO,
        OID_SPMO = EXCLUDED.OID_SPMO,
        DATEEND = EXCLUDED.DATEEND;
        --pgsql:go
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <!-- F034 VUP: F034.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'VUP-')]]">
    <xsl:variable name="zaps" select="zap"/>
    <xsl:variable name="chunk-size" select="500"/>
    <xsl:variable name="total" select="count($zaps)"/>
    <xsl:if test="$total > 0">
      <xsl:for-each select="$zaps[position() mod $chunk-size = 1]">
        <xsl:variable name="real-start-pos" select="(position() - 1) * $chunk-size + 1"/>
        <xsl:variable name="current-chunk" select="$zaps[position() >= $real-start-pos and position() &lt; $real-start-pos + $chunk-size]"/>
        INSERT INTO AKUZ.T_F034_VUP (F034VupID, UIDSPMO, IDADDRESS, MPVID, MPUSL, MPROF, DATEBEG, DATEEND)
        VALUES
        <xsl:for-each select="$current-chunk">
          <xsl:if test="position() &gt; 1">
            <xsl:text>,&#10;</xsl:text>
          </xsl:if>
          <xsl:text>(</xsl:text>
          <xsl:text>uuid_generate_v4(),</xsl:text>
          '<xsl:value-of select="UIDSPMO"/>',
          '<xsl:value-of select="IDADDRESS"/>',
          <xsl:value-of select="MPVID"/>,
          <xsl:value-of select="MPUSL"/>,
          <xsl:value-of select="MPROF"/>,
          TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY'),
          <xsl:choose>
            <xsl:when test="normalize-space(DATEEND) != ''">
              <xsl:text>TO_DATE('</xsl:text>
              <xsl:value-of select="DATEEND"/>
              <xsl:text>', 'DD.MM.YYYY')</xsl:text>
            </xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
          </xsl:choose>
          <xsl:text>)</xsl:text>
        </xsl:for-each>
        ON CONFLICT (UIDSPMO, IDADDRESS, MPVID, MPUSL, MPROF, DATEBEG)
        DO UPDATE SET DATEEND = EXCLUDED.DATEEND;
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
  
  <!-- F038 ADDRMP: F038.xml -->
  <xsl:template match="packet[zglv[starts-with(type, 'ADDRMP-')]]">
    <xsl:for-each select="zap">
      <!-- insert or update F038 ADDRMP -->
      <xsl:call-template name="addrmpZap"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="escape-sql-string">
    <xsl:param name="text"/>
    <xsl:choose>
      <xsl:when test='contains($text, "&apos;")'>
        <xsl:value-of select='substring-before($text, "&apos;")'/>
        <xsl:text>''</xsl:text>
        <xsl:call-template name="escape-sql-string">
          <xsl:with-param name="text" select='substring-after($text, "&apos;")'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- F038 ADDRMP "<addrmp>" template -->
  <xsl:template name="addrmpZap">
    <xsl:variable name="addrEscaped">
      <xsl:call-template name="escape-sql-string">
        <xsl:with-param name="text" select="ADDR"/>
      </xsl:call-template>
    </xsl:variable>
    create extension if not exists "uuid-ossp";
    do $$
    begin
    if not exists(select * from AKUZ.T_F038_ADDRMP where IDADDRESS = '<xsl:value-of select="IDADDRESS"/>' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')) then
    insert into AKUZ.T_F038_ADDRMP (F038AddrmpID, IDADDRESS, UIDMO, UIDSPMO, N_DOC, ADDR, ADDR_GAR, DATEBEG, DATEEND)
    values(
      uuid_generate_v4()
      , '<xsl:value-of select="IDADDRESS"/>'
      , '<xsl:value-of select="UIDMO"/>'
      , '<xsl:value-of select="UIDSPMO"/>'
      , '<xsl:value-of select="N_DOC"/>'
      , '<xsl:value-of select="$addrEscaped"/>'
      , '<xsl:value-of select="ADDR_GAR"/>'
      , TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')
      , <xsl:choose>
          <xsl:when test="DATEEND != ''">
            TO_DATE('<xsl:value-of select="DATEEND"/>', 'DD.MM.YYYY')
          </xsl:when>
          <xsl:otherwise>NULL</xsl:otherwise>
        </xsl:choose>
    );
    else
    update AKUZ.T_F038_ADDRMP
    set
      UIDMO = '<xsl:value-of select="UIDMO"/>'
      , UIDSPMO = '<xsl:value-of select="UIDSPMO"/>'
      , N_DOC = '<xsl:value-of select="N_DOC"/>'
      , ADDR = '<xsl:value-of select="$addrEscaped"/>'
      , ADDR_GAR = '<xsl:value-of select="ADDR_GAR"/>'
      , DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')
      , DATEEND = <xsl:choose>
          <xsl:when test="DATEEND != ''">
            TO_DATE('<xsl:value-of select="DATEEND"/>', 'DD.MM.YYYY')
          </xsl:when>
          <xsl:otherwise>NULL</xsl:otherwise>
        </xsl:choose>
    where IDADDRESS = '<xsl:value-of select="IDADDRESS"/>' and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY');
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>
  
  <!-- V039 KVZ: V039.xml -->
  <xsl:template match="packet[zglv[type='KVZ']]">
    <xsl:for-each select="zap">
      <xsl:call-template name="kvzZap"/>
    </xsl:for-each>
  </xsl:template>

  <!-- V039 KVZ "<kvz>" template -->
  <xsl:template name="kvzZap">
    create extension if not exists "uuid-ossp";
    do $$
    begin
    if not exists(select * from AKUZ.T_V039_KVZ where ID_VZ = <xsl:value-of select="ID_VZ"/> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY')) then
    insert into AKUZ.T_V039_KVZ (V039KvzID, ID_VZ, N_VZ, DATEBEG, DATEEND)
    values(
      uuid_generate_v4(),
      <xsl:value-of select="ID_VZ"/>,
      '<xsl:value-of select="N_VZ"/>',
      TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY'),
      <xsl:choose>
        <xsl:when test="DATEEND != ''">
          TO_DATE('<xsl:value-of select="DATEEND"/>', 'DD.MM.YYYY')
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    );
    else
    update AKUZ.T_V039_KVZ
    set
      N_VZ = '<xsl:value-of select="N_VZ"/>',
      DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY'),
      DATEEND = <xsl:choose>
        <xsl:when test="DATEEND != ''">
          TO_DATE('<xsl:value-of select="DATEEND"/>', 'DD.MM.YYYY')
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>
    where ID_VZ = <xsl:value-of select="ID_VZ"/> and DATEBEG = TO_DATE('<xsl:value-of select="DATEBEG"/>', 'DD.MM.YYYY');
    end if;
    end;
    $$;
    --pgsql:go
  </xsl:template>
  
  <!-- Шаблон загрузки настроек сводной информации ВИМИС регистра -->
  <xsl:template match="VimisConsolidationSettings/Settings">
    <xsl:variable name="priorityPath">
      <xsl:call-template name="replace">
        <xsl:with-param name="text">
          <xsl:value-of select="@priorityXpath" />
        </xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="priorityLexem">
      <xsl:call-template name="replace">
        <xsl:with-param name="text">
          <xsl:value-of select="@priorityLexem" />
        </xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="xsltTemplate">
      <xsl:call-template name="replace">
        <xsl:with-param name="text">
          <xsl:value-of select="@template" />
        </xsl:with-param>
        <xsl:with-param name="replace">'</xsl:with-param>
        <xsl:with-param name="by">''</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    INSERT INTO AKUZ.S_VIMIS_CONSOLIDATION_GroupType(value) select '<xsl:value-of select="@group" />' where not exists (select * from AKUZ.S_VIMIS_CONSOLIDATION_GroupType where value = '<xsl:value-of select="@group" />');

    INSERT INTO AKUZ.T_VIMIS_CONSOLIDATION_SETTING
    (VimisConsolidationSettingID
    ,Entities
    ,GroupType
    ,PriorityXpath
    ,PriorityLexem
    ,XsltTemplate)
    SELECT
    gen_random_uuid()--VimisConsolidationSettingID, uniqueidentifier,
    ,(select e.EntityID from VCLib.T_ENTITIES e where e."schema" || '.' || e.EntName = '<xsl:value-of select="@entity" />' limit 1)--Entities, int,
    ,(select v.id from AKUZ.S_VIMIS_CONSOLIDATION_GroupType v where v.value = '<xsl:value-of select="@group" />' limit 1)--GroupType, int,
    ,<xsl:choose>
      <xsl:when test="@priorityXpath!=''">
        '<xsl:value-of select="$priorityPath" />'
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>--PriorityXpath, varchar(1000),
    ,<xsl:choose>
      <xsl:when test="@priorityLexem!=''">
        '<xsl:value-of select="$priorityLexem" />'
      </xsl:when>
      <xsl:otherwise>NULL</xsl:otherwise>
    </xsl:choose>--PriorityLexem, varchar(1000),
    ,'<xsl:value-of select="$xsltTemplate" />'--XsltTemplate, varchar(max),)
    where not exists
    (
    select *
    from AKUZ.T_VIMIS_CONSOLIDATION_SETTING cs
    join VCLib.T_ENTITIES e on e.EntityID = cs.Entities
    join AKUZ.S_VIMIS_CONSOLIDATION_GroupType v on v.id = cs.GroupType
    where e."schema" || '.' || e.EntName = '<xsl:value-of select="@entity" />'
    and
    v.value = '<xsl:value-of select="@group" />'
    and
    cs.PriorityXpath <xsl:choose>
      <xsl:when test="@priorityXpath!=''">
        = '<xsl:value-of select="$priorityPath" />'
      </xsl:when>
      <xsl:otherwise>is NULL</xsl:otherwise>
    </xsl:choose>
    and
    cs.PriorityLexem <xsl:choose>
      <xsl:when test="@priorityLexem!=''">
        = '<xsl:value-of select="$priorityLexem" />'
      </xsl:when>
      <xsl:otherwise>is NULL</xsl:otherwise>
    </xsl:choose>
    );
    update AKUZ.T_VIMIS_CONSOLIDATION_SETTING cs
    set XsltTemplate = '<xsl:value-of select="$xsltTemplate" />'
    from VCLib.T_ENTITIES e, AKUZ.S_VIMIS_CONSOLIDATION_GroupType v
    where e.EntityID = cs.Entities
    and
    v.id = cs.GroupType
    and
    e."schema" || '.' || e.EntName = '<xsl:value-of select="@entity" />'
    and
    v.value = '<xsl:value-of select="@group" />'
    and
    PriorityXpath <xsl:choose>
      <xsl:when test="@priorityXpath!=''">
        = '<xsl:value-of select="$priorityPath" />'
      </xsl:when>
      <xsl:otherwise>is NULL</xsl:otherwise>
    </xsl:choose>
    and
    PriorityLexem <xsl:choose>
      <xsl:when test="@priorityLexem!=''">
        = '<xsl:value-of select="$priorityLexem" />'
      </xsl:when>
      <xsl:otherwise>is NULL</xsl:otherwise>
    </xsl:choose>;
  </xsl:template>

  <xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="replace">
          <xsl:with-param name="text" select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>