-- dbo.ep_Vyroba_InsertEvidRozpracOperPol
CREATE PROCEDURE dbo.ep_Vyroba_InsertEvidRozpracOperPol
    @IdEvidRozpOper INT,
    @IdPrikaz INT,
    @doklPrPost INT,
    @altPrPost NCHAR(1),
    @IDPaleta INT=NULL,
    @IdVyrCis INT=NULL,
    @cisloPL NVARCHAR(50),
    @sarze NVARCHAR(30),
    @mnozOdv NUMERIC(19,6)=0,
    @skCas NUMERIC(19,6)=0,
    @skCasT TINYINT=1,
    @skCasObsl NUMERIC(19,6)=0,
    @skCasObslT TINYINT=1
AS

-- !! proceduru neupravujte, bude stejne pregenerovana pri startu pluginu PluginHDCRTN !!

SET NOCOUNT ON

DECLARE
 @errMsg NVARCHAR(500),
 @tranPred INT

DECLARE
 @idEROpPol INT=NULL,
 @idTemp INT=NULL,
 @mnEROp NUMERIC(19,6),
 @sumMnEROp NUMERIC(19,6)



IF OBJECT_ID(N'#TabExtKom', N'U') IS NULL
 CREATE TABLE #TabExtKom (Typ TINYINT, Poznamka NVARCHAR(255) NOT NULL DEFAULT N'')




IF EXISTS(SELECT 1 FROM dbo.TabEvidRozpracOperR)
  BEGIN
   SELECT @idEROpPol=MAX(ID) FROM dbo.TabEvidRozpracOperR
   DBCC CHECKIDENT(TabEvidRozpracOperR, RESEED, @idEROpPol)
  END
 ELSE
  DBCC CHECKIDENT(TabEvidRozpracOperR, RESEED, 1)
SET @idEROpPol=NULL




SET @sarze = TRIM(ISNULL(@sarze, N''))
SET @cisloPL = TRIM(ISNULL(@cisloPL, N''))

IF (@IDPaleta IS NOT NULL) AND (@IDVyrCis IS NULL)
 SELECT @idVyrCis=IDVyrCisPrikaz FROM dbo._TabVyroba_Palety WHERE ID=@IDPaleta
IF (@IDVyrCis IS NULL) AND (@cisloPL<>N'') AND (@IdPrikaz IS NOT NULL) AND (@sarze<>N'')
 SET @IDVyrCis = (SELECT ID FROM dbo.TabVyrCisPrikaz WHERE IDPrikaz=@IdPrikaz AND VyrCislo=@cisloPL AND Popis=@sarze)
IF (@IDVyrCis IS NULL) AND (@cisloPL<>N'') AND (@IdPrikaz IS NOT NULL)
 SET @IDVyrCis = (SELECT ID FROM dbo.TabVyrCisPrikaz WHERE IDPrikaz=@IdPrikaz AND VyrCislo=@cisloPL)



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

 INSERT dbo.TabEvidRozpracOperR (IDEvidRozpracOper, IDPrikaz, DokladPrPostup, AltPrPostup, IDVyrCis, Mnoz_odv, Sk_cas, Sk_cas_T, Sk_cas_Obsluhy, Sk_cas_Obsluhy_T)
  SELECT @IdEvidRozpOper, @IdPrikaz, @doklPrPost, @altPrPost, @IdVyrCis, @mnozOdv, @skCas, @skCasT, @skCasObsl, @skCasObslT
 SET @idEROpPol = SCOPE_IDENTITY()

 IF (@idEROpPol IS NOT NULL)
  IF NOT EXISTS (SELECT 1 FROM dbo.TabEvidRozpracOperR_EXT WHERE ID=@idEROpPol)
   INSERT dbo.TabEvidRozpracOperR_EXT (ID) VALUES (@idEROpPol)
 UPDATE dbo.TabEvidRozpracOperR_EXT SET _Sarze=@sarze WHERE ID=@idEROpPol


-- zapis koeficient
 SET @sumMnEROp = ISNULL( (SELECT SUM(Mnoz_odv) FROM dbo.TabEvidRozpracOperR WHERE IDEvidRozpracOper=@IdEvidRozpOper), 0)
 DECLARE crLoc CURSOR LOCAL FOR
  SELECT ID, Mnoz_odv FROM dbo.TabEvidRozpracOperR WHERE IDEvidRozpracOper=@IdEvidRozpOper
 OPEN crLoc
 WHILE (1=1)
  BEGIN
   FETCH NEXT FROM crLoc INTO @idTemp, @mnEROp
   IF (@@FETCH_STATUS<>0) BREAK
   UPDATE dbo.TabEvidRozpracOperR_EXT SET _KoefMnoz=ROUND(@mnEROp/@sumMnEROp, 5) WHERE ID=@idTemp
  END
 CLOSE crLoc
 DEALLOCATE crLoc 


 IF (@tranPred=0) ANd (@@TRANCOUNT>0)
  COMMIT TRAN
END TRY
BEGIN CATCH
 IF CURSOR_STATUS('local','crLoc')>=-1
  BEGIN
   CLOSE crLoc;
   DEALLOCATE crLoc
  END
 IF (@tranPred=0) ANd (@@TRANCOUNT>0)
  ROLLBACK TRAN
 INSERT #TabExtKom (Typ, Poznamka) SELECT 3, ERROR_MESSAGE()
 SET @idEROpPol = NULL
END CATCH

RETURN @idEROpPol
