




/***************************************
* STORED stp_prtCartSterratiByUDC
*
* ottiene il record del Cartellino Sterrati dato l'UDC richiesto x la stampa
*
* Steamware, S.E.L. - G.A.R.
* mod: 2014.01.23
*
****************************************/
CREATE PROCEDURE [dbo].[stp_prtCartSterratiByUDC]
(
	@UDC NVARCHAR(50)
)
AS

    -- estraggo UDC collegati nelle note cmq ( x max 10 )
	DECLARE @tmp		NVARCHAR(500)
	DECLARE @NoteTratt	NVARCHAR(500)
	SET @tmp = ''

    -- estraggo gli UDC parent di quello passato ( x max 10 ) per poi stamparlo nel campo 
    -- NoteTratt che era utilizzato per stampare i trattamenti
    -- prendo solo la parte necessaria dell' UDC per usare meno spazio possibile
	SELECT  TOP 10 @tmp = @tmp + RIGHT(UDC_parent,10) + ' - ' FROM RelazUDC AS r
    INNER JOIN dbo.ElencoCartellini AS e ON r.UDC_parent = e.UDC            -- il parent deve esistere ( escludo i cancellati )
    WHERE r.UDC_child = @UDC
    ORDER BY r.UDC_parent

	SET @NoteTratt = (SELECT SUBSTRING(@tmp, 0, LEN(@tmp)))   -- tolgo ultimo trattino

	-- ora seleziono i dati veri e propri
    SELECT
           e.UDC,
           e.CodCS,
           ISNULL( e.CodCliente,N'') AS      codcliente,
           ISNULL( e.RagSociale,N'') AS      ragsociale,
           ISNULL( e.Particolare,N'') AS     particolare,
           ISNULL( e.DescParticolare,N'') AS DescParticolare,
           ISNULL( e.DisegnoGrezzo,N'') AS   DisegnoGrezzo,
           ISNULL( e.Esponente,N'') AS       esponente,
           ISNULL( e.CodImpianto,N'') AS     CodImpianto,
           ISNULL( e.DescImpianto,N'') AS    DescImpianto,
           ISNULL( e.CodStampo,N'') AS       codstampo,
           ISNULL( e.Figura,N'') AS          figura,
           e.DataFus,
           ISNULL( e.TurnoFus,0) AS          turnofus,
           ISNULL( e.CodImballo,N'') AS      CodImballo,
           ISNULL( e.CodSoggetto,N'') AS     CodSoggetto,
           ISNULL( e.NumCont,0) AS           NumCont,
           ISNULL( e.Tara,0) AS              tara,
           ISNULL( e.Qta,0) AS               qta,
           ISNULL( e.CodStato,N'') AS        CodStato,
           ISNULL( e.IdxPosizione,0) AS      IdxPosizione,
           ISNULL( e.PesoTot,0) AS           PesoTot,
           ISNULL( e.PesoCad,0) AS           PesoCad,
           e.CreateDate,
           e.ModDate,
           ISNULL( o.Cognome,N'') AS         cognome,
           ISNULL( o.Nome,N'') AS            nome,
           ISNULL( sp.DescStato,N'') AS      DescStato,
           ISNULL( i.DescImballo,N'') AS     DescImballo,
           a.CodFamiglia,
           f.DescFamiglia,
           ISNULL( e.Note,N'') AS            Note,
           @NoteTratt AS                     NoteTratt
    FROM ElencoCartellini AS e
    LEFT OUTER JOIN RilPro.AnagParticolari AS a
           ON e.Particolare = a.Particolare
    LEFT OUTER JOIN RilPro.AnagFamiglie AS f
           ON a.CodFamiglia = f.CodFamiglia
    LEFT OUTER JOIN AnagStatiProdotto AS sp
           ON e.CodStato = sp.CodStato
    LEFT OUTER JOIN RilPro.AnagOperatori AS o
           ON e.CodSoggetto = o.CodSoggetto
    LEFT OUTER JOIN RilPro.AnagImballi AS i
           ON e.CodImballo = i.CodImballo
    WHERE e.UDC = @UDC;
 

RETURN



