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 int
declare @curpmttype int
declare @maxid int
declare @counter int
-- Create a temporary table
create 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 payments
insert into #temp
(
CUSTNMBR,
INVNUM,
INVTYPE,
PMTNUM,
PMTTYPE,
INVAMT ,
PMTAMT,
AMTAPPLIED
)
select
CUSTNMBR = a.CUSTNMBR,
INVNUM = b.DOCNUMBR,
INVTYPE = b.RMDTYPAL,
PMTNUM = a.DOCNUMBR,
PMTTYPE = a.RMDTYPAL,
INVAMT = b.CURTRXAM,
PMTAMT = a.CURTRXAM,
AMTAPPLIED = 0
from RM20101 a
join RM20101 b on (a.CUSTNMBR = b.CUSTNMBR)
join RM00101 c on (a.CUSTNMBR = c.CUSTNMBR)
where
a.RMDTYPAL in (7, 8, 9) and
b.RMDTYPAL in (1, 3) and
a.CURTRXAM 0 and
b.CURTRXAM 0
order by
a.custnmbr,
b.DOCDATE,
a.DOCDATE,
a.DOCNUMBR,
b.DOCNUMBR
-- Iterate through each record
select @maxid = max([ID])
from #temp
select @counter = 1
while @counter = @curpmtamt) and (@curpmtamt>0) and (@curinvamt>0)-- if the invoice amount is greater or the same as the payment amount
begin
select @curinvamt = @curinvamt - @curpmtamt -- invoice amount remaining
-- update with the amount that is applied to the current invoice from
-- the current payment
update #temp
set
AMTAPPLIED = @curpmtamt
where
[ID] = @counter
-- update with amount of invoice remaining
update #temp
set
INVAMT = @curinvamt
where
INVNUM = @curinvnum and
INVTYPE = @curinvtype
-- update with amount of payment remaining
update #temp
set
PMTAMT = 0
where
PMTNUM = @curpmtnum and
PMTTYPE = @curpmttype
end
else if (@curinvamt 0) and (@curinvamt>0)-- if the invoice amount is lesser to the payment amount
begin
select @curpmtamt = @curpmtamt - @curinvamt -- payment amount remaining
-- update with the amount that is applied to the current invoice from
-- the current payment
update #temp
set
AMTAPPLIED = @curinvamt
where
[ID] = @counter
-- update with amount of invoice remaining
update #temp
set
INVAMT = 0
where
INVNUM = @curinvnum and
INVTYPE = @curinvtype
-- update with amount of payment remaining
update #temp
set
PMTAMT = @curpmtamt
where
PMTNUM = @curpmtnum and
PMTTYPE = @curpmttype
end
-- go to the next record
select @counter = @counter + 1
end
-- update the RM Open table with the correct amounts
update
RM20101
set
CURTRXAM = b.INVAMT
from
RM20101 a
join #temp b on (a.DOCNUMBR = b.INVNUM and a.RMDTYPAL = b.INVTYPE)
update
RM20101
set
CURTRXAM = b.PMTAMT
from
RM20101 a
join #temp b on (a.DOCNUMBR = b.PMTNUM and a.RMDTYPAL = b.PMTTYPE)
-- create the RM Apply record or update if records already exist
update
RM20201
set
DATE1 = 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.AMTAPPLIED
from
#temp a
join 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 and
d.APFRDCNM = a.PMTNUM and
d.APTODCTY = a.INVTYPE and
d.APTODCNM = a.INVNUM)
where
a.AMTAPPLIED 0
insert into RM20201
(CUSTNMBR,
DATE1,
GLPOSTDT,
POSTED,
APTODCNM,
APTODCTY,< /p>
APTODCDT,
ApplyToGLPostDate,
CURNCYID,
CURRNIDX,
APPTOAMT,
ORAPT OAM,
APFRDCNM,
APFRDCTY,
APFRDCDT,
ApplyFromGLPostDate,
FROMCURR,
< p>APFRMAPLYAMT,ActualApplyToAmount)
select
CUSTNMBR = 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.AMTAPPLIED
from
#temp a
join 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)
where
a.AMTAPPLIED 0 and
not exists (select 1
from RM20201 d
where d.APFRDCTY = a.PMTTYPE and
d.APFRDCNM = a.PMTNUM and
d.APTODCTY = a.INVTYPE and
d.APTODCNM = a.INVNUM)
drop table #temp
About The Author
Andrew 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
![]() |
|
![]() |
|
![]() |
|
![]() |
Intro This concise article will tell you in plain English... Read More
There are several kinds of software piracy. The bottom line... Read More
Anyone who has ever used Microsoft Word knows that it... Read More
The software giants don't do everything and don't always produce... Read More
Imagine something that follows you home and sets itself up... Read More
Combining Microsoft Business Solutions Great Plains ERP with non-Microsoft Business... Read More
New post-recession era has new features, which didn't exist in... Read More
We will base our prognosis on our Microsoft Business Solutions... Read More
So, why should you use any O/R mapping tool? I... Read More
eStore Advantage allows front-office applications to communicate with back-office business... Read More
Software development is a risky business.Many software developers are barely... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
Buying accounting software is a major investment. It's an important... Read More
Just the thought of a duel-boot scares many people away,... Read More
The vast majority of us will have, at some point,... Read More
Microsoft-Outlook is a pretty amazing program. So much more than... Read More
Microsoft Great Plains ? ERM from Microsoft Business Solutions and... Read More
Lotus Domino/Notes ? Microsoft Great Plains tandem as ERP with... Read More
With thousands of web pages added to the Net every... Read More
C++ Function templates are those functions which can handle different... Read More
There are many commands that are used in linux on... Read More
In linux, one of great commands for finding out information... Read More
The most important things you can do for your computer... Read More
Current Microsoft Business Solutions Great Plains has more that 10... Read More
Programming Help for BeginnersWe write programs to instruct computers. When... Read More
Traditionally we were considering functionally rich systems, such as SAP,... Read More
Internet worms. Is your PC infected?If your computer has become... Read More
A LOT OF UNWANTED FILES.When you uninstall an item of... Read More
1. What determines the software price? Is it Per Seat... Read More
Some introduction into Great Plains Software products, now Microsoft Business... Read More
As seeing large number of implementations ? in our case... Read More
This article is the third of a series of articles... Read More
One day, you suddenly realize that your computer started to... Read More
Microsoft Business Solutions Great Plains as new ERP for multinational... Read More
In the new era of internet marketing the problem of... Read More
FTP stands for "file transfer protocol". FTP is basically a... Read More
Once upon a time not so long ago, there was... Read More
We will base our prognosis on our Microsoft Business Solutions... Read More
Most people don't use Photoshop to its fullest capabilities. Here... Read More
Running Applications in Compatibility Mode With Windows XP, you can... Read More
Microsoft CRM was designed to be easily customizable. Microsoft CRM... Read More
Although statistics often is blamed for various deadly sins --... Read More
What is installation in the language of technology? Installation... Read More
I completed an experiment recently. I wanted to find out... Read More
Is Photoshop CS2 worth the upgrade? You bet it is!... Read More
.NET platform does not support multiple inheritance. Do not confuse... Read More
Microsoft Business Solutions is now in process of creating so... Read More
(1) Avoid using the same variable again and again for... Read More
The COSMIC FP (function point) software quality metric, is no... Read More
Looks like Microsoft Great Plains becomes more and more... Read More
It is possible that if one avoided all sources of... Read More
Accounts payable is just one area of office management where... Read More
Healthcare facilities such as clinics, hospitals, and biomedical laboratories can... Read More
Small can be beautiful! Working with Knoppix for the past... Read More
This article is for advanced Microsoft CRM SDK C# developers.... Read More
Cyberspace has opened up a new frontier with exciting possibilities... Read More
One day, you suddenly realize that your computer started to... Read More
If you have Microsoft Great Plains and support it for... Read More
Writing software manuals is boring, isn't it? We often think:... Read More
In the case when you represent mid-size or mid-size-to-large business,... Read More
As you probably know, when Microsoft purchased Great Plains Software... Read More
All your software is stored on a hard-drive. But how... Read More
Using professional icons in your application or website can bring... Read More
All of us know that Microsoft bought former Great Plains... Read More
Assertion facility is added in J2SE 1.4. In order to... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Software |