-- creat tab

create table ElencoListePrelievo_AS(
  CodLista        nvarchar(12)  not null constraint PK_ElencoListePrelievo_AS 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),
  Figura          nvarchar(4),
  CodImballo      nvarchar(15),
  QtaTot          decimal(10,2) not null,
  CodStatoLista   int           not null constraint DF_ElencoListePrelievo_AS_CodStatoLista default (1)
)
go


-- crea tab

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

  constraint PK_RigheListePrelievo_AS primary key(CodLista,UDC)
)
go


--- sistemo tab

set xact_abort on
go

begin transaction
go

alter table Soggetti2Utente drop
  constraint PK_Soggetti2Utente 
go

alter table Soggetti2Utente alter column
  CodSoggetto nchar(17) not null
go


set ANSI_NULLS on
go

commit
go

-- cancello dati e aggiungo struttura corretta


DELETE FROM [GMW_prod].[RilPro].[AnagOperatori]
WHERE CodCS LIKE '%MA%'
GO

DELETE FROM [GMW_prod].[dbo].[Soggetti2Utente]
GO

alter table Soggetti2Utente add
  constraint PK_Soggetti2Utente primary key(CodSoggetto,USER_NAME),
  constraint FK_Soggetti2Utente_AnagOperatori foreign key(CodSoggetto) references RilPro.AnagOperatori(CodSoggetto) on update cascade
go

-- aggiungo dati corretti



set xact_abort on
go

begin transaction
go

INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000000068', N'MA', N'0000083', N'000020', N'Fonderie Mario Mazzucconi               ', N'0000000210', N'Ambivere                                ', N'ROTA                                    ', N'GIANPIETRO                              ')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000000069', N'MA', N'0000084', N'000020', N'Fonderie Mario Mazzucconi               ', N'0000000210', N'Ambivere                                ', N'PANZA                                   ', N'GIOVANNI                                ')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000000080', N'MA', N'0000095', N'000020', N'Fonderie Mario Mazzucconi               ', N'0000000210', N'Ambivere                                ', N'PERICO                                  ', N'FAUSTO                                  ')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000000083', N'MA', N'0000098', N'000020', N'Fonderie Mario Mazzucconi               ', N'0000000210', N'Ambivere                                ', N'BENEDETTI                               ', N'PIETRO                                  ')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000000090', N'MA', N'0000105', N'000020', N'Fonderie Mario Mazzucconi               ', N'0000000210', N'Ambivere                                ', N'TINTORI                                 ', N'ANTONELLO                               ')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O0000000000099999', N'MA', N'', NULL, NULL, NULL, NULL, N'OPERATORE', N'PROVA')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O9900000000000001', N'MA', N'0000001', N'000020', N'Fonderie Mario Mazzucconi', N'0000000210', N'Ambivere', N'Locatelli', N'Samuele')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O9900000000000002', N'MA', N'0000002', N'000020', N'Fonderie Mario Mazzucconi', N'0000000210', N'Ambivere', N'Salvi', N'Marco')
INSERT INTO RilPro.AnagOperatori
  VALUES (N'O9900000000000003', N'MA', N'0000003', N'000020', N'Fonderie Mario Mazzucconi', N'0000000210', N'Ambivere', N'Novali', N'Ermanno')
go

commit transaction
go



set xact_abort on
go

begin transaction
go

INSERT INTO dbo.Soggetti2Utente
  VALUES (N'O9900000000000001', N'samuele', 'STEAMWAREWIN')
INSERT INTO dbo.Soggetti2Utente
  VALUES (N'O9900000000000002', N'marco', 'STEAMWAREWIN')
INSERT INTO dbo.Soggetti2Utente
  VALUES (N'O9900000000000003', N'ermanno', 'STEAMWAREWIN')
go

commit transaction
go


-- modifico vista

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

alter VIEW v_righeListePrelievoPosizione
AS
SELECT        TOP (100) PERCENT dbo.RigheListePrelievo.CodLista, dbo.RigheListePrelievo.UDC, dbo.RigheListePrelievo.Qta, dbo.RigheListePrelievo.Proposto, 
                         dbo.RigheListePrelievo.Prelevato, ISNULL(dbo.Celle.CodCella, N'') AS CodCella, ISNULL(dbo.Blocchi.CodMag, N'') AS CodMag, ISNULL(dbo.Blocchi.CodBlocco, N'') 
                         AS CodBlocco, ISNULL(dbo.Celle.X, 0) AS X, ISNULL(dbo.Celle.Y, 0) AS Y, ISNULL(dbo.Celle.Z, 0) AS Z
FROM            dbo.Celle INNER JOIN
                         dbo.PosizioneUdcCorrente ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella INNER JOIN
                         dbo.Blocchi ON dbo.Celle.IdxBlocco = dbo.Blocchi.IdxBlocco RIGHT OUTER JOIN
                         dbo.RigheListePrelievo ON dbo.PosizioneUdcCorrente.UDC = dbo.RigheListePrelievo.UDC
ORDER BY dbo.RigheListePrelievo.CodLista DESC
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[40] 4[20] 2[20] 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 = "RigheListePrelievo"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 188
               Right = 208
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "PosizioneUdcCorrente"
            Begin Extent = 
               Top = 18
               Left = 377
               Bottom = 183
               Right = 547
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Celle"
            Begin Extent = 
               Top = 6
               Left = 621
               Bottom = 235
               Right = 791
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Blocchi"
            Begin Extent = 
               Top = 5
               Left = 870
               Bottom = 134
               Right = 1040
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 13
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
        ', 'SCHEMA', 'dbo', 'VIEW', 'v_righeListePrelievoPosizione'
go

exec sp_updateextendedproperty 'MS_DiagramPane2', ' GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', 'dbo', 'VIEW', 'v_righeListePrelievoPosizione'
go

commit
go




-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_cellaGetByCodCella
*
* ottiene l'elenco delle celle (1...) dato codice cella varchar
*
* Steamware, S.E.L.
* mod: 2010.06.12
*
****************************************/
create PROCEDURE stp_cellaGetByCodCella
(
	@CodCella VARCHAR(50)
)
AS

	SELECT	*
	FROM    Celle 
	WHERE   CodCella = @CodCella
RETURN
go

commit
go



-- mod stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celleCreaMancantiBlocco
*
* crea il numero di celle richeiste e le assegna al blocco con valori default
*
* Steamware, S.E.L.
* mod: 2010.05.18
*
****************************************/
alter PROCEDURE stp_celleCreaMancantiBlocco
(
	@CodCS VARCHAR(2),
	@CodMag VARCHAR(50),
	@IdxBlocco INT
)
AS

---------------------------------------------------------------------
-- setup iniziale valori XYZ del blocco
---------------------------------------------------------------------
DECLARE	@CodBlocco VARCHAR(3);
SET		@CodBlocco = (SELECT CodBlocco FROM Blocchi WHERE IdxBlocco = @IdxBlocco)
DECLARE @NumX INT;
DECLARE @NumY INT;
DECLARE @NumZ INT;
SET		@NumX = (SELECT NumX FROM Blocchi WHERE IdxBlocco = @IdxBlocco) ;
SET		@NumY = (SELECT NumY FROM Blocchi WHERE IdxBlocco = @IdxBlocco) ;
SET		@NumZ = (SELECT NumZ FROM Blocchi WHERE IdxBlocco = @IdxBlocco) ; 

DECLARE @Index INT ;
DECLARE @IdxTipoCella INT;


---------------------------------------------------------------------
-- creazione temp table x numeri delle dimensioni XYZ del blocco
---------------------------------------------------------------------
DECLARE @DimTable TABLE
(
	dimensione VARCHAR(1),
	valore INT
)

-- inserisco X
SET		@Index = 1 ;
WHILE	@Index <= @NumX 
    BEGIN 
		INSERT @DimTable(dimensione, valore)
		VALUES ('X', @Index)
		SET @Index = @Index + 1 ;
	END	
-- inserisco Y
SET		@Index = 1 ;
WHILE	@Index <= @NumY
    BEGIN 
		INSERT @DimTable(dimensione, valore)
		VALUES ('Y', @Index)
		SET @Index = @Index + 1 ;
	END	
-- inserisco Z
SET		@Index = 1 ;
WHILE	@Index <= @NumZ
    BEGIN 
		INSERT @DimTable(dimensione, valore)
		VALUES ('Z', @Index)
		SET @Index = @Index + 1 ;
	END	


---------------------------------------------------------------------
-- creazione temp table x schema COMPLETO posizioni blocco
---------------------------------------------------------------------
DECLARE @SchemaBlocco TABLE
(
	X INT,
	Y INT,
	Z INT
)
INSERT INTO @SchemaBlocco
SELECT X_val.valore, Y_val.valore, Z_val.valore 
FROM	(SELECT * FROM @DimTable WHERE dimensione='X') AS X_val, 
		(SELECT * FROM @DimTable WHERE dimensione='Y') AS Y_val,
		(SELECT * FROM @DimTable WHERE dimensione='Z') AS Z_val

---------------------------------------------------------------------
-- cerco primo tipo di celle per il mag indicato
---------------------------------------------------------------------
SET @IdxTipoCella = (SELECT        TOP (1) ISNULL(IdxTipoCella, 0) AS IdxTipoCella
                     FROM            TipoCella
                     WHERE        (CodCS = @CodCS) AND (CodMag = @CodMag))
---------------------------------------------------------------------
-- inserisco le celle nelle posizioni LIBERE del blocco
---------------------------------------------------------------------
INSERT INTO Celle(CodCS, CodCella, IdxTipoCella, Descrizione, Attiva, Piena, IdxBlocco,X,Y,Z) 
SELECT @CodCS, @CodBlocco + dbo.f_padLeft(CAST(sb.X AS VARCHAR),2,'0') + dbo.f_padLeft(CAST(sb.Y AS VARCHAR),2,'0') + dbo.f_padLeft(CAST(sb.Z AS VARCHAR),2,'0'), @IdxTipoCella, '-', 1, 0, @IdxBlocco, sb.X, sb.Y, sb.Z
FROM @SchemaBlocco sb LEFT OUTER JOIN (SELECT	*
FROM	Celle 
WHERE	IdxBlocco = @IdxBlocco) c
ON sb.X = c.X AND sb.Y = c.Y AND sb.Z = c.Z
WHERE c.IdxCella IS NULL
ORDER BY sb.X,sb.Y,sb.Z ASC


RETURN
go

commit
go


-- mod stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celleLibereGetByIdxBlocco
*
* ottiene l'elenco delle posizioni LIBERE associate ad un dato blocco
*
* Steamware, S.E.L.
* mod: 2010.06.02
*
****************************************/
alter PROCEDURE stp_celleLibereGetByIdxBlocco
(
	@IdxBlocco INT
)
AS

	SELECT		*
	FROM		Celle 
	WHERE		(IdxBlocco = @IdxBlocco) AND (Attiva = 1) AND (Piena = 0)
RETURN
go

commit
go


-- mod stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celleNonAttiveByIdxBlocco
*
* ottiene l'elenco delle posizioni NON ATTIVE associate ad un dato blocco
*
* Steamware, S.E.L.
* mod: 2010.06.03
*
****************************************/
alter PROCEDURE stp_celleNonAttiveByIdxBlocco
(
	@IdxBlocco INT
)
AS

	SELECT		*
	FROM        Celle
	WHERE      (IdxBlocco = @IdxBlocco) AND (Attiva = 0)
RETURN
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celleOverbooked
*
* elenco delle celle con overbookinbg (più UDC assegnati rispetto alla capienza)
*
* Steamware, S.E.L.
* mod: 2010.06.29
*
****************************************/
create PROCEDURE stp_celleOverbooked
AS

SELECT		IdxCella, Capienza, UdcAssegnati, Piena
FROM        V_statoCelleCapienzaAssegnati
WHERE       (UdcAssegnati > Capienza) 

RETURN
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celle_deleteQuery
*
* cancella cella indicata
*
* Steamware, S.E.L.
* mod: 2010.07.16
*
****************************************/
create PROCEDURE stp_celle_deleteQuery
(
	@Original_IdxCella INT
)
AS

DELETE FROM		Celle
WHERE			([IdxCella] = @Original_IdxCella)

RETURN
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_celle_updateQuery
*
* aggiorna cella indicata
*
* Steamware, S.E.L.
* mod: 2010.07.16
*
****************************************/
create PROCEDURE stp_celle_updateQuery
(
	@Original_IdxCella INT,	
	@CodCS VARCHAR(2),	
	@CodCella VARCHAR(50),
	@IdxTipoCella INT,
	@Descrizione VARCHAR(50),
	@Attiva BIT,
	@Piena BIT,
	@IdxBlocco INT,
	@X INT,
	@Y INT,
	@Z INT
)
AS


UPDATE		Celle 
SET			[CodCS] = @CodCS, [CodCella] = @CodCella, [IdxTipoCella] = @IdxTipoCella, [Descrizione] = @Descrizione, 
			[Attiva] = @Attiva, [Piena] = @Piena, [IdxBlocco] = @IdxBlocco, [X] = @X, [Y] = @Y, [Z] = @Z 
WHERE		([IdxCella] = @Original_IdxCella)


RETURN
go

commit
go



-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_creaListaPrelievo
*
* crea un nuovo record nella ElencoListePrelievo e restituisce il codice ID appena creato
*
* Steamware, S.E.L. 8 (FROM Stintino)
* mod: 2010.07.06
*
************************************/
create PROCEDURE stp_creaListaPrelievo
(
	@Anno NVARCHAR(2),
	@Mese NVARCHAR(2),
	@IdxPosizione INT,
	@CodCS NCHAR(2),
	@CodCliente NVARCHAR(6),
	@RagSociale NVARCHAR(35),
	@CodSoggetto NCHAR(16),
	@Particolare NVARCHAR(15),
	@DescParticolare NVARCHAR(30),
	@DisegnoGrezzo NVARCHAR(30),
	@Esponente NVARCHAR(6),
	@Figura NVARCHAR(4),
	@CodImballo NVARCHAR(15),
	@QtaTot DECIMAL(10,2)
)
AS

-- Dichiarazioni iniziali
DECLARE @CodLista AS NVARCHAR(12)
DECLARE @CodTipoLista AS NVARCHAR(10)
DECLARE @numLista AS INT
DECLARE @temp AS INT
DECLARE @ora AS DATETIME
	
	
SET @CodTipoLista = (SELECT CodTipoLista FROM TipoListaPrelievo WHERE IdxPosizione = @IdxPosizione)	
	
------------------------------------------------------------------------------------------------------
-- codice lista di prelievo KAAMM00000 - K = start char, AA = anno, MM = mese, 00000 = progressivo
------------------------------------------------------------------------------------------------------
-- controllo se ci sia già un UDC per company / flusso anno
BEGIN TRAN
	-- cerco nella tab contatori UDC l'ultimo valido
	SET @numLista = (
		SELECT COUNT(*)
		FROM ContatoriListePrelievo
		WHERE	CodCS		=	@CodCS
		AND		Anno		=	@Anno
		AND		Mese		=	@Mese
	)
	-- controllo se record c'è...
	IF(@numLista > 0)
		BEGIN
			-- se c'è incremento di 1 in tab
			UPDATE ContatoriListePrelievo
			SET LastIdx = LastIdx  + 1
			WHERE	CodCS		=	@CodCS
			AND		Anno		=	@Anno
			AND		Mese		=	@Mese

		END
	ELSE
		BEGIN
			-- lo creo!
			INSERT INTO ContatoriListePrelievo(CodCS, Anno, Mese, LastIdx)
			VALUES (@CodCS, @Anno, @Mese, 1)
		END		
	-- aggiorno numero lista prelievo
	SET @numLista = (
		SELECT LastIdx
		FROM ContatoriListePrelievo
		WHERE	CodCS		=	@CodCS
		AND		Anno		=	@Anno
		AND		Mese		=	@Mese

	)
COMMIT TRAN
------------------------------------------------------------------------------------------------------

BEGIN TRAN

	------------------------------------------------------------------------------------------------------
	-- Ciclo principale x inserimento nuovo record	
	------------------------------------------------------------------------------------------------------
	-- ottengo il nuovo codice UDC completo
	SET @CodLista = 'K' + @CodCS + @Anno + dbo.f_padLeft(CAST(@Mese AS NVARCHAR(2)), 2, '0') + + dbo.f_padLeft(CAST(@numLista AS NVARCHAR(5)), 5, '0')

	INSERT INTO ElencoListePrelievo
						  (CodLista, CodTipoLista, CodCS, DataCreaz, CodCliente, RagSociale, CodSoggetto, Particolare, DescParticolare, DisegnoGrezzo, Esponente, Figura, CodImballo, QtaTot, CodStatoLista)
	VALUES     (@CodLista, @CodTipoLista, @CodCS, GETDATE(), @CodCliente, @RagSociale, @CodSoggetto, @Particolare, @DescParticolare, @DisegnoGrezzo, @Esponente,@Figura, @CodImballo, @QtaTot, 0)

COMMIT TRAN

SELECT * FROM V_elencoListePrelievo WHERE CodLista = @CodLista;

RETURN
go

commit
go



-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoByCodLista
*
* Steamware, E.N.
* mod: 2010.06.25
*
************************************/
create PROCEDURE stp_elencoListePrelievoByCodLista
(
	@CodLista NVARCHAR(12)
)
AS

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

commit
go

-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_elencoListePrelievoFilt
*
* restituisce l'elenco delle liste di prelievo secondo criteri di filtraggio
*
* Steamware, S.E.L.
* mod: 2010.07.06
*
****************************************/
create PROCEDURE stp_elencoListePrelievoFilt
(
	@DataFrom DATETIME,
	@DataTo DATETIME
)
AS
-- utilizza la vista righe lista prelievo e quindi solo le prelevate ...

	SELECT	*
	FROM    V_elencoListePrelievo
	WHERE   (DataCreaz BETWEEN @DataFrom AND @DataTo)
	ORDER BY DataCreaz DESC

RETURN
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getDetailsUdcByUdc
*
* Steamware, E.N.
* mod: 2010.06.28
*
************************************/
create PROCEDURE stp_getDetailsUdcByUdc
(
	@UDC NVARCHAR(50)
)
AS

SELECT *
FROM ElencoCartellini 
WHERE (UDC = @UDC)
go

commit
go


-- creo stored

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
*
************************************/
create PROCEDURE stp_elencoListePrelievoUpdateStato
(
	@NewCodSoggetto NCHAR(16),
	@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


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getPosizioneByUdc
*
* Steamware, E.N.
* mod: 2010.06.25
*
************************************/
create PROCEDURE stp_getPosizioneByUdc
(
	@UDC NVARCHAR(50)
)
AS

SELECT *
FROM PosizioneUdcCorrente
where UDC = @UDC
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getRagioneSocialeByCodCliente
*
* Steamware, E.N.
* mod: 2010.06.25
*
************************************/
create PROCEDURE stp_getRagioneSocialeByCodCliente
(
	@CodCliente NVARCHAR(6)
)
AS

SELECT     *
FROM         RilPro.AnagClienti
WHERE     (CodCliente = @CodCliente)
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getRigaPrelievoByCodLista
*
* Steamware, E.N.
* mod: 2010.06.25
*
************************************/
create PROCEDURE stp_getRigaPrelievoByCodLista
(
	@CodLista NVARCHAR(12)
)
AS

SELECT     *
FROM         RigheListePrelievo
WHERE     (CodLista = @CodLista)
go

commit
go


-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getRigaPrelievoPosizioneByCodLista
*
* Steamware, S.E.L.
* mod: 2010.07.07
*
************************************/
create PROCEDURE stp_getRigaPrelievoPosizioneByCodLista
(
	@CodLista NVARCHAR(12)
)
AS

SELECT	*
FROM    v_RigheListePrelievoPosizione
WHERE   (CodLista = @CodLista)
go

commit
go



-- creo stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getTipoListaByCodTipoLista
*
* Steamware, E.N.
* mod: 2010.06.25
*
************************************/
create PROCEDURE stp_getTipoListaByCodTipoLista
(
	@CodTipoLista NVARCHAR(10)
)
AS

SELECT *
FROM TipoListaPrelievo
where CodTipoLista = @CodTipoLista
go

commit
go


-- mod stored

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_getUdcDetailFullCode
*
* ottiene l'elenco degli UDC con i dati significativi partendo da un valore da cercare come LIKE in 
*  - UDC
*  - Particolare
*
* Steamware, S.E.L.
* mod: 2010.05.24
*
****************************************/
alter PROCEDURE stp_getUdcDetailLikeSearch
(
	@searchVal VARCHAR(50),
	@CodCS VARCHAR(2)	
)
AS

SELECT        ElencoCartellini.UDC, ElencoCartellini.RagSociale, ElencoCartellini.Particolare, ElencoCartellini.DescParticolare, ElencoCartellini.DisegnoGrezzo, 
                         ElencoCartellini.Esponente, ElencoCartellini.DescImpianto, ElencoCartellini.DataFus, ElencoCartellini.TurnoFus, ElencoCartellini.CodImballo, ElencoCartellini.Qta, 
                         AnagStatiProdotto.DescStato, ElencoCartellini.ModDate, ISNULL(Blocchi.CodMag, N'') AS CodMag, ISNULL(Blocchi.CodBlocco, N'') AS CodBlocco, 
                         ISNULL(Celle.CodCella, N'') AS CodCella, ISNULL(Celle.X, 0) AS X, ISNULL(Celle.Y, 0) AS Y, ISNULL(Celle.Z, 0) AS Z
FROM            AnagStatiProdotto RIGHT OUTER JOIN
                         ElencoCartellini ON AnagStatiProdotto.CodStato = ElencoCartellini.CodStato AND AnagStatiProdotto.CodStato = ElencoCartellini.CodStato LEFT OUTER JOIN
                         Celle INNER JOIN
                         PosizioneUdcCorrente ON Celle.IdxCella = PosizioneUdcCorrente.IdxCella AND Celle.IdxCella = PosizioneUdcCorrente.IdxCella AND 
                         Celle.IdxCella = PosizioneUdcCorrente.IdxCella INNER JOIN
                         Blocchi ON Celle.IdxBlocco = Blocchi.IdxBlocco INNER JOIN
                         AnagMag ON Blocchi.CodMag = AnagMag.CodMag AND Blocchi.CodCS = AnagMag.CodCS AND Blocchi.CodMag = AnagMag.CodMag AND 
                         Blocchi.CodCS = AnagMag.CodCS AND Blocchi.CodMag = AnagMag.CodMag AND Blocchi.CodCS = AnagMag.CodCS ON 
                         ElencoCartellini.UDC = PosizioneUdcCorrente.UDC
WHERE        (ElencoCartellini.CodCS = @CodCS) AND ((ElencoCartellini.UDC like '%'+ @searchVal+'%') OR (ElencoCartellini.Particolare like '%'+ @searchVal+'%') OR (ElencoCartellini.RagSociale like '%'+ @searchVal+'%'))

RETURN
go

commit
go


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



