﻿set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***************************************
* STORED stp_ParticolariOverview
*
* elenco di overview sullo stato dei magazzini (dettaglio a "maglie larghe")
*
* Steamware, S.E.L.
* mod: 2010.06.02
*
****************************************/
create PROCEDURE stp_magazzinoOverview
(
	@CodCS VARCHAR(2)
)
AS

	SELECT		dbo.AnagMag.CodCS, dbo.AnagMag.CodMag, dbo.AnagMag.DescMag, dbo.Blocchi.IdxBlocco, dbo.Blocchi.CodBlocco, 
				dbo.Blocchi.NumX * dbo.Blocchi.NumY * dbo.Blocchi.NumZ AS TotCelle, COUNT(dbo.Celle.IdxCella) AS CelleAttive, 
				COUNT(dbo.PosizioneUdcCorrente.UDC) AS CelleOccupate, COUNT(dbo.Celle.IdxCella) - COUNT(dbo.PosizioneUdcCorrente.UDC) AS CelleLibere, 
				ISNULL(MIN(dbo.PosizioneUdcCorrente.DataRif), GETDATE()) 
				AS Oldest, ISNULL(MAX(dbo.PosizioneUdcCorrente.DataRif), GETDATE()) AS Newest
	FROM        dbo.Celle RIGHT OUTER JOIN
				dbo.AnagMag INNER JOIN
				dbo.Blocchi ON dbo.AnagMag.CodMag = dbo.Blocchi.CodMag AND dbo.AnagMag.CodCS = dbo.Blocchi.CodCS ON 
				dbo.Celle.IdxBlocco = dbo.Blocchi.IdxBlocco LEFT OUTER JOIN
				dbo.PosizioneUdcCorrente ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella
	WHERE       (dbo.Celle.Attiva = 1) AND dbo.AnagMag.CodCS = @CodCS
	GROUP BY	dbo.AnagMag.CodCS, dbo.AnagMag.CodMag, dbo.AnagMag.DescMag, dbo.Blocchi.IdxBlocco, dbo.Blocchi.CodBlocco, 
				dbo.Blocchi.NumX, dbo.Blocchi.NumY, dbo.Blocchi.NumZ

RETURN
go

/***************************************
* STORED stp_ParticolariOverview
*
* restituisce solo la riga con il num max di celle DI UN DATO magazzino
*
* Steamware, S.E.L.
* mod: 2010.06.02
*
****************************************/
create PROCEDURE stp_magazzinoOverviewByMag_maxCelle
(
	@CodCS VARCHAR(2),
	@CodMag VARCHAR(50)
)
AS

	SELECT		TOP (1) AnagMag.CodCS, AnagMag.CodMag, AnagMag.DescMag, Blocchi.IdxBlocco, Blocchi.CodBlocco, ISNULL(Blocchi.NumX * Blocchi.NumY * Blocchi.NumZ,1) AS TotCelle, 
	            0 AS CelleAttive, 0 AS CelleOccupate, 0 AS CelleLibere, GETDATE() AS Oldest, GETDATE() AS Newest
	FROM        AnagMag INNER JOIN Blocchi ON AnagMag.CodMag = Blocchi.CodMag AND AnagMag.CodCS = Blocchi.CodCS
	WHERE       (AnagMag.CodCS = @CodCS) AND AnagMag.CodMag = @CodMag
	ORDER BY	TotCelle DESC

RETURN
go

/***************************************
* STORED stp_ParticolariOverview
*
* restituisce solo la riga con il num max di celle
*
* Steamware, S.E.L.
* mod: 2010.06.02
*
****************************************/
create PROCEDURE stp_magazzinoOverview_maxCelle
(
	@CodCS VARCHAR(2)
)
AS

	SELECT		TOP (1) AnagMag.CodCS, AnagMag.CodMag, AnagMag.DescMag, Blocchi.IdxBlocco, Blocchi.CodBlocco, ISNULL(Blocchi.NumX * Blocchi.NumY * Blocchi.NumZ,1) AS TotCelle, 
	            0 AS CelleAttive, 0 AS CelleOccupate, 0 AS CelleLibere, GETDATE() AS Oldest, GETDATE() AS Newest
	FROM        AnagMag INNER JOIN Blocchi ON AnagMag.CodMag = Blocchi.CodMag AND AnagMag.CodCS = Blocchi.CodCS
	WHERE       (AnagMag.CodCS = @CodCS)
	ORDER BY	TotCelle DESC

RETURN
go

commit
go





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