Gossamer Forum
Home : Products : DBMan : Customization :

Calculate field based on other fields

Quote Reply
Calculate field based on other fields
My knowlege of Perl is rudimentary, so I'm having a hard time trying to figure out how to base the value of a field on the entered values of 3 other fields. Below is my attempt at the sub, but I'm not even sure whether it should be incorporated in db.cgi or html.pl
(I have the commented variables, listed here at the end, declared in my db.cfg file)

sub balance {
#----------------------------------
#Calculate the balance based on single-day fee, weekend-fee, and
#member status.

while($rec{'USAF_mem'} eq ('yes')) {
($sat_fee - $rec{'Paid') if $rec{'Attend_Sat'} eq ('Yes') && $rec{'Attend_Sun'} eq ('') ;
($sun_fee - $rec{'Paid') if $rec{'Attend_Sun'} eq ('Yes') && $rec{'Attend_Sat'} eq ('') ;
($both_fee - $rec{'Paid') if $rec{'Attend_Sat'} eq ('Yes') && $rec{'Attend_Sun'} eq ('Yes') ;
{last}}

while($rec{'USAF_mem'} eq ('No')) {

($sat_Non_fee - $rec{'Paid') if $rec{'Attend_Sat'} eq ('Yes') && $rec{'Attend_Sun'} eq ('') ;
($sun_Non_fee - $rec{'Paid') if $rec{'Attend_Sun'} eq ('Yes') && $rec{'Attend_Sat'} eq ('') ;
($both_Non_fee - $rec{'Paid') if $rec{'Attend_Sat'} eq ('Yes') && $rec{'Attend_Sun'} eq ('Yes') ;
{last}}
}





# $sat_fee = 60.00;
# $sun_fee = 40.00;
# $both_fee = 80.00;
# $sat_Non_fee = 75.00;
# $sun_Non_fee = 55.00;
# $both_Non_fee = 100.00;

I'd appreciate any help.
Thanks.
-Brian
Quote Reply
Re: Calculate field based on other fields In reply to
Where you put the code depends on what you want to do with it. Is the amount going to be computed when the record is added? Will this amount be entered into a field in the database? Or is this just something you're computing as you print out the record?

I think you want to use if instead of while, especially if you're computing the value based on one record.

There are a couple of other syntax errors in your code. For one thing, you need to have

$rec{'Paid'}

instead of

$rec{'Paid'

The other problem is that you have no variable to hold, for example,

$both_fee - $rec{'Paid'}

You need something like

$balance = $both_fee - $rec{'Paid'}

However, I think we can make this code a little more compact if I have a little more understanding of what you intend to do.


------------------
JPD





Quote Reply
Re: Calculate field based on other fields In reply to
The amount will be computed when the record is added. The 'paid' field is editable by admin, but I want the 'balance' field to be based on:
1) Days attended (Sat, Sun, Both)
2) Amount Paid (cfg default is zero)
3) Member status (member vs. non-member)

So there's a total of 6 possible fees (based on membership and days of attendance) and the balance will equal the appropriate fee less the amount paid.

Of these fields, only the member status and days of attendance can be changed by the user. I want the user to see the total fees at the outset, as well as the balance once admin has recorded an amount paid.

At present I have fields for 'paid' and 'balance' but I don't have one for total. I suspect it would be easier if I added this.
Quote Reply
Re: Calculate field based on other fields In reply to
It probably would be a good idea to have both a "Total" and a "Balance" field. Actually, you don't need a "Balance" field after all. Just a "Total" field and a "Paid" field. You can compute the balance at the time you print the record out.

To compute the "Total" field, in db.cgi, sub add_record, after

($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);

add

Code:
if ($in{'USAF_mem'} eq ('Yes')) {
if ($in{'Attend_Sat'} eq 'Yes') {
if ($in{'Attend_Sun'} eq 'Yes') {
$in{'Total'} = $both_fee;
}
else {
$in{'Total'} = $sat_fee;
}
}
elsif ($in{'Attend_Sun'} eq 'Yes') {
$in{'Total'} = $sun_fee;
}
}
else {
if ($in{'Attend_Sat'} eq 'Yes') {
if ($in{'Attend_Sun'} eq 'Yes') {
$in{'Total'} = $both_Non_fee;
}
else {
$in{'Total'} = $sat_Non_fee;
}
}
elsif ($in{'Attend_Sun'} eq 'Yes') {
$in{'Total'} = $sun_Non_fee;
}
}

Add the same code to sub modify_record, after

my ($status, $line, @lines, @data, $output, $found, $restricted);

In sub html_record, you can add, after

my (%rec) = @_;

Code:
$balance = $rec{'Total'} - $rec{'Paid'};

and then use the $balance variable to print out what the user owes.

Does this make sense to you?


------------------
JPD





Quote Reply
Re: Calculate field based on other fields In reply to
Yes, that seems to work just fine. And so *seemingly* simple! Thanks mucho.
-Brian