-- This view shows all transactions against a vendor
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 '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()) > 90 THEN '4_Over_90'
END AS Bucket,
P.doctype,
Rtrim(P.vendorid) + '-' + Rtrim(V.vendname) AS VendDisplay
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 )
No comments:
Post a Comment