-- dbo.ep_Vyroba_InsertEvidRozpracOperPol
CREATE PROCEDURE dbo.ep_Vyroba_InsertEvidRozpracOperPol
    @IdEvidRozpOper INT,
    @IdPrikaz INT,
    @doklPrPost INT,
    @altPrPost NCHAR(1),
    @IdVyrCis INT=NULL,
    @mnozOdv NUMERIC(19,6)=0,
    @skCas NUMERIC(19,6)=0,
    @skCasT TINYINT=1,
    @skCasObsl NUMERIC(19,6)=0,
    @skCasObslT TINYINT=1,
    @errMsg NVARCHAR(500)=N'' OUT
AS

-- !! proceduru neupravujte, bude stejne pregenerovana pri startu Windows sluzby HDCDZApi !!

SET NOCOUNT ON

DECLARE
 @tranPred INT,
 @idEROpPol INT=NULL


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



-- cisteni identity
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




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 OBJECT_ID(N'dbo.ep_Vyroba_InsertEvidRozpracOperPol_Po', N'P') IS NOT NULL
  EXEC dbo.ep_Vyroba_InsertEvidRozpracOperPol_Po @idEROpPol


 IF (@tranPred=0) ANd (@@TRANCOUNT>0)
  COMMIT TRAN
END TRY
BEGIN CATCH
 IF (@tranPred=0) ANd (@@TRANCOUNT>0)
  ROLLBACK TRAN
 SET @errMsg = ERROR_MESSAGE()
 INSERT #TabExtKom (Typ, Poznamka) SELECT 3, LEFT(@errMsg,255)
 SET @idEROpPol = NULL
END CATCH

RETURN @idEROpPol
