
/***************************************
* STORED stp_MagFifoByParticolare
*
* restituisce elenco giacenze a magazzino in logica FIFO per un dato particolare
*
* Steamware, S.E.L.
* mod: 2012.10.01
*
****************************************/
create PROCEDURE stp_MagFifoByParticolare
(
	@CodCS VARCHAR(2),
	@Particolare VARCHAR(20)
)
AS

	SELECT	      b.IdxBlocco, b.CodBlocco, b.DescBlocco, ec.Particolare
	            , COUNT(puc.UDC) AS NumUDC
				, ISNULL(MIN(puc.DataRif) , GETDATE()) AS Oldest
				, ISNULL(MAX(puc.DataRif) , GETDATE()) AS Newest
				, SUM(ec.Qta) AS QtaTotPz
	FROM		AnagMag am 
	INNER JOIN Blocchi b ON am.CodMag = b.CodMag AND am.CodCS = b.CodCS 
	INNER JOIN Celle c 
	INNER JOIN ElencoCartellini ec 
	LEFT OUTER JOIN PosizioneUdcCorrente puc ON ec.UDC = puc.UDC ON c.IdxCella = puc.IdxCella ON b.IdxBlocco = c.IdxBlocco 
	INNER JOIN  AnagStatiProdotto asp ON ec.CodStato = asp.CodStato
	WHERE		(c.Attiva = 1) 
	            AND b.CodMag <> 'VMLP' 
	            AND (am.CodCS = @CodCS) 
	            AND (ec.Particolare = @Particolare)
	GROUP BY	b.IdxBlocco, b.CodBlocco, b.DescBlocco, ec.Particolare
	ORDER BY	Oldest ASC
		
RETURN


