Advertisement
Pandaaaa906

Untitled

Sep 7th, 2023
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.99 KB | None | 0 0
  1. declare @cCusCode varchar(255) = '12932'
  2.  
  3. -- from SA_AccDateCheckSa
  4. --取立账单据为发货单的最早未收款完毕记录
  5. select cvouchtype,cdlcode as ccode,ddate,min(a.dgatheringdate) as dgatheringdate,
  6. 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))
  7. else (isnull(isum,0)-(isnull(fretsum,0)+isnull(isettlenum,0)))
  8. end+(isnull(isettlenum,0)-isnull(iexchsum,0))
  9. ) as fmoney,
  10. sum(isnull(isum,0)-(case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)) cond1,
  11. sum((case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)-isnull(iexchsum,0)) cond2
  12.  
  13. from (dispatchlist a with (nolock)
  14. inner join dispatchlists b with (nolock) on (a.dlid=b.dlid))
  15.  
  16. left join customer with(nolock) on a.ccuscode=customer.ccuscode
  17.  
  18. where isnull(bcashsale,0)=0
  19. 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
  20. and ccuscreditcompany = @cCusCode  and isnull(isum,0)<>0
  21. and a.dlid <> 0
  22. and isnull(b.bsettleall,0)=0
  23. and isnull(dcreditstart,N'')<>N''
  24. and isnull(cverifier,'') <> N''
  25. group by cvouchtype,cdlcode,ddate,dgatheringdate
  26. having sum(isnull(isum,0))>0
  27. and (
  28.     sum(isnull(isum,0)-(case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end))>0 -- 还有未结算金额,发票?
  29.     or sum((case when isnull(isum,0)=0 then 0 else cast(isnull(isettlenum,0) as decimal(26,9)) end)-isnull(iexchsum,0))>0
  30. );
  31.  
  32.  
  33. select
  34. cvouchtype,csbvcode as ccode,
  35. ddate,
  36. (case when 1=1 then min(dgatheringdate ) else min(dcreditstart) end) as dgatheringdate,    
  37. sum(isnull(isum,0)-isnull(iexchsum,0)) as fmoney    
  38. from salebillvouch a with (nolock)
  39.  
  40. inner join salebillvouchs b with (nolock)
  41. on a.sbvid=b.sbvid  
  42.  
  43. left join customer with(nolock)
  44. on a.ccuscode=customer.ccuscode    
  45.  
  46. where isnull(bcashsale,0)=0
  47. and a.bcredit=1 and a.breturnflag=0  
  48. and isnull(isum,0)<>0  
  49. and ccuscreditcompany = @cCusCode    
  50. and a.sbvid <> convert(nvarchar(20),1000083656)
  51. and isnull(cinvalider,N'') = N''    and isnull(cverifier,N'') = N''
  52. and isnull(dcreditstart,N'')<>N''
  53.  
  54. and isnull(cverifier,'') <> N''
  55.  
  56. group by cvouchtype,csbvcode,ddate,dgatheringdate;
  57.  
  58.  
  59.  --取立账单据为发票的最早未收款完毕记录  
  60. select cvouchtype,csbvcode as ccode,ddate,(case when 1=1 then min(dgatheringdate ) else min(dcreditstart) end) as dgatheringdate,    
  61. sum(isnull(isum,0)-isnull(iexchsum,0)) as fmoney    
  62. 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    
  63. where isnull(bcashsale,0)=0 and a.bcredit=1 and a.breturnflag=0    and isnull(isum,0)<>0  
  64. and ccuscreditcompany = @cCusCode    
  65. and a.sbvid <> convert(nvarchar(20),1000083656)
  66. and isnull(cinvalider,N'') = N''    and isnull(cverifier,N'') = N''
  67. and isnull(dcreditstart,N'')<>N''
  68.  
  69. GROUP BY a.cvouchtype,a.csbvcode,a.sbvid,ddate,dgatheringdate
  70. having sum(isnull(isum,0)-isnull(iexchsum,0)) > 0;
  71.  
  72.  
  73. select salebillvouch.cvouchtype,csbvcode as ccode,ddate,(case when 1=1 then min(salebillvouch.dgatheringdate ) else min(salebillvouch.dcreditstart) end) as dgatheringdate,    
  74. sum(iDAmount_f - iCAmount_f)
  75. From ar_detail  
  76. inner join salebillvouch
  77. on ar_detail.ccovouchtype=salebillvouch.cvouchtype and ar_detail.ccovouchid=salebillvouch.csbvcode
  78.  
  79. left join customer with(nolock)
  80. on ar_detail.cDwCode=customer.ccuscode
  81. where iflag<=2 And (cCoVouchType like N'2%' Or cCoVouchType like N'R%')
  82. And cSign=N'Z'
  83. and isnull(bcashsale,0)=0 and  salebillvouch.bcredit=1 and breturnflag=0  
  84. and ccuscreditcompany = @cCusCode    
  85. and isnull(salebillvouch.dcreditstart,N'')<>N''
  86.  
  87. GROUP BY salebillvouch.cvouchtype,csbvcode,ddate,salebillvouch.dgatheringdate
  88. Having sum(iDAmount_f - iCAmount_f) <> 0 Or sum(iDAmount - iCAmount) <> 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement