-- Inserisco NUOVO PERMESSO x iniziare lavorazioen di ordini U / D anche SE NON SCHEDULATI...
INSERT INTO TraEv2Stati (CodMappa,CodObj,CodStato,CodEvento,NextCodStato,CmdPlugin,text2show,descrComando) VALUES('E_Stati','D','02','04','04',NULL,NULL,'cambio da stato 2 a 4 (inizio lav)')
INSERT INTO TraEv2Stati (CodMappa,CodObj,CodStato,CodEvento,NextCodStato,CmdPlugin,text2show,descrComando) VALUES('E_Stati','D','01','04','04',NULL,NULL,'cambio da stato 1 a 4 (inizio lav)')
INSERT INTO TraEv2Stati (CodMappa,CodObj,CodStato,CodEvento,NextCodStato,CmdPlugin,text2show,descrComando) VALUES('E_Stati','U','02','04','04',NULL,NULL,'cambio da stato 2 a 4 (inizio lav)')
INSERT INTO TraEv2Stati (CodMappa,CodObj,CodStato,CodEvento,NextCodStato,CmdPlugin,text2show,descrComando) VALUES('E_Stati','U','01','04','04',NULL,NULL,'cambio da stato 1 a 4 (inizio lav)')


-- 2018.03.23 update tab IstObj x salvare VOLUME occupato (come pesoEq / Peso standard)
ALTER TABLE IstObj ADD PesoEquiv float null;
ALTER TABLE IstObj_storico ADD PesoEquiv float null;
UPDATE IstObj_storico SET PesoEquiv = CEILING(Peso/10)*10
UPDATE IstObj SET PesoEquiv =  CEILING(Peso/10)*10

ALTER TABLE AnagLav ADD PesoUnit FLOAT NOT NULL DEFAULT 400;
ALTER TABLE AnagLav ADD RoundVal FLOAT NOT NULL DEFAULT 10;
INSERT INTO AnagLav([IdxLav],[DescrLav],[PesoUnit], RoundVal) VALUES('Fosf-VG', 'Fosfatazione Vasca Grande', 2500, 10)

/****** Object:  StoredProcedure [dbo].[stp_selStatiMinMax]    Script Date: 23/03/2018 11:59:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/***************************************
* STORED stp_impegniSchedulati
* recupera elenco degli impegni schedulati x TipoLav e unità equivalenti
*
* Steamware, S.E.L.
* mod: 2018.03.23
****************************************/
create PROCEDURE [dbo].[stp_impegniSchedulati]
(
	@IdxLav	nvarchar(50) = '*'
)
AS

SELECT @IdxLav = ISNULL(@IdxLav,'*')

;with cteImpegni as
(
	SELECT	* 
	FROM	IstObj
	WHERE	ISNULL(PesoEquiv,0)>0
		AND	CodStato = '03'
		AND	ExtKey = CASE WHEN @IdxLav = '*' THEN ExtKey ELSE @IdxLav END
)
, cteImpGG AS
(
SELECT		dbo.DateOnly(DueDate) AS DueDate, ExtKey as IdxLav, SUM(Peso) AS TotPeso, Sum(PesoEquiv) as TotPesoEquiv
FROM		cteImpegni	
GROUP BY	dbo.DateOnly(DueDate), ExtKey
)

SELECT	i.*, i.TotPesoEquiv / al.PesoUnit AS RatioUnit, CEILING(i.TotPesoEquiv / al.PesoUnit) AS NumUnit
FROM	cteImpGG i
	INNER JOIN AnagLav al ON al.IdxLav=i.IdxLav

RETURN

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/***************************************
* STORED stp_AnagCliGetByKey
*
* ottiene record anagrafica clienti da chiave
*
* Steamware, S.E.L.
* mod: 2018.03.23
*
****************************************/
create PROCEDURE [dbo].[stp_AnagLav]
(
	@IdxLav	NVARCHAR(50)
)
AS

SELECT	*
FROM	AnagLav
WHERE	IdxLav	= @IdxLav

RETURN



/****** Object:  StoredProcedure [dbo].[stp_IstObj_updateStatoTree]    Script Date: 23/03/2018 15:28:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/***************************************
* STORED stp_IstObj_updateStatoTree
*
* aggiorna stato di un intero ramo
*
* Steamware, S.E.L.
* mod: 2011.10.27
*
****************************************/
ALTER PROCEDURE [dbo].[stp_IstObj_updateStatoTree]
(
	@CodStato		NVARCHAR(50),
	@UserMod		NVARCHAR(50),
	@DataMod		DATETIME,
	@IdxObjCommon	NVARCHAR(50)
)
AS

UPDATE  IstObj
SET		CodStato	= @CodStato, 
		UserMod		= @UserMod, 
		DataMod		= @DataMod
WHERE	IdxObj		LIKE @IdxObjCommon

RETURN
GO


/****** Object:  StoredProcedure [dbo].[stp_IstObj_updateStatoTree]    Script Date: 23/03/2018 15:28:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/***************************************
* STORED stp_IstObj_updateStatoPesoEqTree
*
* aggiorna stato di un intero ramo + fix peso EQUIVALENTE
*
* Steamware, S.E.L.
* mod: 2011.10.27
*
****************************************/
create PROCEDURE [dbo].[stp_IstObj_updateStatoPesoEqTree]
(
	@CodStato		NVARCHAR(50),
	@UserMod		NVARCHAR(50),
	@DataMod		DATETIME,
	@IdxObjCommon	NVARCHAR(50),
	@EqPeso			FLOAT
)
AS

UPDATE  IstObj
SET		CodStato	= @CodStato, 
		UserMod		= @UserMod, 
		DataMod		= @DataMod,
		PesoEquiv	= @EqPeso * Peso
WHERE	IdxObj		LIKE @IdxObjCommon

RETURN
GO


/****** Object:  StoredProcedure [dbo].[stp_IstObj_updateStatoPesoEqTree]    Script Date: 23/03/2018 15:51:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/***************************************
* STORED stp_IstObj_updateStatoPesoEqTree
*
* aggiorna stato di un intero ramo + fix peso EQUIVALENTE
*
* Steamware, S.E.L.
* mod: 2011.10.27
*
****************************************/
ALTER PROCEDURE [dbo].[stp_IstObj_updateStatoPesoEqTree]
(
	@CodStato		NVARCHAR(50),
	@UserMod		NVARCHAR(50),
	@DataMod		DATETIME,
	@IdxObjCommon	NVARCHAR(50),
	@EqPeso			FLOAT
)
AS

BEGIN Tran
	-- recupero valore arrotondamento standard x lavorazione...
	DECLARE @RoundVal FLOAT = 1

	SELECT	@RoundVal = ISNULL(RoundVal,1)
	FROM	AnagLav al INNER JOIN IstObj ij ON al.IdxLav=ij.ExtKey
	WHERE	IdxObj LIKE @IdxObjCommon

	UPDATE  IstObj
	SET		CodStato	= @CodStato, 
			UserMod		= @UserMod, 
			DataMod		= @DataMod,
			PesoEquiv	= @EqPeso * CEILING(ISNULL(Peso,0) / @RoundVal ) * @RoundVal
	WHERE	IdxObj		LIKE @IdxObjCommon

COMMIT Tran
RETURN


/****** Object:  StoredProcedure [dbo].[stp_impegniSchedulati]    Script Date: 23/03/2018 18:22:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/***************************************
* STORED stp_impegniSchedulati
* recupera elenco degli impegni schedulati x TipoLav e unità equivalenti
*
* Steamware, S.E.L.
* mod: 2018.03.23
****************************************/
ALTER PROCEDURE [dbo].[stp_impegniSchedulati]
(
	@IdxLav	nvarchar(50) = '*'
)
AS

SELECT @IdxLav = ISNULL(@IdxLav,'*')

;with cteImpegni as
(
	SELECT	* 
	FROM	IstObj
	WHERE	ISNULL(PesoEquiv,0)>0
		AND	CodStato = '03'
		AND	ExtKey = CASE WHEN @IdxLav = '*' THEN ExtKey ELSE @IdxLav END
		AND dbo.DateOnly(DueDate) >= GETDATE()
)
, cteImpGG AS
(
SELECT		dbo.DateOnly(DueDate) AS DueDate, ExtKey as IdxLav, SUM(Peso) AS TotPeso, Sum(PesoEquiv) as TotPesoEquiv
FROM		cteImpegni	
GROUP BY	dbo.DateOnly(DueDate), ExtKey
)

SELECT	i.*, i.TotPesoEquiv / al.PesoUnit AS RatioUnit, CEILING(i.TotPesoEquiv / al.PesoUnit) AS NumUnit
FROM	cteImpGG i
	INNER JOIN AnagLav al ON al.IdxLav=i.IdxLav

RETURN

