
CREATE PROCEDURE [dbo].[API_KntUpdateErp]
	@KntErpId AS INT
AS 
BEGIN
/*
declare @KntErpId int;
set @KntErpId = 2;
*/
-- zabezpieczenie przed pętlą czyli sprawdzenie czy 
IF (NOT EXISTS( select * from  dbo.ApiSyncItems where ObjectId = @KntErpId and ObjectType=3 and  HistId = 
(select MAX(ci.Id) 
from sn_spolka.dbo.ChangeInfos ci JOIN sn_spolka.dbo.Kontrahenci k ON  SourceTable='Kontrahenci' and SourceGuid= k.Guid
where k.ID = @KntErpId)))
BEGIN


UPDATE 
dbo.ApiCustomers -- WITH (UPDLOCK, SERIALIZABLE) 
  SET    
      [ErpId] = cv.ErpId
      ,[Name] = cv.Name
      ,[Code] = cv.Code
      ,[TAXID] = cv.[TAXID]
      ,[Type] = cv.[Type]
      ,[AdrrZip] = cv.[AdrrZip]
      ,[AdrrCity] = cv.[AdrrCity]
      ,[AdrrStreet]= cv.[AdrrStreet]
      ,[AdrrNumber] = cv.[AdrrNumber]
      ,[AdrrAppNumber] = cv.[AdrrAppNumber]
      ,[AdrrCountry] = cv.[AdrrCountry]
      ,[Phone] = cv.[Phone]
      ,[Phone2] =cv.[Phone2]
      ,[Email] = cv.[Email]
      ,[Notes] = cv.[Notes]
      ,[CategoryName] = cv.[CategoryName]
      ,[CategoryId] = cv.[CategoryId]
      ,[Locked] = cv.[Locked]
      ,[payment] =cv.[payment]
      ,[paymentTime] = cv.[paymentTime]
      ,[vendorId] =cv.[vendorId]
      ,[EAN] = cv.[EAN]
      ,[geolat] = cv.[geolat]
      ,[geolong] =cv.[geolong]
      ,[LoyaltyScore] = cv.[LoyaltyScore]
      ,[LoyaltyPasswd] =cv.[LoyaltyPasswd]
      ,[VendorName] = cv.[VendorName]
      ,[VendorPhone] = cv.[VendorPhone]
      ,[VendorMail] = cv.[VendorMail]
      ,[WWW] =cv.[WWW]
      ,[Status] =cv.[Status]
      ,[TS] =CURRENT_TIMESTAMP
      --,[SaveTS] = cv.[SaveTS]
      --,[DeviceUid]  = cv.[DeviceUid]
      ,[State] = cv.[State]
      ,[Discount] = cv.[Discount]
      ,[paymentId] =cv.[paymentId]
	  , RegionId = cv.RegionId
	  ,Features = cv.Features
	  , AdrrCountryCode = cv.AdrrCountryCode
  
  from (select *from  dbo.[ApiCustomerView] cc where cc.ErpId=@KntErpId) cv
  WHERE 
  dbo.ApiCustomers.ErpId = cv.ErpId and 
  dbo.ApiCustomers.ErpId = @KntErpId;

IF @@ROWCOUNT = 0
BEGIN
  
	--select 'trzeba dodac';  
 INSERT INTO [dbo].[ApiCustomers]
([CustId],[ErpId],[Name],[Code],[TAXID],[Type],[AdrrZip],[AdrrCity],[AdrrStreet],[AdrrNumber],[AdrrAppNumber],[AdrrCountry],AdrrCountryCode, [Phone],[Phone2],[Email]
,[Notes],[CategoryName],[CategoryId],[Locked],[payment],[paymentTime],[vendorId],[EAN],[geolat],[geolong],[LoyaltyScore],[LoyaltyPasswd],
[VendorName] ,[VendorPhone],[VendorMail],[WWW],[Status]
,[TS],[SaveTS],[DeviceUid],[State],[Discount],[paymentId],RegionId,Features)
select 
[CustId],[ErpId],[Name],[Code],[TAXID],[Type],[AdrrZip],[AdrrCity],[AdrrStreet],[AdrrNumber],[AdrrAppNumber],[AdrrCountry],AdrrCountryCode,[Phone],[Phone2],[Email]
,[Notes],[CategoryName],[CategoryId],[Locked],[payment],[paymentTime],[vendorId],[EAN],[geolat],[geolong],[LoyaltyScore],[LoyaltyPasswd]
,[VendorName],[VendorPhone],[VendorMail],[WWW],[Status]
,CURRENT_TIMESTAMP, 0 [SaveTS],[DeviceUid],[State],[Discount],[paymentId],RegionId, Features
		   from dbo.ApiCustomerView where ErpId = @KntErpId
  
  --INSERT dbo.table(PK, ...) SELECT @PK, ...;
END

END
END
