﻿create table AnagClasseTurno(
  CodClasseTurno nvarchar(50) not null constraint PK_AnagClasseTurno primary key,
  Descrizione    nvarchar(50),
  hSettStd       int
)
go


set xact_abort on
go

begin transaction
go

INSERT INTO dbo.AnagClasseTurno
  VALUES (N'2t5gg', N'2 turni, 5 gg', 80)
INSERT INTO dbo.AnagClasseTurno
  VALUES (N'3t5gg', N'3 turni, 5 gg', 120)
go

commit transaction
go


create table AnagTurni(
  CodTurno nvarchar(50) not null constraint PK_AnagTurni primary key,
  hStart   int,
  hEnd     int,
  descr    nvarchar(50)
)
go


set xact_abort on
go

begin transaction
go

INSERT INTO dbo.AnagTurni
  VALUES (N'T1', 6, 14, N'Turno 8h standard')
INSERT INTO dbo.AnagTurni
  VALUES (N'T2', 14, 22, N'Turno 8h standard')
INSERT INTO dbo.AnagTurni
  VALUES (N'T3', 22, 6, N'Turno 8h standard')
INSERT INTO dbo.AnagTurni
  VALUES (N'TA', 0, 6, N'Turno 6h std')
INSERT INTO dbo.AnagTurni
  VALUES (N'TB', 6, 12, N'Turno 6h std')
INSERT INTO dbo.AnagTurni
  VALUES (N'TC', 12, 18, N'Turno 6h std')
INSERT INTO dbo.AnagTurni
  VALUES (N'TD', 18, 24, N'Turno 6h std')
go

commit transaction
go


drop table OreAperturaSett
go


create table OreAperturaSettTurni(
  CodClasseTurno nvarchar(50) not null,
  CodTurno       nvarchar(50) not null,
  giorno         int          not null,
  descrGiorno    nvarchar(50),

  constraint PK_OreAperturaSett primary key(CodClasseTurno,giorno,CodTurno)
)
go


set xact_abort on
go

begin transaction
go

INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T1', 1, N'Lun')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T1', 2, N'Mar')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T1', 3, N'Mer')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T1', 4, N'Gio')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T1', 5, N'Ven')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T2', 1, N'Lun')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T2', 2, N'Mar')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T2', 3, N'Mer')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T2', 4, N'Gio')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'2t5gg', N'T2', 5, N'Ven')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T1', 1, N'Lun')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T1', 2, N'Mar')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T1', 3, N'Mer')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T1', 4, N'Gio')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T1', 5, N'Ven')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T2', 1, N'Lun')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T2', 2, N'Mar')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T2', 3, N'Mer')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T2', 4, N'Gio')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T2', 5, N'Ven')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T3', 1, N'Lun')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T3', 2, N'Mar')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T3', 3, N'Mer')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T3', 4, N'Gio')
INSERT INTO dbo.OreAperturaSettTurni
  VALUES (N'3t5gg', N'T3', 5, N'Ven')
go

commit transaction
go


create table OreChiusuraSett(
  giorno      int          not null constraint PK_OreAperturaSett_1 primary key,
  descrGiorno nvarchar(50),
  chiusura    int
)
go


set xact_abort on
go

begin transaction
go

INSERT INTO dbo.OreChiusuraSett
  VALUES (6, N'Sab', 16)
INSERT INTO dbo.OreChiusuraSett
  VALUES (7, N'Dom', 24)
go

commit transaction
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***********************************************************************************
* FUNCTION f_dateOnly
*
* restituisce solo la parte data di un campo datetime (ovvero a mezzanotte...)
*
***********************************************************************************/
create FUNCTION f_dateOnly
(
	@dateIn		DATETIME
)
RETURNS DATETIME
AS
	BEGIN
	RETURN dateadd(dd, -datediff(dd, @dateIn, 1), 1) 
	END
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/***********************************************************************************
* FUNCTION f_diffOreLavorative
*
* calcola le effettive ore lavorative sottraendo chiusure x festività o aperture programmate
*
***********************************************************************************/
create FUNCTION f_diffOreLavorative
(
	@dataFrom		DATETIME,
	@dataTo			DATETIME,
	@idxMacchina	INT -- per ora non lo considero poiché non ci sono calendari diversi x macchina...
)
RETURNS FLOAT
AS
	BEGIN
	
		-- dichiarazioni variabili
		DECLARE @oreTot		AS FLOAT
		DECLARE @oreOff		AS FLOAT
		DECLARE @tmp_dates TABLE (DateValue DATETIME, h INT)
		SET		@oreTot = 0
		
		-- controllo date coerenti
		IF(@dataTo > @dataFrom)
			BEGIN
				-- calcolo totale "raw" delle ore
				SET		@oreTot		= CAST(ISNULL(DATEDIFF(minute, @dataFrom, @dataTo), 0) AS float) / 60;
				-- definisco tabella cte ricorsiva
				WITH mycte AS
				(
					SELECT CAST(dbo.f_dateOnly(DATEADD(dd,1,@dataFROM)) AS DATETIME) DateValue
					UNION ALL
					SELECT	DateValue + 1
					FROM    mycte   
					WHERE   DateValue + 1 < dbo.f_dateOnly(@dataTo)
				)
				-- carico nella tab temporanea i sabati e le domeniche...
				INSERT into @tmp_dates(DateValue, h)
				SELECT *, CASE ((DATEPART(dw, DateValue) + @@DATEFIRST) % 7) WHEN 0 THEN 16 ELSE 24 END AS h
				FROM    mycte
				WHERE ((DATEPART(dw, DateValue) + @@DATEFIRST) % 7) IN (0,1)
				OPTION (MAXRECURSION 0)	
				-- carico le festività per le date NON ancora presenti
				insert into @tmp_dates(DateValue, h)
				select data as DateValue, 24 as h--, descrizione
				from CalendFesteFerie
				where data between @dataFrom and @dataTo and data not in (select distinct DateValue from @tmp_dates)
				-- aggiorno sabati/domenica che fossero festivi (ipotesi che hfest > h sab/dom...
				UPDATE @tmp_dates
				SET h=24
				WHERE DateValue in (SELECT data FROM CalendFesteFerie WHERE data between @dataFrom and @dataTo)
				
				-- calcolo INFINE le ore off!
				SET @oreOff = (SELECT ISNULL(SUM(h),0) FROM @tmp_dates)
			END
		-- setto le nuove ore totali... se ho un numero di ore > delle fermate le sottraggo per evitare ore engative...
		IF(@oreTot > @oreOff)
			BEGIN
				SET @oreTot = @oreTot - @oreOff
			END
			
		RETURN @oreTot
	END
go

commit
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

alter VIEW v_elencoIntervFilt
AS
SELECT        TOP (100) PERCENT dbo.InterventiMtz.numIntMtz, dbo.InterventiMtz.richiesta, dbo.AnagImpianti.codImpianto, dbo.AnagImpianti.nomeImpianto, 
                         dbo.AnagMacchine.codMacchina, dbo.AnagMacchine.nomeMacchina, dbo.InterventiMtz.descrizione, dbo.InterventiMtz.inizioIntervento, 
                         dbo.InterventiMtz.fineIntervento, ISNULL(dbo.InterventiMtz.descrizioneIntervento, 'n.d. (report mtz)') AS descrizioneIntervento, 
                         ISNULL(dbo.AnagCausali.descrCausale, 'n.d. (causale)') AS descrCausale, ROUND(CAST(ISNULL(dbo.v_interventiErogati.totMinErogati, 0) AS float) / 60, 2) 
                         AS minErogati, dbo.f_diffOreLavorative(dbo.InterventiMtz.inizioIntervento, dbo.InterventiMtz.fineIntervento, dbo.InterventiMtz.idxMacchina) AS durataMtz, 
                         CASE (isFermo) WHEN 1 THEN dbo.f_diffOreLavorative(dbo.InterventiMtz.guasto, dbo.InterventiMtz.fineIntervento, InterventiMtz.idxMacchina) 
                         WHEN 0 THEN dbo.f_diffOreLavorative(dbo.InterventiMtz.inizioIntervento, dbo.InterventiMtz.fineIntervento, InterventiMtz.idxMacchina) END AS durataOff, 
                         dbo.InterventiMtz.guasto, dbo.InterventiMtz.idxStato, dbo.InterventiMtz.idxImpianto, dbo.InterventiMtz.idxMacchina, dbo.InterventiMtz.isFermo, 
                         dbo.InterventiMtz.scheduled
FROM            dbo.InterventiMtz INNER JOIN
                         dbo.AnagAmbitoGuasto ON dbo.InterventiMtz.idxAmbito = dbo.AnagAmbitoGuasto.idxAmbito INNER JOIN
                         dbo.AnagImpianti ON dbo.InterventiMtz.idxImpianto = dbo.AnagImpianti.idxImpianto INNER JOIN
                         dbo.AnagMacchine ON dbo.InterventiMtz.idxMacchina = dbo.AnagMacchine.idxMacchina INNER JOIN
                         dbo.AnagTipoGuasto ON dbo.InterventiMtz.idxTipo = dbo.AnagTipoGuasto.idxTipo INNER JOIN
                         dbo.AnagPriorita ON dbo.InterventiMtz.idxPriorita = dbo.AnagPriorita.idxPriorita INNER JOIN
                         dbo.AnagStati ON dbo.InterventiMtz.idxStato = dbo.AnagStati.idxStato LEFT OUTER JOIN
                         dbo.v_interventiErogati ON dbo.InterventiMtz.numIntMtz = dbo.v_interventiErogati.numIntMtz LEFT OUTER JOIN
                         dbo.AnagCausali ON dbo.InterventiMtz.idxCausale = dbo.AnagCausali.idxCausale
ORDER BY dbo.InterventiMtz.numIntMtz DESC
go

exec sp_updateextendedproperty 'MS_DiagramPane1', '[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "AnagAmbitoGuasto"
            Begin Extent = 
               Top = 11
               Left = 82
               Bottom = 89
               Right = 255
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagImpianti"
            Begin Extent = 
               Top = 123
               Left = 219
               Bottom = 231
               Right = 370
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagMacchine"
            Begin Extent = 
               Top = 190
               Left = 826
               Bottom = 298
               Right = 977
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagTipoGuasto"
            Begin Extent = 
               Top = 90
               Left = 1143
               Bottom = 168
               Right = 1294
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagPriorita"
            Begin Extent = 
               Top = 23
               Left = 958
               Bottom = 116
               Right = 1109
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagStati"
            Begin Extent = 
               Top = 263
               Left = 90
               Bottom = 356
               Right = 241
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "v_interventiErogati"
            Begin Extent = 
               Top = 3
               Left = 739
               Bottom = 81
               Right = 890
  ', 'SCHEMA', 'dbo', 'VIEW', 'v_elencoIntervFilt'
go

exec sp_updateextendedproperty 'MS_DiagramPane2', '          End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "AnagCausali"
            Begin Extent = 
               Top = 291
               Left = 1011
               Bottom = 369
               Right = 1162
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "InterventiMtz"
            Begin Extent = 
               Top = 8
               Left = 486
               Bottom = 354
               Right = 671
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 30
         Width = 284
         Width = 915
         Width = 1815
         Width = 690
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1665
         Width = 1665
         Width = 1500
         Width = 1500
         Width = 1050
         Width = 1020
         Width = 1125
         Width = 1815
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', 'dbo', 'VIEW', 'v_elencoIntervFilt'
go

commit
go


create table OreAperturaSett(
  giorno      int          not null constraint PK_OreAperturaSett_2 primary key,
  descrGiorno nvarchar(50),
  apertura    int
)
go


set xact_abort on
go

begin transaction
go

INSERT INTO dbo.OreAperturaSett
  VALUES (0, N'Dom', 0)
INSERT INTO dbo.OreAperturaSett
  VALUES (1, N'Lun', 24)
INSERT INTO dbo.OreAperturaSett
  VALUES (2, N'Mar', 24)
INSERT INTO dbo.OreAperturaSett
  VALUES (3, N'Mer', 24)
INSERT INTO dbo.OreAperturaSett
  VALUES (4, N'Gio', 24)
INSERT INTO dbo.OreAperturaSett
  VALUES (5, N'Ven', 24)
INSERT INTO dbo.OreAperturaSett
  VALUES (6, N'Sab', 8)
go

commit transaction
go


set xact_abort on
go

begin transaction
go

set ANSI_NULLS on
go

/*************************************
* STORED PROCEDURE stp_oreEquivalenti
* 	calcola le ore di apertura equivalenti x una data macchina e periodo
*
* modif.:	S.E.L. - 2011.06.29
**************************************/
create PROCEDURE stp_oreEquivalenti
(
	@dataFrom		DATETIME,
	@dataTo			DATETIME,
	@idxMacchina	INT
)
AS

SELECT dbo.f_diffOreLavorative(@dataFrom, @dataTo, @idxMacchina) AS apertura

RETURN
go

commit
go





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