Gossamer Forum
Home : Products : DBMan : Customization :

auto-calculations in fields?

Quote Reply
auto-calculations in fields?
im wondering if this is possible or if someone has already created a mod for this..

how to have a field be auto-generated based off the calculation from other fields.

such as:

Labor: $
Parts: $
Tax: $ (parts * .06)
Total: (labor + parts + tax)

tax and total would be calculated from the input of parts and labor and stored in the database. tax and total would probably be hidded fields. i have some idea where to start on this, but wanted to see if anyone has attempted this same idea.

Quote Reply
Re: auto-calculations in fields? In reply to
First, I would suggest that you define the tax rate in your .cfg file. Add a line somewhere --

$tax = .06;

If the tax rate changes, this makes it easier to remember where to make the change.

Is it really necessary to have the Tax and Total saved in a field on the record? Would it not be just as easy to compute it in html_record? The only reason I can see to have the fields is if you are using autogenerate.

If you are, the place to put these would be in add_record in db.cgi. Before the line that includes
&validate_record

add

$in{'Tax') = "\$" . ($in{'Parts'} * $tax);
$in{'Total'} = "\$" . ($in{'Labor'} + $in{'Parts'} + $in{'Tax'});

You would probably need to add the same thing to sub modify_record, in case there was a change in the Labor or Parts fields.



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





Quote Reply
Re: auto-calculations in fields? In reply to
Thanks. Much appreciated.

Since I am using auto-generate, how would i define Tax and Total in the .cfg?

Tax => [14, 'numer', -1, 10, 0, '', ''],
Total => [15, 'numer', -1, 10, 0, '', ''],

This didnt seem to do it, neither did making it visible (non-hidden).

Thanks again.

Quote Reply
Re: auto-calculations in fields? In reply to
The calculation will only take place after you add the record. On the add form, the fields will be blank.

If you want them to show up when a record is displayed, you will have to set the field length to something greater than -1.


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





Quote Reply
Re: auto-calculations in fields? In reply to
I would recommend investing some time and using your own forms and turning off auto_generate.

In html_record_form you would only have a field for labour and parts, but when displaying a record you would calcualte the tax, and grand total and display that as well.

It would be more work to setup the forms, but would be a better solution in the long run..

Cheers,

Alex