USP_REPORT_REVENUE
Returns data for the sponsorship revenue report.
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@DIMENSION1 | nvarchar(100) | IN | |
@DIMENSION1VALUE | nvarchar(255) | IN | |
@DIMENSION1VALUE3 | nvarchar(255) | IN | |
@DIMENSION2 | nvarchar(100) | IN | |
@DIMENSION2VALUE | nvarchar(255) | IN | |
@DIMENSION2VALUE3 | nvarchar(255) | IN | |
@DIMENSION3 | nvarchar(100) | IN | |
@DIMENSION3VALUE | nvarchar(255) | IN | |
@DIMENSION3VALUE3 | nvarchar(255) | IN | |
@STARTDATE | datetime | IN | |
@ENDDATE | datetime | IN | |
@DATETYPE | nvarchar(15) | IN | |
@DATERANGEDISPLAY | nvarchar(100) | IN | |
@REPORTUSERID | nvarchar(128) | IN | |
@CURRENCYCODE | smallint | IN | |
@ALTREPORTUSERID | nvarchar(128) | IN |
Definition
Copy
CREATE procedure dbo.USP_REPORT_REVENUE(
@DIMENSION1 as nvarchar(100) = null,
@DIMENSION1VALUE as nvarchar(255) = null,
@DIMENSION1VALUE3 as nvarchar(255) = null,
@DIMENSION2 as nvarchar(100) = null,
@DIMENSION2VALUE as nvarchar(255) = null,
@DIMENSION2VALUE3 as nvarchar(255) = null,
@DIMENSION3 as nvarchar(100) = null,
@DIMENSION3VALUE as nvarchar(255) = null,
@DIMENSION3VALUE3 as nvarchar(255) = null,
@STARTDATE datetime = null,
@ENDDATE datetime = null,
@DATETYPE nvarchar(15) = null,
@DATERANGEDISPLAY nvarchar(100) = null,
@REPORTUSERID nvarchar(128) = null,
@CURRENCYCODE smallint = null,
@ALTREPORTUSERID nvarchar(128) = null
)
with execute as owner
as
set nocount on;
declare @SQLTOEXEC nvarchar(max);
declare @FROMCLAUSE nvarchar(4000);
declare @COLUMNSELECT nvarchar(2000);
declare @WHERECLAUSE nvarchar(1000);
declare @GROUPBYCLAUSE nvarchar(500);
declare @SELECT nvarchar(1000);
declare @FROM nvarchar(1000);
declare @COLUMN nvarchar(1000);
declare @WHERE nvarchar(1000);
declare @LOCATIONCOUNT tinyint = 0;
declare @LOCATIONTYPEENUM tinyint = 2;
declare @DATECOLTEMP nvarchar(800);
declare @DIMENSIONCOLUMNS nvarchar(100) = 'DIMENSION1';
declare @RESULTSELECTION nvarchar(2000);
declare @SELECTEDCURRENCYID uniqueidentifier;
declare @CURRENTAPPUSERID uniqueidentifier = dbo.UFN_APPUSER_GETREPORTAPPUSERID(@REPORTUSERID, @ALTREPORTUSERID);
if @CURRENCYCODE =3
begin
if dbo.UFN_CURRENCYSET_GETAPPUSERCURRENCYSET(@CURRENTAPPUSERID) is not null
begin
select @SELECTEDCURRENCYID = CURRENCYSET.BASECURRENCYID
from dbo.CURRENCYSET
where
CURRENCYSET.ID = dbo.UFN_CURRENCYSET_GETAPPUSERCURRENCYSET(@CURRENTAPPUSERID);
end
else
begin
select @SELECTEDCURRENCYID = CURRENCYSET.BASECURRENCYID
from dbo.CURRENCYSET
where
CURRENCYSET.ID = dbo.UFN_CURRENCYSET_GETAPPUSERDEFAULTCURRENCYSET();
end
end
else
set @SELECTEDCURRENCYID = dbo.UFN_CURRENCY_GETORGANIZATIONCURRENCY();
set @FROMCLAUSE = 'from dbo.SPONSORSHIP SP ' +
' inner join dbo.SPONSORSHIPPAYMENT SPAY ON SPAY.SPONSORSHIPID = SP.ID ' +
' inner join dbo.REVENUESPLIT SPLT on SPLT.ID = SPAY.ID ' +
' inner join dbo.REVENUE RV on RV.ID = SPLT.REVENUEID ' +
' inner join dbo.RECURRINGGIFTACTIVITY RGA on RGA.PAYMENTREVENUEID = SPLT.ID ';
set @WHERECLAUSE = 'where (@STARTDATE is null or RV.DATE >= dbo.UFN_DATE_GETEARLIESTTIME(@STARTDATE)) ' +
' and (@ENDDATE is null or RV.DATE <= dbo.UFN_DATE_GETLATESTTIME(@ENDDATE)) ';
set @COLUMNSELECT = 'select dbo.UFN_REPORT_PROCESSDATE_PARAM('+ @DATETYPE + ',RV.DATE) DATECOL, datename(year, RV.DATE) YEARCOL,' + char(13)+
' case when @CURRENCYCODE = 4 then ' + char(13) +
' RV.AMOUNT ' + char(13) +
'else ' + char(13) +
' case @SELECTEDCURRENCYID when RGA.APPLICATIONCURRENCYID then RGA.AMOUNT ' + char(13) +
' else ' + char(13) +
' dbo.UFN_CURRENCY_CONVERT(RGA.AMOUNT, dbo.UFN_CURRENCYEXCHANGERATE_GETLATESTINCLUDEEXPIRED(RGA.APPLICATIONCURRENCYID, @SELECTEDCURRENCYID, RV.DATE, 1, null)) ' + char(13) +
' end ' + char(13) +
'end as AMOUNT, ' + char(13) +
'RV.BASECURRENCYID as CURRENCYID, ' + char(13) +
'case when @CURRENCYCODE=4 then ' + char(13) +
' (select CURRENCYSYMBOL from dbo.UFN_CURRENCY_GETPROPERTIES(RV.BASECURRENCYID)) ' + char(13) +
'else ' + char(13) +
' (select CURRENCYSYMBOL from dbo.UFN_CURRENCY_GETPROPERTIES(@SELECTEDCURRENCYID)) ' + char(13) +
'end as CURRENCYSYMBOL, ' + char(13) +
'case when @CURRENCYCODE = 4 then ' + char(13) +
' (select ISO4217 from dbo.UFN_CURRENCY_GETPROPERTIES(RV.BASECURRENCYID)) ' + char(13) +
'else ' + char(13) +
' (select ISO4217 from dbo.UFN_CURRENCY_GETPROPERTIES(@SELECTEDCURRENCYID)) ' + char(13) +
'end as ISOCURRENCYCODE, ' + char(13) +
'case when @CURRENCYCODE= 4 then ' + char(13) +
' (select DECIMALDIGITS from dbo.UFN_CURRENCY_GETPROPERTIES(RV.BASECURRENCYID)) ' + char(13) +
'else ' + char(13) +
' (select DECIMALDIGITS from dbo.UFN_CURRENCY_GETPROPERTIES(@SELECTEDCURRENCYID)) ' + char(13) +
'end as DECIMALDIGITS, ' + char(13) +
'case when @CURRENCYCODE= 4 then ' + char(13) +
' (select SYMBOLDISPLAYSETTINGCODE from dbo.UFN_CURRENCY_GETPROPERTIES(RV.BASECURRENCYID)) ' + char(13) +
'else ' + char(13) +
'(select SYMBOLDISPLAYSETTINGCODE from dbo.UFN_CURRENCY_GETPROPERTIES(@SELECTEDCURRENCYID)) ' + char(13) +
'end as CURRENCYSYMBOLDISPLAYSETTINGCODE ';
-- count how many dimension parameters have location type specified
if @DIMENSION1 = @LOCATIONTYPEENUM
begin
set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
end
if @DIMENSION2 is not null and @DIMENSION2 = @LOCATIONTYPEENUM
begin
set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
end
if @DIMENSION3 is not null and @DIMENSION3 = @LOCATIONTYPEENUM
begin
set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
end
-- only process dimension if not null and not Location Type
if @DIMENSION1 is not null and @DIMENSION1 <> @LOCATIONTYPEENUM
begin
exec dbo.USP_REPORT_PROCESSDIMENSION
@DIMENSION1,
'@DIMENSION1VALUE',
@FROM output,
@COLUMN output,
@WHERE output,
'DIMENSION1'
if @FROM is not null
begin
set @FROMCLAUSE = @FROMCLAUSE + @FROM;
end
set @COLUMNSELECT = @COLUMNSELECT + @COLUMN;
set @WHERECLAUSE += @WHERE + ' and @DIMENSION1VALUE3 is null';
set @GROUPBYCLAUSE = 'DIMENSION1';
--
-- set these to null for the next row
--
set @FROM = '';
set @COLUMN = '';
set @WHERE = '';
end
-- only process dimension if it's not null and is not Location Type
if @DIMENSION2 is not null
begin
set @DIMENSIONCOLUMNS = @DIMENSIONCOLUMNS + ', DIMENSION2';
if @DIMENSION2 <> @LOCATIONTYPEENUM
begin
exec dbo.USP_REPORT_PROCESSDIMENSION
@DIMENSION2,
'@DIMENSION2VALUE',
@FROM output,
@COLUMN output,
@WHERE output,
'DIMENSION2'
-- for channel and appeal from clause is not used.
if @FROM is not null
begin
set @FROMCLAUSE = @FROMCLAUSE + @FROM;
end
set @COLUMNSELECT = @COLUMNSELECT + @COLUMN;
set @WHERECLAUSE = @WHERECLAUSE + @WHERE + ' and @DIMENSION2VALUE3 is null';
if @GROUPBYCLAUSE is not null
begin
set @GROUPBYCLAUSE = @GROUPBYCLAUSE + ', DIMENSION2';
end
else
begin
set @GROUPBYCLAUSE = 'DIMENSION2';
end
--
-- set these to null for the next row
--
set @FROM = '';
set @COLUMN = '';
set @WHERE = '';
end
end
else
begin
set @WHERECLAUSE = @WHERECLAUSE + ' and @DIMENSION2VALUE is null and @DIMENSION2VALUE3 is null';
end
--
if @DIMENSION3 is not null
begin
set @DIMENSIONCOLUMNS = @DIMENSIONCOLUMNS + ', DIMENSION3';
if @DIMENSION3 <> @LOCATIONTYPEENUM
begin
exec dbo.USP_REPORT_PROCESSDIMENSION
@DIMENSION3,
'@DIMENSION3VALUE',
@FROM output,
@COLUMN output,
@WHERE output,
'DIMENSION3'
-- for channel and appeal from clause is not used.
if @FROM is not null
begin
set @FROMCLAUSE = @FROMCLAUSE + @FROM;
end
set @COLUMNSELECT = @COLUMNSELECT + @COLUMN;
set @WHERECLAUSE = @WHERECLAUSE + @WHERE + ' and @DIMENSION3VALUE3 is null';
if @GROUPBYCLAUSE is not null
begin
set @GROUPBYCLAUSE = @GROUPBYCLAUSE + ', DIMENSION3';
end
else
begin
set @GROUPBYCLAUSE = 'DIMENSION3';
end
end
end
else
begin
set @WHERECLAUSE = @WHERECLAUSE + ' and @DIMENSION3VALUE is null and @DIMENSION3VALUE3 is null';
end
-- handle processing of location types
If @LOCATIONCOUNT > 0
begin
exec dbo.USP_REPORT_PROCESSLOCATIONTYPE_DIMENSION
@LOCATIONCOUNT,
@DIMENSION1,
@DIMENSION2,
@DIMENSION3,
@COLUMNSELECT,
@FROMCLAUSE,
@WHERECLAUSE,
@GROUPBYCLAUSE,
@SQLTOEXEC output,
'sum(AMOUNT) CNT',
null,
1
end
else
begin
set @SELECT = 'select ' + char(13) +
' sum(AMOUNT) as CNT, ' + char(13) +
' DATECOL, YEARCOL, ISOCURRENCYCODE,DECIMALDIGITS,CURRENCYSYMBOL,CURRENCYSYMBOLDISPLAYSETTINGCODE, '+ @GROUPBYCLAUSE
-- construct the sql query
set @SQLTOEXEC = @SELECT + ' from (' + @COLUMNSELECT + ' ' + @FROMCLAUSE + ' ' + @WHERECLAUSE + ') as QRY group by DATECOL, YEARCOL, ' + @GROUPBYCLAUSE +',ISOCURRENCYCODE,DECIMALDIGITS,CURRENCYSYMBOL,CURRENCYSYMBOLDISPLAYSETTINGCODE';
end
set @DATECOLTEMP = dbo.UFN_REPORT_BUILD_DATECOL_TABLE_STRING(@DATETYPE)
-- append to the query the code that handles blanc column values
set @RESULTSELECTION =' delete from #DATECOL where DATECOL+YEARCOL in (select DATECOL+YEARCOL from #RESULT) ' +
' insert into #RESULT (DATECOL, YEARCOL, CNT, ISOCURRENCYCODE,DECIMALDIGITS,CURRENCYSYMBOL,CURRENCYSYMBOLDISPLAYSETTINGCODE,' + @DIMENSIONCOLUMNS + ') ' +
' select distinct DATEC , YEARC , 0, CROSSJOIN.ISOCURRENCYCODE, CROSSJOIN.DECIMALDIGITS, CROSSJOIN.CURRENCYSYMBOL,CROSSJOIN.CURRENCYSYMBOLDISPLAYSETTINGCODE, ' + @DIMENSIONCOLUMNS +
' from (select #DATECOL.DATECOL as DATEC, #DATECOL.YEARCOL as YEARC, #RESULT.* from #DATECOL cross join #RESULT) as CROSSJOIN ' +
' select CNT, DATECOL, YEARCOL,ISOCURRENCYCODE,DECIMALDIGITS,CURRENCYSYMBOL,CURRENCYSYMBOLDISPLAYSETTINGCODE, ' + @DIMENSIONCOLUMNS + ' from #RESULT '
set @SQLTOEXEC = @DATECOLTEMP + ' select * into #RESULT from (' +@SQLTOEXEC + ') as RES ' + @RESULTSELECTION
--insert into temp_test (test) values(@SQLTOEXEC)
-- execute the query
exec sp_executesql @SQLTOEXEC,
N'@CURRENCYCODE smallint,@SELECTEDCURRENCYID uniqueidentifier, @DIMENSION1VALUE nvarchar(255), @DIMENSION2VALUE nvarchar(255), @DIMENSION3VALUE nvarchar(255), @STARTDATE datetime, @ENDDATE datetime, @DIMENSION1VALUE3 nvarchar(255), @DIMENSION2VALUE3 nvarchar(255), @DIMENSION3VALUE3 nvarchar(255)',
@CURRENCYCODE=@CURRENCYCODE,@SELECTEDCURRENCYID=@SELECTEDCURRENCYID,@DIMENSION1VALUE=@DIMENSION1VALUE, @DIMENSION2VALUE=@DIMENSION2VALUE, @DIMENSION3VALUE=@DIMENSION3VALUE, @STARTDATE=@STARTDATE, @ENDDATE=@ENDDATE, @DIMENSION1VALUE3=@DIMENSION1VALUE3, @DIMENSION2VALUE3=@DIMENSION2VALUE3, @DIMENSION3VALUE3=@DIMENSION3VALUE3 ;