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

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

as

begin

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

select header.DocNo
,header.CustomerID
,'' AS [Description]--customer.[Description]
,'' AS CustomerIDDesc
--,Case
--	when customer.[Description] is null then header.CustomerID
--	else customer.[Description] end as CustomerIDDesc
,header.CompanyID
,header.CustPONumber
,header.DocType
,doctype.DocTypeDesc as DocTypeDesc
,header.DocStatus
,docstatus.[Description] as DocStatusDesc
,header.[Description] AS headerDesc
,header.ExtDocRef
,header.BusinessUnit
,header.DocDate
,case
	when header.DocDate =null then '' 
	when header.DocDate ='' then '' 
		else dbo.fn_formatdatetime_indonesia(header.DocDate, 'DD MMMM YYYY') end as DocDateDesc
,term.PaymentTermID
,payterm.PaymentTermDescription
,term.[Sequence]
,term.[Description] AS Destrem
,term.Percentage
,term.Amount
,term.Currency
,term.AccountingDoc
,case 
	when term.AccountingDoc is null then 'AR' 
	when term.AccountingDoc ='' then 'AR' 
	else term.AccountingDoc end as AccountingDocDesc
,term.[Status]
,case 
	when term.[Status] =0 then 'Plan' 
	when term.[Status] =1 then 'BIlled'
	when term.[Status] =2 then 'Paid'
	else '' end as StatusDesc
,term.PostingDate
,case
	when term.PostingDate =null then '' 
	when term.PostingDate ='' then ''
	when term.PostingDate like '________' then dbo.fn_formatdatetime_indonesia(term.PostingDate, 'DD MMMM YYYY') end as PostingDateDesc
from PTRSALESHEADER header 
left join PTRSALESPAYTERM term ON header.DocNo = term.DocNo
left join PCMBSDOCTYPE doctype on header.DocType = doctype.DocType
left join PCMBSLOGDOCSTATUS docstatus on header.DocStatus = docstatus.DocStatus
left join PCMFILOPAYTERM payterm on term.PaymentTermID = payterm.PaymentTermCode
left join PMDCUS0001 customer on header.CustomerID = customer.CustomerID
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 ='')
end
