Declare @name as Char(170)
Declare @noRegularizate as Int
Declare @inicialDate as Date
Declare @finalDate as Date
Set @inicialDate = '#1#'
Set @finalDate = '#2#'
Set @name= '#3#'
Set @noRegularizate = '#4#'
select cc.ccstamp as stamp,
case when cc.estab>0 then ltrim(rtrim(cc.nome))+' - '+convert(varchar(10),cc.no)+' - '+
convert(varchar(10),cc.estab)
else cc.nome+ ' - ' +convert(varchar(10),cc.no) end as nome,
rtrim(cc.cmdesc)+' '+CAST(cc.nrdoc AS VARCHAR (10)) as documento,
cc.datalc as data, cc.dataven as dataven,
(cc.edeb-cc.ecred) as original,
(cc.edeb-cc.edebf) - (cc.ecred-cc.ecredf) as saldo, datediff(dd,cc.dataven,getdate())as dias,
CASE WHEN dataven>GETDATE() THEN 'Por Vencer' else
case when cast(dataven as DATE)=CAST(getdate()as DATE) then 'Próprio Dia' else
case WHEN dataven<GETDATE() THEN 'Vencida ' end end
END as estado
from cc(nolock)
where (cc.origem='FT' or cc.origem='CC' or ('#5#' = '1' and cc.origem='RD'))
and (@noRegularizate = '1' or (@noRegularizate <> '1' and ((cc.edeb-cc.edebf)-(cc.ecred-cc.ecredf))<>'0'))
and cc.datalc>=@inicialDate and cc.datalc<=@finalDate
and ('#3#' = 'Todos os Cliente' or ( '#3#' <> 'Todos os Cliente' and cc.nome=@name))
order by nome, dataven asc,data asc