<?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">
      -- Страх.организация
      DECLARE
      @orgID uniqueidentifier,
      @type int

      BEGIN
      SET @orgID = '<xsl:value-of select="@OrganizationID"/>'
      SELECT @type = EntityID FROM VCLib.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
      GO
    </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" >
    if not exists(select * from [AKUZ].[T_V009_RESULT] r where r.[IDRMP] = <xsl:value-of select="IDRMP" />)
    INSERT INTO [AKUZ].[T_V009_RESULT] ([IDRMP], [RMPNAME], [DL_USLOV], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDRMP" />
    , '<xsl:value-of select="RMPNAME" />'
    , <xsl:value-of select="DL_USLOV" />
    , 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 r
    set [RMPNAME] = '<xsl:value-of select="RMPNAME" />'
    , [DL_USLOV]= <xsl:value-of select="DL_USLOV" />
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [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_V009_RESULT] r
    where r.[IDRMP] = <xsl:value-of select="IDRMP" />
    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>
  <!-- V020 "<zap>" template -->
  <xsl:template name="KoPrZap" >
    if not exists(select * from [AKUZ].[T_V020_STRUCTURE_BED] r where r.[IDK_PR] = <xsl:value-of select="IDK_PR" />)
    INSERT INTO [AKUZ].[T_V020_STRUCTURE_BED] ([IDK_PR], [K_PRNAME], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDK_PR" />
    , '<xsl:value-of select="K_PRNAME" />'
    , 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 r
    set [K_PRNAME] = '<xsl:value-of select="K_PRNAME" />'
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [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_V020_STRUCTURE_BED] r
    where r.[IDK_PR] = <xsl:value-of select="IDK_PR" />
    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" >
    if not exists(select * from [AKUZ].[T_V021_SPECIALITY] s where s.[Id] = <xsl:value-of select="IDSPEC" /> and s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_V021_SPECIALITY] ([Id], [Name], [DateBegin], [DateEnd], [V021SpecialityID], [POSTNAME], [IDPOST_MZ])
    SELECT <xsl:value-of select="IDSPEC" />
    , '<xsl:value-of select="SPECNAME" />'
    , 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>
    ,NEWID()
    ,<xsl:if test="POSTNAME != ''">
      '<xsl:value-of select="POSTNAME" />'
    </xsl:if>
    <xsl:if test="POSTNAME = ''">NULL</xsl:if>
    ,<xsl:if test="IDPOST_MZ != ''">
      <xsl:value-of select="IDPOST_MZ" />
    </xsl:if>
    <xsl:if test="IDPOST_MZ = ''">NULL</xsl:if>
    else
    update s
    set s.[Name] = '<xsl:value-of select="SPECNAME" />'
    , s.[DateBegin] = 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>
    ,s.[POSTNAME] = <xsl:if test="POSTNAME != ''">
      '<xsl:value-of select="POSTNAME" />'
    </xsl:if>
    <xsl:if test="POSTNAME = ''">NULL</xsl:if>
    ,s.[IDPOST_MZ] = <xsl:if test="IDPOST_MZ != ''">
      <xsl:value-of select="IDPOST_MZ" />
    </xsl:if>
    <xsl:if test="IDPOST_MZ = ''">NULL</xsl:if>
    from [AKUZ].[T_V021_SPECIALITY] s
    where s.[Id] = <xsl:value-of select="IDSPEC" /> and s.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    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" >
    if not exists(select * from [AKUZ].[T_V016_DISP_TYPE] s where s.[IDDT] = LEFT('<xsl:value-of select="IDDT" />', 3))
    INSERT INTO [AKUZ].[T_V016_DISP_TYPE] ([IDDT], [DTNAME], [DATEBEG], [DATEEND], [DTRULE])
    SELECT LEFT('<xsl:value-of select="IDDT" />', 3)
    , LEFT('<xsl:value-of select="DTNAME" />', 256)
    , 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>
    , '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>;</xsl:text>
    </xsl:for-each>'
    else
    update s
    set s.[DTNAME] = LEFT('<xsl:value-of select="DTNAME" />', 256)
    , 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>
    , s.[DTRULE] = '<xsl:for-each select="DTRULE/RULE">
      <xsl:value-of select="." />
      <xsl:text>;</xsl:text>
    </xsl:for-each>'
    from [AKUZ].[T_V016_DISP_TYPE] s
    where s.[IDDT] = LEFT('<xsl:value-of select="IDDT" />', 3)
    and s.[DATEBEG] &lt; CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    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" >
    if not exists(select * from [AKUZ].[T_V017_DISP_RESULT] s where s.[IDDR] = <xsl:value-of select="IDDR" />)
    INSERT INTO [AKUZ].[T_V017_DISP_RESULT] ([IDDR], [DRNAME], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDDR" />
    , LEFT('<xsl:value-of select="DRNAME" />', 256)
    , 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.[DRNAME] = LEFT('<xsl:value-of select="DRNAME" />', 256)
    , 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_V017_DISP_RESULT] s
    where s.[IDDR] = <xsl:value-of select="IDDR" />
    GO
  </xsl:template>

  <!-- VMP_SERVICE: V019.xml elements -->
  <xsl:template match="packet[(zglv[type='HMet'])]">
    <!-- Create MedicalServiceType -->
    if not exists(select * from [AKUZ].[T_MEDICAL_SERVICE_TYPE] ms where ms.[MedicalServiceTypeID] = '642809B3-0C46-4673-8E97-76A6D60C14F8')
    INSERT INTO [AKUZ].[T_MEDICAL_SERVICE_TYPE] ([MedicalServiceTypeID], [Name])
    SELECT '642809B3-0C46-4673-8E97-76A6D60C14F8', 'ВМП(V019)'
    GO
    
    <xsl:for-each select="zap">

      <!-- clear MedicalServiceMkbLinks -->
      delete msml
      from [AKUZ].[T_MEDICAL_SERVICE_MKB_LINK] msml
        join [AKUZ].[T_MEDICAL_SERVICE] ms on ms.MedicalServiceID = msml.MedicalService
      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:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
      and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
      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" >
    if not exists(select * 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:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
    and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
	  INSERT INTO [AKUZ].[T_MEDICAL_SERVICE] ([Code], [Name], [NameFull], [Profile], [MedicalServiceID], [type_], [MedicalServiceType], [ShortName], [PatientModelCode], [EconomicCode], [InternalCode], [PayType], [DateBegin], [DateEnd], [IsCommonService])
	  SELECT '<xsl:value-of select="IDHM" />'
      , LEFT('<xsl:value-of select="HMNAME" />', 256)
      , LEFT('<xsl:value-of select="HMNAME" />', 256)
      , 3
      , NEWID()
      , 1113
      , '642809B3-0C46-4673-8E97-76A6D60C14F8'
      , LEFT('<xsl:value-of select="HMNAME" />', 24)
      , '<xsl:value-of select="IDMODP" />'
      , '<xsl:value-of select="HVID" />'
      , '<xsl:value-of select="HGR" />'
      , 1
      , 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>
	  , 0
    else
      update 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:value-of select="IDMODP" />'
      , [InternalCode] = '<xsl:value-of select="HGR" />'
      , [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_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:value-of select="IDMODP" />' or ms.[PatientModelCode] is null)
        and ms.[DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>
  
  <!-- V019 "DIAG" template -->
  <xsl:template name="HMetDiagnosisElements" >
    INSERT INTO AKUZ.T_MEDICAL_SERVICE_MKB_LINK (MedicalServiceMkbLinkID, MedicalService, MkbCode)
    SELECT
    NEWID() as MedicalServiceMkbLinkID
    , ms.MedicalServiceID
    , MkbCode.Split as MkbCode
    from [AKUZ].[T_MEDICAL_SERVICE] ms
    cross apply dbo.fn_Split(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] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
      and not exists 
      (
        select top 1 MedicalServiceMkbLinkID
        from AKUZ.T_MEDICAL_SERVICE_MKB_LINK msl
        where ms.MedicalServiceID = msl.MedicalService 
          and MkbCode.Split = msl.MkbCode
      )
    GO
  </xsl:template>
  
  <!-- V002_PROFILE: V002.xml -->
  <xsl:template match="packet[(zglv[@type='ProfOt'])]">
    <xsl:for-each select="zap">
      <!-- insert or update MedicalService -->
      <xsl:call-template name="ProfOtZap" />
    </xsl:for-each>
  </xsl:template>
  <!-- V002 "<zap>" template -->
  <xsl:template name="ProfOtZap" >
    if not exists(select * from [AKUZ].[T_V002_PROFILE] p where p.[Id] = '<xsl:value-of select="@IDPR" />')
    INSERT INTO [AKUZ].[T_V002_PROFILE] ([Id], [Name], [DateBegin], [DateEnd])
    SELECT '<xsl:value-of select="@IDPR" />'
    , LEFT('<xsl:value-of select="@PRNAME" />', 256)
    , 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 p
    set [Name] = LEFT('<xsl:value-of select="@PRNAME" />', 256)
    , [DateBegin] = CONVERT(datetime, '<xsl:value-of select="@DATEBEG" />', 104)
    , [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_V002_PROFILE] p
    where p.[Id] = '<xsl:value-of select="@IDPR" />'
    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" >
    if not exists(select * from [AKUZ].[T_V011_DRUG] p where p.[ID] = <xsl:value-of select="ID" />)
    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" />
    , LEFT('<xsl:value-of select="S_NUM_REG" />', 50)
    , 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>
    , LEFT('<xsl:value-of select="S_ORG_NAME" />', 255)
    , LEFT('<xsl:value-of select="S_ORG_COUNTRY" />', 255)
    , LEFT('<xsl:value-of select="S_TORG_NAME" />', 255)
    , LEFT('<xsl:value-of select="S_MNN" />', 255)
    , LEFT('<xsl:value-of select="S_FORMS" />', 255)
    , LEFT('<xsl:value-of select="S_PROD_PHASE" />', 255)
    , LEFT('<xsl:value-of select="S_DOCS" />', 255)
    , LEFT('<xsl:value-of select="S_FARM_GROUP" />', 255)
    , LEFT('<xsl:value-of select="S_CODES" />', 255)
    else
    update p
    set [S_NUM_REG] = LEFT('<xsl:value-of select="S_NUM_REG" />', 50)
    , [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , [S_ORG_NAME] = LEFT('<xsl:value-of select="S_ORG_NAME" />', 255)
    , [S_ORG_COUNTRY] = LEFT('<xsl:value-of select="S_ORG_COUNTRY" />', 255)
    , [S_TORG_NAME] = LEFT('<xsl:value-of select="S_TORG_NAME" />', 255)
    , [S_MNN] = LEFT('<xsl:value-of select="S_MNN" />', 255)
    , [S_FORMS] = LEFT('<xsl:value-of select="S_FORMS" />', 255)
    , [S_PROD_PHASE] = LEFT('<xsl:value-of select="S_PROD_PHASE" />', 255)
    , [S_DOCS] = LEFT('<xsl:value-of select="S_DOCS" />', 255)
    , [S_FARM_GROUP] = LEFT('<xsl:value-of select="S_FARM_GROUP" />', 255)
    , [S_CODES] = LEFT('<xsl:value-of select="S_CODES" />', 255)
    from [AKUZ].[T_V011_DRUG] p
    where p.[ID] = <xsl:value-of select="ID" />
    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" >
    if not exists(
      select * from [AKUZ].[T_V024] p 
      where p.[IDDKK] = LEFT('<xsl:value-of select="IDDKK" />', 255)
        and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
    INSERT INTO [AKUZ].[T_V024] ([V024DoprKrID], [IDDKK], [DKKNAME], [DATEBEG], [DATEEND])    
    SELECT NEWID()
    , LEFT('<xsl:value-of select="IDDKK" />', 255)
    , LEFT('<xsl:value-of select="DKKNAME" />', 500)
    , 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 p
    set <xsl:if test="DKKNAME != ''">
      p.[DKKNAME] = LEFT('<xsl:value-of select="DKKNAME" />', 500),
    </xsl:if>
    p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104),
    p.[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_V024] p
    where p.[IDDKK] = LEFT('<xsl:value-of select="IDDKK" />', 255) and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
    GO
  </xsl:template>

  <!-- V028_RefType: V028.xml -->
  <xsl:template match="packet[(zglv[type='RefType'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="RefType" />
    </xsl:for-each>
  </xsl:template>
  <!-- V028 "<zap>" template -->
  <xsl:template name="RefType" >
    if not exists(select * from [AKUZ].[T_V028_REFERRAL_TYPE] p where p.[IDVN] = <xsl:value-of select="IDVN" />)
    INSERT INTO [AKUZ].[T_V028_REFERRAL_TYPE] ([IDVN], [N_VN], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDVN" />
    , LEFT('<xsl:value-of select="N_VN" />', 255)
    , 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 p
    set <xsl:if test="N_VN != ''">
      [N_VN] = LEFT('<xsl:value-of select="N_VN" />', 255) ,
    </xsl:if>
    <!--[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104), -->
    [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_V028_REFERRAL_TYPE] p
    where p.[IDVN] = <xsl:value-of select="IDVN" /> and [DATEBEG] &lt;= CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V029_ResMet: V029.xml -->
  <xsl:template match="packet[(zglv[type='ResMet'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="ResMet" />
    </xsl:for-each>
  </xsl:template>
  <!-- V029 "<zap>" template -->
  <xsl:template name="ResMet" >
    if not exists(select * from [AKUZ].[T_V029_RESEARCH_METHOD] p where p.[IDMET] = <xsl:value-of select="IDMET" />)
    INSERT INTO [AKUZ].[T_V029_RESEARCH_METHOD] ([IDMET], [N_MET], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDMET" />
    , LEFT('<xsl:value-of select="N_MET" />', 255)
    , 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 p
    set <xsl:if test="N_MET != ''">
      [N_MET] = LEFT('<xsl:value-of select="N_MET" />', 255) ,
    </xsl:if>
    <!--[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104), -->
    [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_V029_RESEARCH_METHOD] p
    where p.[IDMET] = <xsl:value-of select="IDMET" /> and [DATEBEG] &lt;= CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- V031_GroupDrug: V031.xml -->
  <xsl:template match="packet[(zglv[type='GroupDrug'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="GroupDrug" />
    </xsl:for-each>
  </xsl:template>
  <!-- V031 "<zap>" template -->
  <xsl:template name="GroupDrug" >
    if not exists(
    select * from [AKUZ].[T_V031_GROUP_DRUG] p
    where p.[DrugGroupCode] = <xsl:value-of select="DrugGroupCode" />
    and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
    INSERT INTO [AKUZ].[T_V031_GROUP_DRUG] ([V031GroupDrugID], [DrugGroupCode], [ManIndMNN], [DrugGroup], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="DrugGroupCode" />
    <xsl:if test="ManIndMNN != ''">
      , <xsl:value-of select="ManIndMNN" />
    </xsl:if><xsl:if test="ManIndMNN = ''">, NULL</xsl:if>
    , LEFT('<xsl:value-of select="DrugGroup" />', 255)
    , 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 p    
    set 
    <xsl:if test="ManIndMNN != ''">
      p.[ManIndMNN] = <xsl:value-of select="ManIndMNN" />,
    </xsl:if>
    <xsl:if test="DrugGroup != ''">
      p.[DrugGroup] = LEFT('<xsl:value-of select="DrugGroup" />', 255),
    </xsl:if>
    p.[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_V031_GROUP_DRUG] p
    where p.[DrugGroupCode] = <xsl:value-of select="DrugGroupCode" /> and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
    GO
  </xsl:template>

  <!-- V032_DrugGrReg: V032.xml -->
  <xsl:template match="packet[(zglv[type='DrugGrReg'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="DrugGrReg" />
    </xsl:for-each>
  </xsl:template>
  <!-- V032 "<zap>" template -->
  <xsl:template name="DrugGrReg" >
    if not exists(
    select * from [AKUZ].[T_V032_DRUG_GR_REG] p
    where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
    and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
    INSERT INTO [AKUZ].[T_V032_DRUG_GR_REG] ([V032DrugGrRegID], [ScheDrugGrCd], [Name], [SchemCode], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
    , LEFT('<xsl:value-of select="Name" />', 255)
    , LEFT('<xsl:value-of select="SchemCode" />', 255)
    , 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 p
    set <xsl:if test="Name != ''">
      p.[Name] = LEFT('<xsl:value-of select="Name" />', 255),
    </xsl:if>
    <xsl:if test="SchemCode != ''">
      p.[SchemCode] = LEFT('<xsl:value-of select="SchemCode" />', 255),
    </xsl:if>
    p.[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_V032_DRUG_GR_REG] p
    where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255) and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
    GO
  </xsl:template>

	<!-- V033_SchemCodDrug: V033.xml -->
	<xsl:template match="packet[(zglv[type='SchemCodDrug'])]">
		<xsl:for-each select="zap">
			<xsl:call-template name="SchemCodDrug" />
		</xsl:for-each>
	</xsl:template>
	<!-- V033 "<zap>" template -->
	<xsl:template name="SchemCodDrug" >
		if not exists(
		select * from [AKUZ].[T_V033_SCHEM_COD_DRUG] p
		where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		and p.[DrugCode] = LEFT('<xsl:value-of select="DrugCode" />', 255)
		and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG])
		INSERT INTO [AKUZ].[T_V033_SCHEM_COD_DRUG] ([V033SchemCodDrugID], [ScheDrugGrCd], [DrugCode], [DATEBEG], [DATEEND])
		SELECT NEWID()
		, LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		, LEFT('<xsl:value-of select="DrugCode" />', 255)
		, 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 p
		set p.[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_V033_SCHEM_COD_DRUG] p
		where p.[ScheDrugGrCd] = LEFT('<xsl:value-of select="ScheDrugGrCd" />', 255)
		and p.[DrugCode] = LEFT('<xsl:value-of select="DrugCode" />', 255)
		and CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104) = p.[DATEBEG]
		GO
	</xsl:template>

  <!-- V022_ModPac: V022.xml -->
  <xsl:template match="packet[(zglv[type='ModPac'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="ModPac" />
    </xsl:for-each>
  </xsl:template>
  <!-- V022 "<zap>" template -->
  <xsl:template name="ModPac" >
    if not exists(select * from [AKUZ].[T_V022_PATIENT_MODEL] p where p.[IDMPAC] = <xsl:value-of select="IDMPAC" />)
    INSERT INTO [AKUZ].[T_V022_PATIENT_MODEL] ([IDMPAC], [MPACNAME], [DATEBEG], [DATEEND])
    SELECT <xsl:value-of select="IDMPAC" />
    , LEFT('<xsl:value-of select="MPACNAME" />', 2000)
    , 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 p
    set <xsl:if test="MPACNAME != ''">
      p.[MPACNAME] = LEFT('<xsl:value-of select="MPACNAME" />', 255) ,
    </xsl:if>
    [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_V022_PATIENT_MODEL] p
    where p.[IDMPAC] = <xsl:value-of select="IDMPAC" /> and [DATEBEG] &lt;= CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
  </xsl:template>

  <!-- N001_OnkPrOt: N001.xml -->
  <xsl:template match="packet[(zglv[type='OnkPrOt'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkPrOt" />
    </xsl:for-each>
  </xsl:template>
  <!-- N001 "<zap>" template -->
  <xsl:template name="OnkPrOt" >
    if not exists(select * from [AKUZ].[T_N001_OnkPrOt] p where p.[ID_PrOt] = <xsl:value-of select="ID_PrOt" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N001_OnkPrOt] ([N001ID], [ID_PrOt], [PrOt_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_PrOt" />
    , LEFT('<xsl:value-of select="PrOt_NAME" />', 255)
    , 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 p
    set <xsl:if test="PrOt_NAME != ''">
      [PrOt_NAME] = LEFT('<xsl:value-of select="PrOt_NAME" />', 255) ,
    </xsl:if>
    [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_N001_OnkPrOt] p
    where p.[ID_PrOt] = <xsl:value-of select="ID_PrOt" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N001_OnkPrOt t
    join AKUZ.T_N001_OnkPrOt tt on t.ID_PrOt = tt.ID_PrOt and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N002_OnkStad: N002.xml -->
  <xsl:template match="packet[(zglv[type='OnkStad'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkStad" />
    </xsl:for-each>
  </xsl:template>
  <!-- N002 "<zap>" template -->
  <xsl:template name="OnkStad" >
    if not exists(select * from [AKUZ].[T_N002_OnkStad] p where p.[ID_St] = <xsl:value-of select="ID_St" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N002_OnkStad] ([N002ID], [ID_St], [DS_St], [KOD_St], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_St" />
    , LEFT('<xsl:value-of select="DS_St" />', 5)
    , LEFT('<xsl:value-of select="KOD_St" />', 5)
    , 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 p
    set [DS_St] = LEFT('<xsl:value-of select="DS_St" />', 5)
    , [KOD_St] = LEFT('<xsl:value-of select="KOD_St" />', 5)
    , [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_N002_OnkStad] p
    where p.[ID_St] = <xsl:value-of select="ID_St" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N002_OnkStad t
    join AKUZ.T_N002_OnkStad tt on t.ID_St = tt.ID_St and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N003_OnkT: N003.xml -->
  <xsl:template match="packet[(zglv[type='OnkT'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkT" />
    </xsl:for-each>
  </xsl:template>
  <!-- N003 "<zap>" template -->
  <xsl:template name="OnkT" >
    if not exists(select * from [AKUZ].[T_N003_OnkT] p where p.[ID_T] = <xsl:value-of select="ID_T" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N003_OnkT] ([N003ID], [ID_T], [DS_T], [KOD_T], [T_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_T" />
    , LEFT('<xsl:value-of select="DS_T" />', 5)
    , LEFT('<xsl:value-of select="KOD_T" />', 255)
    , LEFT('<xsl:value-of select="T_NAME" />', 255)
    , 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 p
    set [DS_T] = LEFT('<xsl:value-of select="DS_T" />', 5)
    , [KOD_T] = LEFT('<xsl:value-of select="KOD_T" />', 255)
    <xsl:if test="T_NAME != ''">
      , [T_NAME] = LEFT('<xsl:value-of select="T_NAME" />', 255)
    </xsl:if>
    , [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_N003_OnkT] p
    where p.[ID_T] = <xsl:value-of select="ID_T" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N003_OnkT t
    join AKUZ.T_N003_OnkT tt on t.ID_T = tt.ID_T and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N004_OnkN: N004.xml -->
  <xsl:template match="packet[(zglv[type='OnkN'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkN" />
    </xsl:for-each>
  </xsl:template>
  <!-- N004 "<zap>" template -->
  <xsl:template name="OnkN" >
    if not exists(select * from [AKUZ].[T_N004_OnkN] p where p.[ID_N] = <xsl:value-of select="ID_N" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N004_OnkN] ([N004ID], [ID_N], [DS_N], [KOD_N], [N_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_N" />
    , LEFT('<xsl:value-of select="DS_N" />', 5)
    , LEFT('<xsl:value-of select="KOD_N" />', 5)
    , LEFT('<xsl:value-of select="N_NAME" />', 255)
    , 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 p
    set [DS_N] = LEFT('<xsl:value-of select="DS_N" />', 5)
    , [KOD_N] = LEFT('<xsl:value-of select="KOD_N" />', 5)
    <xsl:if test="N_NAME != ''">
      , [N_NAME] = LEFT('<xsl:value-of select="N_NAME" />', 255)
    </xsl:if>
    , [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_N004_OnkN] p
    where p.[ID_N] = <xsl:value-of select="ID_N" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N004_OnkN t
    join AKUZ.T_N004_OnkN tt on t.ID_N = tt.ID_N and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N005_OnkM: N005.xml -->
  <xsl:template match="packet[(zglv[type='OnkM'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkM" />
    </xsl:for-each>
  </xsl:template>
  <!-- N005 "<zap>" template -->
  <xsl:template name="OnkM" >
    if not exists(select * from [AKUZ].[T_N005_OnkM] p where p.[ID_M] = <xsl:value-of select="ID_M" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N005_OnkM] ([N005ID], [ID_M], [DS_M], [KOD_M], [M_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_M" />
    , LEFT('<xsl:value-of select="DS_M" />', 5)
    , LEFT('<xsl:value-of select="KOD_M" />', 5)
    , LEFT('<xsl:value-of select="M_NAME" />', 255)
    , 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 p
    set [DS_M] = LEFT('<xsl:value-of select="DS_M" />', 5)
    , [KOD_M] = LEFT('<xsl:value-of select="KOD_M" />', 5)
    <xsl:if test="M_NAME != ''">
      , [M_NAME] = LEFT('<xsl:value-of select="M_NAME" />', 255)
    </xsl:if>
    , [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_N005_OnkM] p
    where p.[ID_M] = <xsl:value-of select="ID_M" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N005_OnkM t
    join AKUZ.T_N005_OnkM tt on t.ID_M = tt.ID_M and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N006_OnkTNM: N006.xml -->
  <xsl:template match="packet[(zglv[type='OnkTNM'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkTNM" />
    </xsl:for-each>
  </xsl:template>
  <!-- N006 "<zap>" template -->
  <xsl:template name="OnkTNM" >
    if not exists(select * from [AKUZ].[T_N006_OnkTNM] p where p.[ID_gr] = <xsl:value-of select="ID_gr" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N006_OnkTNM] ([N006ID], [ID_gr], [DS_gr], [ID_St], [ID_T], [ID_N], [ID_M], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_gr" />
    , LEFT('<xsl:value-of select="DS_gr" />', 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" />
    , 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 p
    set [DS_gr] = LEFT('<xsl:value-of select="DS_gr" />', 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:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    from [AKUZ].[T_N006_OnkTNM] p
    where p.[ID_gr] = <xsl:value-of select="ID_gr" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N006_OnkTNM t
    join AKUZ.T_N006_OnkTNM tt on t.ID_gr = tt.ID_gr and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N007_OnkMrf: N007.xml -->
  <xsl:template match="packet[(zglv[type='OnkMrf'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrf" />
    </xsl:for-each>
  </xsl:template>
  <!-- N007 "<zap>" template -->
  <xsl:template name="OnkMrf" >
    if not exists(select * from [AKUZ].[T_N007_OnkMrf] p where p.[ID_Mrf] = <xsl:value-of select="ID_Mrf" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N007_OnkMrf] ([N007ID], [ID_Mrf], [Mrf_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_Mrf" />
    , LEFT('<xsl:value-of select="Mrf_NAME" />', 255)
    , 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 p
    set <xsl:if test="Mrf_NAME != ''">
      [Mrf_NAME] = LEFT('<xsl:value-of select="Mrf_NAME" />', 255) ,
    </xsl:if>
    [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_N007_OnkMrf] p
    where p.[ID_Mrf] = <xsl:value-of select="ID_Mrf" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N007_OnkMrf t
    join AKUZ.T_N007_OnkMrf tt on t.ID_Mrf = tt.ID_Mrf and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N008_OnkMrfRt: N008.xml -->
  <xsl:template match="packet[(zglv[type='OnkMrfRt'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrfRt" />
    </xsl:for-each>
  </xsl:template>
  <!-- N008 "<zap>" template -->
  <xsl:template name="OnkMrfRt" >
    if not exists(select * from [AKUZ].[T_N008_OnkMrfRt] p where p.[ID_R_M] = <xsl:value-of select="ID_R_M" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N008_OnkMrfRt] ([N008ID], [ID_R_M], [ID_Mrf], [R_M_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_R_M" />
    , <xsl:value-of select="ID_Mrf" />
    , LEFT('<xsl:value-of select="R_M_NAME" />', 255)
    , 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 p
    set [ID_Mrf] = <xsl:value-of select="ID_Mrf" />
    <xsl:if test="R_M_NAME != ''">
      , [R_M_NAME] = LEFT('<xsl:value-of select="R_M_NAME" />', 255)
    </xsl:if>
    , [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_N008_OnkMrfRt] p
    where p.[ID_R_M] = <xsl:value-of select="ID_R_M" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N008_OnkMrfRt t
    join AKUZ.T_N008_OnkMrfRt tt on t.ID_R_M = tt.ID_R_M and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N009_OnkMrtDS: N009.xml -->
  <xsl:template match="packet[(zglv[type='OnkMrtDS'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkMrtDS" />
    </xsl:for-each>
  </xsl:template>
  <!-- N009 "<zap>" template -->
  <xsl:template name="OnkMrtDS" >
    if not exists(select * from [AKUZ].[T_N009_OnkMrtDS] p where p.[ID_M_D] = <xsl:value-of select="ID_M_D" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N009_OnkMrtDS] ([N009ID], [ID_M_D], [DS_Mrf], [ID_Mrf], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_M_D" />
    , LEFT('<xsl:value-of select="DS_Mrf" />', 5)
    , <xsl:value-of select="ID_Mrf" />
    , 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 p
    set [DS_Mrf] = LEFT('<xsl:value-of select="DS_Mrf" />', 5)
    , [ID_Mrf] = <xsl:value-of select="ID_Mrf" />
    , [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_N009_OnkMrtDS] p
    where p.[ID_M_D] = <xsl:value-of select="ID_M_D" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N009_OnkMrtDS t
    join AKUZ.T_N009_OnkMrtDS tt on t.ID_M_D = tt.ID_M_D and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N010_OnkIgh: N010.xml -->
  <xsl:template match="packet[(zglv[type='OnkIgh'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIgh" />
    </xsl:for-each>
  </xsl:template>
  <!-- N010 "<zap>" template -->
  <xsl:template name="OnkIgh" >
    if not exists(select * from [AKUZ].[T_N010_OnkIgh] p where p.[ID_Igh] = <xsl:value-of select="ID_Igh" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N010_OnkIgh] ([N010ID], [ID_Igh], [KOD_Igh], [Igh_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="KOD_Igh" />', 255)
    , LEFT('<xsl:value-of select="Igh_NAME" />', 255)
    , 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 p
    set [KOD_Igh] = LEFT('<xsl:value-of select="KOD_Igh" />', 255)
    <xsl:if test="Igh_NAME != ''">
      , [Igh_NAME] = LEFT('<xsl:value-of select="Igh_NAME" />', 255)
    </xsl:if>
    , [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_N010_OnkIgh] p
    where p.[ID_Igh] = <xsl:value-of select="ID_Igh" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N010_OnkIgh t
    join AKUZ.T_N010_OnkIgh tt on t.ID_Igh = tt.ID_Igh and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N011_OnkIghRt: N011.xml -->
  <xsl:template match="packet[(zglv[type='OnkIghRt'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghRt" />
    </xsl:for-each>
  </xsl:template>
  <!-- N011 "<zap>" template -->
  <xsl:template name="OnkIghRt" >
    if not exists(select * from [AKUZ].[T_N011_OnkIghRt] p where p.[ID_R_I] = <xsl:value-of select="ID_R_I" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N011_OnkIghRt] ([N011ID], [ID_R_I], [ID_Igh], [KOD_R_I], [R_I_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_R_I" />
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="KOD_R_I" />', 255)
    , LEFT('<xsl:value-of select="R_I_NAME" />', 255)
    , 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 p
    set [ID_Igh] = <xsl:value-of select="ID_Igh" />
    , [KOD_R_I] = LEFT('<xsl:value-of select="KOD_R_I" />', 255)
    <xsl:if test="R_I_NAME != ''">
      , [R_I_NAME] = LEFT('<xsl:value-of select="R_I_NAME" />', 255)
    </xsl:if>
    , [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_N011_OnkIghRt] p
    where p.[ID_R_I] = <xsl:value-of select="ID_R_I" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N011_OnkIghRt t
    join AKUZ.T_N011_OnkIghRt tt on t.ID_R_I = tt.ID_R_I and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N012_OnkIghDS: N012.xml -->
  <xsl:template match="packet[(zglv[type='OnkIghDS'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkIghDS" />
    </xsl:for-each>
  </xsl:template>
  <!-- N012 "<zap>" template -->
  <xsl:template name="OnkIghDS" >
    if not exists(select * from [AKUZ].[T_N012_OnkIghDS] p where p.[ID_I_D] = <xsl:value-of select="ID_I_D" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N012_OnkIghDS] ([N012ID], [ID_I_D], [ID_Igh], [DS_Igh], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_I_D" />
    , <xsl:value-of select="ID_Igh" />
    , LEFT('<xsl:value-of select="DS_Igh" />', 255)
    , 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 p
    set [ID_Igh] = <xsl:value-of select="ID_Igh" />
    , [DS_Igh] = LEFT('<xsl:value-of select="DS_Igh" />', 255)
    , [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_N012_OnkIghDS] p
    where p.[ID_I_D] = <xsl:value-of select="ID_I_D" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N012_OnkIghDS t
    join AKUZ.T_N012_OnkIghDS tt on t.ID_I_D = tt.ID_I_D and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N013_OnkLech: N013.xml -->
  <xsl:template match="packet[(zglv[type='OnkLech'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLech" />
    </xsl:for-each>
  </xsl:template>
  <!-- N013 "<zap>" template -->
  <xsl:template name="OnkLech" >
    if not exists(select * from [AKUZ].[T_N013_OnkLech] p where p.[ID_TLech] = <xsl:value-of select="ID_TLech" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N013_OnkLech] ([N013ID], [ID_TLech], [TLech_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLech" />
    , LEFT('<xsl:value-of select="TLech_NAME" />', 255)
    , 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 p
    set <xsl:if test="TLech_NAME != ''">
      [TLech_NAME] = LEFT('<xsl:value-of select="TLech_NAME" />', 255) ,
    </xsl:if>
    [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_N013_OnkLech] p
    where p.[ID_TLech] = <xsl:value-of select="ID_TLech" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N013_OnkLech t
    join AKUZ.T_N013_OnkLech tt on t.ID_TLech = tt.ID_TLech and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N014_OnkHir: N014.xml -->
  <xsl:template match="packet[(zglv[type='OnkHir'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkHir" />
    </xsl:for-each>
  </xsl:template>
  <!-- N014 "<zap>" template -->
  <xsl:template name="OnkHir" >
    if not exists(select * from [AKUZ].[T_N014_OnkHir] p where p.[ID_THir] = <xsl:value-of select="ID_THir" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N014_OnkHir] ([N014ID], [ID_THir], [THir_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_THir" />
    , LEFT('<xsl:value-of select="THir_NAME" />', 255)
    , 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 p
    set <xsl:if test="THir_NAME != ''">
      [THir_NAME] = LEFT('<xsl:value-of select="THir_NAME" />', 255) ,
    </xsl:if>
    [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_N014_OnkHir] p
    where p.[ID_THir] = <xsl:value-of select="ID_THir" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N014_OnkHir t
    join AKUZ.T_N014_OnkHir tt on t.ID_THir = tt.ID_THir and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </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_L" />
    </xsl:for-each>
  </xsl:template>
  <!-- N015 "<zap>" template -->
  <xsl:template name="OnkLek_L" >
    if not exists(select * from [AKUZ].[T_N015_OnkLek_L] p where p.[ID_TLek_L] = <xsl:value-of select="ID_TLek_L" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N015_OnkLek_L] ([N015ID], [ID_TLek_L], [TLek_NAME_L], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLek_L" />
    , LEFT('<xsl:value-of select="TLek_NAME_L" />', 255)
    , 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 p
    set <xsl:if test="TLek_NAME_L != ''">
      [TLek_NAME_L] = LEFT('<xsl:value-of select="TLek_NAME_L" />', 255) ,
    </xsl:if>
    [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_N015_OnkLek_L] p
    where p.[ID_TLek_L] = <xsl:value-of select="ID_TLek_L" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N015_OnkLek_L t
    join AKUZ.T_N015_OnkLek_L tt on t.ID_TLek_L = tt.ID_TLek_L and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </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_V" />
    </xsl:for-each>
  </xsl:template>
  <!-- N016 "<zap>" template -->
  <xsl:template name="OnkLek_V" >
    if not exists(select * from [AKUZ].[T_N016_OnkLek_V] p where p.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N016_OnkLek_V] ([N016ID], [ID_TLek_V], [TLek_NAME_V], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLek_V" />
    , LEFT('<xsl:value-of select="TLek_NAME_V" />', 255)
    , 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 p
    set <xsl:if test="TLek_NAME_V != ''">
      [TLek_NAME_V] = LEFT('<xsl:value-of select="TLek_NAME_V" />', 255) ,
    </xsl:if>
    [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_N016_OnkLek_V] p
    where p.[ID_TLek_V] = <xsl:value-of select="ID_TLek_V" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N016_OnkLek_V t
    join AKUZ.T_N016_OnkLek_V tt on t.ID_TLek_V = tt.ID_TLek_V and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N017_OnkLuch: N017.xml -->
  <xsl:template match="packet[(zglv[type='OnkLuch'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLuch" />
    </xsl:for-each>
  </xsl:template>
  <!-- N017 "<zap>" template -->
  <xsl:template name="OnkLuch" >
    if not exists(select * from [AKUZ].[T_N017_OnkLuch] p where p.[ID_TLuch] = <xsl:value-of select="ID_TLuch" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N017_OnkLuch] ([N017ID], [ID_TLuch], [TLuch_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_TLuch" />
    , LEFT('<xsl:value-of select="TLuch_NAME" />', 255)
    , 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 p
    set <xsl:if test="TLuch_NAME != ''">
      [TLuch_NAME] = LEFT('<xsl:value-of select="TLuch_NAME" />', 255) ,
    </xsl:if>
    [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_N017_OnkLuch] p
    where p.[ID_TLuch] = <xsl:value-of select="ID_TLuch" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N017_OnkLuch t
    join AKUZ.T_N017_OnkLuch tt on t.ID_TLuch = tt.ID_TLuch and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N018_OnkReas: N018.xml -->
  <xsl:template match="packet[(zglv[type='OnkReas'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkReas" />
    </xsl:for-each>
  </xsl:template>
  <!-- N018 "<zap>" template -->
  <xsl:template name="OnkReas" >
    if not exists(select * from [AKUZ].[T_N018_OnkReas] p where p.[ID_REAS] = <xsl:value-of select="ID_REAS" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N018_OnkReas] ([N018ID], [ID_REAS], [REAS_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_REAS" />
    , LEFT('<xsl:value-of select="REAS_NAME" />', 255)
    , 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 p
    set <xsl:if test="REAS_NAME != ''">
      [REAS_NAME] = LEFT('<xsl:value-of select="REAS_NAME" />', 255) ,
    </xsl:if>
    [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_N018_OnkReas] p
    where p.[ID_REAS] = <xsl:value-of select="ID_REAS" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N018_OnkReas t
    join AKUZ.T_N018_OnkReas tt on t.ID_REAS = tt.ID_REAS and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N019_OnkCons: N019.xml -->
  <xsl:template match="packet[(zglv[type='OnkCons'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkCons" />
    </xsl:for-each>
  </xsl:template>
  <!-- N019 "<zap>" template -->
  <xsl:template name="OnkCons" >
    if not exists(select * from [AKUZ].[T_N019_OnkCons] p where p.[ID_CONS] = <xsl:value-of select="ID_CONS" /> and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N019_OnkCons] ([N019ID], [ID_CONS], [CONS_NAME], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , <xsl:value-of select="ID_CONS" />
    , LEFT('<xsl:value-of select="CONS_NAME" />', 255)
    , 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 p
    set <xsl:if test="CONS_NAME != ''">
      [CONS_NAME] = LEFT('<xsl:value-of select="CONS_NAME" />', 255) ,
    </xsl:if>
    [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_N019_OnkCons] p
    where p.[ID_CONS] = <xsl:value-of select="ID_CONS" /> and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N019_OnkCons t
    join AKUZ.T_N019_OnkCons tt on t.ID_CONS = tt.ID_CONS and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N021_OnkLpsh: N021.xml -->
  <xsl:template match="packet[(zglv[type='OnkLpsh'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLpsh" />
    </xsl:for-each>
  </xsl:template>
  <!-- N021 "<zap>" template -->
  <xsl:template name="OnkLpsh" >
    if not exists(select * from [AKUZ].[T_N021_OnkLpsh] p where p.[ID_ZAP] = '<xsl:value-of select="ID_ZAP" />' and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N021_OnkLpsh] ([N021ID], [ID_ZAP], [CODE_SH], [ID_LEKP], [DATEBEG], [DATEEND], [ID_LEKP_EXT], [LEKP_EXT])
    SELECT NEWID()
    , LEFT('<xsl:value-of select="ID_ZAP" />', 15)
    , LEFT('<xsl:value-of select="CODE_SH" />', 10)
    , LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , 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>
    , LEFT('<xsl:value-of select="ID_LEKP_EXT" />', 25)
    , LEFT('<xsl:value-of select="LEKP_EXT" />', 256)
    else
    update p
    set [CODE_SH] = LEFT('<xsl:value-of select="CODE_SH" />', 10)
    , [ID_LEKP] = LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , [DATEEND] = <xsl:if test="DATEEND != ''">
      CONVERT(datetime, '<xsl:value-of select="DATEEND" />', 104)
    </xsl:if><xsl:if test="DATEEND = ''">NULL</xsl:if>
    , [ID_LEKP_EXT] = LEFT('<xsl:value-of select="ID_LEKP_EXT" />', 25)
    , [LEKP_EXT] = LEFT('<xsl:value-of select="LEKP_EXT" />', 256)
    from [AKUZ].[T_N021_OnkLpsh] p
    where p.[ID_ZAP] = LEFT('<xsl:value-of select="ID_ZAP" />', 15) and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N021_OnkLpsh t
    join AKUZ.T_N021_OnkLpsh tt on t.ID_ZAP = tt.ID_ZAP and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </xsl:template>

  <!-- N020_OnkLekp: N020.xml -->
  <xsl:template match="packet[(zglv[type='OnkLekp'])]">
    <xsl:for-each select="zap">
      <xsl:call-template name="OnkLekp" />
    </xsl:for-each>
  </xsl:template>
  <!-- N020 "<zap>" template -->
  <xsl:template name="OnkLekp" >
    if not exists(select * from [AKUZ].[T_N020_OnkLekp] p where p.[ID_LEKP] = '<xsl:value-of select="ID_LEKP" />' and p.[DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104))
    INSERT INTO [AKUZ].[T_N020_OnkLekp] ([N020ID], [ID_LEKP], [MNN], [DATEBEG], [DATEEND])
    SELECT NEWID()
    , LEFT('<xsl:value-of select="ID_LEKP" />', 6)
    , LEFT('<xsl:value-of select="MNN" />', 300)
    , 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 p
    set [MNN] = LEFT('<xsl:value-of select="MNN" />', 300)
    , [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_N020_OnkLekp] p
    where p.[ID_LEKP] = LEFT('<xsl:value-of select="ID_LEKP" />', 6) and [DATEBEG] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    GO
    update t
    set t.DATEEND = DATEADD(DAY, -1, tt.DATEBEG)
    from AKUZ.T_N020_OnkLekp t
    join AKUZ.T_N020_OnkLekp tt on t.ID_LEKP = tt.ID_LEKP and tt.DATEBEG > t.DATEBEG
    where t.DATEEND is null
  </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" >
    if not exists(select * from [AKUZ].[T_COUNCIL_GOAL] p
    where p.[Code] = <xsl:value-of select="ID_CONS" /> and p.[Type] = 1)
    INSERT INTO [AKUZ].[T_COUNCIL_GOAL] ([CouncilGoalID], [Code], [Name], [Type], [DateBegin], [DateEnd])
    SELECT
    NEWID()
    , <xsl:value-of select="ID_CONS" />
    , LEFT('<xsl:value-of select="CONS_NAME" />', 255)
    , 1
    , 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 p
    set <xsl:if test="CONS_NAME != ''">
      [Name] = LEFT('<xsl:value-of select="CONS_NAME" />', 255) ,
    </xsl:if>
    [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_COUNCIL_GOAL] p
    where p.[Code] = <xsl:value-of select="ID_CONS" /> and p.[Type] = 1 and [DateBegin] = CONVERT(datetime, '<xsl:value-of select="DATEBEG" />', 104)
    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
            NEWID()--VimisConsolidationSettingID, uniqueidentifier,
           ,(select top 1 e.EntityID from VCLib.T_ENTITIES e where e.[Schema] + '.' + e.EntName = '<xsl:value-of select="@entity" />')--Entities, int,
           ,(select top 1 v.id from AKUZ.S_VIMIS_CONSOLIDATION_GroupType v where v.value = '<xsl:value-of select="@group" />')--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 cs
set XsltTemplate = '<xsl:value-of select="$xsltTemplate" />'
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
    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>
