EXEC(N'CREATE OR ALTER VIEW HM.InventoryDocumentsPositionsCardIndexView
AS 
SELECT
	poz.id
	,nag.id AS Super
	,nag.kod AS Code
	,nag.datasp AS IssueDate
	,poz.lp AS lp
	,CASE ISNULL(poz.idpozkoryg,-1) WHEN -1 THEN ABS(poz.ilosc)
	ELSE 
	Case When nag.subtyp = 81 then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 1)*-1 
		 When nag.subtyp = 83 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 1)*-1
		 When nag.subtyp = 75 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 1)*-1
		 Else hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 1) 
	END
	
	END AS Quantity
	,CASE ISNULL(poz.idpozkoryg,-1) WHEN -1 then ABS(poz.iloscwp)
	ELSE 
		Case When nag.subtyp = 81 then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 2)*-1 
		 When nag.subtyp = 83 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 2)*-1
		 When nag.subtyp = 75 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 2)*-1
		 Else hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 2) 
	END
	END AS InputQuantity
	,poz.jm
	,poz.jmwp
	,poz.kod AS ProductCode
	,CASE WHEN tw.subtypext = 0 THEN ''artykuł''
		  WHEN tw.subtypext = 4 THEN ''usługa marża''
          WHEN tw.subtypext = 5 THEN ''opakowanie''
		  ELSE '''' END AS ProductType
	,poz.opis AS ProductDescription
	,tw.JPK_V7 AS GTU
	,CASE ISNULL(poz.idpozkoryg,-1) WHEN -1 then CASE WHEN nag.subtyp in(76,78, 84) then poz.wartNetto else poz.wartNetto*-1 end 
	ELSE 
		Case When nag.subtyp = 81 then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 3)*-1 
		 When nag.subtyp = 83 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 3)*-1
		 When nag.subtyp = 75 Then hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 3)*-1
	     Else hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 3) 
	END 
	END AS NetValue
	,CASE WHEN nag.bufor = 0 THEN ''wystawione'' ELSE ''w buforze'' END AS Buffer
	,nag.aktywny AS IsActive
	,ISNULL((SELECT KH.kod FROM hm.kh WHERE KH.id = poz.magazyn), '''') AS InventoryCode
	,ROUND( CASE WHEN poz.idpozkoryg IS NULL THEN poz.cena
                 WHEN hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 4) = 0 THEN poz.cena
                 ELSE hm.GetValueItemForInventoryPositionCardIndex(nag.id, poz.lp, 4) * IIF(nag.przychod <> 0, -1, 1) END, 6) AS PriceJm
	,ISNULL(ctr.kod, '''') AS KhCode
	,ISNULL(AD.nazwa,'''') AS KhName 
	,ISNULL(AD.NIP, '''') AS NIP
	,pc.*
FROM  HM.MZ poz
JOIN HM.MG nag ON nag.id = poz.super
JOIN HM.AppUserWarehouses UsrRights ON UsrRights.WarehouseId = nag.magazyn
JOIN HM.DocumentTypePermission DocPermission ON DocPermission.UserId=UsrRights.UserId AND DocPermission.DocumentTypeCode = nag.typ_dk
LEFT JOIN HM.TW ON tw.id = poz.idtw and tw.subtypext <> 3
LEFT JOIN HM.AD ON AD.id = nag.khadid
LEFT JOIN SSCommon.vKh ctr ON ctr.id = nag.khid
INNER JOIN SSCommon.STGukView guk on ISNULL(guk.ContractorId,0)= ISNULL(nag.khid,0)
LEFT JOIN SSCommon.HMF_WarehouseDocumentPositionClassification pc ON pc.ElementId = poz.id
WHERE poz.kompensata=0
')   
