USE [MinovaES_DEMO_GolfCourse]
GO
/****** Object:  StoredProcedure [dbo].[SPERPRPTSALESDOCDETAIL]    Script Date: 20/10/2025 14.34.27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[SPERPRPTSALESDOCDETAIL]

 @DocNo varchar(8),
 @DocDate varchar (8),
 @DocType varchar (8)

 as

 begin

--declare @DocNo varchar(8) =''
--declare @DocDate varchar (8) =''
--declare @DocType varchar (8) =''

select distinct salesitems.DocNo
,header.DocDate
,header.DocType
,doctype.DocTypeDesc as DocTypeDesc
,salesitems.ItemType as ServiceType
,_itemtype.[Description] as ServiceTypeDesc
,case
	when _itemtype.ItemType =0 then mat01.ExternalID
	when _itemtype.ItemType =1 then v01.ExternalID
	when _itemtype.ItemType =2 then ast01.AssetType end as ServiceID
--,mat01.Description as ServiceMaterialAssetDesc
,case 
	when salesitems.ItemType =0 then mat01.[Description]
	--when salesitems.ItemType =1 then srv01.[Description]
	when salesitems.ItemType =2 then ast01.AssetName
	end as ServiceDesc
,salesitems.PriceAmount
,salesitems.QuantityMaterial
,case 
	when salesitems.QuantityMaterial ='' then '1' 
	when salesitems.QuantityMaterial is null then '1'
	else salesitems.QuantityMaterial 
	end as Quantity
,salesitems.QuantityTransaction
,salesitems.Discount
,salesitems.NetAmount
,salesitems.NetAmount*0.11 as TaxAmount
,salesitems.TotAmount 
,salesitems.Currency

from PTRSALESITEMS salesitems
full outer join PTRSALESHEADER header 
	on salesitems.DocNo = header.DocNo
left join PCMBSDOCTYPE doctype 
	on header.DocType = doctype.DocType
left join PCMLOGITEMTYPE _itemtype 
	on salesitems.ItemType = _itemtype.ItemType
left join PMDMAT0001 mat01 
	on salesitems.Material = mat01.MaterialID
left join PMDSRV0001 srv01 
	on salesitems.Material = srv01.ServiceID
left join PMDAST0001 ast01 
	on salesitems.Material = ast01.AssetCode
left join PMDSRV0001 v01 
	on salesitems.Material = v01.ServiceID

	
where 
	(salesitems.DocNo = @DocNo or @DocNo='')
	and
	(header.DocDate =@DocDate or @DocDate='')
	 and
	 (header.DocType = @DocType or @DocType ='') 
end
