dhowells
10th June 2002, 16:50
Does anyone have any slick code (or variables like total..n/count..n) for percentages in reports. I am getting increased requests to have these appear on some custom reports.
An example would be a report for sales by order type by month. For each month they want a percentage of the sales by order type. I typically use the report to do the sorting, summing and totalling. Because of this I do not know what my total count is at the time the data is being written to the report to do the math. I am basically trying to keep from looping through the data twice.
Thanks for any input,
Dan
mark_h
10th June 2002, 17:16
I do not have any slick code, but the way I usually handle this(at least now) is with the report script. Early on I used the total..n type variables and then every time the user changed the reports I had to redo all the calculations. So I avoid using those now.
If you have to do some type of reporting on every page, you can always read through the data file before the report starts. You could use file.stat or something like it to get the total number of records. Let me find the post for reading the input file prior to print the report.
Here is the link (http://www.baanboard.com/baanboard/showthread.php?s=&threadid=1908). Not sure if this helpful, but it may give you some ideas.
Mark
dbinderbr
23rd August 2002, 22:43
Hello,
This is an example of using percentages in a report. I hope it can help you.
It makes the report run slowly, but it's a solution.
|******************************************************************************
|* tssmao401 0 VRC B40O c4 sfp0
|* Print Total Used Hours by Cost Center
|* Daniel C Binder
|* 25/07/02 [15:38]
|******************************************************************************
|* Script Type: 4
|******************************************************************************
|****************************** DECLARATION SECTION ***************************
declaration:
table ttssma301
table ttssma310
table ttfgld422
table ttfgld010
table ttssma986
extern domain tcdate date.f, date.t
extern domain tfgld.dimx dimx.f, dimx.t
extern domain tcacap qthr.o, tot.hours
extern domain tcperc perc.r
string dimx.o(6,200)
domain tcacap hour.o(200), perc.o(200)
domain tcbool found
#define MAX 200
|****************************** FORM SECTION ***************************
form.1:
init.form:
get.screen.defaults()
|****************************** CHOICE SECTION ***************************
choice.cont.process:
on.choice:
execute(print.data)
choice.print.data:
on.choice:
read.tssma986()
if rprt_open() then
status.mess("Imprimindo...")
read.main.table()
rprt_close()
status.del()
message("Impressão Finalizada.")
endif
|****************************** FIELD SECTION ***************************
field.date.f:
when.field.changes:
date.t = date.f
field.dimx.f:
when.field.changes:
dimx.t = dimx.f
|****************************** FUNCTION SECTION ***************************
functions:
function read.main.table()
{
set.mem(perc.o, 0)
set.mem(hour.o, 0)
set.mem(dimx.o, "")
calculate.percentages()
|# Any modifications made in the selection below have to
|# be done into the function calculate percentages.
select tssma310.*
from tssma310
where tssma310.date inrange :date.f and :date.t
and tssma310.tcst = tssma.tcst.worktime
order by tssma310._index1
selectdo
select tssma301.*
from tssma301
where tssma301._index1 = {:tssma310.orno}
and (tssma301.swor = tssma.swor.costed
or tssma301.swor = tssma.swor.history)
as set with 1 rows
selectdo
if dimx.is.ok() then
qthr.o = tssma310.qana
print.dimx.percentage()
rprt_send()
endif
endselect
endselect
}
function long dimx.is.ok()
{
select tfgld422.*
from tfgld422
where tfgld422._index1 = {:tssma986.inel.o, 1, 1}
and tfgld422.svf1 <= :tssma301.cins
and tfgld422.svt1 >= :tssma301.cins
as set with 1 rows
selectdo
select tfgld010.desc
from tfgld010
where tfgld010._index1 = {1, :tfgld422.dimx}
as set with 1 rows
selectdo
if strip$(tfgld422.dimx) >= dimx.f and strip$(tfgld422.dimx) <= dimx.t then
return(true)
endif
endselect
endselect
return(false)
}
function calculate.percentages()
{
select tssma310.*
from tssma310
where tssma310.date inrange :date.f and :date.t
and tssma310.tcst = tssma.tcst.worktime
order by tssma310._index1
selectdo
select tssma301.*
from tssma301
where tssma301._index1 = {:tssma310.orno}
and (tssma301.swor = tssma.swor.costed
or tssma301.swor = tssma.swor.history)
selectdo
if dimx.is.ok() then
qthr.o = tssma310.qana
fill.arrays()
endif
endselect
endselect
calculate.total.hours()
calculate.dimx.percentages()
}
function fill.arrays()
{
long h.elem
h.elem = find.dimx.in.array()
if found then
hour.o(h.elem) = hour.o(h.elem) + qthr.o
else
dimx.o(1,h.elem) = tfgld422.dimx
hour.o(h.elem) = qthr.o
endif
}
function long find.dimx.in.array()
{
long c
for c = 1 to MAX
if isspace(dimx.o(1,c)) then
break
endif
if strip$(dimx.o(1,c)) = strip$(tfgld422.dimx) then
found = true
return(c)
endif
endfor
found = false
return(c)
}
function calculate.total.hours()
{
long c
for c = 1 to MAX
if isspace(dimx.o(1,c)) then
break
endif
tot.hours = tot.hours + hour.o(c)
endfor
}
function calculate.dimx.percentages()
{
long c
for c = 1 to MAX
if isspace(dimx.o(1,c)) then
break
endif
perc.o(c) = (hour.o(c) / tot.hours) * 100
endfor
}
function print.dimx.percentage()
{
long c
for c = 1 to MAX
if isspace(dimx.o(1,c)) then
perc.r = 0
break
endif
if strip$(dimx.o(1,c)) = strip$(tfgld422.dimx) then
perc.r = perc.o(c)
break
endif
endfor
}
function read.tssma986()
{
select tssma986.*
from tssma986
as set with 1 rows
selectdo
break
selectempty
message("Parameters for Service Reports not found. Use session tssmao101m000.")
choice.again()
endselect
}
Any doubts about the logic used just ask me.
Best Regards,
Daniel C Binder
:cool: