Tuesday, January 1, 2013
Dynamics GP - AP Historical Aged Trial Balance - Stored Procedure
--This code replicates the AP Historical Aged Trial Balance report in GP.
--You'll need to set a date to go back to in the @ASOF variable.
--This is a modification of original code that was created by Steve Gray of 4penny.net(?)
--AP Aging
--we have four cases to handle:
--open invoices
--open payments and credits
--closed invoices
--closed payments and credits
--we take the doc amount and subtract the
--apply amounts from the appropriate apply table
--We need to set a cutoff date
--usage EXEC AP_HATB '2012-11-02 00:00:00.000'
--If no date is provided, it uses the current date by default
CREATE PROCEDURE [dbo].[Ap_hatb]
-- Add the parameters for the stored procedure here
(@asof DATETIME=NULL)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET nocount ON;
IF @asof IS NULL
SET @asof = Getdate()
-- Insert statements for procedure here
SELECT pmTrans.vendorid,
vendMaster.vendname,
pmTrans.vchrnmbr,
pmTrans.docdate,
CASE pmTrans.doctype
WHEN 1 THEN 'Invoice'
WHEN 2 THEN 'Finance Charge'
WHEN 3 THEN 'Misc Charge'
END AS docType,
pmTrans.docamnt AS docamnt,
pmTrans.docnumbr,
apply.appldamt,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -30, @asof) AND @asof THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt0to30,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -60, @asof) AND
Dateadd(d, -31, @asof)
THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt31to60,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -90, @asof) AND
Dateadd(d, -61, @asof)
THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt61to90,
CASE
WHEN pstgdate < Dateadd(d, -90, @asof) THEN pmTrans.docamnt -
Isnull(apply.appldamt, 0)
ELSE 0
END AS amtOver91,
bachnumb
FROM pm20000 pmTrans
LEFT JOIN pm00200 vendMaster
ON vendMaster.vendorid = pmTrans.vendorid
LEFT JOIN (SELECT aptvchnm,
aptodcty,
Sum(appldamt) AS appldamt
FROM pm20100
WHERE docdate <= @asOf
GROUP BY aptvchnm,
aptodcty) apply
ON pmTrans.vchrnmbr = apply.aptvchnm
AND pmTrans.doctype = apply.aptodcty
WHERE pstgdate <= @asof
AND pmTrans.docamnt - Isnull(apply.appldamt, 0) <> 0
AND pmTrans.doctype <= 3
AND voided = 0
UNION
SELECT pmTrans.vendorid,
vendMaster.vendname,
pmTrans.vchrnmbr,
pmTrans.docdate,
CASE pmTrans.doctype
WHEN 4 THEN 'Return'
WHEN 5 THEN 'Credit'
WHEN 6 THEN 'Payment'
ELSE CONVERT(VARCHAR(2), pmTrans.doctype)
END AS docType,
-pmTrans.docamnt AS docamnt,
pmTrans.docnumbr,
apply.appldamt,
-pmTrans.docamnt + Isnull(apply.appldamt, 0) AS amt0to30,
0 AS amt31to60,
0 AS amt61to90,
0 AS amtOver91,
bachnumb
FROM pm20000 pmTrans
LEFT JOIN pm00200 vendMaster
ON vendMaster.vendorid = pmTrans.vendorid
LEFT JOIN (SELECT vchrnmbr,
doctype,
Sum(appldamt) AS appldamt
FROM pm20100
WHERE docdate <= @asOf
GROUP BY vchrnmbr,
doctype) apply
ON pmTrans.vchrnmbr = apply.vchrnmbr
AND pmTrans.doctype = apply.doctype
WHERE pstgdate <= @asof
AND pmTrans.docamnt - Isnull(apply.appldamt, 0) <> 0
AND pmTrans.doctype >= 4
AND voided = 0
UNION
SELECT pmTrans.vendorid,
vendMaster.vendname,
pmTrans.vchrnmbr,
pmTrans.docdate,
CASE pmTrans.doctype
WHEN 1 THEN 'Invoice'
WHEN 2 THEN 'Finance Charge'
WHEN 3 THEN 'Misc Charge'
END AS docType,
pmTrans.docamnt AS docamnt,
pmTrans.docnumbr,
apply.appldamt,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -30, @asof) AND @asof THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt0to30,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -60, @asof) AND
Dateadd(d, -31, @asof)
THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt31to60,
CASE
WHEN pstgdate BETWEEN Dateadd(d, -90, @asof) AND
Dateadd(d, -61, @asof)
THEN
pmTrans.docamnt - Isnull(apply.appldamt, 0)
ELSE 0
END AS amt61to90,
CASE
WHEN pstgdate < Dateadd(d, -90, @asof) THEN pmTrans.docamnt -
Isnull(apply.appldamt, 0)
ELSE 0
END AS amtOver91,
bachnumb
FROM pm30200 pmTrans
LEFT JOIN pm00200 vendMaster
ON vendMaster.vendorid = pmTrans.vendorid
LEFT JOIN (SELECT aptvchnm,
aptodcty,
Sum(appldamt) AS appldamt
FROM pm30300
WHERE glpostdt <= @asOf
GROUP BY aptvchnm,
aptodcty) apply
ON apply.aptvchnm = pmTrans.vchrnmbr
AND pmTrans.doctype = apply.aptodcty
WHERE pstgdate <= @asof
AND pmTrans.docamnt - Isnull(apply.appldamt, 0) <> 0
AND pmTrans.doctype <= 3
AND voided = 0
UNION
SELECT pmTrans.vendorid,
vendMaster.vendname,
pmTrans.vchrnmbr,
pmTrans.docdate,
CASE pmTrans.doctype
WHEN 4 THEN 'Return'
WHEN 5 THEN 'Credit'
WHEN 6 THEN 'Payment'
ELSE CONVERT(VARCHAR(2), pmTrans.doctype)
END AS docType,
-pmTrans.docamnt AS docamnt,
pmTrans.docnumbr,
apply.appldamt,
-pmTrans.docamnt + Isnull(apply.appldamt, 0) AS amt0to30,
0 AS amt31to60,
0 AS amt61to90,
0 AS amtOver91,
bachnumb
FROM pm30200 pmTrans
LEFT JOIN pm00200 vendMaster
ON vendMaster.vendorid = pmTrans.vendorid
LEFT JOIN (SELECT vchrnmbr,
doctype,
Sum(appldamt) AS appldamt
FROM pm30300
WHERE glpostdt <= @asOf
GROUP BY vchrnmbr,
doctype) apply
ON pmTrans.vchrnmbr = apply.vchrnmbr
AND pmTrans.doctype = apply.doctype
WHERE pstgdate <= @asof
AND pmTrans.docamnt - Isnull(apply.appldamt, 0) <> 0
AND pmTrans.doctype >= 4
AND voided = 0
ORDER BY bachnumb
END
Subscribe to:
Post Comments (Atom)
What about transactions voided after the @asof date? They should be included.
ReplyDeleteMark, have you updated the script to accommodate that scenario? Is so, will you please share it here or be willing to e-mail it to me? dprigmore@advancedintegrators.com
Deletewhat about doctype of 7 -- scheduled payments??
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Mark,
ReplyDeleteWhat a great proc!!! Have you had a chance to update it based on the inputs on this post? If so, can you please post the updated version or email it to dwisch@tmc-la.com? It would be greatly appreciated as this proc is greatly needed.
Thanks in advance!
Least I can do is THX!
ReplyDeleteIt seems that when I run this SP at different times of the day I get much different results, seems crazy why this would happen, I always run as of 8/2/2017
ReplyDeleteThe only way that could happen is if users are backposting to previous dates
DeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete