Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @cCusCode varchar(255) = '12932'
- -- from SA_AccDateCheckSa
- --取立账单据为发货单的最早未收款完毕记录
- select cvouchtype,cdlcode as ccode,ddate,min(a.dgatheringdate) as dgatheringdate,
- sum(case when isnull(iquantity,0)+isnull(tbquantity,0)<>0 then isnull(iUnitPrice,0) * (isnull(iquantity,0)+isnull(tbquantity,0)-isnull(isettlequantity,0)-isnull(fretqtywkp,0))
- else (isnull(isum,0)-(isnull(fretsum,0)+isnull(isettlenum,0)))
- end+(isnull(isettlenum,0)-isnull(iexchsum,0))
- ) as fmoney,
- sum(isnull(isum,0)-(case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)) cond1,
- sum((case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)-isnull(iexchsum,0)) cond2
- from (dispatchlist a with (nolock)
- inner join dispatchlists b with (nolock) on (a.dlid=b.dlid))
- left join customer with(nolock) on a.ccuscode=customer.ccuscode
- where isnull(bcashsale,0)=0
- and (a.bFirst=1 or (a.bFirst=0 and a.dDate>=(select cvalue from accinformation where csysid=N'SA' and cName=N'dStartDate'))) and a.cvouchtype<>N'00' and a.bcredit=1 and a.breturnflag=0
- and ccuscreditcompany = @cCusCode and isnull(isum,0)<>0
- and a.dlid <> 0
- and isnull(b.bsettleall,0)=0
- and isnull(dcreditstart,N'')<>N''
- and isnull(cverifier,'') <> N''
- group by cvouchtype,cdlcode,ddate,dgatheringdate
- having sum(isnull(isum,0))>0
- and (
- sum(isnull(isum,0)-(case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end))>0 -- 还有未结算金额,发票?
- or sum((case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)-isnull(iexchsum,0))>0
- );
- select
- cvouchtype,csbvcode as ccode,
- ddate,
- (case when 1=1 then min(dgatheringdate ) else min(dcreditstart) end) as dgatheringdate,
- sum(isnull(isum,0)-isnull(iexchsum,0)) as fmoney
- from salebillvouch a with (nolock)
- inner join salebillvouchs b with (nolock)
- on a.sbvid=b.sbvid
- left join customer with(nolock)
- on a.ccuscode=customer.ccuscode
- where isnull(bcashsale,0)=0
- and a.bcredit=1 and a.breturnflag=0
- and isnull(isum,0)<>0
- and ccuscreditcompany = @cCusCode
- and a.sbvid <> convert(nvarchar(20),1000083656)
- and isnull(cinvalider,N'') = N'' and isnull(cverifier,N'') = N''
- and isnull(dcreditstart,N'')<>N''
- and isnull(cverifier,'') <> N''
- group by cvouchtype,csbvcode,ddate,dgatheringdate;
- --取立账单据为发票的最早未收款完毕记录
- select cvouchtype,csbvcode as ccode,ddate,(case when 1=1 then min(dgatheringdate ) else min(dcreditstart) end) as dgatheringdate,
- sum(isnull(isum,0)-isnull(iexchsum,0)) as fmoney
- from salebillvouch a with (nolock) inner join salebillvouchs b with (nolock) on a.sbvid=b.sbvid left join customer with(nolock) on a.ccuscode=customer.ccuscode
- where isnull(bcashsale,0)=0 and a.bcredit=1 and a.breturnflag=0 and isnull(isum,0)<>0
- and ccuscreditcompany = @cCusCode
- and a.sbvid <> convert(nvarchar(20),1000083656)
- and isnull(cinvalider,N'') = N'' and isnull(cverifier,N'') = N''
- and isnull(dcreditstart,N'')<>N''
- GROUP BY a.cvouchtype,a.csbvcode,a.sbvid,ddate,dgatheringdate
- having sum(isnull(isum,0)-isnull(iexchsum,0)) > 0;
- select salebillvouch.cvouchtype,csbvcode as ccode,ddate,(case when 1=1 then min(salebillvouch.dgatheringdate ) else min(salebillvouch.dcreditstart) end) as dgatheringdate,
- sum(iDAmount_f - iCAmount_f)
- From ar_detail
- inner join salebillvouch
- on ar_detail.ccovouchtype=salebillvouch.cvouchtype and ar_detail.ccovouchid=salebillvouch.csbvcode
- left join customer with(nolock)
- on ar_detail.cDwCode=customer.ccuscode
- where iflag<=2 And (cCoVouchType like N'2%' Or cCoVouchType like N'R%')
- And cSign=N'Z'
- and isnull(bcashsale,0)=0 and salebillvouch.bcredit=1 and breturnflag=0
- and ccuscreditcompany = @cCusCode
- and isnull(salebillvouch.dcreditstart,N'')<>N''
- GROUP BY salebillvouch.cvouchtype,csbvcode,ddate,salebillvouch.dgatheringdate
- Having sum(iDAmount_f - iCAmount_f) <> 0 Or sum(iDAmount - iCAmount) <> 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement