Great Plains Customization – Programming Auto-apply in Accounts Receivable
Microsoft Great Plains is one of three Microsoft Business Solutions mid-market ERP products: Great Plains, Solomon, Navision. Considering that Great Plains is now very good candidate for integration with POS application, such as Microsoft Retail Management System or RMS and Client Relation Systems, such as Microsoft CRM – there is common need in Great Plains customizations and integrations, especially on the level of MS SQL Server transact SQL queries and stored procedures.In this small article we’ll show you how to create auto-apply utility, when you integrate huge number of sales transactions and payments. We will be working with RM20101 – Receivables Open File and RM20201 – Receivables Apply Open File.Let’s see SQL code:declare @curpmtamt numeric(19,5)declare @curinvamt numeric(19,5)declare @curpmtnum varchar(20)declare @curinvnum varchar(20)declare @curinvtype intdeclare @curpmttype intdeclare @maxid intdeclare @counter int-- Create a temporary tablecreate table #temp([ID] int identity(1,1) primary key,CUSTNMBR varchar(15),INVNUM varchar(20),INVTYPE int,PMTNUM varchar(20),PMTTYPE int,INVAMT numeric(19,5),PMTAMT numeric(19,5),AMTAPPLIED numeric(19,5))create index IDX_INVNUM on #temp (INVNUM)create index IDX_PMTNUM on #temp (PMTNUM)-- Insert unapplied invoices and paymentsinsert into #temp(CUSTNMBR,INVNUM,INVTYPE,PMTNUM,PMTTYPE,INVAMT,PMTAMT,AMTAPPLIED)selectCUSTNMBR = a.CUSTNMBR,INVNUM = b.DOCNUMBR,INVTYPE = b.RMDTYPAL,PMTNUM = a.DOCNUMBR,PMTTYPE = a.RMDTYPAL,INVAMT = b.CURTRXAM,PMTAMT = a.CURTRXAM,AMTAPPLIED = 0from RM20101 ajoin RM20101 b on (a.CUSTNMBR = b.CUSTNMBR)join RM00101 c on (a.CUSTNMBR = c.CUSTNMBR)wherea.RMDTYPAL in (7, 8, 9) andb.RMDTYPAL in (1, 3) anda.CURTRXAM 0 andb.CURTRXAM 0order bya.custnmbr,b.DOCDATE,a.DOCDATE,a.DOCNUMBR,b.DOCNUMBR-- Iterate through each recordselect @maxid = max([ID])from #tempselect @counter = 1while @counter = @curpmtamt) and (@curpmtamt>0) and (@curinvamt>0)-- if the invoice amount is greater or the same as the payment amountbeginselect @curinvamt = @curinvamt - @curpmtamt -- invoice amount remaining-- update with the amount that is applied to the current invoice from-- the current paymentupdate #tempsetAMTAPPLIED = @curpmtamtwhere[ID] = @counter-- update with amount of invoice remainingupdate #tempsetINVAMT = @curinvamtwhereINVNUM = @curinvnum andINVTYPE = @curinvtype-- update with amount of payment remainingupdate #tempsetPMTAMT = 0wherePMTNUM = @curpmtnum andPMTTYPE = @curpmttypeendelse if (@curinvamt 0) and (@curinvamt>0)-- if the invoice amount is lesser to the payment amountbeginselect @curpmtamt = @curpmtamt - @curinvamt -- payment amount remaining-- update with the amount that is applied to the current invoice from-- the current paymentupdate #tempsetAMTAPPLIED = @curinvamtwhere[ID] = @counter-- update with amount of invoice remainingupdate #tempsetINVAMT = 0whereINVNUM = @curinvnum andINVTYPE = @curinvtype-- update with amount of payment remainingupdate #tempsetPMTAMT = @curpmtamtwherePMTNUM = @curpmtnum andPMTTYPE = @curpmttypeend-- go to the next recordselect @counter = @counter + 1end-- update the RM Open table with the correct amountsupdateRM20101setCURTRXAM = b.INVAMTfromRM20101 ajoin #temp b on (a.DOCNUMBR = b.INVNUM and a.RMDTYPAL = b.INVTYPE)updateRM20101setCURTRXAM = b.PMTAMTfromRM20101 ajoin #temp b on (a.DOCNUMBR = b.PMTNUM and a.RMDTYPAL = b.PMTTYPE)-- create the RM Apply record or update if records already existupdateRM20201setDATE1 = convert(varchar(10), getdate(), 101),GLPOSTDT = convert(varchar(10), getdate(), 101),APPTOAMT = APPTOAMT + a.AMTAPPLIED,ORAPTOAM = ORAPTOAM + a.AMTAPPLIED,APFRMAPLYAMT = APFRMAPLYAMT + a.AMTAPPLIED,ActualApplyToAmount = APFRMAPLYAMT + a.AMTAPPLIEDfrom#temp ajoin RM20101 b on (b.DOCNUMBR = a.INVNUM and b.RMDTYPAL = a.INVTYPE)join RM20101 c on (c.DOCNUMBR = a.PMTNUM and c.RMDTYPAL = a.PMTTYPE)join RM20201 d on (d.APFRDCTY = a.PMTTYPE andd.APFRDCNM = a.PMTNUM andd.APTODCTY = a.INVTYPE andd.APTODCNM = a.INVNUM)wherea.AMTAPPLIED 0insert into RM20201(CUSTNMBR,DATE1,GLPOSTDT,POSTED,APTODCNM,APTODCTY,APTODCDT,ApplyToGLPostDate,CURNCYID,CURRNIDX,APPTOAMT,ORAPTOAM,APFRDCNM,APFRDCTY,APFRDCDT,ApplyFromGLPostDate,FROMCURR,APFRMAPLYAMT,ActualApplyToAmount)selectCUSTNMBR = a.CUSTNMBR,DATE1 = convert(varchar(10), getdate(), 101),GLPOSTDT = convert(varchar(10), getdate(), 101),POSTED = 1,APTODCNM = a.INVNUM,APTODCTY = a.INVTYPE,APTODCDT = b.DOCDATE,ApplyToGLPostDate = b.GLPOSTDT,CURNCYID = b.CURNCYID,CURRNIDX = '',APPTOAMT = a.AMTAPPLIED,ORAPTOAM = a.AMTAPPLIED,APFRDCNM = a.PMTNUM,APFRDCTY = a.PMTTYPE,APFRDCDT = c.DOCDATE,ApplyFromGLPostDate = c.GLPOSTDT,FROMCURR = c.CURNCYID,APFRMAPLYAMT = a.AMTAPPLIED,ActualApplyToAmount = a.AMTAPPLIEDfrom#temp ajoin RM20101 b on (b.DOCNUMBR = a.INVNUM and b.RMDTYPAL = a.INVTYPE)join RM20101 c on (c.DOCNUMBR = a.PMTNUM and c.RMDTYPAL = a.PMTTYPE)wherea.AMTAPPLIED 0 andnot exists (select 1from RM20201 dwhere d.APFRDCTY = a.PMTTYPE andd.APFRDCNM = a.PMTNUM andd.APTODCTY = a.INVTYPE andd.APTODCNM = a.INVNUM)drop table #tempAbout The AuthorAndrew Karasev is Chief Technology Officer in Alba Spectrum Technologies – USA nationwide Great Plains, Microsoft CRM customization company, with offices in Chicago, San Francisco, Los Angeles, San Diego, Phoenix, Houston, Miami, Atlanta, New York, Madrid, Brazil, Moscow ( http://www.albaspectrum.com), you can reach Andrew 1-866-528-0577, he is Dexterity, SQL, C#.Net, Crystal Reports and Microsoft CRM SDK developer; akarasev@albaspectrum.com
Andrew Karasev