Files
2021-03-26 17:17:28 +01:00

300 lines
8.7 KiB
Transact-SQL

set xact_abort on;
go
begin transaction;
go
set ANSI_NULLS on;
go
/**********************************************************
* STORED stp_import_SpsDataRaw
*
* importa tracciato dati SPS x utente e date indicate
*
* mod: S.E.L. 2012.11.20
*
**********************************************************/
alter PROCEDURE import.stp_import_SpsDataRaw
(
@inizio DATETIME,
@fine DATETIME
)
AS
/**************************************************
* inizio importando eventuali clienti...
**************************************************/
;WITH myCteClienti AS
(
select * from ZZZ_SPS_AnagClienti
)
INSERT INTO AnagClienti(RagSociale, indirizzo, cap, citta, prov, nota, oldIdx, Attivo, codExt)
SELECT cte.RagSoc, cte.Indirizzo, cte.cap, cte.citta, cte.prov, cte.codCliExt +' - ' + cte.RagSoc, cte.idxCli, 1,cte.codCliExt
FROM AnagClienti anag
RIGHT OUTER JOIN myCteClienti cte ON anag.RagSociale = cte.RagSoc
WHERE anag.idxCliente IS NULL
/**************************************************
* importo eventuali progetti
**************************************************/
;WITH myCteProgetti AS
(
SELECT * FROM ZZZ_SPS_AnagCommesse
)
INSERT INTO AnagProgetti(idxCliente, nomeProj, descrProj, budgetTime, Attivo, codExt)
SELECT cli.idxCliente, cte.codExt, cte.Descrizione + ' ['+ISNULL(cte.Codice_Disegno,' - ')+']', cte.BudgetOre, 1, cte.codExt
FROM AnagProgetti anag
RIGHT OUTER JOIN myCteProgetti cte ON anag.nomeProj = cte.codExt INNER JOIN
AnagClienti Cli ON Cli.codExt = RIGHT(cte.codExt,3)
WHERE anag.idxProgetto IS NULL
/**************************************************
* importo eventuali fasi
**************************************************/
;WITH myCteFasi AS
(
SELECT DISTINCT codCom, Fase FROM zzz_rawData_SPS
)
INSERT INTO AnagFasi(idxProgetto, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
SELECT Pro.idxProgetto, '', cte.Fase, cte.Fase, 1, 0, 1
FROM myCteFasi cte INNER JOIN AnagProgetti Pro ON cte.codCom = Pro.codExt
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.Fase
WHERE anag.idxFase IS NULL
/**************************************************
* importo eventuali sottofasi
**************************************************/
;WITH myCteSubFasi AS
(
SELECT DISTINCT codCom, Fase, attivita FROM zzz_rawData_SPS
)
INSERT INTO AnagFasi(idxProgetto, idxFaseAncest, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
SELECT Pro.idxProgetto, Fas.idxFase as idxFaseAncest, '', cte.attivita, cte.attivita, 1, 0, 1
FROM myCteSubFasi cte
INNER JOIN AnagProgetti Pro ON cte.codCom = Pro.codExt
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.Fase AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.attivita AND anag.idxFaseAncest = Fas.idxFase
WHERE anag.idxFaseAncest IS NULL
/**************************************************
* update cognome nome dipendenti
**************************************************/
UPDATE Dipendenti
SET codDipendenteExt = Nome + ' ' + Cognome
UPDATE Dipendenti
SET codDipendenteExt='Bohdan Khomonchak'
WHERE cognome='Khomonchak'
/**************************************************
* importo records!
**************************************************/
-- faccio un sort x dipendente-giorno x accodare i records
-- faccio un sort x dipendente-giorno x accodare i records
;WITH myCteStep1 AS
(
SELECT CognomeNome
,Data
, ROW_NUMBER() OVER(PARTITION BY CognomeNome,Data ORDER BY CognomeNome,Data) as riga
, codCom
, Fase
, attivita
, durata*60 as minuti
, descrizione
FROM zzz_rawData_SPS
)
, myCteStep2 AS
(
SELECT *
, DATEADD(MINUTE,480+120*(riga-1),Data) AS Inizio
, DATEADD(MINUTE,480+120*(riga-1)+minuti,Data) AS Fine
FROM myCteStep1
)
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione)
SELECT Dip.idxDipendente, SFas.idxFase, cte.Inizio,cte.Fine, ISNULL(cte.Descrizione,'-')
FROM myCteStep2 cte
INNER JOIN Dipendenti Dip ON cte.CognomeNome = dip.codDipendenteExt
INNER JOIN AnagProgetti Pro ON cte.codCom = Pro.codExt -- and pro.idxCliente=cli.idxCliente
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.Fase AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
INNER JOIN AnagFasi SFas ON SFas.nomeFase = cte.attivita AND SFas.idxProgetto = Pro.idxProgetto AND SFas.idxFaseAncest=fas.idxFase
LEFT OUTER JOIN RegAttivita dati ON dati.inizio = cte.Inizio AND dati.fine = cte.Fine AND dati.descrizione = cte.Descrizione AND dati.idxDipendente = Dip.idxDipendente
WHERE dati.idxRA IS NULL
--- forzo ricalcolo attività expl!
EXEC stp_ricalcolaRegAttivitaExpl_byPeriodoUser 0, '20120101', '20130131'
RETURN
go
commit;
go
truncate table regattivita
-- faccio un sort x dipendente-giorno x accodare i records
;WITH myCteStep1 AS
(
SELECT CognomeNome
,Data
, ROW_NUMBER() OVER(PARTITION BY CognomeNome,Data ORDER BY CognomeNome,Data) as riga
, codCom
, Fase
, attivita
, durata*60 as minuti
, descrizione
FROM zzz_rawData_SPS
)
, myCteStep2 AS
(
SELECT *
, DATEADD(MINUTE,480+120*(riga-1),Data) AS Inizio
, DATEADD(MINUTE,480+120*(riga-1)+minuti,Data) AS Fine
FROM myCteStep1
)
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione)
SELECT Dip.idxDipendente, SFas.idxFase, cte.Inizio,cte.Fine, ISNULL(cte.Descrizione,'-')
FROM myCteStep2 cte
INNER JOIN Dipendenti Dip ON cte.CognomeNome = dip.codDipendenteExt
INNER JOIN AnagProgetti Pro ON cte.codCom = Pro.codExt -- and pro.idxCliente=cli.idxCliente
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.Fase AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
INNER JOIN AnagFasi SFas ON SFas.nomeFase = cte.attivita AND SFas.idxProgetto = Pro.idxProgetto AND SFas.idxFaseAncest=fas.idxFase
LEFT OUTER JOIN RegAttivita dati ON dati.inizio = cte.Inizio AND dati.fine = cte.Fine AND dati.descrizione = cte.Descrizione AND dati.idxDipendente = Dip.idxDipendente
WHERE dati.idxRA IS NULL
select * from RegAttivita
select * from RegAttivitaExpl
set xact_abort on;
go
begin transaction;
go
set ANSI_NULLS on;
go
alter TRIGGER trg_RegAtt_upsert
ON RegAttivita
/*----------------------------------
* Trigger su insert/update
*
* Aggiorna la tab dei RegAttivitaExpl
*
* creato da: S.E. Locatelli - 2013.01.21
* ultima modifica:
*----------------------------------*/
FOR INSERT, UPDATE
AS
IF(UPDATE(idxDipendente) OR UPDATE(inizio) OR UPDATE(fine) OR UPDATE(idxFase))
BEGIN
-- dichiarazione variabili
DECLARE @inizio DATETIME
DECLARE @fine DATETIME
DECLARE @idxDipendente INT
-- recupero valori OLD
SELECT @inizio = DATEADD(DAY,-1,CONVERT(DATE,d.inizio)), @fine = DATEADD(DAY,1,CONVERT(DATE,d.inizio)), @idxDipendente = d.idxDipendente FROM deleted d
--chiamo la stored di ricalcolo
EXEC stp_ricalcolaRegAttivitaExpl_byPeriodoUser @idxDipendente, @inizio, @fine
-- recupero valori NEW
SELECT @inizio = DATEADD(DAY,-1,CONVERT(DATE,i.inizio)), @fine = DATEADD(DAY,1,CONVERT(DATE,i.inizio)), @idxDipendente = i.idxDipendente FROM inserted i
--chiamo la stored di ricalcolo
EXEC stp_ricalcolaRegAttivitaExpl_byPeriodoUser @idxDipendente, @inizio, @fine
END
go
commit;
go
set xact_abort on;
go
begin transaction;
go
set ANSI_NULLS on;
go
/**********************************************************
* STORED stp_RA_clonaLastRA_Utente
*
* clona un attività utente: se c'è da ultima altrimenti da zero x una certa data
*
* mod: S.E.L. 2013.01.17
*
**********************************************************/
alter PROCEDURE stp_RA_clonaLastRA_Utente
(
@idxDipendente INT,
@dataRif DATETIME -- data x cui creare un record attività
)
AS
-- variabili
DECLARE @idxFase INT
DECLARE @idxRA INT
-- arrotondo data richiesta solo all'ora...
SELECT @dataRif = DATEADD(HOUR, DATEPART(HOUR,@dataRif), CONVERT(DATETIME,(CONVERT(DATE,@dataRif))))
-- cerco se esista ultima reg attività utente...
SELECT @idxRA=ISNULL((SELECT idxRA FROM RegAttivita WHERE idxDipendente = @idxDipendente),0)
-- se non ho trovato records, ovvero idxRA = 0, prendo ultima fase da elenco
IF (@idxRA = 0)
BEGIN
-- calcolo ultima fase inserita...
SELECT TOP 1 @idxFase=ISNULL(idxFase,0) FROM AnagFasi ORDER BY idxFase DESC
-- se trovata fase inserisco!
IF(@idxFase > 0)
BEGIN
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione, importo)
VALUES (@idxDipendente, @idxFase, @dataRif, DATEADD(HOUR,1,@dataRif), '...',0)
END
END
ELSE
BEGIN
-- inserisco su data richeista duplicazione dell'ultima attività utente...
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione, importo)
SELECT idxDipendente, idxFase, @dataRif, DATEADD(minute, DATEDIFF(MINUTE, inizio, fine), @dataRif), descrizione, importo
FROM RegAttivita
WHERE (idxRA = @idxRA)
END
go
commit;
go