

/****** Object:  StoredProcedure [dbo].[API_TaskMeetingUpdateErp]    Script Date: 2021-09-08 09:58:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[API_TaskMeetingUpdateErp]
	@TaskErpId AS INT
AS 
BEGIN

-- zabezpieczenie przed pętlą czyli sprawdzenie czy 

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

UPDATE
dbo.ApiCrmTasks -- WITH (UPDLOCK, SERIALIZABLE) 
  SET  
  [ErpId] = cv.[ErpId],[DefId] = cv.[DefId],[CustId] = cv.[CustId], [CustLocationId] = cv.[CustLocationId], [ParentId] = cv.[ParentId], [Priority] = cv.[Priority]
  ,[Title] = cv.[Title]
  , [Description] = cv.[Description],[Notes] = cv.[Notes], [PlanDateFrom] = cv.[PlanDateFrom],[PlanDateTo] = cv.[PlanDateTo]
,[StateId] = cv.StateId,[ProductId]=cv.[ProductId],[TypeId] = cv.[TypeId],[CategoryId] = cv.[CategoryId],[DocumentId] = cv.[DocumentId],[DeviceId] = cv.[DeviceId],[TimeSpend]=cv.[TimeSpend]
,[Number] = cv.[Number],[Year] = cv.[Year],[Month]=cv.[Month],[Series]=cv.[Series],[NumberString]=cv.[NumberString],[Modified]=cv.[Modified],[DayLp]=cv.[DayLp]
,[RouteDistanceTo]=cv.[RouteDistanceTo],[RouteTimeTo]=cv.[RouteTimeTo],[OwnerId]=cv.[OwnerId],[VendorId]=cv.[VendorId],[EventId]=cv.[EventId],[TS]=CURRENT_TIMESTAMP
,[CustInfo]=cv.[CustInfo],[DocumentId2]=cv.[DocumentId2],[DocumentId3]=cv.[DocumentId3],[SaveTS]=cv.[SaveTS],[DeviceUid]=cv.[DeviceUid],[CustCustId]=cv.[CustCustId]
,[CustLocLocId]=cv.[CustLocLocId],[TaskSummaryId]=cv.[TaskSummaryId],[ModTS]=CURRENT_TIMESTAMP ,[Distance]=cv.[Distance],[Income]=cv.[Income],[Cost]=cv.[Cost]
,[CustErpId]=cv.[CustErpId],[CustLocErpId]=cv.[CustLocErpId],[DepartamentId]=cv.[DepartamentId],[CustContactPersonId]=cv.[CustContactPersonId],[ProjectId]=cv.[ProjectId],[ProjectLineId]=cv.[ProjectLineId]
  
  from (select *from  dbo.[ApiCrmTaskTupleView] cc where cc.ErpId=@TaskErpId) as cv
  WHERE 
  dbo.ApiCrmTasks.ErpId = cv.ErpId and 
  dbo.ApiCrmTasks.ErpId = @TaskErpId;


IF @@ROWCOUNT = 0
BEGIN
  
	--select 'trzeba dodac';  
 INSERT INTO [dbo].[ApiCrmTasks]
           ([TaskId],[ErpId],[DefId],[CustId],[CustLocationId],[ParentId],[Priority]
           ,[Title],[Description],[Notes],[PlanDateFrom],[PlanDateTo]
           ,[StateId],[ProductId],[TypeId],[CategoryId],[DocumentId],[DeviceId],[TimeSpend]
           ,[Number],[Year],[Month],[Series],[NumberString],[Modified],[DayLp]
           ,[RouteDistanceTo],[RouteTimeTo],[OwnerId],[VendorId],[EventId],[TS]
           ,[CustInfo],[DocumentId2],[DocumentId3],[SaveTS],[DeviceUid],[CustCustId]
           ,[CustLocLocId],[TaskSummaryId],[ModTS],[Distance],[Income],[Cost]
           ,[CustErpId],[CustLocErpId],[DepartamentId],[CustContactPersonId],[ProjectId],[ProjectLineId])
    select 
            [TaskId],[ErpId],[DefId],[CustId],[CustLocationId],[ParentId],[Priority]
           ,[Title],[Description],[Notes],[PlanDateFrom],[PlanDateTo]
           ,[StateId],[ProductId],[TypeId],[CategoryId],[DocumentId],[DeviceId],[TimeSpend]
           ,[Number],[Year],[Month],[Series],[NumberString],[Modified],[DayLp]
           ,[RouteDistanceTo],[RouteTimeTo],[OwnerId],[VendorId],[EventId],[TS]
           ,[CustInfo],[DocumentId2],[DocumentId3],[SaveTS],[DeviceUid],[CustCustId]
           ,[CustLocLocId],[TaskSummaryId],CURRENT_TIMESTAMP,[Distance],[Income],[Cost]
           ,[CustErpId],[CustLocErpId],[DepartamentId],[CustContactPersonId],[ProjectId],[ProjectLineId]
		   from dbo.[ApiCrmTaskTupleView] where ErpId = @TaskErpId

END




END
END


--select dbo.ApiTaskView.ErpId from dbo.ApiTaskView 
GO


