
CREATE PROCEDURE [dbo].[API_DokUpdateErp]
	@DokErpId AS INT
AS 
BEGIN



-- zabezpieczenie przed pętlą czyli sprawdzenie czy 

IF (NOT EXISTS( select * from  dbo.ApiSyncItems where ObjectId = @DokErpId and ObjectType=0 and  HistId = 
(select MAX(ci.Id) 
from dbo.ChangeInfos ci JOIN dbo.Zadania z ON  SourceTable='DokHandlowe' and SourceGuid= z.Guid
where z.ID = @DokErpId)))
BEGIN


UPDATE 
dbo.ApiDocuments -- WITH (UPDLOCK, SERIALIZABLE) 
  SET 
[ErpId] =cv.ErpId,[DocId] = cv.DocId ,[CustId] = cv.[CustId]
      ,[Number] = cv.[Number],[Year]  = cv.[Year] ,[Month]  = cv.Month
      --,[DefId] = cv.[DefId]
	  ,[NumberString] = cv.[NumberString],[ErpNumberString] = cv.[ErpNumberString]
      ,[Series] = cv.[Series]
      ,[Value] = cv.[Value]
      ,[Description] =cv.[Description]
      ,[Date] = cv. [Date]
      ,[Date2] =cv.[Date2]
      ,[State] = cv.[State]
      ,[vendorId] = cv.[vendorId]
      ,[TS] =CURRENT_TIMESTAMP
      ,[VeryfiedDoc] = cv.[VeryfiedDoc]
      ,[BarCode] = cv.[BarCode]
      ,[Lines] = cv.[Lines]
      ,[Info] = cv.[Info]
      ,[FlowDirection] =cv.[FlowDirection]
      ,[CustCustId] =cv.[CustCustId]
      ,[CustLocId] = cv.[CustLocId]
      ,[CustLocLocId] = cv.[CustLocLocId]
      ,[Currency] =cv.[Currency]
      ,[RepositoryId] = cv.[RepositoryId]
      ,[PrecedentDoc] = cv.[PrecedentDoc]
      ,[PrecedentDocNumber] = cv.[PrecedentDocNumber]
      ,[ConfirmTransfer] = cv.[ConfirmTransfer]
      ,[SaveTS] = cv.[SaveTS]
      ,[DeviceUid] = cv.[DeviceUid]
      ,[ForeginNumber] = cv.[ForeginNumber]
      ,[Category] =cv.[Category]
      ,[NettoBrutto]= cv.[NettoBrutto]
      ,[Netto] =cv.[Netto]
      ,[Brutto] = cv.[Brutto]
      ,[StateDoc] =cv.[StateDoc]
      ,[OperatorId] = cv.[OperatorId]
      ,[ReservationResource] = cv.[ReservationResource]
      ,[ReservationType] =cv.[ReservationType]
      ,[ProjectId] = cv.[ProjectId]
      ,[ProjectLineId] = cv.[ProjectLineId]
	  , CreateTS = cv.CreateTS

  from (select *from  dbo.[ApiDocumentsViewBase] cc where cc.ErpId=@DokErpId) cv
  WHERE 
  dbo.ApiDocuments.ErpId = cv.ErpId and 
  dbo.ApiDocuments.ErpId = @DokErpId;

IF @@ROWCOUNT = 0
BEGIN
  
	--select 'trzeba dodac';  
 INSERT INTO [dbo].ApiDocuments
           ([ErpId]
      ,[DocId]
      ,[CustId]
      ,[Number]
      ,[Year]
      ,[Month]
      ,[DefId]
      ,[NumberString]
      ,[ErpNumberString]
      ,[Series]
      ,[Value]
      ,[Description]
      ,[Date]
      ,[Date2]
      ,[State]
      ,[vendorId]
      ,[TS]
      ,[VeryfiedDoc]
      ,[BarCode]
      ,[Lines]
      ,[Info]
      ,[FlowDirection]
      ,[CustCustId]
      ,[CustLocId]
      ,[CustLocLocId]
      ,[Currency]
      ,[RepositoryId]
      ,[PrecedentDoc]
      ,[PrecedentDocNumber]
      ,[ConfirmTransfer]
      ,[SaveTS]
      ,[DeviceUid]
      ,[ForeginNumber]
      ,[Category]
      ,[NettoBrutto]
      ,[Netto]
      ,[Brutto]
      ,[StateDoc]
      ,[OperatorId]
      ,[ReservationResource]
      ,[ReservationType]
      ,[ProjectId]
      ,[ProjectLineId]
	  ,CreateTS) 
    select 
            d.[ErpId]
      ,[DocId]
      ,[CustId]
      ,[Number]
      ,[Year]
      ,[Month]
      ,def.Id [DefId]
      ,[NumberString]
      ,[ErpNumberString]
      ,[Series]
      ,[Value]
      ,[Description]
      ,[Date]
      ,[Date2]
      ,[State]
      ,[vendorId]
      ,[TS]
      ,[VeryfiedDoc]
      ,[BarCode]
      ,[Lines]
      ,[Info]
      ,d.[FlowDirection]
      ,[CustCustId]
      ,[CustLocId]
      ,[CustLocLocId]
      ,[Currency]
      ,[RepositoryId]
      ,[PrecedentDoc]
      ,[PrecedentDocNumber]
      ,[ConfirmTransfer]
      ,[SaveTS]
      ,[DeviceUid]
      ,[ForeginNumber]
      ,[Category]
      ,d.[NettoBrutto]
      ,[Netto]
      ,[Brutto]
      ,[StateDoc]
      ,[OperatorId]
      ,[ReservationResource]
      ,d.[ReservationType]
      ,[ProjectId]
      ,[ProjectLineId]
	  , CreateTS
		   from dbo.[ApiDocumentsViewBase] d
		  JOIN dbo.ApiDefinitions def on def.ErpId =DefId and def.Behaviour = 4
		   where d.ErpId = @DokErpId

END

END

declare @DokId int;
select @DokId = Id from dbo.ApiDocuments where ErpId = @DokErpId;

-- doajemy te linie których nie ma
INSERT INTO dbo.ApiLines(
      [ErpId]
      ,[lineId]
      ,[documentId]
      ,[IdDocId]
      ,[lp]
      ,[itemId]
      ,[itemName]
      ,[itemCode]
      ,[itemDescription]
      ,[itemUM]
      ,[itemUM2]
      ,[itemPrice]
      ,[itemOrgPrice]
      ,[discount]
      ,[quantity]
      ,[value]
      ,[veryfiedQuantity]
      ,[itemEAN]
      ,[vendorId]
      ,[LocationName]
      ,[Batch]
      ,[BatchId]
      ,[BatchEAN]
      ,[Status]
      ,[CategoryId]
      ,[CategoryName]
      ,[ItemStatus]
      ,[lineStatus]
      ,[lineChance]
      ,[CategoryName2]
      ,[LineType]
      ,[Netto]
      ,[Brutto]
      ,[TaxDesc]
      ,[TaxRate]
      ,[ReservationType]
      ,[Direction]
      ,[RepositoryId])
select [ErpId]
      ,[lineId]
      , @DokId [documentId] -- podmiana na właściwy
      , @DokId [IdDocId]
      ,[lp]
      ,[itemId]
      ,[itemName]
      ,[itemCode]
      ,[itemDescription]
      ,[itemUM]
      ,[itemUM2]
      ,[itemPrice]
      ,[itemOrgPrice]
      ,[discount]
      ,[quantity]
      ,[value]
      ,[veryfiedQuantity]
      ,[itemEAN]
      ,[vendorId]
      ,[LocationName]
      ,[Batch]
      ,[BatchId]
      ,[BatchEAN]
      ,[Status]
      ,[CategoryId]
      ,[CategoryName]
      ,[ItemStatus]
      ,[lineStatus]
      ,[lineChance]
      ,[CategoryName2]
      ,[LineType]
      ,[Netto]
      ,[Brutto]
      ,[TaxDesc]
      ,[TaxRate]
      ,[ReservationType]
      ,[Direction]
      ,[RepositoryId]
  FROM [dbo].[ApiLinesInfoView]
  where [documentId] = @DokErpId and [ErpId] NOT in ( select ErpId from dbo.ApiDeviceLines)

-- update wszystkich lini lini....
update dbo.ApiLines set
[ErpId] = cv.ErpId
      ,[lineId] = cv.lineId
      ,[lp] = cv.lp
      ,[itemId] = cv.itemId
      ,[itemName] = cv.itemName
      ,[itemCode] = cv.itemCode
      ,[itemDescription] = cv.itemDescription
      ,[itemUM] = cv.itemUM
      ,[itemUM2] = cv.itemUM2
      ,[itemPrice] = cv.itemPrice
      ,[itemOrgPrice] = cv.itemOrgPrice
      ,[discount] = cv.discount
      ,[quantity] = cv.quantity
      ,[value] = cv.value
      ,[veryfiedQuantity] = cv.veryfiedQuantity
      ,[itemEAN] = cv.itemEAN
      ,[vendorId] = cv.vendorId
      ,[LocationName] = cv.LocationName
      ,[Batch] = cv.Batch
      ,[BatchId]= cv.BatchId
      ,[BatchEAN] = cv.BatchEAN
      ,[Status] = cv.Status 
      ,[CategoryId] = cv.CategoryId
      ,[CategoryName] = cv.CategoryName
      ,[ItemStatus] = cv.ItemStatus
      ,[lineStatus] = cv.lineStatus
      ,[lineChance] = cv.lineChance
      ,[CategoryName2] = cv.CategoryName2
      ,[LineType] = cv.LineType
      ,[Netto] = cv.Netto
      ,[Brutto]= cv.Brutto
      ,[TaxDesc] = cv.TaxDesc
      ,[TaxRate] = cv.TaxRate
      ,[ReservationType] = cv.ReservationType
      ,[Direction] = cv.Direction
      ,[RepositoryId] = cv.RepositoryId

from
(select *from  dbo.ApiLinesInfoView cc where cc.[documentId]=@DokErpId) cv
  WHERE 
  dbo.ApiLines.ErpId = cv.ErpId and 
  dbo.ApiLines.documentId = @DokId;



-- usunąć te linie co są w tym dokumencie ale nie ma ich w erp
delete dbo.ApiLines where documentId =@DokId and ErpId<>0 and not exists ( select * from dbo.ApiLinesInfoView where dbo.ApiLinesInfoView.ErpId =  dbo.ApiLines.ErpId);

END



