-- -- Eventi per update a vers 5.0 (Jetco)
UPDATE LinkMenuJQM
   SET Testo = 'User'
      ,NavigateUrl = '~/User.aspx'
      ,icona = 'fa-user'
 WHERE idxLink=14 OR Testo='Logout'
GO


UPDATE LinkMenuJQM
   SET NavigateUrl = '~/Fermate.aspx'
 WHERE Testo = 'Fermate'
GO

/* TAB Registro controlli */ 
/****** Object:  Table [dbo].[RegistroControlli]    Script Date: 13/03/2018 18:30:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[RegistroControlli](
	[IdxControllo] [int] IDENTITY(1,1) NOT NULL,
	[DataOra] [datetime] NOT NULL,
	[IdxMacchina] [nvarchar](50) NOT NULL,
	[IdxODL] [int] NOT NULL,
	[CodArticolo] [nvarchar](50) NOT NULL,
	[MatrOpr] [int] NOT NULL,
	[EsitoOK] [bit] NOT NULL,
	[Note] [nvarchar](250) NOT NULL,
 CONSTRAINT [PK_RegistroControlli] PRIMARY KEY CLUSTERED 
(
	[IdxControllo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[RegistroControlli] ADD  CONSTRAINT [DF_RegistroControlli_CodArticolo]  DEFAULT ('-') FOR [CodArticolo]
GO

ALTER TABLE [dbo].[RegistroControlli] ADD  CONSTRAINT [DF_RegistroControlli_EsitoOK]  DEFAULT ((0)) FOR [EsitoOK]
GO

ALTER TABLE [dbo].[RegistroControlli] ADD  CONSTRAINT [DF_RegistroControlli_Note]  DEFAULT ('') FOR [Note]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'matricola dell''approvatore del record' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RegistroControlli', @level2type=N'COLUMN',@level2name=N'MatrOpr'
GO




/*************************************
* STORED PROCEDURE stp_RC_getByFilt
* 
* modif.:   Steamware
* modif:    2018.03.12 recupero elenco controlli filtrato
**************************************/
CREATE PROCEDURE [dbo].[stp_RC_getByFilt]
( 
    @IdxMacchina    NVARCHAR(50)	 = 0,
    @DataFrom	     DATETIME = NULL,
    @DataTo	     DATETIME = NULL
)
AS
BEGIN

    SET NOCOUNT ON;
    SET XACT_ABORT ON;
    
    IF @DataFrom IS NULL SET @DataFrom = DATEADD(YEAR, -1,GETDATE()) 
    IF @DataTo IS NULL SET @DataTo = GETDATE() 
    
    -- recupero da tab ODL il codice odl stesso e l'articolo data macchina e data/ora...
    SELECT  *
    FROM	  v_RegistroControlli
    WHERE	  IdxMacchina = CASE WHEN @IdxMacchina = 0 THEN IdxMacchina ELSE @IdxMacchina END
	   AND DataOra BETWEEN @DataFrom AND @DataTo
    ORDER BY	 DataOra DESC
	   
END
GO

/*************************************
* STORED PROCEDURE stp_RC_insert
*
*     registra i controlli di produzione OK / KO (con note)
* 
* 
* modif.:   Steamware
* modif:    2018.03.12 gestione x Jetco controlli     
**************************************/
CREATE PROCEDURE [dbo].[stp_RC_insert]
( 
    @IdxMacchina    NVARCHAR(50),
    @MatrOpr        INT,
    @EsitoOk		BIT,
    @Note			NVARCHAR(50),
    @DataOra	     DATETIME = NULL
)
AS
BEGIN

    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    IF @DataOra IS NULL SET @DataOra = GETDATE() 
    -- imposto valori accessori
    DECLARE @IdxODL		   INT = 0
    DECLARE @CodArticolo	   NVARCHAR(50) = 'ND'

    -- recupero da tab ODL il codice odl stesso e l'articolo data macchina e data/ora...
    SELECT  @IdxODL = ISNULL(IdxODL,0), @CodArticolo = ISNULL(CodArticolo,'-')
    FROM	  ODL
    WHERE	  IdxMacchina = @IdxMacchina
	   AND DataInizio <= @DataOra
	   AND ISNULL(DataFine, GETDATE()) >= @DataOra
    -- verifica parametri...
    --SELECT @DataOra, @IdxMacchina, @IdxODL, @CodArticolo, @MatrOpr, @EsitoOk, @Note

    -- insert valori!
    INSERT INTO RegistroControlli (DataOra,IdxMacchina,IdxODL,CodArticolo,MatrOpr,EsitoOK,Note)
    VALUES (@DataOra, @IdxMacchina, @IdxODL, @CodArticolo, @MatrOpr, @EsitoOk, @Note)

END
GO



/****** Object:  StoredProcedure [dbo].[stp_vsODL_getUnused]    Script Date: 15/03/2018 17:07:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*************************************
* STORED PROCEDURE stp_vsODL_getUnused
* 	ottiene elenco ODL non ancora impiegati per macchina (se IdxMacchina <> 0)

** modif.:	S.E.L. - 2018.03.15
**************************************/
ALTER PROCEDURE [dbo].[stp_vsODL_getUnused]
(
	@IdxMacchina	NVARCHAR(50) = '0'
)
AS

WITH cteODL AS (
	SELECT   dbo.ODL.IdxODL AS value
		,CASE WHEN @IdxMacchina <> '0' THEN '' ELSE m.Nome	+ ' | ' END 
				+ 'ODL ' + CAST(dbo.ODL.IdxODL AS NVARCHAR(50)) 
				+ ' - art.' + dbo.ODL.CodArticolo + ' ' + aa.DescArticolo
				+ ' | ' + CAST(dbo.ODL.NumPezzi AS NVARCHAR(10)) 
				+ ' pz | TC: ' + CAST(CAST(dbo.f_TC_100_60(dbo.ODL.TCAssegnato) AS DECIMAL(9, 2)) AS NVARCHAR(10)) 
				+ ' (min.sec) | ' + CAST(CAST(dbo.ODL.TCAssegnato AS DECIMAL(9, 3)) AS NVARCHAR(10)) 
				+ ' (min.cent)' AS label
		,dbo.ODL.DataInizio AS conditio
		,ODL.IdxMacchina
	FROM    dbo.ODL 
		INNER JOIN dbo.Macchine m ON dbo.ODL.IdxMacchina = m.IdxMacchina
		INNER JOIN dbo.AnagArticoli aa ON dbo.ODL.CodArticolo = aa.CodArticolo
)

SELECT 0 as value, 'Selezionare ODL' as label, NULL as conditio

UNION

SELECT	value, label, conditio
FROM	cteODL
WHERE	conditio IS NULL
	AND	IdxMacchina = CASE WHEN @IdxMacchina <> '0' THEN @IdxMacchina ELSE IdxMacchina END

RETURN
GO