SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[v_selMag]
AS
SELECT TOP ( SELECT COUNT(*) * 100 FROM dbo.AnagMag ) 
	 CodMag AS value, DescMag AS label, CodCS AS conditio
FROM dbo.AnagMag
ORDER BY label

GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[v_RapQualSunto]
AS
SELECT  TOP ( SELECT COUNT(*) * 100 FROM RilPro.RapQual ) CAST(nRapQual AS NVARCHAR(20)) AS nRapQual, DataRapQual, CodFor, CodLega, SUM(Qta) AS Qta, BenesQual, COUNT(*) AS UdcTot, 
        COUNT(UDC) AS UdcAssoc, LegaScaric
FROM  RilPro.RapQual
GROUP BY nRapQual, DataRapQual, CodFor, CodLega, BenesQual, LegaScaric
ORDER BY nRapQual

GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[V_elencoListePrelievo]
AS
SELECT  TOP ( SELECT COUNT(*) * 100 FROM dbo.v_righePrelevate )
                         dbo.ElencoListePrelievo.CodLista, dbo.ElencoListePrelievo.CodTipoLista, dbo.ElencoListePrelievo.CodCS, dbo.ElencoListePrelievo.DataCreaz, 
                         dbo.ElencoListePrelievo.CodCliente, dbo.ElencoListePrelievo.RagSociale, dbo.ElencoListePrelievo.CodSoggetto, dbo.ElencoListePrelievo.Particolare, 
                         dbo.ElencoListePrelievo.DescParticolare, dbo.ElencoListePrelievo.DisegnoGrezzo, dbo.ElencoListePrelievo.Esponente, dbo.ElencoListePrelievo.Figura, 
                         dbo.ElencoListePrelievo.CodImballo, dbo.ElencoListePrelievo.QtaTot, dbo.ElencoListePrelievo.CodStatoLista, ISNULL(dbo.v_righePrelevate.TotQta, 0) AS Prelevato, 
                         dbo.ElencoListePrelievo.Destinatario, ISNULL(dbo.ElencoListePrelievo.DestinatarioDescrizione, N'nd') AS DestinatarioDescrizione
FROM            dbo.ElencoListePrelievo LEFT OUTER JOIN
                         dbo.v_righePrelevate ON dbo.ElencoListePrelievo.CodLista = dbo.v_righePrelevate.CodLista
ORDER BY dbo.ElencoListePrelievo.DataCreaz DESC

GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



ALTER VIEW [dbo].[v_selMagLogico]
AS
SELECT DISTINCT TOP ( SELECT COUNT(*) * 100 FROM dbo.ElencoCartellini ) 
                CAST(ec.IdxPosizione AS NVARCHAR(20)) AS value, 'Mag. ' + CAST(ec.IdxPosizione AS NVARCHAR(20)) AS label, ec.CodCS AS conditio
FROM            dbo.ElencoCartellini AS ec INNER JOIN
                dbo.AnagMag AS am ON CAST(ec.IdxPosizione AS NVARCHAR(50)) = am.CodMag
WHERE        (ec.IdxPosizione > 0)
ORDER BY label

GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[V_MagazziniLogiciOverview]
AS
SELECT     TOP (SELECT COUNT(*) * 1000 FROM  ElencoCartellini) 
            el.CodCS, anpo.IdxPosizione AS CodMagLogico, anpo.DescPosizione AS DescMagLogico, Blocchi.IdxBlocco, AnagMag.CodMag, 
            Blocchi.CodBlocco, COUNT(el.UDC) AS CelleOccupate, ISNULL(MIN(PosUdcCorr.DataRif), GETDATE()) AS Oldest, ISNULL(MAX(PosUdcCorr.DataRif), GETDATE()) AS Newest
FROM        Celle 
			INNER JOIN Blocchi 
			INNER JOIN AnagMag ON Blocchi.CodMag = AnagMag.CodMag AND Blocchi.CodCS = AnagMag.CodCS ON Celle.IdxBlocco = Blocchi.IdxBlocco 
			INNER JOIN ElencoCartellini AS el 
            INNER JOIN PosizioneUdcCorrente AS PosUdcCorr ON el.UDC = PosUdcCorr.UDC 
            INNER JOIN  AnagPosizioni AS anpo ON el.IdxPosizione = anpo.IdxPosizione ON Celle.IdxCella = PosUdcCorr.IdxCella
GROUP BY el.CodCS, anpo.IdxPosizione, anpo.DescPosizione, Blocchi.IdxBlocco, Blocchi.CodBlocco, AnagMag.CodMag
ORDER BY CodMagLogico, Blocchi.CodBlocco

GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[v_righeListePrelievoPosizione]
AS
SELECT      TOP (SELECT COUNT(*) * 1000 FROM  dbo.RigheListePrelievo ) 
			dbo.RigheListePrelievo.CodLista, dbo.RigheListePrelievo.UDC, dbo.RigheListePrelievo.Qta, dbo.RigheListePrelievo.Proposto, 
                         dbo.RigheListePrelievo.Prelevato, ISNULL(dbo.Celle.CodCella, N'') AS CodCella, ISNULL(dbo.Blocchi.CodMag, N'') AS CodMag, ISNULL(dbo.Blocchi.CodBlocco, N'') 
                         AS CodBlocco, ISNULL(dbo.Celle.X, 0) AS X, ISNULL(dbo.Celle.Y, 0) AS Y, ISNULL(dbo.Celle.Z, 0) AS Z
FROM            dbo.Celle INNER JOIN
                         dbo.PosizioneUdcCorrente ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella INNER JOIN
                         dbo.Blocchi ON dbo.Celle.IdxBlocco = dbo.Blocchi.IdxBlocco RIGHT OUTER JOIN
                         dbo.RigheListePrelievo ON dbo.PosizioneUdcCorrente.UDC = dbo.RigheListePrelievo.UDC
ORDER BY dbo.RigheListePrelievo.CodLista DESC

GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [RilPro].[vDatiLineeGiornata]
AS
  -- Non tiene conto del Turno ma prende tutti gli stampi della giornata
  WITH  cteUltStampo
          AS (
               SELECT CodStampo
                     ,InizioValidita
                     ,ProgFigure
                     ,FineValidita
                     ,NumCommessa
                     ,NumFigure
                     ,CodCliente
                     ,CodArticolo
                     ,Particolare
               FROM   RilPro.StampoArticolo AS A
               WHERE  ( GETDATE() BETWEEN InizioValidita AND FineValidita )
             ),
        cteEspAttivi
          AS (
               SELECT s.CodStampo
                     ,s.Esponente
                     ,s.EspAttivo
                     ,s.DataInserim
                     ,s.DataValidita
               FROM   RilPro.StampoEsponente AS s
               INNER JOIN (
                            SELECT  CodStampo
                                   ,MAX(DataValidita) AS DataValidita
                            FROM    RilPro.StampoEsponente
                            WHERE   ( DataValidita <= GETDATE() )
                                    AND ( EspAttivo = 'S' )
                            GROUP BY CodStampo
                          ) AS s2
               ON     s.CodStampo = s2.CodStampo
                      AND s.DataValidita = s2.DataValidita
             ),
        cteLineeAttive
          AS (
               SELECT CodImpianto
                     ,CodStampo
                     ,NumStampate
                     ,CONVERT(DATETIME, DataInizio) AS DataInizio
                     ,DataFine
                     ,TurnoInizio
                     ,TurnoFine
               FROM   RilPro.LineaStampi
               WHERE  ( DataFine = '99991231' )
             )
  SELECT TOP ( SELECT COUNT(*) * 1000 FROM cteLineeAttive )    -- TOP 100 PERCENT non va bene 
          l.CodImpianto
         ,l.CodStampo
         ,l.NumStampate
         ,l.DataInizio
         ,l.TurnoInizio
         ,g.InizioValidita
         ,g.FineValidita
         ,g.NumFigure
         ,CAST(g.CodCliente AS varchar(50)) AS CodCliente
         ,cl.RagSociale
         ,CAST(g.CodArticolo AS varchar(50)) AS CodArticolo
         ,g.Particolare
         ,pa.DescParticolare
         ,pa.DisegnoGrezzo
         ,pa.CodFamiglia
         ,fa.DescFamiglia
         ,fi.Figura
         ,es.Esponente
  FROM    cteLineeAttive AS l
  INNER JOIN cteUltStampo AS g
  ON      l.CodStampo = g.CodStampo
  LEFT OUTER JOIN RilPro.StampoFigure AS fi
  ON      l.CodStampo = fi.CodStampo
  LEFT OUTER JOIN cteEspAttivi AS es
  ON      g.CodStampo = es.CodStampo
  LEFT OUTER JOIN RilPro.AnagParticolari AS pa
  ON      g.Particolare = pa.Particolare
  LEFT OUTER JOIN RilPro.AnagFamiglie AS fa
  ON      pa.CodFamiglia = fa.CodFamiglia
  LEFT OUTER JOIN RilPro.AnagClienti AS cl
  ON      g.CodCliente = cl.CodCliente
  ORDER BY l.CodImpianto

GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[v_selPeriodiTrad]
AS
SELECT   TOP ( SELECT COUNT(*) * 1000 FROM dbo.AnagPeriodi ) 
         dbo.AnagPeriodi.codPeriodo AS value, dbo.v_vocabolario.Traduzione AS label, dbo.v_vocabolario.Lingua AS conditio
FROM            dbo.AnagPeriodi INNER JOIN
                         dbo.v_vocabolario ON dbo.AnagPeriodi.lemmaPeriodo = dbo.v_vocabolario.Lemma collate Latin1_General_CI_AS
ORDER BY label

GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[V_MagazziniOverview]
AS
SELECT        TOP ( SELECT COUNT(*) * 1000 FROM dbo.Blocchi )
						dbo.AnagMag.CodCS, dbo.AnagMag.CodMag, dbo.AnagMag.DescMag, dbo.Blocchi.IdxBlocco, dbo.Blocchi.CodBlocco, dbo.Blocchi.DescBlocco, 
                         dbo.TipoCella.Capienza * dbo.Blocchi.NumX * dbo.Blocchi.NumY * dbo.Blocchi.NumZ AS TotCelle, dbo.TipoCella.Capienza * COUNT(DISTINCT dbo.Celle.IdxCella) 
                         AS CelleAttive, COUNT(dbo.PosizioneUdcCorrente.UDC) AS CelleOccupate, dbo.TipoCella.Capienza * COUNT(DISTINCT 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.TipoCella INNER JOIN
                         dbo.AnagMag INNER JOIN
                         dbo.Blocchi ON dbo.AnagMag.CodMag = dbo.Blocchi.CodMag AND dbo.AnagMag.CodCS = dbo.Blocchi.CodCS ON dbo.TipoCella.CodMag = dbo.AnagMag.CodMag AND
                          dbo.TipoCella.CodCS = dbo.AnagMag.CodCS LEFT OUTER JOIN
                         dbo.Celle ON dbo.TipoCella.IdxTipoCella = dbo.Celle.IdxTipoCella AND dbo.Blocchi.IdxBlocco = dbo.Celle.IdxBlocco LEFT OUTER JOIN
                         dbo.PosizioneUdcCorrente ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella
WHERE        (dbo.Celle.Attiva = 1)
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, dbo.TipoCella.Capienza, dbo.Blocchi.DescBlocco
ORDER BY dbo.AnagMag.CodMag, dbo.Blocchi.CodBlocco

GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



ALTER VIEW [dbo].[v_specParticolari]
AS
SELECT DISTINCT TOP ( SELECT COUNT(*) FROM dbo.ElencoCartellini )  CodCliente, RagSociale, Particolare, DescParticolare, DisegnoGrezzo, Esponente, CodStampo, Figura
FROM            dbo.ElencoCartellini
WHERE        (NOT (Particolare IS NULL)) AND (NOT (DisegnoGrezzo IS NULL))

GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[V_DettMagPart]
AS
SELECT        TOP ( SELECT COUNT(*) * 1000 FROM dbo.ElencoCartellini ) 
						dbo.AnagMag.CodCS, dbo.AnagMag.CodMag, dbo.AnagMag.DescMag, dbo.Blocchi.IdxBlocco, dbo.Blocchi.CodBlocco, 
                         dbo.ElencoCartellini.Particolare, dbo.ElencoCartellini.Esponente, COUNT(dbo.PosizioneUdcCorrente.UDC) AS NumUDC, 
                         ISNULL(MIN(dbo.PosizioneUdcCorrente.DataRif), GETDATE()) AS Oldest, ISNULL(MAX(dbo.PosizioneUdcCorrente.DataRif), GETDATE()) AS Newest, 
                         SUM(dbo.ElencoCartellini.Qta) AS QtaTotPz, dbo.AnagStatiProdotto.DescStato
FROM            dbo.AnagMag INNER JOIN
                         dbo.Blocchi ON dbo.AnagMag.CodMag = dbo.Blocchi.CodMag AND dbo.AnagMag.CodCS = dbo.Blocchi.CodCS INNER JOIN
                         dbo.Celle INNER JOIN
                         dbo.ElencoCartellini INNER JOIN
                         dbo.PosizioneUdcCorrente ON dbo.ElencoCartellini.UDC = dbo.PosizioneUdcCorrente.UDC ON dbo.Celle.IdxCella = dbo.PosizioneUdcCorrente.IdxCella ON 
                         dbo.Blocchi.IdxBlocco = dbo.Celle.IdxBlocco INNER JOIN
                         dbo.AnagStatiProdotto ON dbo.ElencoCartellini.CodStato = dbo.AnagStatiProdotto.CodStato
WHERE        (dbo.Celle.Attiva = 1)
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, dbo.ElencoCartellini.Particolare, dbo.AnagStatiProdotto.DescStato, dbo.ElencoCartellini.Esponente
ORDER BY QtaTotPz DESC

GO








