CREATE TRIGGER [dbo].[Calcul_Delais]
ON [dbo].[ZI_QteOUT]
AFTER INSERT
AS
BEGIN
DECLARE @Chaine TINYINT
DECLARE @NumeroIntervention INT
DECLARE @DateReception DATETIME
DECLARE @DateDeballage DATETIME
DECLARE @DateExpedition DATETIME
DECLARE @DelaiRecepJO INT
DECLARE @DelaiRecepJC INT
DECLARE @DelaiBrutJO INT
DECLARE @DelaiBrutJC INT
DECLARE @DelaiOuvreDevis INT
DECLARE @DelaiTotalDevis INT
DECLARE @DelaiSansDevisJC INT
DECLARE @DelaiSansDevisJO INT
DECLARE @DelaiNetJO INT
DECLARE @DelaiNetJC INT
DECLARE @DebutAttente DATETIME
DECLARE @FinAttente DATETIME
DECLARE @TypeAttente TINYINT
DECLARE @FinAttenteMax DATETIME
DECLARE @NbJoursFeriesBrut INT
DECLARE @NbJoursFeriesDelta INT
DECLARE @NbJoursFeriesAttente INT
DECLARE @DelaiTotal INT
DECLARE @DebutAttenteDevisMin DATETIME
DECLARE @FinAttenteDevisMax DATETIME
DECLARE @DeltaDevis INT
DECLARE @NbJoursFeriesDevis INT
DECLARE @iCmpt INT
--SET @iCmpt = 0
DECLARE @mIntervention int
select @mIntervention = Inserted.NumeroIntervention, @DateReception = Inserted.DateReception, @DateDeballage = Inserted.DateDeballage, @DateExpedition = Inserted.DateExpedition from Inserted;
UPDATE dbo.ZI_QteOUT
SET etat = 'trigger'
WHERE NumeroIntervention = @mIntervention
--Début Calcul
BEGIN
--print CONVERT(VARCHAR,@iCmpt )
--SET @iCmpt = @iCmpt + 1
SET @DelaiRecepJC = 0
SET @DelaiRecepJO = 0
SET @DelaiBrutJO = 0
SET @DelaiBrutJC = 0
SET @DelaiOuvreDevis = 0
SET @DelaiTotalDevis = 0
SET @DelaiSansDevisJC = 0
SET @DelaiSansDevisJO = 0
SET @DelaiNetJO = 0
SET @DelaiNetJC = 0
SET @NbJoursFeriesDelta = 0
SET @NbJoursFeriesAttente = 0
SET @DelaiTotal = 0
SET @DeltaDevis = 0
SET @NbJoursFeriesDevis = 0
SET @NbJoursFeriesBrut = 0
SET @DebutAttente = NULL
SET @FinAttente = NULL
SET @TypeAttente = NULL
DECLARE Delai CURSOR READ_ONLY
FOR
SELECT DebutAttente, FinAttente, TypeAttente
FROM dbo.Zi_ATTENTE WITH (NOLOCK)
WHERE NumeroIntervention = @mIntervention
ORDER BY DebutAttente
OPEN Delai
FETCH NEXT FROM Delai INTO @DebutAttente, @FinAttente, @TypeAttente
SET @FinAttenteMax = @FinAttente
SET @NbJoursFeriesDelta = 0
SET @NbJoursFeriesAttente = 0
SET @DelaiTotal = 0
SET @DeltaDevis = 0
SET @NbJoursFeriesDevis = 0
SET @DelaiTotalDevis = 0
SET @DelaiOuvreDevis = 0
--IF @DebutAttente IS NOT NULL AND @FinAttente IS NOT NULL
--BEGIN
-- SET @NbJoursFeriesDelta = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @DebutAttente AND JourFerie < @FinAttente AND Chaine = @Chaine)
-- SET @NbJoursFeriesAttente = @NbJoursFeriesAttente + @NbJoursFeriesDelta
-- SET @DelaiTotal = @DelaiTotal + DATEDIFF("mi", @DebutAttente, @FinAttente)
--END
WHILE @@FETCH_STATUS = 0
BEGIN
IF @FinAttenteMax > @DebutAttente
BEGIN
IF @FinAttenteMax < @FinAttente
BEGIN
SET @NbJoursFeriesDelta = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @FinAttenteMax AND JourFerie < @FinAttente AND Chaine = @Chaine)
SET @NbJoursFeriesAttente = @NbJoursFeriesAttente + @NbJoursFeriesDelta
SET @DelaiTotal = @DelaiTotal + DATEDIFF("mi", @FinAttenteMax, @FinAttente)
SET @FinAttenteMax = @FinAttente
END
END
ELSE
BEGIN
SET @NbJoursFeriesDelta = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @DebutAttente AND JourFerie < @FinAttente AND Chaine = @Chaine)
SET @NbJoursFeriesAttente = @NbJoursFeriesAttente + @NbJoursFeriesDelta
SET @DelaiTotal = @DelaiTotal + DATEDIFF("mi", @DebutAttente, @FinAttente)
SET @FinAttenteMax = @FinAttente
END
IF @TypeAttente IN (1, 2, 4, 12)
BEGIN
SET @DebutAttenteDevisMin = @DebutAttente
SET @FinAttenteDevisMax = @FinAttente
SET @DeltaDevis = 0
IF @DebutAttente <= @FinAttenteDevisMax
BEGIN
IF @FinAttente > @FinAttenteDevisMax
SET @FinAttenteDevisMax = @FinAttente
END
ELSE
BEGIN
SET @DeltaDevis = @DeltaDevis + DATEDIFF("mi", @FinAttenteDevisMax, @DebutAttente)
SET @FinAttenteDevisMax = @FinAttente
END
SET @DelaiTotalDevis = DATEDIFF("mi", @DebutAttenteDevisMin, @FinAttenteDevisMax) - @DeltaDevis
SET @NbJoursFeriesDevis = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @DebutAttenteDevisMin AND JourFerie < @FinAttenteDevisMax AND Chaine = @Chaine)
SET @DelaiOuvreDevis = @DelaiTotalDevis - @NBJoursFeriesDevis * 1440
END
FETCH NEXT FROM Delai INTO @DebutAttente, @FinAttente, @TypeAttente
END
CLOSE Delai
DEALLOCATE Delai
SET @DelaiRecepJC = DATEDIFF("mi", @DateReception ,@DateDeballage)
SET @NbJoursFeriesBrut = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @DateReception AND JourFerie < @DateDeballage AND Chaine = @Chaine)
SET @DelaiRecepJO = @DelaiRecepJC - @NbJoursFeriesBrut * 1440
SET @DelaiBrutJC = DATEDIFF("mi", @DateDeballage, @DateExpedition)
SET @NbJoursFeriesBrut = (SELECT COUNT(*) FROM dbo.Zi_JoursFeries WITH (NOLOCK) WHERE JourFerie >= @DateDeballage AND JourFerie < @DateExpedition AND Chaine = @Chaine)
SET @DelaiBrutJO = @DelaiBrutJC - @NbJoursFeriesBrut * 1440
SET @DelaiSansDevisJC = @DelaiBrutJC - @DelaiTotalDevis
SET @DelaiSansDevisJo = @DelaiBrutJo - @DelaiOuvreDevis
SET @DelaiNetJC = @DelaiBrutJC - @DelaiTotal
SET @DelaiNetJO = @DelaiNetJC - @NbJoursFeriesBrut * 1440 + @NbJoursFeriesAttente * 1440
--FinCalcul
--Maj de la ligne avec le calcul
UPDATE dbo.ZI_QteOUT
SET DelaiBrutJOReceptionDeballage = @DelaiRecepJO,
DelaiBrutJODeballageExpedition = @DelaiBrutJO,
DelaiBrutJCEmissionReponseDevis = @DelaiTotalDevis,
DelaiBrutJOEmissionReponseDevis = @DelaiOuvreDevis,
DelaiBrutJCDeballageExpeditionSansDevis = @DelaiSansDevisJC,
DelaiBrutJODeballageExpeditionSansDevis = @DelaiSansDevisJO,
DelaiNetJCDeballageExpedition = @DelaiNetJC,
DelaiNetJODeballageExpedition = @DelaiNetJO
WHERE NumeroIntervention = @mIntervention --CURRENT OF RechercheIntervention
END
END
Partager