


CREATE VIEW [dbo].[ApiWMSErpToImportList] as
select  d.* from dbo.DokHandlowe d where 
  (TypPartii=1  OR Definicja = 85) -- lub ZD
  and  d.NumerSymbol  NOT LIKE 'KWPZ%'
	and Stan = 1 and KierunekMagazynu <>0
	-- wykluczyć te co są zrobione z ZD
	and NOT EXISTS (select * from dbo.RelacjeHandlowe r JOIN dbo.DokHandlowe zd ON r.Nadrzedny = zd.ID where zd.Definicja = 85 and r.Podrzedny = d.ID)
   and NOT EXISTS ( select * from dbo.[ApiSyncItems] s where s.[SchemaId] = -10 and s.ObjectType = 40 and s.[ObjectId] = d.ID and s.Status = 2)
   and NOT EXISTS ( select * from dbo.Features where Name='WMS_Niegeneruj' and ParentType='DokHandlowe' and Parent = d.ID and DataKey=1)
   and d.Id not in ( select ErpId from dbo.ApiWmsDocuments where ObjectType =0 and ObjectId=0) 
GO


