USE [MinovaES_DEMO_GolfCourse]
GO
/****** Object:  StoredProcedure [dbo].[ERPRPTSALESDOCDETAILINDUK]    Script Date: 20/10/2025 14.36.09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ERPRPTSALESDOCDETAILINDUK]

@DocDateFrom varchar (10),
 @DocDateTo varchar (10),
 @DocNoFrom varchar (8),
 @DocNoTo varchar (8),
 @DocType varchar (8),
  @DocStatus varchar(8)

  AS

--declare @DocDateFrom varchar (10) =''
--declare @DocDateTo varchar (10) =''
--declare @DocNoFrom varchar (8) =''
--declare @DocNoTo varchar (8) =''
--declare @DocType varchar (8) =''
--declare @DocStatus varchar (10) =''


SELECT * 
FROM (
select  DocNo, DocDate, DocType, DocStatus from PTRSALESHEADER header
where (header.DocNo >= @DocNoFrom or @DocNoFrom='')
	and
	 (header.DocNo <= @DocNoTo or @DocNoTo='')
	and 
	(header.DocDate >= @DocDateFrom or @DocDateFrom='')
	and
	 (header.DocDate <= @DocDateTo or @DocDateTo='')
	and 
	(header.DocType = @DocType or @DocType ='')
	and
	(header.DocStatus = @DocStatus or @DocStatus='')
union 
select salesitems.DocNo,  header.DocDate, header.DocType, header.DocStatus from PTRSALESITEMS salesitems
left join PTRSALESHEADER header on salesitems.DocNo = header.DocNo
where (salesitems.DocNo >= @DocNoFrom or @DocNoFrom='')
	and
	 (salesitems.DocNo <= @DocNoTo or @DocNoTo='')
	 and 
	(header.DocDate >= @DocDateFrom or @DocDateFrom='')
	and
	 (header.DocDate <= @DocDateTo or @DocDateTo='')
	 and 
	(header.DocType = @DocType or @DocType ='')
	and
	(header.DocStatus = @DocStatus or @DocStatus='')
union 
select salescost.DocNo, header.DocDate, header.DocType, header.DocStatus from PTRSALESOTHRCOST salescost
left join PTRSALESHEADER header on salescost.DocNo = header.DocNo
where (salescost.DocNo >= @DocNoFrom or @DocNoFrom='')
	and
	 (salescost.DocNo <= @DocNoTo or @DocNoTo='')
	 and 
	(header.DocDate >= @DocDateFrom or @DocDateFrom='')
	and
	 (header.DocDate <= @DocDateTo or @DocDateTo='')
	 and 
	(header.DocType = @DocType or @DocType ='')
	and
	(header.DocStatus = @DocStatus or @DocStatus='')
union 
select salespayterm.DocNo, header.DocDate, header.DocType, header.DocStatus from PTRSALESPAYTERM salespayterm
left join PTRSALESHEADER header on salespayterm.DocNo = header.DocNo
where (salespayterm.DocNo >= @DocNoFrom or @DocNoFrom='')
	and
	 (salespayterm.DocNo <= @DocNoTo or @DocNoTo='')
	 and 
	(header.DocDate >= @DocDateFrom or @DocDateFrom='')
	and
	 (header.DocDate <= @DocDateTo or @DocDateTo='')
	 and 
	(header.DocType = @DocType or @DocType ='')
	and
		(header.DocStatus = @DocStatus or @DocStatus='')
) AS a
where a.DocStatus <> ''