/****** Object:  View [dbo].[V_ENTITY_ATTRIBUTES]    Script Date: 24.12.2018 14:43:15 ******/
DROP VIEW [dbo].[V_ENTITY_ATTRIBUTES]
GO

/****** Object:  View [dbo].[V_ENTITY_ATTRIBUTES]    Script Date: 24.12.2018 14:43:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[V_ENTITY_ATTRIBUTES]
AS
SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900+t1.SectionLevel AS [Index],
    1 AS [AttributeType],
    t1.SectionAlias AS [Name],
    t1.Name AS  [Description],
    (case when t3.EntName is not null then t3.[Schema]+'.'+t3.EntName else null end) AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CASE WHEN t2.EntName= 'PLAN_ITEM' THEN CAST(1 as bit) ELSE CAST(0 as bit) END AS   [IsRequired],
    1 AS [MappingType],
    t1.SectionAlias AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (900+(select max(SectionLevel)+1 from [FIES].T_PLAN_SECTION)-t1.SectionLevel) * -1 AS [AttributeId]

    FROM [FIES].T_PLAN_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName in ('PLAN_ITEM'
                                                , 'PLAN_ITEM_LIMITS'
                                                , 'SECTION_DEPEND_SETTINGS'
                                                , 'SMO_COEF_MONTH_VALUES'
                                                , 'FACT_ITEM'
                                                , 'MEETING_RESOLVE'
                                                , 'SPLIT_SETTINGS'
                                                , 'FACT_ITEM_SUM'
                                                , 'EXTERNAL_PLAN_SUM'
                                                , 'COEFFICENT_STATINFO'
                                                , 'BASE_TARIFF'
                                                , 'MO_PARAMETER_ITEM'
                                                , 'TP_FIN_CLOSE_ITEM'
                                                , 'TP_TYPE'
                                                , 'PLAN_FACT_STATISTICS')
    LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
    
    UNION ALL
    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias AS [Name],
    t1.Name AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(1 as bit) AS   [IsRequired],
    1 AS [MappingType],
    t1.PeriodAlias AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (900+(select max([Priority])+1 from [FIES].T_PERIOD_SECTION)-t1.[Priority]) * -1 AS [AttributeId]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'MONTH_VALUES'
    where t1.PeriodSection is null

    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+920+t1.ParametrOrder AS [Index],
    CASE WHEN t1.DependType = 0 THEN 1 ELSE 0 END AS [AttributeType],
    t1.[ParameterAlias] AS [Name],
    t1.Name AS  [Description],
    CASE WHEN t1.DependType = 0 THEN 'FIES.PLAN_VALUE' ELSE 'float' END AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL as varchar(3)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CASE WHEN t1.DependType = 0 THEN 
    CAST('<lx:ChildCount xmlns:lx="lexem">
    <condition>
        <lx:Criterion type="=">
            <lx:Path OwnerQueryLevel="0">
                <lx:StepInt index="5440002" />
                <lx:StepInt index="5420001" />
            </lx:Path>
            <lx:ConstOperand type="Guid">'+ cast(t1.PayParametrID as varchar(40))+'</lx:ConstOperand>
        </lx:Criterion>
    </condition>
    <lx:Path OwnerQueryLevel="0">
        <lx:StepInt index="5410003" />
    </lx:Path>
    <lx:First>
        <lx:Path OwnerQueryLevel="0">
        <lx:StepCurrent />
    </lx:Path>
    </lx:First>
</lx:ChildCount>'  AS varchar(max)) ELSE t1.ParameterLexem END AS   [AttrLexem],

    CASE WHEN t1.DependType = 0 THEN '{5410003}' ELSE CAST(NULL  AS varchar(255)) END AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (920+(select max(ParametrOrder)+1 from [FIES].[T_PLAN_PARAMETER])-t1.ParametrOrder) * -1 AS [AttributeId]
    FROM [FIES].[T_PLAN_PARAMETER] t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM'
    

    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+1000+(t3.DisplayOrder*10+t1.ParametrOrder) AS [Index],
    1 AS [AttributeType],
    t3.TypeAlias +  t1.GroupAlias AS [Name],
    t1.Name + ' (' + t3.Name + ')' AS  [Description],
    'FIES.LIMIT_VALUE' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL as varchar(3)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST('<lx:QueryValue entity="FIES.LIMIT_VALUE" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470002" />
                    <lx:StepInt index="5460001" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5460001" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470006" />
                    <lx:StepInt index="5520001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ cast(t1.ParameterGroupID as varchar(36)) +'</lx:ConstOperand>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5470005" />
                    <lx:StepInt index="5510001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ cast(t3.PlanLimitTypeID as varchar(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepInt index="5470001" />
        </lx:Path>
    </value>
</lx:QueryValue>'  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL AS VARCHAR(MAX)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (1000+(select max(DisplayOrder*10+ParametrOrder)+1 from [FIES].T_PLAN_LIMIT_TYPE, [FIES].T_PARAMETER_GROUP)-(t3.DisplayOrder*10+t1.ParametrOrder)) * -1 AS [AttributeId]
    from [FIES].T_PARAMETER_GROUP t1, [VCLib].T_ENTITIES t2, [FIES].T_PLAN_LIMIT_TYPE t3
    where t2.EntName= 'PLAN_ITEM_LIMITS'

    UNION ALL

    -- ССЫЛКА НА ДЕЙСТВУЮЩУЮ ЗАПИСЬ ЗАЯВКИ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+888 AS [Index],
    1 AS [AttributeType],
    'ActualPlanItem' AS [Name],
    'Действующая запись плана' AS  [Description],
    t2.[Schema]+'.'+t2.EntName AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.PLAN_ITEM" top="1"  xmlns:lx="lexem">
  <condition>
    <lx:Criteria type="And">
      <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Plan" />
                    <lx:StepString name="PlanPeriod" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="Plan" />
                    <lx:StepString name="PlanPeriod" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Mo" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="Mo" />
                </lx:Path>
            </lx:Criterion>' +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
              </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
            JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM'
            LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
            WHERE t1.SectionDependType = 0
    ) + 
    (
            SELECT dbo.Joint('<lx:Criteria type="Or">
                  <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                      <lx:StepString name="'+t1.SectionAlias+'" />
                    </lx:Path>
                    <lx:Path OwnerQueryLevel="1">
                      <lx:StepString name="'+t1.SectionAlias+'" />
                    </lx:Path>
                  </lx:Criterion>
                  <lx:Criteria type="And">
                    <lx:Criterion type="=">
                      <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="'+t1.SectionAlias+'" />
                      </lx:Path>
                      <lx:ConstOperand isNull="1" />
                    </lx:Criterion>
                    <lx:Criterion type="=">
                      <lx:Path OwnerQueryLevel="1">
                        <lx:StepString name="'+t1.SectionAlias+'" />
                      </lx:Path>
                      <lx:ConstOperand isNull="1" />
                    </lx:Criterion>
                  </lx:Criteria>
                </lx:Criteria>
            ', '')
            FROM [FIES].T_PLAN_SECTION t1
            JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM'
            LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
            WHERE t1.SectionDependType = 1
    )
    + '<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Approved" />
                </lx:Path>
                <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
            </lx:Criterion>
            <lx:Criterion type="&lt;">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Plan" />
                    <lx:StepAs as="FIES.PLAN_REQUEST" />
                    <lx:StepString name="RequestTime" />
                </lx:Path>
                    <lx:Argument>
                        <lx:ConstOperand type="Int32">0</lx:ConstOperand>
                    </lx:Argument>
            </lx:Criterion>
    </lx:Criteria>
  </condition>
  <orderBy>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepString name="Plan" />
      <lx:StepAs as="FIES.PLAN_REQUEST" />
      <lx:StepString name="RequestTime" />
    </lx:Path>
    <desc>True</desc>
    <lx:Path OwnerQueryLevel="0">
        <lx:StepString name="PlanItemID" />
    </lx:Path>
    <desc>False</desc>
  </orderBy>
  <value>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepString name="PlanItemID" />
    </lx:Path>
  </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    '<?xml version="1.0" encoding="utf-16"?><Parameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Parameter Name="OnDate" Description="На дату" Type="datetime"><lx:ParamHolder xmlns:lx="lexem"><lx:StepString name="CURRENT_DATETIME" /></lx:ParamHolder></Parameter></Parameters>' as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    888 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where EntName= 'PLAN_ITEM'

    UNION ALL

    -- ССЫЛКА НА ПРЕДЫДУЩУЮ ЗАПИСЬ ЗАЯВКИ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+889 AS [Index],
    1 AS [AttributeType],
    'PrevPlanItem' AS [Name],
    'Предыдущая запись плана' AS  [Description],
    t2.[Schema]+'.'+t2.EntName AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.PLAN_ITEM" top="1"  xmlns:lx="lexem">
  <condition>
    <lx:Criteria type="And">
      <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Plan" />
                    <lx:StepString name="PlanPeriod" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="Plan" />
                    <lx:StepString name="PlanPeriod" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Mo" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="Mo" />
                </lx:Path>
            </lx:Criterion>' +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                  <lx:StepInt index="'+CAST(t2.EntityID*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
              </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
            JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM'
            LEFT JOIN [VCLib].T_ENTITIES t3 on t3.EntityID = t1.Entities
    )
    + '<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="Approved" />
                </lx:Path>
                <lx:ConstOperand type="Boolean">True</lx:ConstOperand>
            </lx:Criterion>
      <lx:Criterion type="&lt;">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5410002" />
                    <lx:StepAs as="FIES.PLAN_REQUEST" />
                    <lx:StepInt index="5400006" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5410002" />
                    <lx:StepAs as="FIES.PLAN_REQUEST" />
                    <lx:StepInt index="5400006" />
                </lx:Path>
        </lx:Criterion>
    </lx:Criteria>
  </condition>
  <orderBy>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410002" />
      <lx:StepAs as="FIES.PLAN_REQUEST" />
      <lx:StepInt index="5400006" />
    </lx:Path>
    <desc>True</desc>
    <lx:Path OwnerQueryLevel="0">
        <lx:StepInt index="5410001" />
    </lx:Path>
    <desc>False</desc>
  </orderBy>
  <value>
    <lx:Path OwnerQueryLevel="0">
      <lx:StepInt index="5410001" />
    </lx:Path>
  </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    889 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where EntName= 'PLAN_ITEM'

    -- ССЫЛКА НА ПОДПРОГРАММУ
    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+900 AS [Index],
    1 AS [AttributeType],
    'Subprogramm' AS [Name],
    'Подпрограмма' AS  [Description],
    'FIES.SUBPROGRAMM' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CASE WHEN (SELECT COUNT(*) FROM [FIES].T_SUBPROGRAMM)= 0 THEN '<lx:ConstOperand isNull="1" xmlns:lx="lexem" />'
         ELSE '<lx:QueryValue entity="FIES.SUBPROGRAMM" top="1"  xmlns:lx="lexem">
        <condition>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5720001" />
                </lx:Path>
                <lx:Case>' +
                 (
                    CAST((SELECT  CAST('<When xmlns:lx="lexem">
                      <lx:Criteria type="And">
                          <lx:Criteria type="And">
                            <lx:Criterion type="&lt;=">
                              <lx:ConstOperand type="DateTime">' + convert(varchar(10), t1.DateBegin, 101) + '</lx:ConstOperand>
                              <lx:ParamHolder>
                                <lx:StepString name="CURRENT_DATE" />
                              </lx:ParamHolder>
                            </lx:Criterion>' + 
                            case
                                when t1.DateEnd is null then ''
                                else '<lx:Criterion type="&gt;=">
                                  <lx:ConstOperand type="DateTime">' + convert(varchar(10), t1.DateEnd, 101) + '</lx:ConstOperand>
                                  <lx:ParamHolder>
                                    <lx:StepString name="CURRENT_DATE" />
                                  </lx:ParamHolder>
                                </lx:Criterion>'
                            end + '</lx:Criteria>
                        '+ REPLACE([dbo].[RegExReplace](cast(t1.Condition as varchar(max)), '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>'), '<lx:Path OwnerQueryLevel="0">', '<lx:Path OwnerQueryLevel="1">')
                         +'
                      </lx:Criteria>
                    </When>
                    <Then xmlns:lx="lexem">
                        <lx:ConstOperand type="Guid">' + CAST(t1.SubprogrammID as varchar(36)) + '</lx:ConstOperand>
                    </Then>' AS xml)
                    FROM [FIES].T_SUBPROGRAMM t1
                    ORDER BY t1.[Priority]
                    FOR XML PATH(''), TYPE    ) AS VARCHAR(MAX))    
                    
                 )
                 + '            <Else>
                        <lx:ConstOperand isNull="1" />
                    </Else>
                </lx:Case>
            </lx:Criterion>
        </condition>
        <value>
            <lx:Path OwnerQueryLevel="0">
                <lx:StepInt index="5720001" />
            </lx:Path>
        </value>
    </lx:QueryValue>'
         END AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    900 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where EntName= 'PLAN_VALUE'
    --[dbo].[RegExReplace](@test, '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
   

   -- ССЫЛКА НА ПОДПРОГРАММУ ID
    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+901 AS [Index],
    0 AS [AttributeType],
    'SubprogrammID' AS [Name],
    'Подпрограмма(ID)' AS  [Description],
    'guid' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CASE WHEN (SELECT COUNT(*) FROM [FIES].T_SUBPROGRAMM)= 0 THEN '<lx:ConstOperand isNull="1" xmlns:lx="lexem" />'
         ELSE '<lx:Case  xmlns:lx="lexem">' +
                 (
                    CAST((SELECT  CAST('<When xmlns:lx="lexem">
                      <lx:Criteria type="And">
                          <lx:Criteria type="And">
                            <lx:Criterion type="&lt;=">
                              <lx:ConstOperand type="DateTime">' + convert(varchar(10), t1.DateBegin, 101) + '</lx:ConstOperand>
                              <lx:ParamHolder>
                                <lx:StepString name="CURRENT_DATE" />
                              </lx:ParamHolder>
                            </lx:Criterion>' + 
                            case
                                when t1.DateEnd is null then ''
                                else '<lx:Criterion type="&gt;=">
                                  <lx:ConstOperand type="DateTime">' + convert(varchar(10), t1.DateEnd, 101) + '</lx:ConstOperand>
                                  <lx:ParamHolder>
                                    <lx:StepString name="CURRENT_DATE" />
                                  </lx:ParamHolder>
                                </lx:Criterion>'
                            end + '</lx:Criteria>
                        '+ [dbo].[RegExReplace](cast(t1.Condition as varchar(max)), '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
                         +'
                      </lx:Criteria>
                    </When>
                    <Then xmlns:lx="lexem">
                        <lx:ConstOperand type="Guid">' + CAST(t1.SubprogrammID as varchar(36)) + '</lx:ConstOperand>
                    </Then>' AS xml)
                    FROM [FIES].T_SUBPROGRAMM t1
                    ORDER BY t1.[Priority]
                    FOR XML PATH(''), TYPE    ) AS VARCHAR(MAX))    
                    
                 )
                 + '            <Else>
                        <lx:ConstOperand isNull="1" />
                    </Else>
                </lx:Case>'
         END AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    901 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where EntName= 'PLAN_VALUE'
    --[dbo].[RegExReplace](@test, '<lx:BaseObjectOperand Entity="[\w\.]+" PrimaryKey="(?<guid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"\s*\/>', '<lx:ConstOperand type="Guid">${guid}</lx:ConstOperand>')
   

   UNION ALL

-- Фактические показатели по месяцам

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+950+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT' AS [Name],
    t1.Name + ' (Факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>'
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

    
    +'        <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360005" />
                    <lx:StepInt index="5600001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360006" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (950+(select max([Priority])+1 from [FIES].T_PERIOD_SECTION)-t1.[Priority]) * -1 AS [AttributeId]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM_MONTH_VALUES'
    where t1.PeriodSection is null



    UNION ALL

-- Фактические показатели по месяцу

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+950 AS [Index],
    0 AS [AttributeType],
    'ValueFact' AS [Name],
    'Значение (Факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="MonthValues" />
                    <lx:StepAs as="FIES.PLAN_ITEM_MONTH_VALUES" />
                    <lx:StepString name="PlanValue" />
                    <lx:StepString name="PlanItem" />
                    <lx:StepString name="Plan" />
                    <lx:StepString name="PlanPeriod" />
                    <lx:StepString name="Year" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="MonthValues" />
                    <lx:StepAs as="FIES.PLAN_ITEM_MONTH_VALUES" />
                    <lx:StepString name="PlanValue" />
                    <lx:StepString name="PlanItem" />
                    <lx:StepString name="Plan" />
                    <lx:StepString name="Mo" />
                </lx:Path>
            </lx:Criterion>'
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="MonthValues" />
                    <lx:StepAs as="FIES.PLAN_ITEM_MONTH_VALUES" />
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

    
    +'        <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="MonthValues" />
                    <lx:StepAs as="FIES.PLAN_ITEM_MONTH_VALUES" />
                    <lx:StepString name="PlanValue" />
                    <lx:StepString name="PlanParameter" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360005" />
                    <lx:StepInt index="5600001" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="PeriodSection" />
                    <lx:StepString name="PeriodSectionID" />
                </lx:Path>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360006" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    950 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where t2.EntName= 'MONTH_VALUE'


UNION ALL
-- Фактические показатели по месяцам ПО СМО

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+950+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT' AS [Name],
    t1.Name + ' (Факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360002" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550006" />
                </lx:Path>
            </lx:Criterion>
            '
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )
    
    +'
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360901" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410901" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360902" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410902" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360903" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410903" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360904" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410904" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360005" />
                    <lx:StepInt index="5600001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360006" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (950+(select max([Priority])+1 from [FIES].T_PERIOD_SECTION)-t1.[Priority]) * -1 AS [AttributeId]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM_MONTH_VALUES_SMO'
    where t1.PeriodSection is null

    UNION ALL

-- Сумма фактических показателей по году

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+890 AS [Index],
    0 AS [AttributeType],
    'ValueFact' AS [Name],
    'Значение (факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    '{0:0.######}' AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>
            '
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

    
    +'
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360006" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    890 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntName= 'PLAN_VALUE'


 UNION ALL

-- Фактические показатели по месяцам СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+965+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT_SUM' AS [Name],
    t1.Name + ' (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>'
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

    
    +'        <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5450001" />
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360005" />
                    <lx:StepInt index="5600001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360008" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (965+(select max([Priority])+1 from [FIES].T_PERIOD_SECTION)-t1.[Priority]) * -1 AS [AttributeId]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM_MONTH_VALUES'
    where t1.PeriodSection is null



UNION ALL
-- Фактические показатели по месяцам ПО СМО СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+965+t1.[Priority] AS [Index],
    0 AS [AttributeType],
    t1.PeriodAlias + '_FACT_SUM' AS [Name],
    t1.Name + ' (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360002" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550006" />
                </lx:Path>
            </lx:Criterion>
            '
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )
    
    +'
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360901" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410901" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360902" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410902" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360903" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410903" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360904" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410904" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5560001" />
                    <lx:StepInt index="5550007" />
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360005" />
                    <lx:StepInt index="5600001" />
                </lx:Path>
                <lx:ConstOperand type="Guid">'+ CAST(t1.PeriodSectionID AS VARCHAR(36)) +'</lx:ConstOperand>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360008" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (965+(select max([Priority])+1 from [FIES].T_PERIOD_SECTION)-t1.[Priority]) * -1 AS [AttributeId]
    FROM [FIES].T_PERIOD_SECTION t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName= 'PLAN_ITEM_MONTH_VALUES_SMO'
    where t1.PeriodSection is null

    UNION ALL

-- Сумма фактических показателей по году СУММЫ

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+891 AS [Index],
    0 AS [AttributeType],
    'ValueFactSum' AS [Name],
    'Значение (Сумма факт)' AS  [Description],
    'float' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(20  AS int) AS [Precision],
    CAST(6  AS int) AS [Scale],
    '{0:0.######}' AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.FACT_ITEM" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360007" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390006" />
                    <lx:StepInt index="5500002" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360003" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="5410002" />
                    <lx:StepInt index="5390004" />
                </lx:Path>
            </lx:Criterion>
            '
            +
    (
            SELECT dbo.Joint('<lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="'+CAST(536*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440003" />
                    <lx:StepInt index="'+CAST(541*10000+900+t1.SectionLevel AS VARCHAR(20))+'" />
                </lx:Path>
            </lx:Criterion>', '')
            FROM [FIES].T_PLAN_SECTION t1
    )

    
    +'
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360004" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepInt index="5440002" />
                </lx:Path>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <value>
        <lx:IsNull>
            <lx:Sum>
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepInt index="5360008" />
                </lx:Path>
            </lx:Sum>
            <lx:ConstOperand type="Decimal">0,0</lx:ConstOperand>
        </lx:IsNull>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    891 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntName= 'PLAN_VALUE'




    -- COEFFICIENT Информация, о том в каких срезах собирается статистика...
    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*10000+901 AS [Index],
    4 AS [AttributeType],
    'StatGroupLevels' AS [Name],
    'Срезы статистики' AS  [Description],
    'int' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(1 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    1 AS [MappingType],
    'StatGroupLevels' AS   [MappingName],
    CAST(1 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST('<?xml version="1.0" encoding="utf-16"?><ChoiceItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        '+
        CAST((select Name as '@Name' , POWER(2, SectionLevel-1) as '@Index'
from FIES.T_PLAN_SECTION
order by SectionLevel 
for xml path('ChoiceItem'))AS VARCHAR(MAX))
        +'
    </ChoiceItems>'
      AS varchar(max)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    'T_COEFFICIENT' as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    901 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2 
    where EntName= 'COEFFICIENT'
    
    UNION ALL

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    t2.EntityID*1000+900+t1.Position AS [Index],
    0 AS [AttributeType],
    t1.Alias AS [Name],
    t1.Name AS  [Description],
    'int' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    1 AS [MappingType],
    t1.Alias AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    CAST(NULL  AS varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],    
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    (900+(select max(Position)+1 from [FIES].T_AGE_GROUP)-t1.Position) * -1 AS [AttributeId]

    FROM [FIES].T_AGE_GROUP t1
    JOIN [VCLib].T_ENTITIES t2 on t2.EntName in ('MO_PEOPLE')



    
      UNION ALL

-- Получение тарифа из Финансового расчета

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    null as [Index],
    1 AS [AttributeType],
    'TpFinTarif' AS [Name],
    'Тарифы' AS  [Description],
    'FIES.TP_FIN_CLOSED_MONTH_TARIFS' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(NULL  AS int) AS [Precision],
    CAST(NULL  AS int) AS [Scale],
    CAST(NULL  AS varchar(255)) AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    cast('<lx:QueryValue entity="FIES.TP_FIN_CLOSED_MONTH_TARIFS" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="TpFinCloseItem" />
                    <lx:StepString name="Mo" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="PlanItem" />
                    <lx:StepString name="Plan" />
                    <lx:StepString name="Mo" />
                </lx:Path>
            </lx:Criterion>
                ' as varchar(max))
            +
            (
                        SELECT cast('<!-- Обман сортировки :) '+cast(t1.[Index] as varchar(max))+' --><lx:Criteria type="Or" xmlns:lx="lexem">
                            <lx:Criterion type="=">
                                <lx:Path OwnerQueryLevel="0">
                                    <lx:StepString name="TpFinCloseItem" />
                                    <lx:StepString name="'+t1.SectionAlias+'" />
                                </lx:Path>
                                <lx:ConstOperand isNull="1" />
                            </lx:Criterion>
                            <lx:Criterion type="=">
                                <lx:Path OwnerQueryLevel="0">
                                    <lx:StepString name="TpFinCloseItem" />
                                    <lx:StepString name="'+t1.SectionAlias+'" />
                                </lx:Path>
                                <lx:Path OwnerQueryLevel="1">
                                    <lx:StepString name="PlanItem" />
                                    <lx:StepString name="'+t1.SectionAlias+'" />
                                </lx:Path>
                            </lx:Criterion>                            
                        </lx:Criteria>' as xml)
                       --select *         
                       FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY SectionLevel) as [Index] FROM [FIES].T_PLAN_SECTION t100) t1
                       for xml path('')
            )
            +
            cast('
                <!--<lx:Criteria type="Or">
                    <lx:Criterion type="=">
                        <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="TpFinCloseItem" />
                            <lx:StepString name="Subprogramm" />
                        </lx:Path>
                        <lx:ConstOperand isNull="1" />
                    </lx:Criterion>
                    <lx:Criterion type="=">
                        <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="TpFinCloseItem" />
                            <lx:StepString name="Subprogramm" />
                        </lx:Path>
                        <lx:Path OwnerQueryLevel="1">
                            <lx:StepString name="Subprogramm" />
                        </lx:Path>
                    </lx:Criterion>
                </lx:Criteria>-->
                <lx:Criteria type="Or">
                <lx:Criteria type="And">
                    <lx:Criterion type="=">
                        <lx:Argument>
                            <lx:ConstOperand type="Int32">0</lx:ConstOperand>
                        </lx:Argument>
                        <lx:ConstOperand isNull="1" />
                    </lx:Criterion>
                    <lx:Criterion type="=">
                        <lx:Path OwnerQueryLevel="0">
                            <lx:StepString name="TpFinCloseItem" />
                            <lx:StepString name="TpFinClose" />
                            <lx:StepString name="TpFin" />
                            <lx:StepString name="PlanPeriod" />
                        </lx:Path>
                        <lx:Path OwnerQueryLevel="1">
                            <lx:StepString name="PlanItem" />
                            <lx:StepString name="Plan" />
                            <lx:StepString name="PlanPeriod" />
                        </lx:Path>
                    </lx:Criterion>
                </lx:Criteria>
                <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="TpFinCloseItem" />
                        <lx:StepString name="TpFinClose" />
                        <lx:StepString name="TpFin" />
                    </lx:Path>
                    <lx:Argument>
                        <lx:ConstOperand type="Int32">0</lx:ConstOperand>
                    </lx:Argument>
                </lx:Criterion>
            </lx:Criteria>
            <lx:Criterion type="&lt;=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="TpFinCloseItem" />
                    <lx:StepString name="TpFinClose" />
                    <lx:StepString name="TpFin" />
                    <lx:StepString name="TpDate" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="PlanItem" />
                    <lx:StepString name="Plan" />
                    <lx:StepString name="CreateDate" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criteria type="Or">
                <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="TpFinClosedValue" />
                        <lx:StepString name="TpFinItemTarif" />
                        <lx:StepString name="AgeGroup" />
                    </lx:Path>
                    <lx:Path OwnerQueryLevel="1">
                        <lx:StepString name="PlanParameter" />
                        <lx:StepString name="AgeGroup" />
                    </lx:Path>
                </lx:Criterion>
                <lx:Criterion type="=">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="TpFinClosedValue" />
                    </lx:Path>
                    <lx:ConstOperand isNull="1" />
                </lx:Criterion>
            </lx:Criteria>
        </lx:Criteria>
    </condition>
    <orderBy>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="TpFinCloseItem" />
            <lx:StepString name="TpFinClose" />
            <lx:StepString name="TpFin" />
            <lx:StepString name="TpDate" />
        </lx:Path>
        <desc>True</desc>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="TpFinCloseItem" />
            <lx:StepString name="TpFinClose" />
            <lx:StepString name="TpFin" />
            <lx:StepString name="CreateDate" />
        </lx:Path>
        <desc>True</desc>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="MonthValuesID" />
        </lx:Path>
        <desc>True</desc>
    </orderBy>
    <value>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="MonthValuesID" />
        </lx:Path>
    </value>
</lx:QueryValue>
    ' as varchar(max)) AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    '<?xml version="1.0" encoding="utf-16"?><Parameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Parameter Name="OnTpFin" Description="Финансовый расчет" Type="guid"><lx:BaseObjectOperand Entity="FIES.TP_FIN" isNull="1" xmlns:lx="lexem" /></Parameter></Parameters>' as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    892 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntName= 'PLAN_VALUE'

      UNION ALL

-- Получение группы из последнего закрытого финансового плана

    SELECT 
    NEWID() AS [EntityAttributesID] ,
    t2.EntityID AS [EntityID],
    null,
    1 AS [AttributeType],
    'ClosedTpFinGroup' AS [Name],
    'Предыдущая закрытая группа' AS  [Description],
    'FIES.TP_FIN_GROUP' AS   [SystemType],
    CAST(NULL  AS int) AS [MaxLength],
    CAST(null  AS int) AS [Precision],
    CAST(null  AS int) AS [Scale],
    null AS   [DefaultFormat],
    CAST(0 as bit) AS  [IsCalculated],
    CAST(0 as bit) AS   [IsFlag] ,
    CAST(0 as bit) AS   [IsRequired],
    4 AS [MappingType],
    CAST(NULL  AS varchar(255)) AS   [MappingName],
    CAST(0 as bit) AS   [Save],
    CAST(NULL  AS varchar(255)) AS   [LinkAttributeName],
    CAST(NULL  AS varchar(1024)) AS   [ChoiceAttributeData],
    CAST(1 as bit) AS   [IsStored],
    '<lx:QueryValue entity="FIES.TP_FIN_GROUP" top="1" xmlns:lx="lexem">
    <condition>
        <lx:Criteria type="And">
            <lx:Criterion not="1" type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="TpFin" />
                    <lx:StepString name="TpFinClose" />
                </lx:Path>
                <lx:BaseObjectOperand Entity="FIES.TP_FIN_CLOSE" isNull="1" />
            </lx:Criterion>
            <lx:Criterion type="=">
                <lx:Path OwnerQueryLevel="0">
                    <lx:StepString name="TpFinTypeLink" />
                    <lx:StepString name="TpFinType" />
                </lx:Path>
                <lx:Path OwnerQueryLevel="1">
                    <lx:StepString name="TpFinTypeLink" />
                    <lx:StepString name="TpFinType" />
                </lx:Path>
            </lx:Criterion>
            <lx:Criterion type="&lt;">
                <lx:DatePart datepart="' + case when (select COUNT(*) from FIES.T_PERIOD_SECTION) = 4 then 'Quarter' else 'Month' end + '">
                    <lx:Path OwnerQueryLevel="0">
                        <lx:StepString name="TpFin" />
                        <lx:StepString name="TpDate" />
                    </lx:Path>
                </lx:DatePart>
                <lx:DatePart datepart="' + case when (select COUNT(*) from FIES.T_PERIOD_SECTION) = 4 then 'Quarter' else 'Month' end + '">
                    <lx:Path OwnerQueryLevel="1">
                        <lx:StepString name="TpFin" />
                        <lx:StepString name="TpDate" />
                    </lx:Path>
                </lx:DatePart>
            </lx:Criterion>
        </lx:Criteria>
    </condition>
    <orderBy>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="TpFin" />
            <lx:StepString name="TpDate" />
        </lx:Path>
        <desc>True</desc>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="TpFin" />
            <lx:StepString name="CreateDate" />
        </lx:Path>
        <desc>True</desc>
    </orderBy>
    <value>
        <lx:Path OwnerQueryLevel="0">
            <lx:StepString name="TpFinGroupID" />
        </lx:Path>
    </value>
</lx:QueryValue>' AS   [AttrLexem],
    CAST(NULL  AS varchar(max)) AS   [PathsAttributes],
    CAST(0  AS bit) AS   [IsCashed],
    CAST(NULL  AS varchar(255)) as [MappingTable],
    CAST(NULL  AS VARCHAR(255)) AS [MappingRefTable],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableSearchMode],
    CAST(NULL  AS VARCHAR(255)) AS [RefTableHashFunc],
    CAST(NULL  AS VARCHAR(8000)) as [ParametersData],
    CAST(NULL  AS VARCHAR(8000)) as [CustomsData],
    CAST(NULL  AS VARCHAR(8000)) as [RequiredAttributes],
    CAST(NULL  AS VARCHAR(8000)) as [SortingAttribute],
    CAST(NULL  AS VARCHAR(8000)) as [EnumType],
    CAST(0  AS bit) AS [IsIdentity],
    CAST(0  AS bit) AS [IsCustom],
    CAST(1  AS bit) AS [RemoveAction],
    CAST(0  AS bit) AS [IsPrimaryKey],
    893 * -1 AS [AttributeId]
    FROM [VCLib].T_ENTITIES t2
    where t2.EntName= 'TP_FIN_GROUP'



GO


