/* Sistemo parametri x cambio posizioni da web.config */
set xact_abort on
go

begin transaction
go

INSERT INTO dbo.AnagTipoEvento
  VALUES (N'UDC_FINITO', N'Richiesta UDC Finito')
INSERT INTO dbo.AnagTipoEvento
  VALUES (N'UDC_WIP', N'Richiesta UDC WIP')
go

commit transaction
go


exec sp_updateextendedproperty 'MS_DiagramPane1', '[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[41] 4[20] 2[11] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "Celle"
            Begin Extent = 
               Top = 149
               Left = 629
               Bottom = 313
               Right = 799
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagMag"
            Begin Extent = 
               Top = 6
               Left = 662
               Bottom = 118
               Right = 832
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Blocchi"
            Begin Extent = 
               Top = 36
               Left = 974
               Bottom = 257
               Right = 1144
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "PosizioneUdcCorrente"
            Begin Extent = 
               Top = 159
               Left = 201
               Bottom = 288
               Right = 371
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "TipoCella"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 135
               Right = 224
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 12
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1665
         Width = 1665
         Width = 1500
      End
   End
   Begin CriteriaPane = 
 ', 'SCHEMA', 'dbo', 'VIEW', 'V_MagazziniOverview'
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

alter VIEW V_statoCelleCapienzaAssegnati
AS
SELECT        dbo.Celle.IdxCella, dbo.Celle.CodCella, dbo.TipoCella.Capienza, COUNT(dbo.PosizioneUdcCorrente.UDC) AS UdcAssegnati, dbo.Celle.Piena
FROM            dbo.TipoCella INNER JOIN
                         dbo.Celle ON dbo.TipoCella.IdxTipoCella = dbo.Celle.IdxTipoCella INNER JOIN
                         dbo.PosizioneUdcCorrente ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella
GROUP BY dbo.TipoCella.Capienza, dbo.Celle.IdxCella, dbo.Celle.Piena, dbo.Celle.CodCella
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_anagOperatoriGetByCodSogg
*
* ottiene l'elenco degli operatori da CodSoggetto
*
* Steamware, S.E.L.
* mod: 2010.06.12
*
****************************************/
create PROCEDURE stp_anagOperatoriGetByCodSogg
(
	@CodSoggetto VARCHAR(17)
)
AS

	SELECT		*
	FROM        RilPro.AnagOperatori
	WHERE       (CodSoggetto = @CodSoggetto)


RETURN
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoUpdateStato
*
* aggiorna la lista di prelievo indicando lo stato richiesto
*
* Steamware, S.E.L.
* mod: 2010.07.06
*
************************************/
alter PROCEDURE stp_elencoListePrelievoUpdateStato
(
	@NewCodSoggetto NCHAR(17),
	@CodListaAttuale AS varchar(12),
	@CodStatoLista INT
)
AS

UPDATE    ElencoListePrelievo
SET       CodSoggetto = @NewCodSoggetto, CodStatoLista = @CodStatoLista
WHERE     (CodLista = @CodListaAttuale)

SELECT * FROM V_elencoListePrelievo WHERE (CodLista = @CodListaAttuale)
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoUpdateStato
*
* aggiorna la lista di prelievo indicando lo stato richiesto
*
* Steamware, S.E.L.
* mod: 2010.07.06
*
************************************/
alter PROCEDURE stp_elencoListePrelievoUpdateStato
(
	@NewCodSoggetto NCHAR(17),
	@CodListaAttuale AS varchar(12),
	@CodStatoLista INT
)
AS

UPDATE    ElencoListePrelievo
SET       CodSoggetto = @NewCodSoggetto, CodStatoLista = @CodStatoLista
WHERE     (CodLista = @CodListaAttuale)

SELECT * FROM V_elencoListePrelievo WHERE (CodLista = @CodListaAttuale)
go

commit
go

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoUpdateStato
*
* aggiorna la lista di prelievo indicando lo stato richiesto
*
* Steamware, S.E.L.
* mod: 2010.07.06
*
************************************/
alter PROCEDURE stp_elencoListePrelievoUpdateStato
(
	@NewCodSoggetto NCHAR(17),
	@CodListaAttuale AS varchar(12),
	@CodStatoLista INT
)
AS

UPDATE    ElencoListePrelievo
SET       CodSoggetto = @NewCodSoggetto, CodStatoLista = @CodStatoLista
WHERE     (CodLista = @CodListaAttuale)

SELECT * FROM V_elencoListePrelievo WHERE (CodLista = @CodListaAttuale)
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoByCodLista
*
* elenco liste di prelievo ATTIVE da codice soggetto
*
* Steamware, S.E.L
* mod: 2010.08.25
*
************************************/
create PROCEDURE stp_elencoLP_attiveByCodSoggetto
(
	@CodSoggetto NVARCHAR(17)
)
AS

SELECT	*
FROM	V_elencoListePrelievo 
WHERE	CodStatoLista = 2 AND (CodSoggetto = @CodSoggetto)
go

commit
go

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_spostaUdc
*
* sposta un UDC, salvandone la posizione precedente (se esiste) nella tab storica
*
* Steamware, S.E.L.
* mod: 2010.06.11
*
****************************************/
alter PROCEDURE stp_spostaUdc
(
	@CodCS VARCHAR(2),
	@UDC VARCHAR(50),
	--@IdxCellaFrom INT,
	@IdxCellaTo INT
)
AS

BEGIN TRAN
	-- inizio mettendo in storico la posizione attuale dell'UDC (SE esiste)
	INSERT INTO PosizioneUdcStorico(UDC, IdxCella, CodCS, DataInizio, DataFine)
	(
		SELECT	UDC, IdxCella, CodCS, DataRif as DataInizio, GETDATE() as DataFine
		FROM	PosizioneUdcCorrente
		WHERE	UDC = @UDC-- AND IdxCella = @IdxCellaFrom
	)
	
	-- libero la posizione attuale dell'UDC (se c'è...)
	DELETE
	FROM	PosizioneUdcCorrente
	WHERE	UDC = @UDC-- AND IdxCella = @IdxCellaFrom
	
	-- creo una nuova posizione per l'UDC
	INSERT INTO PosizioneUdcCorrente(UDC, IdxCella, CodCS, DataRif)
	VALUES(@UDC, @IdxCellaTo, @CodCS, GETDATE())
	
COMMIT TRAN

RETURN
go

/***************************************
* STORED stp_StatoCelleByCodCella
*
* stato cella filtrato x CodCella
*
* Steamware, S.E.L.
* mod: 2010.08.03
*
****************************************/
create PROCEDURE stp_StatoCelleByCodCella
(
	@CodCella VARCHAR(50)
)
AS

SELECT		*
FROM        V_statoCelleCapienzaAssegnati
WHERE       (CodCella= @CodCella) 

RETURN
go

/***************************************
* STORED stp_StatoCelleByIdxCella
*
* stato cella filtrato x idxCella
*
* Steamware, S.E.L.
* mod: 2010.08.03
*
****************************************/
create PROCEDURE stp_StatoCelleByIdxCella
(
	@IdxCella INT
)
AS

SELECT		*
FROM        V_statoCelleCapienzaAssegnati
WHERE       (IdxCella= @IdxCella) 

RETURN
go

/***************************************
* STORED stp_StatoCelleOverbooked
*
* elenco delle celle con overbookinbg (UDC assegnati >= capienza)
*
* Steamware, S.E.L.
* mod: 2010.08.03
*
****************************************/
create PROCEDURE stp_StatoCelleOverbooked
AS

SELECT		*
FROM        V_statoCelleCapienzaAssegnati
WHERE       (UdcAssegnati >= Capienza) 

RETURN
go

commit
go








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