|
|

楼主 |
发表于 2011/11/10 13:03:58
|
显示全部楼层
ARTB 也写出来了
Select a.CustomerID,a.InvoiceNumber,a.InvoiceDate,
Round(Sum(IsNull(a.TotalInvoiceForeignAmount,0) - Isnull(b.CashReceiptAppliedLocalAmount,0)),2) as TotalInvoiceAmount
into #Rep_ARInvoiceAmount
From
(Select CustomerID,InvoiceNumber,InvoiceDate,
Sum(Case When InvoiceType ='R' Then TotalInvoiceForeignAmount*-1
Else TotalInvoiceForeignAmount End) as TotalInvoiceForeignAmount
From FS_ARInvoiceHeader Where InvoiceStatus <>'U'
Group By CustomerID,InvoiceNumber,InvoiceDate ) a
Left Join
(Select h.CustomerID,p.InvoiceNumber,h.InvoiceDate,
Sum(Case When InvoiceType ='R' Then CashReceiptAppliedLocalAmount*-1
Else CashReceiptAppliedLocalAmount End) as CashReceiptAppliedLocalAmount
From FS_ARInvoiceHeader h Inner Join FS_ARInvoicePayment p On h.ARInvoiceHeaderKey = p.ARInvoiceHeaderKey
Where h.InvoiceStatus <>'U'
Group By h.CustomerID,p.InvoiceNumber,h.InvoiceDate ) b
On a.CustomerID = b.CustomerID And a.InvoiceNumber = b.InvoiceNumber
Group By a.CustomerID,a.InvoiceNumber,a.InvoiceDate
Having Round(Sum(IsNull(a.TotalInvoiceForeignAmount,0) - Isnull(b.CashReceiptAppliedLocalAmount,0)),2)<>0
Order By a.CustomerID
Select * From
( Select r.CustomerID, c.CustomerName,c.CreditLimitControllingAmount,r.InvoiceNumber,g.TRANS_DESC,
r.InvoiceDate,'' as UnappliedCashBalanceForeignAmount,
Case When DateDiff(Day,r.InvoiceDate,Getdate())>0 And DateDiff(Day,r.InvoiceDate,Getdate())<=30
Then r.TotalInvoiceAmount END As A1_30,
Case When DateDiff(Day,r.InvoiceDate,Getdate())>30 And DateDiff(Day,r.InvoiceDate,Getdate())<=60
Then r.TotalInvoiceAmount END As A31_60,
Case When DateDiff(Day,r.InvoiceDate,Getdate())>60 And DateDiff(Day,r.InvoiceDate,Getdate())<=90
Then r.TotalInvoiceAmount END As A61_90,
Case When DateDiff(Day,r.InvoiceDate,Getdate())>90 And DateDiff(Day,r.InvoiceDate,Getdate())<=180
Then r.TotalInvoiceAmount END As A91_180,
Case When DateDiff(Day,r.InvoiceDate,Getdate())>180 Then r.TotalInvoiceAmount END As A180
From #Rep_ARInvoiceAmount r Inner Join FS_Customer c On r.CustomerID = c.CustomerID
Left Join (Select Distinct REF_NO4,BATCH_NO,TRANS_DESC From GL_BATCHDETAILACCT Where BATCH_SRCE in ('INVA','IVIE','IVPR')) g
On r.InvoiceNumber = g.REF_NO4
Union All
Select c.CustomerID,c.CustomerName,c.CreditLimitControllingAmount,'' as InvoiceDate, '' as TRANS_DESC,
'' as InvoiceDate, cd.UnappliedCashBalanceForeignAmount,
'' as A1_30,'' as A31_60,'' as A61_90, '' as A91_180, '' as A180
From FS_Customer c Inner Join FS_CustomerData cd On c.CustomerKey = cd.CustomerKey
Where c.CustomerStatus <>'I' And c.CustomerID <>'000000' ) a
Order By a.CustomerID,a.InvoiceNumber Desc
|
|