set xact_abort on
go

begin transaction
go

alter table AS400_MagCont add
  CodMagAS_compensaz nvarchar(50) constraint DF_AS400_MagCont_CodMagAS_compensaz default (0)
go

update AS400_MagCont set CodMagAS_compensaz=0
go

set ANSI_NULLS on
go

-- =============================================
-- Author:		Samuele E. Locatelli
-- Create date: 2009-09-20
-- Description:	trigger x inserimento movimenti
--				nella coda di trasferimento batch
--				verso AS400 x UPDATE UDC
-- =============================================
alter TRIGGER trg_updMovAS 
   ON ElencoCartellini
   AFTER UPDATE
AS 
BEGIN

	-- setup variabili e verbosità
	SET NOCOUNT ON;
	DECLARE @partOk AS BIT
	DECLARE @IdxPosFrom AS INT
	DECLARE @IdxPosTo AS INT
	DECLARE @magFrom AS NVARCHAR(50)
	DECLARE @magTo AS NVARCHAR(50)
	DECLARE @qta AS DECIMAL(10,2)
	DECLARE @qtaOld AS DECIMAL(10,2)
	DECLARE @IdxPosizioneComp INT
	
	-- controllo che CI SIA il particolare (altrimenti non eseguo)
	SET @partOk = (SELECT CASE WHEN ISNULL(Particolare,'n.d.') = 'n.d.' THEN 0 ELSE 1 END FROM deleted)
	
	IF(@partOk = 1)
		SET @qta = (SELECT Qta FROM inserted)
		SET @qtaOld = (SELECT Qta FROM deleted)
		-- se c'è stata modifica di quantità inizio a rettificare quella
		IF (UPDATE(Qta)) AND(@qta <> @qtaOld)
		BEGIN
			-- registro il VERO movimento
			INSERT INTO AS400_BatchMovimenti(DataIns, CodEvento, Particolare, UDC, CodMagAS, Quantita)
				(
					SELECT	GETDATE(), 'UDC_MOD', Particolare, UDC, CAST(IdxPosizione AS NVARCHAR(50)), @qta - Qta
					FROM	deleted
					WHERE	CAST(IdxPosizione AS NVARCHAR(50)) IN (SELECT CodMagAS FROM AS400_MagCont WHERE Attivo = 1)
				)
			-- controllo: se il magazzino di destinazione richiede una compensazione registro il 2° movimento di compensazione
			SET @IdxPosFrom = (SELECT IdxPosizione FROM deleted)
			SET @IdxPosTo	= (SELECT IdxPosizione FROM inserted)
			-- calcolo il mag di compensazione
			SET @IdxPosizioneComp = (SELECT CAST(ISNULL(CodMagAS_compensaz,0) AS INT) FROM AS400_MagCont WHERE (Attivo = 1) AND (CodMagAS = CAST(@IdxPosFrom AS NVARCHAR(50))))
			IF (@IdxPosizioneComp > 0)
				BEGIN
					INSERT INTO AS400_BatchMovimenti(DataIns, CodEvento, Particolare, UDC, CodMagAS, Quantita)
						(
							SELECT	GETDATE(), 'UDC_COMP', Particolare, UDC, CAST(@IdxPosizioneComp AS NVARCHAR(50)), Qta - @qta
							FROM	deleted
							WHERE	CAST(IdxPosizione AS NVARCHAR(50)) IN (SELECT CodMagAS FROM AS400_MagCont WHERE Attivo = 1)
						)
				END
		END
	
		-- controllo poi se sia cambiato il magazzino, altrimenti non lo inserisco
		IF UPDATE(IdxPosizione)
		BEGIN
			-- Prendo le posizioni di magazzino precedente e nuova 
			SET @IdxPosFrom = (SELECT IdxPosizione FROM deleted)
			SET @IdxPosTo	= (SELECT IdxPosizione FROM inserted)
			-- Le trasformo nelle equivalenti stringhe del codMag SE attive
			SET @magFrom	= (SELECT ISNULL(CodMagAS,'---') FROM AS400_MagCont WHERE Attivo = 1 AND CodMagAs = CAST(@IdxPosFrom AS NVARCHAR(50)))
			SET @magTo		= (SELECT ISNULL(CodMagAS,'---') FROM AS400_MagCont WHERE Attivo = 1 AND CodMagAs = CAST(@IdxPosTo AS NVARCHAR(50)))
			
			-- inserisco un movimento di deposito verso mag nuovo (SE attivo)
			IF (@magFrom <> '---')
				BEGIN
					INSERT INTO AS400_BatchMovimenti(DataIns, CodEvento, Particolare, UDC, CodMagAS, Quantita)
					(
						SELECT	GETDATE(), 'UDC_MOV', Particolare, UDC, CAST(IdxPosizione AS NVARCHAR(50)), -@qta
						FROM	deleted
						WHERE	CAST(IdxPosizione AS NVARCHAR(50)) IN (SELECT CodMagAS FROM AS400_MagCont WHERE Attivo = 1)
					)
				END
			-- inserisco un movimento di prelievo verso mag vecchio (SE attivo)
			IF (@magTo <> '---')
				BEGIN
					INSERT INTO AS400_BatchMovimenti(DataIns, CodEvento, Particolare, UDC, CodMagAS, Quantita)
					(
						SELECT	GETDATE(), 'UDC_MOV', Particolare, UDC, CAST(IdxPosizione AS NVARCHAR(50)), @qta
						FROM	inserted
						WHERE	CAST(IdxPosizione AS NVARCHAR(50)) IN (SELECT CodMagAS FROM AS400_MagCont WHERE Attivo = 1)
					)
				END
		END

END
go

commit
go


set xact_abort on
go

begin transaction
go

UPDATE dbo.AS400_MagCont SET 
  CodMagAS_compensaz=N'84'
  WHERE CodMagAS=N'54'
UPDATE dbo.AS400_MagCont SET 
  CodMagAS_compensaz=N'84'
  WHERE CodMagAS=N'88'
go

commit transaction
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

create VIEW v_specParticolari
AS
SELECT DISTINCT TOP (100) PERCENT CodCliente, RagSociale, Particolare, DescParticolare, DisegnoGrezzo, Esponente, CodStampo, Figura
FROM            dbo.ElencoCartellini
WHERE        (NOT (Particolare IS NULL)) AND (NOT (DisegnoGrezzo IS NULL))
go

exec sp_addextendedproperty '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 = "ElencoCartellini"
            Begin Extent = 
               Top = 6
               Left = 246
               Bottom = 322
               Right = 416
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         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
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', 'dbo', 'VIEW', 'v_specParticolari'
go

exec sp_addextendedproperty 'MS_DiagramPaneCount', 1, 'SCHEMA', 'dbo', 'VIEW', 'v_specParticolari'
go

commit
go



set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/*(
* STORED stp_ParticolariGetLike
*
* elenco dei particolari con ricerca LIKE
*
* Steamware, S.E.L.
* mod: 2010.07.07
*
****************************************/
create PROCEDURE stp_ParticolariGetLike
(
	@searchVal VARCHAR(50),
	@CodCS VARCHAR(2)
)
AS

	SELECT		RilPro.AnagParticolari.*
	FROM        RilPro.AnagParticolari 
	WHERE       (RilPro.AnagParticolari.CodCS = @CodCS) AND (RilPro.AnagParticolari.Particolare LIKE '%' + @searchVal + '%')

RETURN
go

commit
go

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_SpecParticolare
*
* ottiene il dettaglio delle specifiche di un particolare dato il suo codice
*
* Steamware, S.E.L.
* mod: 2010.10.29
*
****************************************/
create PROCEDURE stp_SpecParticolare
(
	@Particolare NVARCHAR(15)
)
AS

SELECT		*
FROM        v_specParticolari
WHERE       Particolare=@Particolare


RETURN
go

commit
go



set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_UDC_updateQty
*
* aggiorna un cartellino x la sua quantità
*
* Steamware, S.E.L.
* mod: 2010.06.12
*
****************************************/
alter PROCEDURE stp_UDC_updateQty
(
	@UDC VARCHAR(20),
	@CodSoggetto VARCHAR(17),
	@CodTipoDichiaraz CHAR(1),
	@CodEvento VARCHAR(10),
	@Qta DECIMAL(10,2)
)
AS


------------------------------------------------------------------------------------------------------
-- Ciclo principale x edit record
------------------------------------------------------------------------------------------------------
BEGIN TRAN

	/*
	-- registro in StoricoCartellini
	INSERT INTO StoricoCartellini(DataEv, CodEvento, CodTipoDichiaraz, UDC, CodCS,CodCliente, RagSociale, Particolare, DescParticolare,
						DisegnoGrezzo, Esponente, CodImpianto, DescImpianto, CodStampo, Figura, DataFus, TurnoFus, CodImballo, CodSoggetto, 
						NumCont, Tara, Qta, CodStato, IdxPosizione, PesoTot, PesoCad)
	SELECT		GETDATE() AS DataEv, @CodEvento AS CodEvento, @CodTipoDichiaraz AS CodTipoDichiaraz, UDC, CodCS,
				CodCliente, RagSociale, Particolare, DescParticolare, DisegnoGrezzo, Esponente, CodImpianto, DescImpianto, CodStampo, Figura,
				DataFus, TurnoFus, CodImballo, @CodSoggetto AS CodSoggetto, NumCont, Tara, Qta, CodStato, IdxPosizione, PesoTot, PesoCad
	FROM        ElencoCartellini
	WHERE       (UDC = @UDC)
	*/
		
	-- modifico il record 
	UPDATE	ElencoCartellini
	SET		Qta = @Qta, ModDate = GETDATE()
	WHERE	UDC = @UDC
	
COMMIT TRAN
------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------
-- restituisco la tab dati con l'udc appena inserito
------------------------------------------------------------------------------------------------------
SELECT * 
FROM ElencoCartellini
WHERE  UDC = @UDC
------------------------------------------------------------------------------------------------------

RETURN
go

commit
go



set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

alter VIEW v_selDestinatariListePrelievo
AS
SELECT        CAST('*' AS NVARCHAR(6)) AS value, CAST('*' AS NVARCHAR(50)) AS label, '01-PreFus' AS conditio
UNION
SELECT        CAST(CodCliente AS NVARCHAR(6)) AS value, CAST(RagSociale AS NVARCHAR(50)) AS label, '02-PreCli' AS conditio
FROM            RilPro.AnagClienti
UNION
SELECT        CAST(CodTerzista AS NVARCHAR(6)) AS value, CAST(DescTerzista AS NVARCHAR(50)) AS label, '03-TerWip' AS conditio
FROM            RilPro.AnagDepositi
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 = 
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 3360
         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
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', 'dbo', 'VIEW', 'v_selDestinatariListePrelievo'
go

commit
go

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_rettMovAs400
*
* inserisce nella tabella per i trasferimenti batch verso magazzino AS una quantità di rettifica (con segno - )per UDC eventualmente già scaricati a sistema
*
* Steamware, S.E.L.
* mod: 2010.11.02
*
****************************************/
alter PROCEDURE stp_rettMovAs400
(
	@Particolare NVARCHAR(50),
	@UDC NVARCHAR(20),
	@CodMagAS NVARCHAR(50),
	@Qta DECIMAL(10,2)
)
AS

BEGIN
	
	INSERT INTO AS400_BatchMovimenti(DataIns, CodEvento, Particolare, UDC, CodMagAS, Quantita)
	VALUES (GETDATE(), 'UDC_NOQTY', @Particolare, @UDC, @CodMagAS, -@Qta)

END

RETURN
go

commit
go

set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_selDestListePrelByConditio
*
* Ottiene i destinatari delle liste di prelievo data la condizione indicata
*
* Steamware, S.E.L.
* mod: 2010.11.02
*
****************************************/
create PROCEDURE stp_selDestListePrelByConditio
(
	@conditio NVARCHAR(20)
)
AS

IF (@conditio='01-PreFus')
BEGIN
	SELECT        CAST('*' AS NVARCHAR(6)) AS value, CAST('*' AS NVARCHAR(50)) AS label, '01-PreFus' AS conditio
END

ELSE IF (@conditio='02-PreCli') 
BEGIN
	SELECT        CAST(CodCliente AS NVARCHAR(6)) AS value, CAST(RagSociale AS NVARCHAR(50)) AS label, '02-PreCli' AS conditio
	FROM            RilPro.AnagClienti
	ORDER BY label
END

ELSE
BEGIN
	SELECT        CAST(CodTerzista AS NVARCHAR(6)) AS value, CAST(DescTerzista AS NVARCHAR(50)) AS label, '03-TerWip' AS conditio
	FROM            RilPro.AnagDepositi
	ORDER BY label
END

RETURN
go

commit
go







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