/***** Object:  Table [dbo].[T_PLAN_ITEM_SMO] *****/
CREATE TABLE [dbo].[T_PLAN_ITEM_SMO]
(
[PlanItemSmoID] uniqueidentifier NOT NULL,
      [SmoStatus] int NOT NULL,
      [SmoPlanRequestStatus] uniqueidentifier NOT NULL,
      [PlanItem] uniqueidentifier NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[T_PLAN_ITEM_SMO] WITH NOCHECK ADD CONSTRAINT [PK_T_PLAN_ITEM_SMO]
PRIMARY KEY CLUSTERED ([PlanItemSmoID]) ON [PRIMARY]
GO

ALTER TABLE [dbo].[T_PLAN_ITEM_SMO] WITH NOCHECK ADD CONSTRAINT [FK_T_SMO_PLAN_REQUEST_STATUS_T_PLAN_ITEM_SMO_SMOPLANREQUESTSTATUS] FOREIGN KEY([SmoPlanRequestStatus])
REFERENCES [dbo].[T_SMO_PLAN_REQUEST_STATUS] ([SmoPlanRequestStatusID])
GO
ALTER TABLE [dbo].[T_PLAN_ITEM_SMO] WITH NOCHECK ADD CONSTRAINT [FK_T_PLAN_ITEM_T_PLAN_ITEM_SMO_PLANITEM] FOREIGN KEY([PlanItem])
REFERENCES [dbo].[T_PLAN_ITEM] ([PlanItemID])
GO



ALTER TABLE [dbo].[T_PLAN_NOTE] ADD [PlanItemSmo] uniqueidentifier
GO
ALTER TABLE [dbo].[T_PLAN_NOTE] WITH NOCHECK ADD CONSTRAINT [FK_T_PLAN_ITEM_SMO_T_PLAN_NOTE_PlanItemSmo] FOREIGN KEY ([PlanItemSmo]) REFERENCES [dbo].[T_PLAN_ITEM_SMO] ([PlanItemSmoID])
GO


IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_T_SMO_PLAN_REQUEST_STATUS_T_PLAN_VALUE_SMO_SmoPlanRequestStatus]') AND parent_object_id = OBJECT_ID(N'[dbo].[T_PLAN_VALUE_SMO]'))
ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] DROP CONSTRAINT [FK_T_SMO_PLAN_REQUEST_STATUS_T_PLAN_VALUE_SMO_SmoPlanRequestStatus]
GO
ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] DROP COLUMN [SmoPlanRequestStatus]
GO
ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] ADD [PlanItemSmo] uniqueidentifier
GO
ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] WITH NOCHECK ADD CONSTRAINT [FK_T_PLAN_ITEM_SMO_T_PLAN_VALUE_SMO_PlanItemSmo] FOREIGN KEY ([PlanItemSmo]) REFERENCES [dbo].[T_PLAN_ITEM_SMO] ([PlanItemSmoID])
GO



ALTER TABLE [dbo].[T_SMO_PLAN_REQUEST_STATUS] ADD [SentDateTime] datetime
GO



ALTER TABLE [dbo].[T_PLAN_NOTE] ADD [PlanItemSmo_UQ] AS (CASE WHEN [PlanItemSmo] IS NOT NULL THEN NULL ELSE [PlanNoteID] END)
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_PLAN_NOTE_PlanItemSmo] ON [dbo].[T_PLAN_NOTE] ([PlanItemSmo] ASC, [PlanItemSmo_UQ] ASC) ON [PRIMARY]
GO



--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=561)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (561,'PLAN_ITEM_SMO',NULL,'Primary SQL Server','Заявка плана СМО',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='PLAN_ITEM_SMO',EntDesc='Заявка плана СМО',BaseEntity=NULL WHERE EntityID=561
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="PLAN_ITEM_SMO" 
Index="561" 
Description="Заявка плана СМО" 
PrimaryKey="PlanItemSmoID">
  <Fields>
    <Attribute Name="PlanItemSmoID" Index="5610001" Type="guid" Description="Идентификатор" />
    <Parent Name="SmoPlanRequestStatus" Index="5610003" Type="SMO_PLAN_REQUEST_STATUS" Description="Статус заявки в СМО" Required="1" />
    <Parent Name="PlanItem" Index="5610006" Type="PLAN_ITEM" Description="Запись плана" Required="1" />
    <Child Name="PlanValueSmos" Index="5610004" Type="PLAN_VALUE_SMO" Description="Значение параметра" ChildAttribute="PlanItemSmo" RemoveAction="1" />
    <Choice Name="SmoStatus" Index="5610002" Description="Статус утверждения в СМО" Required="1">
      <Item Name="Редактирование" Index="0" />
      <Item Name="На утверждении" Index="1" />
      <Item Name="Утвержден" Index="2" />
      <Item Name="Не утвержден" Index="3" />
    </Choice>
    <Link Name="PlanNote" Index="5610005" Type="PLAN_NOTE" Description="Замечание на заявку" LinkAttribute="PlanItemSmo" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_PLAN_ITEM_SMO">
      <Map Name="PlanItemSmoID" Column="PlanItemSmoID" />
      <Map Name="SmoStatus" Column="SmoStatus" />
      <Map Name="SmoPlanRequestStatus" Column="SmoPlanRequestStatus" />
      <Map Name="PlanItem" Column="PlanItem" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=561
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=554)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (554,'PLAN_NOTE',NULL,'Primary SQL Server','Замечание на заявку',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='PLAN_NOTE',EntDesc='Замечание на заявку',BaseEntity=NULL WHERE EntityID=554
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="PLAN_NOTE" 
Index="554" 
Description="Замечание на заявку" 
PrimaryKey="PlanNoteID">
  <Fields>
    <Attribute Name="PlanNoteID" Index="5540001" Type="guid" Description="Идентификатор" />
    <Attribute Name="NoteText" Index="5540002" Type="text" Description="Текст замечания" Required="1" />
    <Parent Name="Users" Index="5540003" Type="USERS" Description="Пользователь системы" />
    <Parent Name="PlanRequest" Index="5540004" Type="PLAN_REQUEST" Description="Заявка" />
    <Parent Name="PlanItem" Index="5540005" Type="PLAN_ITEM" Description="Запись заявки" />
    <Parent Name="PlanValue" Index="5540006" Type="PLAN_VALUE" Description="Значение параметра" />
    <Parent Name="PlanItemSmo" Index="5540007" Type="PLAN_ITEM_SMO" Description="Заявка плана СМО" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_PLAN_NOTE">
      <Map Name="PlanNoteID" Column="PlanNoteID" />
      <Map Name="NoteText" Column="NoteText" />
      <Map Name="Users" Column="Users" />
      <Map Name="PlanRequest" Column="PlanRequest" />
      <Map Name="PlanItem" Column="PlanItem" />
      <Map Name="PlanValue" Column="PlanValue" />
      <Map Name="PlanItemSmo" Column="PlanItemSmo" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=554
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=555)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (555,'PLAN_VALUE_SMO',NULL,'Primary SQL Server','Значение параметра',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='PLAN_VALUE_SMO',EntDesc='Значение параметра',BaseEntity=NULL WHERE EntityID=555
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="PLAN_VALUE_SMO" 
Index="555" 
Description="Значение параметра" 
PrimaryKey="PlanValueSmoID">
  <Fields>
    <Attribute Name="PlanValueSmoID" Index="5550001" Type="guid" Description="Идентификатор" />
    <Attribute Name="Value" Index="5550004" Type="int" Description="Значение" />
    <Parent Name="Smo" Index="5550006" Type="SMO" Description="Страховая медицинская организация" CharacterMaximumLength="5" Required="1" />
    <Parent Name="PlanValue" Index="5550007" Type="PLAN_VALUE" Description="Значение параметра" Required="1" />
    <Parent Name="PlanItemSmo" Index="5550008" Type="PLAN_ITEM_SMO" Description="Заявка плана СМО" />
    <Link Name="PlanItemMonthValuesSmo" Index="5550005" Type="PLAN_ITEM_MONTH_VALUES_SMO" Description="Разбивка по месяцам по СМО" LinkAttribute="PlanValueSmo" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_PLAN_VALUE_SMO">
      <Map Name="PlanValueSmoID" Column="PlanValueSmoID" />
      <Map Name="Value" Column="Value" />
      <Map Name="Smo" Column="Smo" />
      <Map Name="PlanValue" Column="PlanValue" />
      <Map Name="PlanItemSmo" Column="PlanItemSmo" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=555
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=501)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (501,'SMO_PLAN_REQUEST_STATUS',NULL,'Primary SQL Server','Статус заявки в СМО',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='SMO_PLAN_REQUEST_STATUS',EntDesc='Статус заявки в СМО',BaseEntity=NULL WHERE EntityID=501
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="SMO_PLAN_REQUEST_STATUS" 
Index="501" 
Description="Статус заявки в СМО" 
PrimaryKey="SmoPlanRequestStatusID">
  <Fields>
    <Attribute Name="SmoPlanRequestStatusID" Index="5010001" Type="guid" Description="Идентификатор" />
    <Attribute Name="SendDateTime" Index="5010006" Type="datetime" Description="Дата и время последней отправки заявки на рассмотрение" />
    <Parent Name="PlanRequest" Index="5010002" Type="PLAN_REQUEST" Description="Заявка на утверждение плановых показателей" Required="1" />
    <Parent Name="Smo" Index="5010005" Type="SMO" Description="Страховая медицинская организация" CharacterMaximumLength="5" Required="1" />
    <Child Name="PlanItemSmos" Index="5010007" Type="PLAN_ITEM_SMO" Description="Заявка плана СМО" ChildAttribute="SmoPlanRequestStatus" />
    <Choice Name="Status" Index="5010003" Description="Статус заявки" Required="1">
      <Item Name="Редактирование" Index="0" />
      <Item Name="На утверждении" Index="1" />
      <Item Name="Утвержден частично" Index="3" />
      <Item Name="Не утвержден" Index="4" />
      <Item Name="Утвержден" Index="5" />
    </Choice>
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_SMO_PLAN_REQUEST_STATUS">
      <Map Name="SmoPlanRequestStatusID" Column="SmoPlanRequestStatusID" />
      <Map Name="PlanRequest" Column="PlanRequest" />
      <Map Name="Status" Column="Status" />
      <Map Name="Smo" Column="Smo" />
      <Map Name="SendDateTime" Column="SentDateTime" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=501
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT