Great Plains Customization - Programming Auto-apply in Accounts Receivable

Microsoft Great Plains is one of three Microsoft[ID] = @counter
Business Solutions mid-market ERP products: Great-- update with amount of invoice remainingupdate
Plains, Solomon, Navision. Considering that Great Plains#tempset
is now very good candidate for integration with POSINVAMT = 0where
application, such as Microsoft Retail ManagementINVNUM = @curinvnum and
System or RMS and Client Relation Systems, such asINVTYPE = @curinvtype
Microsoft CRM - there is common need in Great-- update with amount of payment remainingupdate
Plains customizations and integrations, especially on the#tempset
level of MS SQL Server transact SQL queries andPMTAMT = @curpmtamtwhere
stored procedures.PMTNUM = @curpmtnum and
In this small article we'll show you how to createPMTTYPE = @curpmttypeend
auto-apply utility, when you integrate huge number of-- go to the next recordselect @counter = @counter
sales transactions and payments. We will be working+ 1end
with RM20101 - Receivables Open File and RM20201 --- update the RM Open table with the correct
Receivables Apply Open File.amountsupdate
Let's see SQL code:declare @curpmtamtRM20101set
numeric(19,5)declare @curinvamt numeric(19,5)declareCURTRXAM = b.INVAMTfrom
@curpmtnum varchar(20)declare @curinvnumRM20101 ajoin #temp b on (a.DOCNUMBR =
varchar(20)declare @curinvtype intdeclareb.INVNUM and a.RMDTYPAL = b.INVTYPE)update
@curpmttype intdeclare @maxid intdeclare @counterRM20101set
intCURTRXAM = b.PMTAMTfrom
-- Create a temporary tablecreate table #tempRM20101 ajoin #temp b on (a.DOCNUMBR =
(b.PMTNUM and a.RMDTYPAL = b.PMTTYPE)
[ID] int identity(1,1) primary key,-- create the RM Apply record or update if records
CUSTNMBR varchar(15),already existupdate
INVNUM varchar(20),RM20201set
INVTYPE int,DATE1 = convert(varchar(10), getdate(), 101),
PMTNUM varchar(20),GLPOSTDT = convert(varchar(10), getdate(), 101),
PMTTYPE int,APPTOAMT = APPTOAMT + a.AMTAPPLIED,
INVAMT numeric(19,5),ORAPTOAM = ORAPTOAM + a.AMTAPPLIED,
PMTAMT numeric(19,5),APFRMAPLYAMT = APFRMAPLYAMT +
AMTAPPLIED numeric(19,5)a.AMTAPPLIED,
)create index IDX_INVNUM on #temp (INVNUM)createActualApplyToAmount = APFRMAPLYAMT +
index IDX_PMTNUM on #temp (PMTNUM)a.AMTAPPLIEDfrom
-- Insert unapplied invoices and paymentsinsert into#temp ajoin RM20101 b on (b.DOCNUMBR =
#tempa.INVNUM and b.RMDTYPAL = a.INVTYPE)join
(RM20101 c on (c.DOCNUMBR = a.PMTNUM and
CUSTNMBR,c.RMDTYPAL = a.PMTTYPE)join RM20201 d on
INVNUM,(d.APFRDCTY = a.PMTTYPE andd.APFRDCNM =
INVTYPE,a.PMTNUM andd.APTODCTY = a.INVTYPE
PMTNUM,andd.APTODCNM = a.INVNUM)wherea.AMTAPPLIED
PMTTYPE,0insert into RM20201
INVAMT,(CUSTNMBR,
PMTAMT,DATE1,
AMTAPPLIEDGLPOSTDT,
)selectPOSTED,
CUSTNMBR = a.CUSTNMBR,APTODCNM,
INVNUM = b.DOCNUMBR,APTODCTY,
INVTYPE = b.RMDTYPAL,APTODCDT,
PMTNUM = a.DOCNUMBR,ApplyToGLPostDate,
PMTTYPE = a.RMDTYPAL,CURNCYID,
INVAMT = b.CURTRXAM,CURRNIDX,
PMTAMT = a.CURTRXAM,APPTOAMT,
AMTAPPLIED = 0from RM20101 ajoin RM20101 b onORAPTOAM,
(a.CUSTNMBR = b.CUSTNMBR)join RM00101 c onAPFRDCNM,
(a.CUSTNMBR = c.CUSTNMBR)wherea.RMDTYPALAPFRDCTY,
in (7, 8, 9) andb.RMDTYPAL in (1, 3) anda.CURTRXAMAPFRDCDT,
0 andb.CURTRXAM 0orderApplyFromGLPostDate,
b.DOCNUMBRFROMCURR,
-- Iterate through each recordselect @maxid =APFRMAPLYAMT,
max([ID])from #tempselect @counter = 1whileActualApplyToAmount)select
@counter = @curpmtamt) and (@curpmtamt>0)CUSTNMBR = a.CUSTNMBR,
and (@curinvamt>0)-- if the invoice amount isDATE1 = convert(varchar(10), getdate(), 101),
greater or the same as the paymentGLPOSTDT = convert(varchar(10), getdate(), 101),
amountbeginselect @curinvamt = @curinvamt -POSTED = 1,
@curpmtamt -- invoice amount remainingAPTODCNM = a.INVNUM,
-- update with the amount that is applied to the currentAPTODCTY = a.INVTYPE,
invoice fromAPTODCDT = b.DOCDATE,
-- the current paymentupdate #tempsetApplyToGLPostDate = b.GLPOSTDT,
AMTAPPLIED = @curpmtamtwhereCURNCYID = b.CURNCYID,
[ID] = @counterCURRNIDX = '',
-- update with amount of invoice remainingupdateAPPTOAMT = a.AMTAPPLIED,
#tempsetORAPTOAM = a.AMTAPPLIED,
INVAMT = @curinvamtwhereAPFRDCNM = a.PMTNUM,
INVNUM = @curinvnum andAPFRDCTY = a.PMTTYPE,
INVTYPE = @curinvtypeAPFRDCDT = c.DOCDATE,
-- update with amount of payment remainingupdateApplyFromGLPostDate = c.GLPOSTDT,
#tempsetFROMCURR = c.CURNCYID,
PMTAMT = 0whereAPFRMAPLYAMT = a.AMTAPPLIED,
PMTNUM = @curpmtnum andActualApplyToAmount = a.AMTAPPLIEDfrom
PMTTYPE = @curpmttypeendelse if (@curinvamt 0)#temp ajoin RM20101 b on (b.DOCNUMBR =
and (@curinvamt>0)-- if the invoice amount is lessera.INVNUM and b.RMDTYPAL = a.INVTYPE)join
to the payment amountbeginselect @curpmtamt =RM20101 c on (c.DOCNUMBR = a.PMTNUM and
@curpmtamt - @curinvamt -- payment amountc.RMDTYPAL = a.PMTTYPE)wherea.AMTAPPLIED 0
remainingandnot exists (select 1from RM20201 dwhere
-- update with the amount that is applied to the currentd.APFRDCTY = a.PMTTYPE andd.APFRDCNM =
invoice froma.PMTNUM andd.APTODCTY = a.INVTYPE
-- the current paymentupdate #tempsetandd.APTODCNM = a.
AMTAPPLIED = @curinvamtwhere