496 lines
14 KiB
Transact-SQL
496 lines
14 KiB
Transact-SQL
create table zzz_RawDataPaymo(
|
|
idx int not null identity constraint PK_zzz_RawDataPaymo primary key,
|
|
Cliente nvarchar(250),
|
|
Dipendente nvarchar(250),
|
|
Progetto nvarchar(250),
|
|
TaskList nvarchar(250),
|
|
Task nvarchar(250),
|
|
Inizio datetime,
|
|
Fine datetime,
|
|
Descrizione nvarchar(2500),
|
|
Ore float
|
|
);
|
|
go
|
|
|
|
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table RegAttivita drop
|
|
constraint DF_RegAttivita_money ,
|
|
column money;
|
|
go
|
|
|
|
alter table RegAttivita add
|
|
importo decimal(19,4) constraint DF_RegAttivita_money default ((0));
|
|
go
|
|
|
|
create index ix_idxDip on RegAttivita(idxDipendente)
|
|
include(idxFase,inizio);
|
|
go
|
|
|
|
create index ix_idxFase on RegAttivita(idxFase);
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_RA_updateQuery
|
|
*
|
|
* update attività
|
|
*
|
|
* mod: S.E.L. 2012.11.12
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_RA_updateQuery
|
|
(
|
|
@idxDipendente int,
|
|
@idxFase int,
|
|
@inizio datetime,
|
|
@fine datetime,
|
|
@descrizione nvarchar(500),
|
|
@importo decimal(19, 4),
|
|
@Original_idxRA int
|
|
)
|
|
AS
|
|
SET NOCOUNT OFF;
|
|
UPDATE RegAttivita
|
|
SET idxDipendente = @idxDipendente, idxFase = @idxFase, inizio = @inizio, fine = @fine, descrizione = @descrizione, importo = @importo
|
|
WHERE (idxRA = @Original_idxRA);
|
|
|
|
SELECT idxRA, idxDipendente, idxFase, inizio, fine, descrizione, oreTot, importo FROM RegAttivita WHERE (idxRA = @Original_idxRA)
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_RA_getByKey
|
|
*
|
|
* recupera elenco attività da chiave
|
|
*
|
|
* mod: S.E.L. 2012.11.12
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_RA_getByKey
|
|
(
|
|
@idxRA INT
|
|
)
|
|
AS
|
|
|
|
SELECT idxRA, idxDipendente, idxFase, inizio, fine, CASE WHEN ISNULL(descrizione,'') = '' THEN '-' ELSE ISNULL(descrizione,'') END AS descrizione, oreTot, importo
|
|
FROM RegAttivita
|
|
WHERE idxRA = @idxRA
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_RA_getByIdxDipData
|
|
*
|
|
* recupera elenco attività da idxDipendente + date
|
|
*
|
|
* mod: S.E.L. 2012.11.06
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_RA_getByIdxDipData
|
|
(
|
|
@idxDipendente INT,
|
|
@dataFrom DATETIME,
|
|
@dataTo DATETIME
|
|
)
|
|
AS
|
|
|
|
SELECT idxRA, idxDipendente, idxFase, inizio, fine, CASE WHEN ISNULL(descrizione,'') = '' THEN '-' ELSE ISNULL(descrizione,'') END AS descrizione, oreTot, importo
|
|
FROM RegAttivita
|
|
WHERE (idxDipendente = @idxDipendente) AND (inizio BETWEEN @dataFrom AND @dataTo) OR
|
|
(idxDipendente = @idxDipendente) AND (fine BETWEEN @dataFrom AND @dataTo)
|
|
ORDER BY inizio
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_RA_clona
|
|
*
|
|
* clona un attività inserendola in coda alle altre del giorno x lo stesso utente, per pari durata
|
|
*
|
|
* mod: S.E.L. 2013.01.17
|
|
*
|
|
**********************************************************/
|
|
create PROCEDURE stp_RA_clona
|
|
(
|
|
@idxRA INT
|
|
)
|
|
AS
|
|
|
|
-- variabili
|
|
DECLARE @inizio DATETIME
|
|
DECLARE @dataRif DATETIME
|
|
DECLARE @idxDip INT
|
|
-- calcolo dati attività indicata
|
|
SELECT @idxDip=idxDipendente, @dataRif=dbo.DateOnly(inizio)
|
|
FROM RegAttivita
|
|
WHERE idxRA=@idxRA
|
|
|
|
-- calcolo fine ultima attività del giorno x l'utente
|
|
SELECT TOP 1 @inizio = fine
|
|
FROM RegAttivita
|
|
WHERE idxDipendente = @idxDip
|
|
AND dbo.DateOnly(fine) = @dataRif
|
|
ORDER BY fine DESC
|
|
|
|
-- ora inserisco la vera nuova attività!
|
|
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione, importo)
|
|
SELECT idxDipendente, idxFase, @inizio, DATEADD(minute, oreTot*60, @inizio), descrizione, importo
|
|
FROM RegAttivita
|
|
WHERE (idxRA = @idxRA)
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_RA_deleteQuery
|
|
*
|
|
* delete attività da chiave
|
|
*
|
|
* mod: S.E.L. 2012.11.12
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_RA_deleteQuery
|
|
(
|
|
@Original_idxRA int
|
|
)
|
|
AS
|
|
SET NOCOUNT OFF;
|
|
DELETE FROM [dbo].[RegAttivita] WHERE ([idxRA] = @Original_idxRA)
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
create table AnagClassiOrarie(
|
|
codClasse nvarchar(10) not null constraint PK_AnagClassiOrarie primary key,
|
|
descrClasse nvarchar(250),
|
|
peso decimal(6,3)
|
|
);
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'codice univoco', 'SCHEMA', 'dbo', 'TABLE', 'AnagClassiOrarie', 'COLUMN', 'codClasse';
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'peso (per calcolo ore "produttive")', 'SCHEMA', 'dbo', 'TABLE', 'AnagClassiOrarie', 'COLUMN', 'peso';
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table AnagFasi alter column
|
|
nomeFase nvarchar(250);
|
|
go
|
|
|
|
alter table AnagFasi add
|
|
codClasse nvarchar(10);
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'codice univoco', 'SCHEMA', 'dbo', 'TABLE', 'AnagFasi', 'COLUMN', 'codClasse';
|
|
go
|
|
|
|
alter table AnagFasi add
|
|
constraint FK_AnagFasi_AnagClassiOrarie foreign key(codClasse) references AnagClassiOrarie(codClasse) on update cascade;
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
-- =============================================
|
|
-- Author: S.E. Locatelli
|
|
-- Create date: 2012.10.31
|
|
-- Description: Gestione trigger calcolo codFase e idxProgetto
|
|
-- =============================================
|
|
alter TRIGGER trg_AF_calcCodFaseIdxProj
|
|
ON AnagFasi
|
|
AFTER INSERT,UPDATE
|
|
AS
|
|
BEGIN
|
|
IF (@@ROWCOUNT = 0) -- SE NESSUNA RIGA AGGIORNATA O INSERITA ESCO
|
|
RETURN
|
|
|
|
-- SET NOCOUNT ON added to prevent extra result sets from
|
|
-- interfering with SELECT statements.
|
|
SET NOCOUNT ON;
|
|
|
|
-- calcolo il codFase ed idxProgetto x record corrente
|
|
UPDATE AF
|
|
SET AF.codFase = dbo.f_calcolaCodFase(i.idxFase)
|
|
,AF.idxProgetto = CASE WHEN dbo.f_calcolaIdxProgetto(i.idxFase) > 0
|
|
THEN dbo.f_calcolaIdxProgetto(i.idxFase)
|
|
ELSE i.idxProgetto
|
|
END
|
|
FROM AnagFasi AF
|
|
INNER JOIN inserted i ON AF.idxFase = i.idxFase
|
|
|
|
-- aggiorno IN CASCATA i record child x idxProgetto!!!
|
|
UPDATE AF
|
|
SET AF.idxProgetto = CASE WHEN dbo.f_calcolaIdxProgetto(i.idxFase) > 0
|
|
THEN dbo.f_calcolaIdxProgetto(i.idxFase)
|
|
ELSE i.idxProgetto
|
|
END
|
|
,AF.codClasse = i.codClasse
|
|
FROM AnagFasi AF
|
|
INNER JOIN inserted i ON AF.idxFaseAncest = i.idxFase
|
|
|
|
END
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_import_paymoRaw
|
|
*
|
|
* importa tracciato dati da paymo x utente e date indicate
|
|
*
|
|
* mod: S.E.L. 2012.11.20
|
|
*
|
|
**********************************************************/
|
|
create PROCEDURE stp_import_paymoRaw
|
|
(
|
|
@inizio DATETIME,
|
|
@fine DATETIME
|
|
)
|
|
AS
|
|
|
|
/**************************************************
|
|
* inizio importando eventuali clienti...
|
|
**************************************************/
|
|
;WITH myCteClienti AS
|
|
(
|
|
SELECT DISTINCT Cliente FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagClienti(RagSociale, nota, Attivo)
|
|
SELECT cte.Cliente, cte.Cliente, 1
|
|
FROM AnagClienti anag
|
|
RIGHT OUTER JOIN myCteClienti cte ON anag.RagSociale = cte.Cliente
|
|
WHERE anag.idxCliente IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali progetti
|
|
**************************************************/
|
|
;WITH myCteProgetti AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagProgetti(idxCliente, nomeProj, descrProj, Attivo)
|
|
SELECT cli.idxCliente, cte.Progetto, cte.Progetto, 1
|
|
FROM AnagProgetti anag
|
|
RIGHT OUTER JOIN myCteProgetti cte ON anag.nomeProj = cte.Progetto INNER JOIN AnagClienti Cli ON Cli.RagSociale = cte.Cliente
|
|
WHERE anag.idxProgetto IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali fasi
|
|
**************************************************/
|
|
;WITH myCteFasi AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto, TaskList FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagFasi(idxProgetto, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
|
|
SELECT Pro.idxProgetto, '', cte.TaskList, cte.TaskList, 1, 0, 1
|
|
FROM myCteFasi cte INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj
|
|
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.TaskList
|
|
WHERE anag.idxFase IS NULL
|
|
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali sottofasi
|
|
**************************************************/
|
|
;WITH myCteSubFasi AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto, TaskList, Task FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagFasi(idxProgetto, idxFaseAncest, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
|
|
SELECT Pro.idxProgetto, Fas.idxFase as idxFaseAncest, '', cte.task, cte.task, 1, 0, 1
|
|
FROM myCteSubFasi cte
|
|
INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale
|
|
INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj -- and pro.idxCliente=cli.idxCliente
|
|
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.TaskList AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
|
|
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.Task AND anag.idxFaseAncest = Fas.idxFase
|
|
WHERE anag.idxFaseAncest IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo records!
|
|
**************************************************/
|
|
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione)
|
|
SELECT Dip.idxDipendente, Fas.idxFase, cte.Inizio, cte.Fine, cte.Descrizione
|
|
FROM zzz_RawDataPaymo cte
|
|
INNER JOIN Dipendenti Dip ON cte.Dipendente = dip.codDipendenteExt
|
|
INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale
|
|
INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj -- and pro.idxCliente=cli.idxCliente
|
|
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.TaskList AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
|
|
INNER JOIN AnagFasi SFas ON SFas.nomeFase = cte.Task 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
|
|
|
|
|
|
|
|
RETURN
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table Dipendenti add
|
|
codDipendenteExt nvarchar(50);
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'nome/codice dipendente per sistema esterno da importare', 'SCHEMA', 'dbo', 'TABLE', 'Dipendenti', 'COLUMN', 'codDipendenteExt';
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_import_paymoRaw
|
|
*
|
|
* importa tracciato dati da paymo x utente e date indicate
|
|
*
|
|
* mod: S.E.L. 2012.11.20
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_import_paymoRaw
|
|
(
|
|
@inizio DATETIME,
|
|
@fine DATETIME
|
|
)
|
|
AS
|
|
|
|
/**************************************************
|
|
* inizio importando eventuali clienti...
|
|
**************************************************/
|
|
;WITH myCteClienti AS
|
|
(
|
|
SELECT DISTINCT Cliente FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagClienti(RagSociale, nota, Attivo)
|
|
SELECT cte.Cliente, cte.Cliente, 1
|
|
FROM AnagClienti anag
|
|
RIGHT OUTER JOIN myCteClienti cte ON anag.RagSociale = cte.Cliente
|
|
WHERE anag.idxCliente IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali progetti
|
|
**************************************************/
|
|
;WITH myCteProgetti AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagProgetti(idxCliente, nomeProj, descrProj, Attivo)
|
|
SELECT cli.idxCliente, cte.Progetto, cte.Progetto, 1
|
|
FROM AnagProgetti anag
|
|
RIGHT OUTER JOIN myCteProgetti cte ON anag.nomeProj = cte.Progetto INNER JOIN AnagClienti Cli ON Cli.RagSociale = cte.Cliente
|
|
WHERE anag.idxProgetto IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali fasi
|
|
**************************************************/
|
|
;WITH myCteFasi AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto, TaskList FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagFasi(idxProgetto, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
|
|
SELECT Pro.idxProgetto, '', cte.TaskList, cte.TaskList, 1, 0, 1
|
|
FROM myCteFasi cte INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj
|
|
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.TaskList
|
|
WHERE anag.idxFase IS NULL
|
|
|
|
|
|
|
|
/**************************************************
|
|
* importo eventuali sottofasi
|
|
**************************************************/
|
|
;WITH myCteSubFasi AS
|
|
(
|
|
SELECT DISTINCT Cliente, Progetto, TaskList, Task FROM zzz_RawDataPaymo
|
|
)
|
|
INSERT INTO AnagFasi(idxProgetto, idxFaseAncest, codFase, nomeFase, descrizioneFase, enableTime, enableMoney, Attivo)
|
|
SELECT Pro.idxProgetto, Fas.idxFase as idxFaseAncest, '', cte.task, cte.task, 1, 0, 1
|
|
FROM myCteSubFasi cte
|
|
INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale
|
|
INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj -- and pro.idxCliente=cli.idxCliente
|
|
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.TaskList AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
|
|
LEFT OUTER JOIN AnagFasi anag ON anag.nomeFase = cte.Task AND anag.idxFaseAncest = Fas.idxFase
|
|
WHERE anag.idxFaseAncest IS NULL
|
|
|
|
|
|
/**************************************************
|
|
* importo records!
|
|
**************************************************/
|
|
INSERT INTO RegAttivita(idxDipendente, idxFase, inizio, fine, descrizione)
|
|
SELECT Dip.idxDipendente, Fas.idxFase, cte.Inizio, cte.Fine, cte.Descrizione
|
|
FROM zzz_RawDataPaymo cte
|
|
INNER JOIN Dipendenti Dip ON cte.Dipendente = dip.codDipendenteExt
|
|
INNER JOIN AnagClienti Cli ON cte.Cliente = Cli.RagSociale
|
|
INNER JOIN AnagProgetti Pro ON cte.Progetto = Pro.nomeProj -- and pro.idxCliente=cli.idxCliente
|
|
INNER JOIN AnagFasi Fas ON Fas.nomeFase = cte.TaskList AND Fas.idxProgetto = Pro.idxProgetto AND Fas.idxFaseAncest=0
|
|
INNER JOIN AnagFasi SFas ON SFas.nomeFase = cte.Task 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
|
|
|
|
|
|
|
|
RETURN
|
|
go
|
|
|
|
/**********************************************************
|
|
* STORED stp_AD_deleteQuery
|
|
*
|
|
* elimina un dipendente da anagrafica
|
|
*
|
|
* mod: S.E.L. 2012.10.29
|
|
*
|
|
**********************************************************/
|
|
alter PROCEDURE stp_AD_deleteQuery
|
|
(
|
|
@Original_idxDipendente int
|
|
)
|
|
AS
|
|
SET NOCOUNT OFF;
|
|
DELETE FROM [Dipendenti] WHERE (([idxDipendente] = @Original_idxDipendente))
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- registro versione...
|
|
INSERT INTO [dbo].[LogUpdateDb] ([Versione],[Data]) VALUES(230, GETDATE())
|
|
GO
|
|
SELECT * FROM LogUpdateDb ORDER BY Versione DESC
|