Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics GP (Archived)

Add debit and credit from RM10101

(0) ShareShare
ReportReport
Posted on by 3,015

Is it possible for someone to help me add the DEBIT and CREDIT fields from RM10101 to the following query?

SELECT T.CUSTNMBR Customer_ID ,

CM.CUSTNAME Customer_Name ,

T.DOCDATE Document_Date ,

T.GLPOSTDT GL_Posting_Date ,

CASE T.RMDTYPAL

WHEN 7 THEN 'Credit Memo'

WHEN 8 THEN 'Return'

WHEN 9 THEN 'Payment'

END AS RM_Doc_Type ,

T.BACHNUMB Payment_Batch ,

T.CHEKNMBR Check_Number ,

T.DOCNUMBR Document_Number ,

ISNULL(PMD.DSTINDX,0) AS Cash_Account_index,

ISNULL(GLM.ACTDESCR,0) AS Cash_Account_Description,

ISNULL(GLM.ACTNUMBR_1,0) AS Cash_Account_Seg1,

ISNULL(GLM.ACTNUMBR_2,0) AS Cash_Account_Seg2,

ISNULL(GLM.ACTNUMBR_3,0) AS Cash_Account_Seg3,

ISNULL(GLM.ACTNUMBR_4,0) AS Cash_Account_Seg4,

ISNULL(GLM.ACTNUMBR_5,0) AS Cash_Account_Seg5,

T.ORTRXAMT Original_Trx_Amount ,

T.CURTRXAM Current_Trx_Amount ,

T.amountApplied Total_Applied_Amount ,

A.APPTOAMT Amount_Applied ,

A.APTODCTY Applied_to_Doc_Type ,

A.debitType Applied_to_Doc_Type_Name ,

A.APTODCNM Applied_to_Doc_Number ,

A.APTODCDT Applied_to_Document_Date ,

A.ApplyToGLPostDate Applied_to_GL_Posting_Date ,

A.DISTKNAM Discount ,

A.WROFAMNT Writeoff ,

A.DATE1 Apply_Document_Date ,

A.GLPOSTDT Apply_GL_Posting_Date ,

D.ORTRXAMT Applied_To_Doc_Total ,

D.DINVPDOF Applied_To_Date_Paid_Off ,

D.CURTRXAM Applied_To_Doc_Unapplied_Amount ,

D.CSPORNBR Customer_PO_Number

FROM ( SELECT CUSTNMBR ,

DOCDATE ,

GLPOSTDT ,

RMDTYPAL ,

CHEKNMBR ,

DOCNUMBR ,

ORTRXAMT ,

CURTRXAM ,

BACHNUMB ,

ORTRXAMT - CURTRXAM amountApplied

FROM RM20101

WHERE ( RMDTYPAL > 6 )

AND ( VOIDSTTS = 0 )

UNION

SELECT CUSTNMBR ,

DOCDATE ,

GLPOSTDT ,

RMDTYPAL ,

CHEKNMBR ,

DOCNUMBR ,

ORTRXAMT ,

CURTRXAM ,

BACHNUMB ,

ORTRXAMT - CURTRXAM amountApplied

FROM RM30101

WHERE ( RMDTYPAL > 6 )

AND ( VOIDSTTS = 0 )

) T

LEFT OUTER JOIN

( SELECT DOCNUMBR, DISTTYPE, RMDTYPAL, DSTINDX FROM dbo.RM10101

WHERE RMDTYPAL = 9 AND DISTTYPE = 1

UNION

SELECT DOCNUMBR, DISTTYPE, RMDTYPAL, DSTINDX FROM dbo.RM30301

WHERE RMDTYPAL = 9 AND DISTTYPE = 1

) AS PMD

ON PMD.DOCNUMBR = T.DOCNUMBR

LEFT OUTER JOIN dbo.GL00100 AS GLM ON GLM.ACTINDX = PMD.DSTINDX

INNER JOIN RM00101 CM ON T.CUSTNMBR = CM.CUSTNMBR

INNER JOIN GL00100 AS ACT ON ACT.ACTINDX = CM.RMARACC

INNER JOIN ( SELECT tO1.CUSTNMBR ,

APTODCTY ,

APTODCNM ,

APFRDCTY ,

APFRDCNM ,

CASE APTODCTY

WHEN 1 THEN 'Sale / Invoice'

WHEN 2 THEN 'Scheduled Payment'

WHEN 3 THEN 'Debit Memo'

WHEN 4 THEN 'Finance Charge'

WHEN 5 THEN 'Service Repair'

WHEN 6 THEN 'Warranty'

END AS debitType ,

APPTOAMT ,

ApplyToGLPostDate ,

APTODCDT ,

tO2.DISTKNAM ,

tO2.WROFAMNT ,

tO2.DATE1 ,

tO2.GLPOSTDT

FROM RM20201 tO2

INNER JOIN RM20101 tO1 ON tO2.APTODCTY = tO1.RMDTYPAL

AND tO2.APTODCNM = tO1.DOCNUMBR

UNION

SELECT tH1.CUSTNMBR ,

APTODCTY ,

APTODCNM ,

APFRDCTY ,

APFRDCNM ,

CASE APTODCTY

WHEN 1 THEN 'Sale / Invoice'

WHEN 2 THEN 'Scheduled Payment'

WHEN 3 THEN 'Debit Memo'

WHEN 4 THEN 'Finance Charge'

WHEN 5 THEN 'Service Repair'

WHEN 6 THEN 'Warranty'

END AS debitType ,

APPTOAMT ,

ApplyToGLPostDate ,

APTODCDT ,

tH2.DISTKNAM ,

tH2.WROFAMNT ,

tH2.DATE1 ,

tH2.GLPOSTDT

FROM RM30201 tH2

INNER JOIN RM30101 tH1 ON tH2.APTODCTY = tH1.RMDTYPAL

AND tH2.APTODCNM = tH1.DOCNUMBR

) A ON A.APFRDCTY = T.RMDTYPAL

AND A.APFRDCNM = T.DOCNUMBR

INNER JOIN ( SELECT RMDTYPAL ,

DOCNUMBR ,

ORTRXAMT ,

DINVPDOF ,

CURTRXAM ,

CSPORNBR

FROM RM20101

UNION

SELECT RMDTYPAL ,

DOCNUMBR ,

ORTRXAMT ,

DINVPDOF ,

CURTRXAM = 0 ,

CSPORNBR

FROM RM30101

) D ON A.APTODCTY = D.RMDTYPAL

AND A.APTODCNM = D.DOCNUMBR

*This post is locked for comments

  • Verified answer
    Mahmoud Saadi Profile Picture
    Mahmoud Saadi 32,738 on at
    RE: Add debit and credit from RM10101

    The numbers shown above are just dummy numbers as retrieved from a test environment. As for the Script, find it below upon your requirements and be sure to test that on a test environment and check how header is duplicated several times with distribution and other associated documents as explained above. (apply the filter at the bottom of the script in order to check the duplication issue).

    In case the script is enhanced in Crystal Report, you need to consider "Group By" at several levels in order to get correct results. I have included both tables, RM10101 and RM30301 (Distribution for Work and History)


    SELECT
    T.CUSTNMBR Customer_ID ,
    CM.CUSTNAME Customer_Name ,
    T.DOCDATE Document_Date ,
    T.GLPOSTDT GL_Posting_Date ,
    CASE T.RMDTYPAL
    WHEN 7 THEN 'Credit Memo'
    WHEN 8 THEN 'Return'
    WHEN 9 THEN 'Payment'
    END AS RM_Doc_Type ,
    T.BACHNUMB Payment_Batch ,
    T.CHEKNMBR Check_Number ,
    T.DOCNUMBR Document_Number ,
    CASE DST.DISTTYPE
    WHEN 1 THEN 'SALES'
    WHEN 2 THEN 'RECV'
    WHEN 3 THEN 'CASH'
    WHEN 4 THEN 'TAKEN'
    WHEN 5 THEN 'AVAIL'
    WHEN 6 THEN 'TRADE'
    WHEN 7 THEN 'FREIGHT'
    WHEN 8 THEN 'MISC'
    WHEN 9 THEN 'TAXES'
    WHEN 10 THEN 'MARK'
    WHEN 11 THEN 'COMMEXP'
    WHEN 12 THEN 'COMMPAY'
    WHEN 13 THEN 'OTHER'
    WHEN 14 THEN 'COGS'
    WHEN 15 THEN 'INV'
    WHEN 16 THEN 'RETURN'
    WHEN 17 THEN 'IN USE'
    WHEN 18 THEN 'IN SERVICE'
    WHEN 19 THEN 'DAMAGED'
    WHEN 20 THEN 'UNIT'
    WHEN 21 THEN 'DEPOSITS'
    WHEN 22 THEN 'ROUND'
    WHEN 23 THEN 'REBATE'
    END AS DistributionType,

    DST.DEBITAMT,
    DST.CRDTAMNT,
    ISNULL(PMD.DSTINDX,0) AS Cash_Account_index,
    ISNULL(GLM.ACTDESCR,0) AS Cash_Account_Description,
    ISNULL(GLM.ACTNUMBR_1,0) AS Cash_Account_Seg1,
    ISNULL(GLM.ACTNUMBR_2,0) AS Cash_Account_Seg2,
    ISNULL(GLM.ACTNUMBR_3,0) AS Cash_Account_Seg3,
    ISNULL(GLM.ACTNUMBR_4,0) AS Cash_Account_Seg4,
    ISNULL(GLM.ACTNUMBR_5,0) AS Cash_Account_Seg5,
    T.ORTRXAMT Original_Trx_Amount ,
    T.CURTRXAM Current_Trx_Amount ,
    T.amountApplied Total_Applied_Amount ,
    A.APPTOAMT Amount_Applied ,
    A.APTODCTY Applied_to_Doc_Type ,
    A.debitType Applied_to_Doc_Type_Name ,
    A.APTODCNM Applied_to_Doc_Number ,
    A.APTODCDT Applied_to_Document_Date ,
    A.ApplyToGLPostDate Applied_to_GL_Posting_Date ,
    A.DISTKNAM Discount ,
    A.WROFAMNT Writeoff ,
    A.DATE1 Apply_Document_Date ,
    A.GLPOSTDT Apply_GL_Posting_Date ,
    D.ORTRXAMT Applied_To_Doc_Total ,
    D.DINVPDOF Applied_To_Date_Paid_Off ,
    D.CURTRXAM Applied_To_Doc_Unapplied_Amount ,
    D.CSPORNBR Customer_PO_Number
    FROM ( SELECT CUSTNMBR ,
    DOCDATE ,
    GLPOSTDT ,
    RMDTYPAL ,
    CHEKNMBR ,
    DOCNUMBR ,
    ORTRXAMT ,
    CURTRXAM ,
    BACHNUMB ,
    ORTRXAMT - CURTRXAM amountApplied
    FROM RM20101
    WHERE ( RMDTYPAL > 6 )
    AND ( VOIDSTTS = 0 )
    UNION
    SELECT CUSTNMBR ,
    DOCDATE ,
    GLPOSTDT ,
    RMDTYPAL ,
    CHEKNMBR ,
    DOCNUMBR ,
    ORTRXAMT ,
    CURTRXAM ,
    BACHNUMB ,
    ORTRXAMT - CURTRXAM amountApplied
    FROM RM30101
    WHERE ( RMDTYPAL > 6 )
    AND ( VOIDSTTS = 0 )
    ) T
    LEFT OUTER JOIN
    ( SELECT DOCNUMBR, DISTTYPE, RMDTYPAL, DSTINDX FROM dbo.RM10101
    WHERE RMDTYPAL = 9 AND DISTTYPE = 1
    UNION
    SELECT DOCNUMBR, DISTTYPE, RMDTYPAL, DSTINDX FROM dbo.RM30301
    WHERE RMDTYPAL = 9 AND DISTTYPE = 1
    ) AS PMD
    ON PMD.DOCNUMBR = T.DOCNUMBR
    LEFT OUTER JOIN
    (
    SELECT DOCNUMBR, DISTTYPE, DEBITAMT,CRDTAMNT FROM RM10101
    UNION
    SELECT DOCNUMBR, DISTTYPE, DEBITAMT,CRDTAMNT FROM RM30301
    ) AS DST
    ON DST.DOCNUMBR = T.DOCNUMBR
    LEFT OUTER JOIN dbo.GL00100 AS GLM ON GLM.ACTINDX = PMD.DSTINDX
    INNER JOIN RM00101 CM ON T.CUSTNMBR = CM.CUSTNMBR
    INNER JOIN GL00100 AS ACT ON ACT.ACTINDX = CM.RMARACC
    INNER JOIN ( SELECT tO1.CUSTNMBR ,
    APTODCTY ,
    APTODCNM ,
    APFRDCTY ,
    APFRDCNM ,
    CASE APTODCTY
    WHEN 1 THEN 'Sale / Invoice'
    WHEN 2 THEN 'Scheduled Payment'
    WHEN 3 THEN 'Debit Memo'
    WHEN 4 THEN 'Finance Charge'
    WHEN 5 THEN 'Service Repair'
    WHEN 6 THEN 'Warranty'
    END AS debitType ,
    APPTOAMT ,
    ApplyToGLPostDate ,
    APTODCDT ,
    tO2.DISTKNAM ,
    tO2.WROFAMNT ,
    tO2.DATE1 ,
    tO2.GLPOSTDT
    FROM RM20201 tO2
    INNER JOIN RM20101 tO1 ON tO2.APTODCTY = tO1.RMDTYPAL
    AND tO2.APTODCNM = tO1.DOCNUMBR
    UNION
    SELECT tH1.CUSTNMBR ,
    APTODCTY ,
    APTODCNM ,
    APFRDCTY ,
    APFRDCNM ,
    CASE APTODCTY
    WHEN 1 THEN 'Sale / Invoice'
    WHEN 2 THEN 'Scheduled Payment'
    WHEN 3 THEN 'Debit Memo'
    WHEN 4 THEN 'Finance Charge'
    WHEN 5 THEN 'Service Repair'
    WHEN 6 THEN 'Warranty'
    END AS debitType ,
    APPTOAMT ,
    ApplyToGLPostDate ,
    APTODCDT ,
    tH2.DISTKNAM ,
    tH2.WROFAMNT ,
    tH2.DATE1 ,
    tH2.GLPOSTDT
    FROM RM30201 tH2
    INNER JOIN RM30101 tH1 ON tH2.APTODCTY = tH1.RMDTYPAL
    AND tH2.APTODCNM = tH1.DOCNUMBR
    ) A ON A.APFRDCTY = T.RMDTYPAL
    AND A.APFRDCNM = T.DOCNUMBR
    INNER JOIN ( SELECT RMDTYPAL ,
    DOCNUMBR ,
    ORTRXAMT ,
    DINVPDOF ,
    CURTRXAM ,
    CSPORNBR
    FROM RM20101
    UNION
    SELECT RMDTYPAL ,
    DOCNUMBR ,
    ORTRXAMT ,
    DINVPDOF ,
    CURTRXAM = 0 ,
    CSPORNBR
    FROM RM30101
    ) D ON A.APTODCTY = D.RMDTYPAL
    AND A.APTODCNM = D.DOCNUMBR
    -- WHERE T.DOCNUMBR = 'DOCNUMBER'

  • painterisaac Profile Picture
    painterisaac 3,015 on at
    RE: Add debit and credit from RM10101

    Using your example, wouldn't it be 39 and 760 (799-39)? The sum of the debits and credits would be greater than the actual applied amount of 799 using 39 and 799.

    I believe I see your point. We understand that there are likely duplicates that will show up but that is fine for our purposes.

  • Suggested answer
    Mahmoud Saadi Profile Picture
    Mahmoud Saadi 32,738 on at
    RE: Add debit and credit from RM10101

    Although it is technically possible, but I would never recommend such join in which the header will be duplicated after being joined with the details. See the chart below for further illustrations on how the header records will be duplicated. 

    If the script is used in Pivot Table for instance, the results would be misleading, such as document amount, applied amount .. etc.

    Therefore, my recommendation is to have a separate view for that purpose including the document number, distribution type, debit and credit from both tables; RM10101 and RM30303 (Work and History) accordingly.

    Your feedback is highly appreciated.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans