<?xml version="1.0" encoding="UTF-8"?>
<defs>


 <def typ="TAB" nazev="_hdc_DataZone_konfig" verze="20230209">
  <sql_text><![CDATA[
IF OBJECT_ID(N'dbo._hdc_DataZone_konfig', N'U') IS NULL~
 CREATE TABLE dbo._hdc_DataZone_konfig (~
  ID int IDENTITY(1, 1) NOT NULL,~
  Typ NCHAR(3) NOT NULL,~
  Nazev NVARCHAR(100) NOT NULL,~
  Verze NCHAR(8) NOT NULL,~
  Obsah nvarchar(max) COLLATE Czech_CI_AS NULL,~
  DatPorizeni DATETIME NOT NULL DEFAULT GETDATE(),~
  DatZmeny DATETIME,~
  CONSTRAINT PK_hdc_DataZone_konfig PRIMARY KEY CLUSTERED (ID)~
    WITH (~
      PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF,~
      ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)~
 )
]]>
  </sql_text>
 </def>



 <def typ="PRO" nazev="ep_hdcdzapi_GetKusovnik" verze="20230601">
  <sql_text><![CDATA[
CREATE PROCEDURE dbo.ep_hdcdzapi_GetKusovnik  AS~
BEGIN~
 SET NOCOUNT ON~
 DECLARE @RespekExistDoklady bit=0~
~
 IF OBJECT_ID(N'tempdb..#apiKusovnik', N'U') IS NULL~
  CREATE TABLE #apiKusovnik (ID INT IDENTITY(1,1) NOT NULL, IDPrKV INT, MnozstviPoz NUMERIC(19,6), pom_pomer_A NUMERIC(19,6), pom_pomer_B NUMERIC(19,6), Doklad INT, IDPrikaz INT,~
    Prednastaveno BIT, Sklad NVARCHAR(30), VyrStredisko NVARCHAR(30), IDPracoviste INT, mnoz_zad NUMERIC(19,6), Mnoz_nepotrebne NUMERIC(19,6), Mnoz_skut_realizovane NUMERIC(19,6),~
    Cena_real NUMERIC(19,6), Mnoz_odv NUMERIC(19,6), Cena_odv NUMERIC(19,6), priorita INT, vyssi INT, nizsi INT, DavkaTPV NUMERIC(19,6), pozice INT, Operace NVARCHAR(4),~
    FixniMnozstvi NUMERIC(19,6), mnozstvi NUMERIC(19,6), ProcZtrat NUMERIC(19,6), mnozstviSeZtratou NUMERIC(19,6), Prirez NUMERIC(19,6), Poznamka NVARCHAR(500), SZVyssi NVARCHAR(3),~
    RCVyssi NVARCHAR(30), Nazev1Vyssi NVARCHAR(100), SZNizsi NVARCHAR(3), RCNizsi NVARCHAR(30), Nazev1Nizsi NVARCHAR(100))~
 IF OBJECT_ID(N'tempdb..#apiOdved', N'U') IS NULL~
  CREATE TABLE #apiOdved (ID INT, IDPrikaz INT, Mnozstvi NUMERIC(19,6) DEFAULT 0.0)~
~
 INSERT #apiKusovnik (IDPrKV, MnozstviPoz, pom_pomer_A, pom_pomer_B, Doklad, IDPrikaz, Prednastaveno, Sklad, VyrStredisko, IDPracoviste, mnoz_zad, Mnoz_nepotrebne, Mnoz_skut_realizovane, Cena_real, Mnoz_odv, Cena_odv, priorita, vyssi, nizsi, DavkaTPV,~
                      pozice, Operace, FixniMnozstvi, mnozstvi, ProcZtrat, mnozstviSeZtratou, Prirez, Poznamka, SZVyssi, RCVyssi, Nazev1Vyssi, SZNizsi, RCNizsi, Nazev1Nizsi)~
  SELECT PrKV.ID, PrKV.mnoz_zad*O.mnozstvi/P.kusy_zad - CASE WHEN @RespekExistDoklady=1 THEN PrKV.VydanoRefMnoz + (SELECT ISNULL(SUM(PZ2.prepmnozstvi*(PZ2.mnozstvi-PZ2.MnOdebrane) * PrKV2.RefMnoz / PrKV2.mnoz_zad), 0.0) FROM [dbo].[TabPohybyZbozi] PZ2~
            INNER JOIN [dbo].[TabDokladyZbozi] DZ2 ON (DZ2.ID=PZ2.IDDoklad AND DZ2.splneno=0)~
            INNER JOIN [dbo].[TabStavSkladu] SS2 ON (SS2.ID=PZ2.IDZboSklad)~
            INNER JOIN [dbo].[TabPrKVazby] PrKV2 ON (PrKV2.IDPrikaz=PZ2.IDPrikaz AND PrKV2.Doklad=PZ2.DokladPrikazu AND PrKV2.nizsi=SS2.IDKmenZbozi AND PrKV2.IDOdchylkyDo IS NULL)~
            WHERE PZ2.TypVyrobnihoDokladu=1 AND PZ2.IDPrikaz=PrKV.IDPrikaz AND PZ2.DokladPrikazu=PrKV.Doklad AND PZ2.Splneno=0 AND PZ2.druhPohybuZbo IN (2,4,9,10) AND PZ2.SkutecneDatReal IS NULL) ELSE 0.0 END, CASE WHEN @RespekExistDoklady=0 THEN O.mnozstvi END, CASE WHEN @RespekExistDoklady=0 THEN P.kusy_zad END,~
         PrKV.Doklad, PrKV.IDPrikaz, PrKV.Prednastaveno, PrKV.Sklad, PrKV.VyrStredisko, PrKV.IDPracoviste, PrKV.mnoz_zad, PrKV.Mnoz_nepotrebne, PrKV.Mnoz_skut_realizovane , PrKV.Cena_real, PrKV.Mnoz_odv, PrKV.Cena_odv, PrKV.priorita, PrKV.vyssi, PrKV.nizsi, PrKV.DavkaTPV, PrKV.pozice, PrKV.Operace, PrKV.FixniMnozstvi,~
         PrKV.mnozstvi , PrKV.ProcZtrat, PrKV.mnozstviSeZtratou, PrKV.Prirez, PrKV.Poznamka, KZV.SkupZbo, KZV.RegCis, KZV.Nazev1, KZ.SkupZbo, KZ.RegCis, KZ.Nazev1~
  FROM (SELECT IDPrikaz=O1.IDPrikaz, Mnozstvi=SUM(O1.mnozstvi) + CASE WHEN @RespekExistDoklady=1 THEN MAX(P1.kusy_odved+P1.kusy_ztrac+P1.kusy_zmet) + (SELECT ISNULL(SUM(PZ1.prepmnozstvi*PZ1.mnozstvi), 0.0)~
            FROM [dbo].[TabPohybyZbozi] PZ1 WHERE PZ1.TypVyrobnihoDokladu=0 AND PZ1.IDPrikaz=O1.IDPrikaz AND PZ1.druhPohybuZbo=0 AND PZ1.SkutecneDatReal IS NULL) ELSE 0.0 END~
      FROM #apiOdved O1~
       INNER JOIN [dbo].[TabPrikaz] P1 ON (P1.ID=O1.IDPrikaz)~
       GROUP BY O1.IDPrikaz) O~
 INNER JOIN [dbo].[TabPrikaz] P ON (P.ID=O.IDPrikaz AND P.StavPrikazu IN (20,30,50))~
 INNER JOIN TabRadyPrikazu RP ON (RP.rada=P.rada)~
 INNER JOIN [dbo].[TabPrKVazby] PrKV ON (PrKV.IDPrikaz=O.IDPrikaz AND /*PrKV.predzpracovano=0 AND*/ PrKV.prednastaveno=1 AND PrKV.uzavreno=0 AND PrKV.RezijniMat=0  AND PrKV.IDOdchylkyDo IS NULL)~
 INNER JOIN [dbo].[TabKmenZbozi] KZ ON (KZ.ID=PrKV.nizsi AND KZ.sluzba=0)~
 LEFT JOIN [dbo].[TabKmenZbozi] KZV ON (KZV.ID=PrKV.vyssi AND KZV.sluzba=0)~
 WHERE (P.kusy_zive>0.0 OR RP.KontrolovatRozpracPriUzav=0)~
~
 SELECT * FROM #apiKusovnik~
END~
]]>
  </sql_text>
 </def>



 <def typ="TAB" nazev="_hdc_DataZone_Tasky" verze="20230209">
  <sql_text><![CDATA[
IF OBJECT_ID(N'dbo._hdc_DataZone_Tasky', N'U') IS NULL~
CREATE TABLE dbo._hdc_DataZone_Tasky (~
  ID int IDENTITY(1, 1) NOT NULL,~
  NovyZaznam bit DEFAULT 1 NOT NULL,~
  DeviceID nvarchar(30) COLLATE Czech_CI_AS NULL,~
  Stredisko nvarchar(30) COLLATE Czech_CI_AS NULL,~
  IdDataZoneTaskID int NOT NULL,~
  Obsah varchar(max) COLLATE Czech_CI_AS NULL,~
  Typ tinyint NULL,~
  DatZpracovani datetime NULL,~
  IDDoklad int NULL,~
  IDKoopObj int NULL,~
  CisloKoopObj nvarchar(30) COLLATE Czech_CI_AS NULL,~
  Kooperace bit DEFAULT 0 NOT NULL,~
  StatusText nvarchar(30) COLLATE Czech_CI_AS NULL,~
  DatumStart datetime NULL,~
  DatumKonec datetime NULL,~
  KodPracoviste nvarchar(5) COLLATE Czech_CI_AS NULL,~
  ZamestnanecID int NULL,~
  Zamestnanec int NULL,~
  Nezpracovavat bit DEFAULT 0 NOT NULL,~
  DatPorizeni datetime DEFAULT getdate() NOT NULL,~
  PosledniChyba nvarchar(255) COLLATE Czech_CI_AS DEFAULT N'' NOT NULL,~
  Autor nvarchar(80) COLLATE Czech_CI_AS DEFAULT suser_sname() NULL,~
  Blokovano bit DEFAULT 0 NOT NULL,~
  Barcode nvarchar(30) COLLATE Czech_CI_AS NULL,~
  CONSTRAINT PK_hdc_DataZone_Tasky PRIMARY KEY CLUSTERED (ID DESC)~
    WITH (~
      PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF,~
      ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)~
)~
ON [PRIMARY]~
~
IF EXISTS(SELECT 1 FROM sys.extended_properties WHERE [name]=N'MS_Description' AND major_id=OBJECT_ID(N'_hdc_DataZone_Tasky') AND CONVERT(nvarchar, [value]) LIKE N'1=%')~
 EXEC sp_dropextendedproperty @name='MS_Description', @level0type='schema', @level0name='dbo', @level1type='table', @level1name='_hdc_DataZone_Tasky', @level2type='column', @level2name='Typ'~
EXEC sp_addextendedproperty @name='MS_Description', @value='1=Kooperace, 2=Potvrzeni polKoopObj', @level0type='schema', @level0name='dbo', @level1type='table', @level1name='_hdc_DataZone_Tasky',~
                            @level2type='column', @level2name='Typ'~
~
IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE OBJECT_ID=OBJECT_ID(N'dbo._hdc_DataZone_Tasky') AND [name]=N'ei_hdc_DataZone_Tasky_KoopObj')~
CREATE NONCLUSTERED INDEX ei_hdc_DataZone_Tasky_KoopObj ON dbo._hdc_DataZone_Tasky (IDKoopObj)~
WITH (~
  PAD_INDEX = OFF,~
  DROP_EXISTING = OFF,~
  STATISTICS_NORECOMPUTE = OFF,~
  SORT_IN_TEMPDB = OFF,~
  ONLINE = OFF,~
  ALLOW_ROW_LOCKS = ON,~
  ALLOW_PAGE_LOCKS = ON)~
ON [PRIMARY]~
~
~
~
DECLARE @sql NVARCHAR(max)~
SET @sql=N'~
CREATE TRIGGER dbo.et_hdc_DataZone_Tasky_IU ON dbo._hdc_DataZone_Tasky~
WITH EXECUTE AS CALLER~
FOR INSERT, UPDATE~
AS~
BEGIN~
 SET NOCOUNT ON~
~ 
 DECLARE~
  @id INT,~
  @cZam INT,~
  @idZam INT~
~  
 DECLARE c CURSOR LOCAL FAST_FORWARD FOR~
  SELECT i.ID, i.Zamestnanec, i.ZamestnanecID FROM inserted i~
 OPEN c~
 WHILE (1=1)~
  BEGIN~
   FETCH NEXT FROM c INTO @id, @cZam, @idZam~
   IF (@@FETCH_STATUS<>0) BREAK~
~   
   IF (@cZam IS NOT NULL) AND (@idZam IS NULL)~
    UPDATE dbo._hdc_DataZone_Tasky SET ZamestnanecID=(SELECT ID FROM dbo.TabCisZam WHERE Cislo=@cZam) WHERE ID=@id~
~   
  END~
 CLOSE c~
 DEALLOCATE c~
~ 
END~
'~
IF NOT EXISTS(SELECT 1 FROM sys.triggers WHERE [name]=N'et_hdc_DataZone_Tasky_IU')~
 EXEC sp_executesql @sql~
~
~
~
~
--DECLARE @sql NVARCHAR(max)~
SET @sql=N'~
CREATE TRIGGER dbo.et_hdc_DataZone_Tasky_D ON dbo._hdc_DataZone_Tasky~
WITH EXECUTE AS CALLER~
FOR DELETE~
AS~
BEGIN~
 SET NOCOUNT ON~
~ 
 DECLARE~
  @iTemp INT,~
  @id INT~
~ 
~  
 DECLARE c CURSOR LOCAL FAST_FORWARD FOR~
  SELECT d.ID FROM deleted d~
 OPEN c~
 WHILE (1=1)~
  BEGIN~
   FETCH NEXT FROM c INTO @id~
   IF (@@FETCH_STATUS<>0) BREAK~
~   
   DELETE FROM dbo._hdc_DataZone_Subtasky WHERE IdTask=@id~
   IF NOT EXISTS(SELECT ID FROM dbo._hdc_DataZone_Subtasky)~
     TRUNCATE TABLE dbo._hdc_DataZone_Subtasky~
    ELSE~
     BEGIN~
      SELECT @iTemp=MAX(ID) FROM dbo._hdc_DataZone_Subtasky~
      DBCC CHECKIDENT(_hdc_DataZone_Subtasky, RESEED, @iTemp)~
     END~
~   
  END~
 CLOSE c~
 DEALLOCATE c~
~
~
 IF NOT EXISTS(SELECT ID FROM dbo._hdc_DataZone_Tasky)~
   TRUNCATE TABLE dbo._hdc_DataZone_Tasky~
  ELSE~
   BEGIN~
    SELECT @iTemp=MAX(ID) FROM dbo._hdc_DataZone_Tasky~
    DBCC CHECKIDENT(_hdc_DataZone_Tasky, RESEED, @iTemp)~
   END~
~
END'~
IF NOT EXISTS(SELECT 1 FROM sys.triggers WHERE [name]=N'et_hdc_DataZone_Tasky_D')~
 EXEC sp_executesql @sql~
]]>
  </sql_text>
 </def>



 <def typ="TAB" nazev="_hdc_DataZone_Subtasky" verze="20230209">
  <sql_text><![CDATA[
IF OBJECT_ID(N'dbo._hdc_DataZone_Subtasky', N'U') IS NULL~
 CREATE TABLE dbo._hdc_DataZone_Subtasky (~
  ID int IDENTITY(1, 1) NOT NULL,~
  IDTask int NOT NULL,~
  IdDataZoneTaskID int NULL,~
  Pozice tinyint DEFAULT 1 NOT NULL,~
  DatumStart datetime NULL,~
  DatumKonec datetime NULL,~
  Kusy_odv numeric(15, 2) DEFAULT 0.0 NULL,~
  BarcodeOperace nvarchar(20) COLLATE Czech_CI_AS NULL,~
  IDPrPostup int NULL,~
  StatusText nvarchar(30) COLLATE Czech_CI_AS NULL,~
  IDPolKoopObj int NULL,~
  IDPrikaz int NULL,~
  Doklad int NULL,~
  Alt nchar(1) COLLATE Czech_CI_AS NULL,~
  PosledniChyba nvarchar(255) COLLATE Czech_CI_AS DEFAULT N'' NULL,~
  CONSTRAINT PK_hdc_DataZone_Subtasky PRIMARY KEY CLUSTERED (ID DESC)~
    WITH (~
      PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF,~
      ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)~
)~
ON [PRIMARY]~
~
~
IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE OBJECT_ID=OBJECT_ID(N'dbo._hdc_DataZone_Subtasky') AND [name]=N'ei_hdc_DataZone_Subtasky_Task')~
CREATE NONCLUSTERED INDEX ei_hdc_DataZone_Subtasky_Task ON dbo._hdc_DataZone_Subtasky (IDTask)~
WITH (~
  PAD_INDEX = OFF,~
  DROP_EXISTING = OFF,~
  STATISTICS_NORECOMPUTE = OFF,~
  SORT_IN_TEMPDB = OFF,~
  ONLINE = OFF,~
  ALLOW_ROW_LOCKS = ON,~
  ALLOW_PAGE_LOCKS = ON)~
ON [PRIMARY]~
~
~
~
~
DECLARE @sql NVARCHAR(max)~
SET @sql=N'~
CREATE TRIGGER dbo.et_hdc_DataZone_Subtasky_IU ON dbo._hdc_DataZone_Subtasky~
WITH EXECUTE AS CALLER~
FOR INSERT, UPDATE~
AS~
BEGIN~
 SET NOCOUNT ON~
~ 
 DECLARE~
  @id INT,~
  @idTask INT,~
  @idDZTask INT,~
  @bc NVARCHAR(20),~
  @idPrPost INT,~
  @idVPr INT,~
  @dokl INT,~
  @alt NCHAR(1)~
~  
~  
 DECLARE c CURSOR LOCAL FAST_FORWARD FOR~
  SELECT i.ID, i.IdDataZoneTaskID, i.BarcodeOperace, i.IDPrPostup FROM inserted i~
 OPEN c~
 WHILE (1=1)~
  BEGIN~
   FETCH NEXT FROM c INTO @id, @idDZTask, @bc, @idPrPost~
   IF (@@FETCH_STATUS<>0) BREAK~
~
   IF (@idPrPost IS NULL) AND (@bc<>N'''')~
    BEGIN~
     SET @idPrPost=(SELECT ID FROM dbo.TabPrPostup WHERE IDOdchylkyDo IS NULL AND BarCode=@bc)~
     IF (@idPrPost IS NOT NULL)~
      BEGIN~
       SELECT @idVPr=IDPrikaz, @dokl=Doklad, @alt=Alt FROM dbo.TabPrPostup WHERE ID=@idPrPost~
       UPDATE dbo._hdc_DataZone_Subtasky SET IdPrPostup=@idPrPost, IDPrikaz=@idVPr, Doklad=@dokl, Alt=@alt WHERE ID=@id AND IDPrPostup IS NULL~
      END~
    END~
~
   SET @idTask=(SELECT ID FROM dbo._hdc_DataZone_Tasky WHERE IdDataZoneTaskID=@idDZTask)~
   UPDATE dbo._hdc_DataZone_Subtasky SET IDTask=@idTask WHERE ID=@id AND IDTask<>@idTask~
~
  END~
 CLOSE c~
 DEALLOCATE c~
~ 
END~
'~
IF NOT EXISTS(SELECT 1 FROM sys.triggers WHERE [name]=N'et_hdc_DataZone_Subtasky_IU')~
 EXEC sp_executesql @sql~
]]>
  </sql_text>
 </def>


 <def typ="TAB" nazev="_hdc_DataZone_MicharnaPozadavek" verze="20230516">
  <sql_text><![CDATA[
IF OBJECT_ID(N'dbo._hdc_DataZone_MicharnaPozadavek', N'U') IS NULL~
 CREATE TABLE dbo._hdc_DataZone_MicharnaPozadavek (~
  ID int IDENTITY(1, 1) NOT NULL,~
  IDDZReq INT NOT NULL,~
  DeviceID NVARCHAR(40) NULL,~
  DatPorizeni DATETIME NOT NULL DEFAULT GETDATE(),~
  DatPotvrzeni DATETIME NULL,~
  DatZpracovani DATETIME NULL,~
  IDDoklad INT,~
  Pozadavek nvarchar(max) NOT NULL,~
  JeJSON AS (CONVERT(bit, IIF(IsJson(Pozadavek)=1, 1, 0))),~
  CONSTRAINT PK_hdc_DataZone_MicharnaPozadavek PRIMARY KEY CLUSTERED (ID)~
    WITH (~
      PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF,~
      ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)~
 )~
ON [PRIMARY]~
~
~
IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE OBJECT_ID=OBJECT_ID(N'dbo._hdc_DataZone_MicharnaPozadavek') AND [name]=N'ei_hdc_DataZone_MicharnaPozadavek_DZReq')~
CREATE NONCLUSTERED INDEX ei_hdc_DataZone_MicharnaPozadavek_DZReq ON dbo._hdc_DataZone_MicharnaPozadavek~
  (IDDZReq)~
WITH (~
  PAD_INDEX = OFF,~
  DROP_EXISTING = OFF,~
  STATISTICS_NORECOMPUTE = OFF,~
  SORT_IN_TEMPDB = OFF,~
  ONLINE = OFF,~
  ALLOW_ROW_LOCKS = ON,~
  ALLOW_PAGE_LOCKS = ON)~
ON [PRIMARY]~
~
~
IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE OBJECT_ID=OBJECT_ID(N'dbo._hdc_DataZone_MicharnaPozadavek') AND [name]=N'ei_hdc_DataZone_MicharnaPozadavek_Doklad')~
CREATE NONCLUSTERED INDEX ei_hdc_DataZone_MicharnaPozadavek_Doklad ON dbo._hdc_DataZone_MicharnaPozadavek~
  (IDDoklad)~
WITH (~
  PAD_INDEX = OFF,~
  DROP_EXISTING = OFF,~
  STATISTICS_NORECOMPUTE = OFF,~
  SORT_IN_TEMPDB = OFF,~
  ONLINE = OFF,~
  ALLOW_ROW_LOCKS = ON,~
  ALLOW_PAGE_LOCKS = ON)~
ON [PRIMARY]
]]>
  </sql_text>
 </def>

 <def typ="PRO" nazev="ep_HDC_DataZone_Vyroba_VytvorKoopObjednavku" verze="20230525">
  <sql_text><![CDATA[
CREATE PROCEDURE dbo.ep_HDC_DataZone_Vyroba_VytvorKoopObjednavku~
   @idTask INT=NULL,~
   @opakovane BIT=0,~
   @radaKObj NVARCHAR(10)=NULL~
AS~
~
SET NOCOUNT ON~
~
DECLARE~
 @aktDate DATETIME=GETDATE(),~
 @errMsg NVARCHAR(500),~
 @iTemp INT,~
 @idSubtask INT,~
 @idDZTask INT,~
 @idKoopObj INT,~
 @cisKoopObj NVARCHAR(20),~
 @bc NVARCHAR(20),~
 @idOrgKoop INT,~
 @idPolKoopObj INT,~
 @idTypKoop INT,~
 @idKoop INT,~
 @idVPr INT,~
 @dokl INT,~
 @alt NCHAR(1),~
 @idPrPost INT,~
 @mnoz NUMERIC(19,6),~
 @idZam INT,~
 @cZam INT,~
 @loginName NVARCHAR(50),~
 @tranPred INT,~
 @idOrg INT,~
 @dicOrg NVARCHAR(15),~
 @jsonStr NVARCHAR(MAX)~
~
~
DECLARE @idKooperace TABLE (ID INT NOT NULL)~
~
~
~
SET @iTemp=ISNULL( (SELECT MAX(ID) FROM dbo.TabKoopObj), 1)~
DBCC CHECKIDENT(TabKoopObj, RESEED, @iTemp)~
~
SET @iTemp=ISNULL( (SELECT MAX(ID) FROM dbo.TabPolKoopObj), 1)~
DBCC CHECKIDENT(TabPolKoopObj, RESEED, @iTemp)~
~
~
IF (@idTask IS NULL)~
  DECLARE d CURSOR LOCAL FOR~
   SELECT ID, IdDataZoneTaskID, ZamestnanecID, Zamestnanec FROM dbo._hdc_DataZone_Tasky~
     WHERE Kooperace=1 AND DatZpracovani IS NULL AND IDKoopObj IS NULL AND Nezpracovavat=0 -- AND Blokovano=0~
       AND DATEDIFF(d, DatPorizeni, GETDATE())<2~
 ELSE~
  IF (@opakovane=0)~
    DECLARE d CURSOR LOCAL FOR~
     SELECT ID, IdDataZoneTaskID, ZamestnanecID, Zamestnanec FROM dbo._hdc_DataZone_Tasky~
       WHERE Kooperace=1 AND DatZpracovani IS NULL AND IDKoopObj IS NULL AND Nezpracovavat=0 AND ID=@idTask -- AND Blokovano=0~
   ELSE~
    DECLARE d CURSOR LOCAL FOR~
     SELECT ID, IdDataZoneTaskID, ZamestnanecID, Zamestnanec FROM dbo._hdc_DataZone_Tasky~
       WHERE Kooperace=1 AND ID=@idTask -- AND Blokovano=0~
OPEN d~
WHILE (1=1)~
 BEGIN~
  FETCH NEXT FROM d INTO @idTask, @idDZTask, @idZam, @cZam~
  IF (@@FETCH_STATUS<>0) BREAK~
~
  UPDATE dbo._hdc_DataZone_Tasky SET Blokovano=1 WHERE ID=@idTask~
~
  SET @loginName=(SELECT LoginId FROM dbo.TabCisZam WHERE Cislo=@cZam)~
  IF (ISNULL(@loginName,N'')=N'')~
   SET @loginName=(SELECT PrijmeniJmeno FROM dbo.TabCisZam WHERE ID=@idZam)~
  IF (ISNULL(@loginName,N'')=N'')~
   SET @loginName=ISNULL( (SELECT PrijmeniJmeno FROM dbo.TabCisZam WHERE Cislo=@cZam), SUSER_SNAME())~
~  
~
-- mam zapsane subtasky ?~
  IF NOT EXISTS(SELECT 1 FROM dbo._hdc_DataZone_Subtasky WHERE IDTask=@idTask)~
   BEGIN~
    SET @jsonStr=(SELECT Obsah FROM dbo._hdc_DataZone_Tasky WHERE ID=@idTask)~
    IF (@jsonStr IS NOT NULL)~
     IF (CHARINDEX(N'subtask', @jsonStr)>0) AND (ISJSON(@jsonStr)=1)~
      BEGIN~
       IF OBJECT_ID(N'tempdb..#TabJSONData', N'U') IS NOT NULL~
        DROP TABLE #TabJSONData~
       CREATE TABLE #TabJSONData (ID INT IDENTITY(1,1) NOT NULL, doc NVARCHAR(max))~
       INSERT #TabJSONData (doc) SELECT @jsonStr~
~
       INSERT dbo._hdc_DataZone_Subtasky (IDTask, IdDataZoneTaskID, Pozice, DatumStart, DatumKonec, Kusy_odv, BarcodeOperace, StatusText)~
        SELECT @idTask, @idDZTask, ROW_NUMBER() OVER (ORDER BY(SELECT NULL)), CONVERT(datetime2(0), startedAt), CONVERT(datetime2(0), finishedAt),~
               CONVERT(NUMERIC(19,6), v.kusy_odv), code, [status] -- JSON_VALUE(d.doc, '$.worker.code') AS CisloZam~
         FROM #TabJSONData d~
          CROSS APPLY OPENJSON(d.doc, '$.subtask')~
           WITH(code nvarchar(20), [status] NVARCHAR(30), startedAt NVARCHAR(25), finishedAt NVARCHAR(25), [values] nvarchar(max) AS JSON)~
            CROSS APPLY OPENJSON([values])~
             WITH (kusy_odv nvarchar(100)) as v~
~
       UPDATE st SET st.IDPrikaz=pp.IDPrikaz, st.Doklad=pp.Doklad, st.Alt=pp.Alt, st.IdPrPostup=pp.ID~
        FROM dbo._hdc_DataZone_Subtasky st~
         INNER JOIN dbo.TabPrPostup pp ON (pp.Barcode=st.BarcodeOperace AND pp.IDOdchylkyDo IS NULL)~
        WHERE st.IDTask=@idTask~
~
       IF OBJECT_ID(N'tempdb..#TabJSONData', N'U') IS NOT NULL~
        DROP TABLE #TabJSONData~
      END~
   END~
~
~
-- zapis nezpracovane subtasky~
  IF EXISTS(SELECT ID FROM dbo._hdc_DataZone_Subtasky WHERE IDTask=@idTask AND IDPolKoopObj IS NULL)~
   BEGIN~
    IF (@radaKObj IS NULL)~
     SET @radaKObj = (SELECT TOP(1) Rada FROM dbo.TabRadyKoopObj ORDER BY ID)~
    INSERT @idKooperace (ID) SELECT DISTINCT(IDkooperace) FROM dbo.TabPrPostup~
                               WHERE BarCode IN (SELECT BarcodeOperace FROM dbo._hdc_DataZone_Subtasky WHERE IDTask=@idTask)~
                                 AND IDOdchylkyDo IS NULL AND Typ=2 GROUP BY IDkooperace~
    IF (1=(SELECT COUNT(*) FROM (SELECT DISTINCT(ID) FROM @idKooperace GROUP BY ID) x))~
      BEGIN~
       SET @idTypKoop=(SELECT TOP(1) ID FROM @idKooperace)~
       SELECT @radaKObj=cke._RadaKoopObj FROM dbo.TabCKoop ck INNER JOIN dbo.TabRadyCKoop rck ON (rck.Rada=ck.Rada)~
         INNER JOIN dbo.TabRadyCKoop_EXT cke ON (cke.ID=ck.ID) WHERE ck.ID=@idTypKoop~
~
       IF (@radaKObj IS NOT NULL)~
        BEGIN~
         BEGIN TRY~
          SET @tranPred=@@TRANCOUNT~
          IF (@tranPred=0)~
            BEGIN TRAN~
           SET @aktDate = CONVERT(datetime, CONVERT(int, CONVERT(float, @aktDate)))~
           EXEC dbo.hp_InsertHlavickyKoopObj @IDObjednavky=@idKoopObj OUT, @Rada=@radaKObj, @TerminOdeslani=@aktDate~
           IF (@idKoopObj IS NOT NULL)~
            BEGIN~
             UPDATE dbo.TabKoopObj SET Autor=@loginName WHERE ID=@idKoopObj~
             SELECT @cisKoopObj=Objednavka FROM dbo.TabKoopObj WHERE ID=@idKoopObj~
~
             DECLARE p CURSOR LOCAL FOR~
              SELECT ID, IDPrikaz, Doklad, Alt, Kusy_odv, BarcodeOperace, IDPrPostup FROM dbo._hdc_DataZone_Subtasky~
                WHERE IDTask=@idTask AND IDPolKoopObj IS NULL~
             OPEN p~
             WHILE (1=1)~
              BEGIN~
               FETCH NEXT FROM p INTO @idSubtask, @idVPr, @dokl, @alt, @mnoz, @bc, @idPrPost~
               IF (@@FETCH_STATUS<>0) BREAK~
~
               IF (@idPrPost IS NULL)~
                SET @idPrPost=(SELECT ID FROM dbo.TabPrPostup WHERE IDOdchylkyDo IS NULL AND BarCode=@bc)~
               IF NOT EXISTS(SELECT ID FROM dbo.TabPrPostup WHERE ID=@idPrPost)~
                SET @idPrPost = (SELECT ID FROM dbo.TabPrPostup WHERE IDOdchylkyDo IS NULL AND BarCode=@bc)~
               SELECT @idVPr=IDPrikaz, @dokl=Doklad, @alt=Alt, @idKoop=IDKooperace FROM dbo.TabPrPostup WHERE ID=@idPrPost~
               IF EXISTS(SELECT pp.ID FROM dbo.TabPrPostup pp INNER JOIN dbo.TabPrikaz p ON (p.ID=pp.IDPrikaz) WHERE pp.ID=@idPrPost AND pp.predzpracovano=1 AND p.StavPrikazu=30)~
                UPDATE dbo.TabPrPostup SET predzpracovano=0 WHERE ID=@idPrPost~
~
               EXEC dbo.hp_InsertPolozkyKoopObj @IDPolozky=@idPolKoopObj OUT, @IDObjednavky=@idKoopObj, @IDPrikaz=@idVPr, @Doklad=@dokl, @Alt=@alt, @Kusy=@mnoz, @IDKooperace=@idKoop~
               IF (@idPolKoopObj IS NOT NULL)~
                BEGIN~
                 UPDATE dbo._hdc_DataZone_Subtasky SET IDPolKoopObj=@idPolKoopObj WHERE ID=@idSubtask~
                 UPDATE dbo.TabPolKoopObj SET Autor=@loginName WHERE ID=@idPolKoopObj~
                END~
~
              END~
             CLOSE p~
             DEALLOCATE p~
~
             SET @idOrg=(SELECT dodavatel FROM dbo.TabCKoop WHERE ID=@idKoop)~
             SET @dicOrg=(SELECT DIC FROM dbo.TabCisOrg WHERE ID=@idOrg)~
             UPDATE dbo.TabKoopObj SET IDOrganizace=@idOrg, DIC=@dicOrg WHERE ID=@idKoopObj~
~
             UPDATE dbo._hdc_DataZone_Tasky SET IDKoopObj=@idKoopObj, DatZpracovani=GETDATE(), Nezpracovavat=0, CisloKoopObj=@cisKoopObj, Blokovano=0~
               WHERE ID=@idTask~
~
            END -- @IDObjednavky IS NOT NULL~
          IF (@@TRANCOUNT>0) AND (@tranPred=0)~
           COMMIT TRAN~
         END TRY~
         BEGIN CATCH~
          IF (@@TRANCOUNT>0) AND (@tranPred=0)~
           ROLLBACK TRAN~
          SET @errMsg = ERROR_MESSAGE()~
~
          IF (CHARINDEX(N'2001434', @errMsg)>0) -- vyrobni operace jiz neexistuje~
            UPDATE dbo._hdc_DataZone_Tasky SET DatZpracovani=GETDATE(), Nezpracovavat=1, PosledniChyba=N'Výrobní operace již neexistuje' WHERE ID=@idTask~
           ELSE~
            INSERT dbo._hdc_Log (Typ, LogText, intVal) SELECT 11, N'Import kooperace - chyba: ' + @errMsg, @idTask~
~
         END CATCH~
        END -- @radaKObj IS NOT NULL~
~
      END -- jen jeden druh kooperace~
     ELSE~
      BEGIN~
       UPDATE dbo._hdc_DataZone_Tasky SET PosledniChyba=N'Naskenováno víc typů kooperace', Blokovano=0 WHERE ID=@idTask~
      END~
~
   END -- existuji subtasky ? ano, vytvor kooperacni objednavku~
~
 END~
CLOSE d~
DEALLOCATE d
]]>
  </sql_text>
 </def>


 <def typ="COL">
  <sql_text><![CDATA[
DECLARE @currDat DATETIME=GETDATE()~
IF (COL_LENGTH(N'dbo.TabRadyCKoop_EXT', N'_RadaKoopObj') IS NULL)~ 
 EXEC dbo.hp_ImportUzivAtr @Externi=1, @NazevTabulkySys=N'TabRadyCKoop_EXT', @NazevAtrSys=N'_RadaKoopObj', @Skupina=N'HDC API', @NazevAtrVer=N'Řada kooperačních objednávek',~
         @NazevAtrVerZkr=N'Řada koopObj', @TypAtr=N'NVARCHAR', @MaskaAtr=NULL, @VerejnyAtr=N'A', @SirkaSloupceAtr=8, @SumovatAtr=N'N', @TiskAtr=N'A', @ProcPruhAtr=0,~
         @KonverzeAtr=NULL, @ExtEd_Poradi=1, @ExtEd_Zalozka=NULL, @ExtEd_BrowseID=11142, @ExtEd_Atr=N'Rada', @ExtEd_PrimaEditace=1, @DatPorizeni=@currDat, @DatZmeny=NULL,~
         @TypAtrSQL=N'NVARCHAR', @ExtEd_BrowseID_DPSN=NULL, @Poznamka=NULL, @Definice=NULL, @Podminka=NULL
]]>
  </sql_text>
 </def>


 <def typ="COL">
  <sql_text><![CDATA[
DECLARE @currDat DATETIME=GETDATE()~
IF (COL_LENGTH(N'dbo.TabDokumenty_EXT', N'_DokladProAPI') IS NULL)~ 
 EXEC dbo.hp_ImportUzivAtr @Externi=1, @NazevTabulkySys=N'TabDokumenty', @NazevAtrSys=N'_DokladProAPI', @Skupina=N'HDC API', @NazevAtrVer=N'Přednastaveno pro HDC API',~
         @NazevAtrVerZkr=N'Pro HDCAPI', @TypAtr=N'BIT', @MaskaAtr=NULL, @VerejnyAtr=N'A', @SirkaSloupceAtr=12, @SumovatAtr=N'N', @TiskAtr=N'A', @ProcPruhAtr=0,~
         @KonverzeAtr=NULL, @ExtEd_Poradi=1, @ExtEd_Zalozka=NULL, @ExtEd_BrowseID=NULL, @ExtEd_Atr=NULL, @ExtEd_PrimaEditace=1, @DatPorizeni=@currDat, @DatZmeny=NULL,~
         @TypAtrSQL=N'BIT', @ExtEd_BrowseID_DPSN=NULL, @Poznamka=NULL, @Definice=NULL, @Podminka=NULL, @HromadneZmenyAtr=1~
EXEC dbo.hp_ExterniTabulka N'TabDokumenty',N'_DokladProAPI',N'BIT'
]]>
  </sql_text>
 </def>


</defs>
