-- elimina tabella COPIA se desiderato

-- drop table AnagOperatori_Copia
-- go

-- crea tab AnagPeriodi ok

create table AnagPeriodi(
  codPeriodo   nvarchar(20) not null constraint PK_AnagPeriodi primary key,
  lemmaPeriodo nvarchar(50)
)
go


-- crea tab AnagStatiListe ok

create table AnagStatiListe(
  CodStatoLista int          not null constraint PK_AnagStatiListe primary key,
  DescStato     nvarchar(50)
)
go


-- modifica tab Celle utilizzando tab temporanee ok

set xact_abort on
go

begin transaction
go

alter table PosizioneUdcCorrente drop
  constraint FK_PosizioneUdcCorrente_Celle 
go

alter table Celle drop
  constraint DF_Celle_IdxBlocco 
go

exec sp_rename 'PK_Celle', 'tmp__PK_Celle', 'OBJECT'
go

exec sp_rename 'Celle', 'tmp__Celle_2', 'OBJECT'
go

create table Celle(
  IdxCella     int          not null identity constraint PK_Celle primary key,
  CodCS        nchar(2)     not null,
  CodCella     nvarchar(50) not null,
  IdxTipoCella int,
  Descrizione  nvarchar(50),
  Attiva       bit,
  Piena        bit          not null constraint DF_Celle_Piena default (0),
  IdxBlocco    int          not null constraint DF_Celle_IdxBlocco default (0),
  X            int          not null,
  Y            int          not null,
  Z            int          not null
)
go

exec sp_addextendedproperty 'MS_Description', N'indica se sia attiva, se non lo fosse è impedito caricamento nella stessa', 'SCHEMA', 'dbo', 'TABLE', 'Celle', 'COLUMN', 'Attiva'
go

exec sp_addextendedproperty 'MS_Description', 'posizione X (colonna)', 'SCHEMA', 'dbo', 'TABLE', 'Celle', 'COLUMN', 'X'
go

exec sp_addextendedproperty 'MS_Description', 'posizione Y (livello/ripiano)', 'SCHEMA', 'dbo', 'TABLE', 'Celle', 'COLUMN', 'Y'
go

exec sp_addextendedproperty 'MS_Description', 'posizione Z (profondita)', 'SCHEMA', 'dbo', 'TABLE', 'Celle', 'COLUMN', 'Z'
go

set identity_insert Celle on
go

insert into Celle(IdxCella,CodCS,CodCella,IdxTipoCella,Descrizione,Attiva,IdxBlocco,X,Y,Z) select IdxCella,CodCS,CodCella,IdxTipoCella,Descrizione,Attiva,IdxBlocco,X,Y,Z from tmp__Celle_2
go

set identity_insert Celle off
go

drop table tmp__Celle_2
go

alter table PosizioneUdcCorrente add
  constraint FK_PosizioneUdcCorrente_Celle foreign key(IdxCella) references Celle(IdxCella) on update cascade
go

commit
go

-- crea tab ContatoriListePrelievo ok

create table ContatoriListePrelievo(
  CodCS   nvarchar(2) not null,
  Anno    varchar(2)  not null,
  Mese    varchar(2)  not null,
  LastIdx int         constraint DF_ContatoriListePrelievo_LastIdx default (0),

  constraint PK_ContatoriListePrelievo primary key(CodCS,Anno,Mese)
)
go



-- crea tab tipolistaPrelievo ok

create table TipoListaPrelievo(
  CodTipoLista nvarchar(10)  not null constraint PK_TipoListaPrelievo primary key,
  DescrLista   nvarchar(250),
  IdxPosizione int,
  CodCella     nvarchar(50)  not null
)
go



-- crea tab ElencoListe prelievo ok

set xact_abort on
go

begin transaction
go

create table ElencoListePrelievo(
  CodLista        nvarchar(12)  not null constraint PK_ElencoListePrelievo primary key,
  CodTipoLista    nvarchar(10)  not null,
  CodCS           nchar(2)      not null,
  DataCreaz       datetime      not null,
  CodCliente      nvarchar(6),
  RagSociale      nvarchar(35),
  CodSoggetto     nchar(17)     not null,
  Particolare     nvarchar(15)  not null,
  DescParticolare nvarchar(30),
  DisegnoGrezzo   nvarchar(30),
  Esponente       nvarchar(6),
  CodImballo      nvarchar(15),
  QtaTot          decimal(10,2) not null,
  CodStatoLista   int           not null constraint DF_ElencoListePrelievo_CodStatoLista default (1)
)
go

-- The script may need editing: Some other object must have been synchronized first!
go

alter table ElencoListePrelievo add
  constraint FK_ElencoListePrelievo_AnagStatiListe foreign key(CodStatoLista) references AnagStatiListe(CodStatoLista) on update cascade
go

-- The script may need editing: Some other object must have been synchronized first!
go

alter table ElencoListePrelievo add
  constraint FK_ElencoListePrelievo_TipoListaPrelievo1 foreign key(CodTipoLista) references TipoListaPrelievo(CodTipoLista) on update cascade
go

commit
go

-- crea tab RigheLIstePrelievo ok

set xact_abort on
go

begin transaction
go

create table RigheListePrelievo(
  CodLista  nvarchar(12)  not null,
  UDC       nvarchar(50)  not null,
  Qta       decimal(10,2) not null,
  Proposto  bit,
  Prelevato bit,

  constraint PK_RigheListePrelievo primary key(CodLista,UDC)
)
go

alter table RigheListePrelievo add
  constraint FK_RigheListePrelievo_ElencoCartellini foreign key(UDC) references ElencoCartellini(UDC) on update cascade
go

-- The script may need editing: Some other object must have been synchronized first!
go

alter table RigheListePrelievo add
  constraint FK_RigheListePrelievo_ElencoListePrelievo1 foreign key(CodLista) references ElencoListePrelievo(CodLista) on update cascade
go

commit
go

-- mod tab rilproAnagOperatori ok

set xact_abort on
go

begin transaction
go

alter table RilPro.AnagOperatori drop
  constraint PK_AnagOperatori_1 
go

alter table RilPro.AnagOperatori alter column
  CodSoggetto nchar(17) not null
go

alter table RilPro.AnagOperatori add
  constraint PK_AnagOperatori_1 primary key(CodSoggetto)
go

commit
go


-- mod tab tipocella

set xact_abort on
go

begin transaction
go

alter table TipoCella add
  CheckPiena bit not null constraint DF_TipoCella_CheckPiena default (0)
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_TipoCella_updateQuery
*
* aggiorna tipo cella
*
* Steamware, S.E.L.
* mod: 2010.07.16
*
****************************************/
create PROCEDURE stp_TipoCella_updateQuery
(
	@Original_IdxTipoCella INT,	
	@CodMag VARCHAR(50),
	@CodCS VARCHAR(2),	
	@Quantita INT,
	@Capienza INT,
	@Max_X FLOAT,
	@Max_Y FLOAT,
	@Max_Z FLOAT,
	@Max_Kg FLOAT,
	@CheckPiena BIT
)

AS

UPDATE       TipoCella
SET          CodMag = @CodMag, CodCS = @CodCS, Quantita = @Quantita, Capienza = @Capienza, Max_X = @Max_X, Max_Y = @Max_Y, Max_Z = @Max_Z, 
             Max_Kg = @Max_Kg, CheckPiena = @CheckPiena
WHERE        (IdxTipoCella = @Original_IdxTipoCella)

RETURN
go

/***************************************
* STORED stp_TipoCella_deleteQuery
*
* elimina tipo cella
*
* Steamware, S.E.L.
* mod: 2010.07.16
*
****************************************/
create PROCEDURE stp_TipoCella_deleteQuery
(
	@Original_IdxTipoCella INT
)

AS

DELETE		FROM TipoCella
WHERE       (IdxTipoCella = @Original_IdxTipoCella)

RETURN
go

commit
go

-- registro versione...
INSERT INTO [dbo].[LogUpdateDb] ([Versione],[Data]) VALUES(95, GETDATE())
GO