-- dbo.ep_Vyroba_ZrusZakazkuPlanuDleFinPrikazu
CREATE PROCEDURE dbo.ep_Vyroba_ZrusZakazkuPlanuDleFinPrikazu
  @idAdvKP INT=NULL,
  @idVPrFin INT=NULL,
  @idStroj INT=NULL,
  @jeUdrzba BIT=0,
  @idSadaDavek INT=NULL,
  @errorMsg NVARCHAR(500)=N'' OUT
AS

-- HD Consulting Strakonice, T. Buzin
-- !! proceduru neupravujte, bude stejne pregenerovana pri startu pluginu pro planovani

SET NOCOUNT ON

IF (@idAdvKP IS NULL)
 BEGIN
  SET @errorMsg = N'Neni speficikovan kapacitni plan'
  RETURN
 END
IF (@idStroj IS NULL)
 BEGIN
  SET @errorMsg = N'Neni specifikovan stroj'
  RETURN
 END


DECLARE
 @tranPred INT,
 @cnt INT,
 @iTemp INT,
 @xml XML,
 @retVal INT,
 @idVPr INT,
 @idPlan INT,
 @minCasDo DATETIME=NULL,
 @maxCasDo DATETIME=NULL,
 @idDavka INT,
 @casOd DATETIME,
 @casOdNew DATETIME,
 @casDo DATETIME,
 @idKalendarStroje INT


DECLARE @davkyZa TABLE (ID INT, IDPrikaz INT, Mnozstvi NUMERIC(19,6), CasOd DATETIME, CasDo DATETIME)
DECLARE @sadaDavek TABLE (ID INT NOT NULL)

SET @idKalendarStroje = (SELECT IDPlanKalend FROM dbo.TabCisStroju WHERE ID=@idStroj)
SET @maxCasDo = CONVERT(datetime, N'1.1.2020', 104)
SET @maxCasDo = @minCasDo -- CONVERT(datetime, N'1.1.2020', 104) 



DELETE FROM @sadaDavek
IF (@idSadaDavek IS NOT NULL)
 INSERT @sadaDavek (ID) SELECT ID FROM dbo.TabAdvKPDavky_EXT WHERE _VazbaPlgPlan=@idSadaDavek 



BEGIN TRY

SET @tranPred=@@TRANCOUNT
IF( @tranPred=0)
 BEGIN TRAN

DECLARE vp CURSOR LOCAL FOR
 SELECT ID, IDPlan
  FROM dbo.TabPrikaz
  WHERE IDPrikazRidici=@idVPrFin
    AND StavPrikazu<40
  ORDER BY UrovenVnoreni DESC
OPEN vp
WHILE (1=1)
 BEGIN
  FETCH NEXT FROM vp INTO @idVPr, @idPlan
  IF (@@FETCH_STATUS<>0) BREAK


  IF EXISTS(SELECT 1 FROM dbo.TabAdvKPDavky WHERE Zamek=1 AND IDAdvKapacPlan=@idAdvKP AND IDPrikaz=@idVPr AND IDStroje=@idStroj)
   CONTINUE

  IF EXISTS(SELECT ID FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDPrikaz=@idVPr AND IDStroje=@idStroj AND CasDo>@maxCasDo)
   SET @maxCasDo = (SELECT MAX(CasDo) FROM dbo.TabAdvKPDavky WHERE Zamek=0 AND IDAdvKapacPlan=@idAdvKP AND IDPrikaz=@idVPr AND IDStroje=@idStroj AND CasDo>@maxCasDo)

-- zkontroluje zda je neco z prikazu uz odvedeno a pripadne smaze vsechny vazby (kusovnik, operace, VC, naradi, OPN, davky advKP, kalk/plan naklady...)
  EXEC @retVal = dbo.hp_TabPrikaz_TriggerBeforeDelete @ID=@idVPr, @PouzeTestPredZrusenimStavuZadano=0
  IF (@@ERROR=0) AND (@retVal=0)
   DELETE FROM dbo.TabPrikaz WHERE ID=@idVPr
--  EXEC @retVal=dbo.hp_TabPlan_TriggerBeforeDelete @ID=@idPlan
--  IF (@@ERROR=0) AND (@retVal=0)
--   IF NOT EXISTS (SELECT 1 FROM dbo.TabPrikaz WHERE IDPlan=@idPlan)
--    DELETE FROM dbo.TabPlan WHERE ID=@idPlan
 END
CLOSE vp
DEALLOCATE vp



SET @minCasDo = DATEADD(hour, 1, DATEADD(hour, DATEDIFF(hour, 0, @minCasDo), 0))
-- SET @maxCasDo = @minCasDo -- CONVERT(datetime, N'1.1.2020', 104) 
IF EXISTS (SELECT 1 FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDStroje=@idStroj AND CasDo<@maxCasDo)
 BEGIN
  SELECT @minCasDo=MAX(CasDo) FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDStroje=@idStroj AND CasDo<@maxCasDo
  SET @casOdNew = DATEADD(second, 1, @minCasDo)
 END

IF EXISTS (SELECT 1 FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDPrikaz=@idVPrFin AND IDStroje=@idStroj AND CasDo>@maxCasDo)
 SET @maxCasDo = (SELECT MAX(CasDo) FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDPrikaz=@idVPrFin AND IDStroje=@idStroj AND CasDo>@maxCasDo)





SELECT @idPlan=IDPlan FROM dbo.TabPrikaz WHERE ID=@idVPrFin
-- zkontroluje zda je neco z prikazu uz odvedeno a pripadne smaze vsechny vazby (kusovnik, operace, VC, naradi, OPN, davky advKP, kalk/plan naklady...)
EXEC @retVal = dbo.hp_TabPrikaz_TriggerBeforeDelete @ID=@idVPrFin, @PouzeTestPredZrusenimStavuZadano=0
DELETE FROM dbo.TabPrikaz WHERE ID=@idVPrFin
EXEC @retVal=dbo.hp_TabPlan_TriggerBeforeDelete @ID=@idPlan
IF (@@ERROR=0) AND (@retVal=0)
 DELETE FROM dbo.TabPlan WHERE ID=@idPlan



-- nasledujici neblokovane davky
INSERT @davkyZa (ID, CasOd, CasDo)
 SELECT ID, CasOd, CasDo FROM dbo.TabAdvKPDavky WHERE Zamek=0 AND IDAdvKapacPlan=@idAdvKP AND IDStroje=@idStroj AND CasOd>@maxCasDo
SET @xml=(SELECT * FROM @davkyZa FOR XML AUTO)

-- presun je dopredu, na konec predchozi prace na tom samem stroji, pokud ma stroj kalendar, koukej i na nej
DECLARE dav CURSOR LOCAL FOR
 SELECT ID, CasOd, CasDo
  FROM @davkyZa
  ORDER BY CasOd
OPEN dav
WHILE (1=1)
 BEGIN
  FETCH NEXT FROM dav INTO @idDavka, @casOd, @casDo
  IF (@@FETCH_STATUS<>0) BREAK

  SET @casOd = @casOdNew
-- begin: test data/casu zahajeni na kalendar stroje
  IF (@idKalendarStroje IS NULL) -- pokud kalendar nemam, per to tam
    BEGIN
     SET @cnt = 0
     WHILE EXISTS(SELECT 1 FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDStroje=@idStroj AND Zamek=1 AND @casOd BETWEEN CasOd AND CasDo)
           AND (@cnt<=3000) -- kvuli zamrznuti
      BEGIN
       SET @casOd = DATEADD(hour, 1, @casOd)
       SET @cnt = @cnt + 1
      END
     SET @casDo = DATEADD(second, -1, DATEADD(hour, 1, @casOd))
    END
   ELSE
    BEGIN -- pokud kalendar mam, zkontroluj jestli neni na dany datum/cas vyjimka
     SET @cnt = 0
     WHILE (EXISTS(SELECT 1 FROM dbo.TabAdvKPDavky WHERE IDAdvKapacPlan=@idAdvKP AND IDStroje=@idStroj AND Zamek=1 AND @casOd BETWEEN CasOd AND CasDo)
            OR EXISTS(SELECT 1 FROM dbo.TabPlanKalendPol WHERE IDStroje=@idStroj AND @casOd BETWEEN CasOd AND CasDo)
           )
           AND (@cnt<=3000) -- kvuli zamrznuti
      BEGIN
       SET @casOd = DATEADD(hour, 1, @casOd)
       SET @cnt = @cnt + 1
      END
     SET @casDo = DATEADD(second, -1, DATEADD(hour, 1, @casOd))
    END
-- end: test data/casu zahajeni na kalendar stroje
  UPDATE dbo.TabAdvKPDavky SET CasOd=@casOd, CasDo=@casDo WHERE ID=@idDavka


 END
CLOSE dav
DEALLOCATE dav



IF (@tranPred=0) AND (@@TRANCOUNT>0)
 COMMIT TRAN

END TRY

BEGIN CATCH
 IF (@tranPred=0) AND (@@TRANCOUNT>0)
  ROLLBACK TRAN
 SET @errorMsg = ERROR_MESSAGE()

 IF (CURSOR_STATUS('local','dav')>=-1)
  BEGIN
   CLOSE dav
   DEALLOCATE dav
  END

 IF (CURSOR_STATUS('local','vp')>=-1)
  BEGIN
   CLOSE vp
   DEALLOCATE vp
  END

END CATCH
