

/****** Object:  View [dbo].[ApiDocsStatusView]    Script Date: 13.09.2021 14:22:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE VIEW [dbo].[ApiDocsStatusView] as
SELECT  [Id]
      , ErpId      
      , [ErpNumberString]
	  ,ISNULL(podrz.TrN_NumerPelny,'')  [ErpNumberString2]
      ,
	  case when (  ISNULL(podrz.TrN_NumerPelny,'')<>'')
	  then 5 
	  else case when (ErpId<>0) then 4
	  else 3 end end   
	    [State]

  FROM [dbo].[ApiDocuments]  dok
  
  LEFT OUTER JOIN
  CDN.TraNagRelacje rel  ON rel.TrR_TrNId = dok.ErpId
  LEFT OUTER JOIN CDN.TraNag podrz ON podrz.TrN_TrNId = rel.TrR_FaId
 

GO


