Friday, July 7, 2017

Dynamics GP - Customer Payment Summary Inquiry does not show correct YTD and LTD calculations

Average Days to pay manual calculation
https://vaidymohan.com/2014/12/11/average-days-to-pay-calculation-history-open-sql-script/

Issue:
In Inquiry>Sales>Customer Payment Summary>Life to Date - Average days to pay is 0

Resolution1 - Script
--------------------------

/****** Object:  Table dbo.JG00101    Script Date: 10/13/98 11:09:02 AM ******/
if exists (select * from sysobjects where id = object_id('dbo.JG00101') and sysstat & 0xf = 3)
drop table dbo.JG00101
GO

/****** Object:  Table dbo.JG00101    Script Date: 10/13/98 11:09:02 AM ******/
CREATE TABLE dbo.JG00101 (
CUSTNMBR char (15) NOT NULL ,
NUMBCUST numeric(19, 5)  NULL ,
THECOUNT numeric(19, 5)  NULL ,

)
GO

setuser 'dbo'
GO



setuser
GO



declare @x numeric(19, 5)
declare @x2 numeric(19, 5)
declare @CUSTNMBR  char (15)
declare @AVGDTPYR numeric(19, 5)
declare @cStatement varchar(255)
declare @DATE1 numeric(19,5)
declare @DATE2 numeric(19,5)
declare @DATE3 numeric(19,5)
declare T_cursor CURSOR for select distinct RM20101.CUSTNMBR from RM20101

set nocount on
OPEN T_cursor
FETCH NEXT FROM T_cursor INTO @CUSTNMBR
WHILE (@@FETCH_STATUS <> -1)
begin
PRINT @CUSTNMBR

select @DATE1 = sum(DATEDIFF(day, DOCDATE, DINVPDOF)) from RM20101 where DINVPDOF >
'Jan 1 1900 12:00AM' and RMDTYPAL = 1 and RM20101.CUSTNMBR = @CUSTNMBR

select @x = count(RM20101.CUSTNMBR) from RM20101 where RM20101.CUSTNMBR = @CUSTNMBR
and RMDTYPAL = 1

if @x is null select @x = 0, @DATE1 = 0

insert into JG00101 (CUSTNMBR, NUMBCUST, THECOUNT)
select @CUSTNMBR, @x, @DATE1

FETCH NEXT FROM T_cursor INTO @CUSTNMBR
end
DEALLOCATE T_cursor


declare T_cursor CURSOR for select distinct RM30101.CUSTNMBR from RM30101

set nocount on
OPEN T_cursor
FETCH NEXT FROM T_cursor INTO @CUSTNMBR
WHILE (@@FETCH_STATUS <> -1)
begin
PRINT @CUSTNMBR

select @DATE2 = sum(DATEDIFF(day, DOCDATE, DINVPDOF)) from RM30101 where DINVPDOF >
'Jan 1 1900 12:00AM' and RM30101.CUSTNMBR = @CUSTNMBR
select @DATE2
select @x2 = count(RM30101.CUSTNMBR) from RM30101 where RM30101.CUSTNMBR = @CUSTNMBR
and RMDTYPAL = 1

if exists (select CUSTNMBR from JG00101 where CUSTNMBR = @CUSTNMBR)
update JG00101 set THECOUNT = @DATE2 + THECOUNT, NUMBCUST = NUMBCUST + @x2
where JG00101.CUSTNMBR = @CUSTNMBR
else
insert into JG00101 (CUSTNMBR, NUMBCUST, THECOUNT)
select @CUSTNMBR, @x2, @DATE2

FETCH NEXT FROM T_cursor INTO @CUSTNMBR
end
DEALLOCATE T_cursor


delete JG00101 where THECOUNT is null
update JG00101 set THECOUNT = 1, NUMBCUST = 1 where NUMBCUST = 0

update  RM00103 set AVGDTPYR = THECOUNT / NUMBCUST from JG00101, RM00103
where RM00103.CUSTNMBR = JG00101.CUSTNMBR

print 'Script is finished'
----------------------------------------------

If you get an error message when running this script, most likely there is too much data for it to handle, and you will need to open a support request from Microsoft to let them provide you with a cnk mod that can be used to recalculate and reset the numbers.
If the issue persists, the mod can be installed on all clients to constantly recalculate and update the values.

No comments:

Post a Comment