--This view can be used to show Current AP aging Detail, as well as a foundation for your AP aged trial balance
SELECT Rtrim(P.vendorid) AS Vendor_ID,
V.vndclsid,
Rtrim(V.vendname) AS Vendor_Name,
P.vchrnmbr AS Voucher,
CASE P.doctype
WHEN 1 THEN 'Invoice'
WHEN 2 THEN 'Finance Charge'
WHEN 3 THEN 'Misc Charge'
WHEN 4 THEN 'Return'
WHEN 5 THEN 'Credit Memo'
WHEN 6 THEN 'Payment'
END AS Document_Type,
P.docdate AS Document_Date,
P.duedate AS Due_Date,
P.docnumbr AS Document_Number,
CASE
WHEN doctype <= 3 THEN docamnt
ELSE docamnt * -1
END AS Document_Amount,
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END AS Unapplied_Amount,
P.trxdscrn AS Description,
CASE
WHEN Datediff(day, docdate, Getdate()) <= 30 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END AS Under_30,
CASE
WHEN Datediff(day, docdate, Getdate()) BETWEEN 31 AND 60 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END AS B31_To_60,
CASE
WHEN Datediff(day, docdate, Getdate()) BETWEEN 61 AND 90 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END AS B61_To_90,
CASE
WHEN Datediff(day, docdate, Getdate()) > 120 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END AS Over_120,
CASE
WHEN Datediff(day, docdate, Getdate()) <= 30 THEN '1_Under_30'
WHEN Datediff(day, docdate, Getdate()) BETWEEN 31 AND 60 THEN
'2_B31_To_60'
WHEN Datediff(day, docdate, Getdate()) BETWEEN 61 AND 90 THEN
'3_B61_To_90'
WHEN Datediff(day, docdate, Getdate()) BETWEEN 91 AND 120 THEN
'4_B91_To_120'
WHEN Datediff(day, docdate, Getdate()) > 120 THEN '5_Over_120'
END AS Bucket,
Rtrim(P.vendorid) + '-' + Rtrim(V.vendname) AS VendDisplay,
CASE
WHEN Datediff(day, docdate, Getdate()) BETWEEN 91 AND 120 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END AS B90_To_120
FROM (SELECT vendorid,
vchrnmbr,
doctype,
docdate,
pstgdate,
duedate,
docnumbr,
docamnt,
curtrxam,
trxdscrn,
voided
FROM dbo.pm20000
UNION ALL
SELECT vendorid,
vchrnmbr,
doctype,
docdate,
pstgdate,
duedate,
docnumbr,
docamnt,
curtrxam,
trxdscrn,
voided
FROM dbo.pm30200) AS P
INNER JOIN dbo.pm00200 AS V
ON V.vendorid = P.vendorid
WHERE ( P.voided = 0 )
AND ( CASE
WHEN Datediff(day, docdate, Getdate()) <= 30 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END <> 0 )
OR ( P.voided = 0 )
AND ( CASE
WHEN Datediff(day, docdate, Getdate()) BETWEEN 31 AND 60 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END <> 0 )
OR ( P.voided = 0 )
AND ( CASE
WHEN Datediff(day, docdate, Getdate()) BETWEEN 61 AND 90 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END <> 0 )
OR ( P.voided = 0 )
AND ( CASE
WHEN Datediff(day, docdate, Getdate()) > 120 THEN
CASE
WHEN doctype <= 3 THEN curtrxam
ELSE curtrxam * -1
END
ELSE 0
END <> 0 )
No comments:
Post a Comment