declare @userid varchar(20)
Select @userid=c.[U_NAME]
From [master].[dbo].[sysprocesses] As a 
Inner Join [dbo].[USR5] As b On a.[spid]=b.[SessionID] And a.[hostprocess]=b.[ProcessID] 
Inner Join [dbo].[OUSR] As c On b.[UserCode]=c.[USER_CODE]
Where a.[spid]=@@spid And b.[Action]='I'
Order By b.[Date] Desc

SELECT T0.[DocEntry] as'单据编号',T3.[SlpName]as'销售员',T0.[CardName] as '客户名称', T10.[PymntGroup] as'付款条件', CASE WHEN T0.[DocStatus]='C' THEN N'已结算' WHEN T0.[DocStatus]

='O' THEN N'未清' END 单据状态,
T0.[DocDate] as'发票开单日期'
T0.[DocDueDate] as '计划收款日期'
 max(T20.DocDate) AS'实际收款日期',

CASE WHEN (DATEDIFF(DAY,T0.[DocDueDate],max(T20.[DocDate]))) > '5' THEN '收款延期'+cast

(((DATEDIFF(DAY,T0.[DocDueDate],max(T20.[DocDate])))-5) as nvarchar)+'天' WHEN 
(DATEDIFF(DAY,T0.[DocDueDate],max(T20.[DocDate]))) < '5' THEN '正常' END as '付款状态',T0.[DocTotal] as '单据总计' , T0.[PaidToDate] as'至今已付款', 
T0.[DocCur] as'货币'
FROM [DBO].[OINV] T0  INNER JOIN [DBO].[OSLP] T3   ON T3.[SlpCode] = T0.[SlpCode] 
 Inner Join OCTG T10 On T10.[GroupNum]=T0.[GroupNum]
 Inner Join RCT2 T22 On T0.DocEntry=T22.DocEntry And T0.ObjType=T22.InvType
 Inner Join ORCT T20  On T20.DocEntry=T22.DocNum 
 WHERE T0.[DocStatus] ='C' and T20.DocDate >='[%1]' and T20.DocDate <='[%2]' and T3.[SlpName]=@userid and T0.[Comments] NOT Like '%%APP客户端开发费%%' 
 Group By T0.[DocEntry],T3.[SlpName],T0.[CardName], T10.[PymntGroup], CASE WHEN T0.[DocStatus]='C' THEN N'已结算' WHEN T0.[DocStatus]='O' THEN N'未清' END,T0.[DocDate],T0.[DocDueDate],t0.[DocTotal], T0.[PaidToDate], T0.[DocCur],t0.[DocNum]