Simple math

Please report bugs and any annoyances here. Kindly include all possible details: steps to reproduce, expected result, actual result, screenshots, ... etc.
Post Reply
buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Simple math

Post by buck1942 » 2016-08-23 05:48

Using Appgini 5.51.

Given three fields:

budget
actual
remaining

The following syntax works:

sql("UPDATE monthly_budget set remaining = budget - actual ", $eo);

ONLY if actual is not NULL...

for example, if "budget" = $1000.00 and "actual" = null, then "remaining" will NOT calculate; however, if "budget" = $1000.00 and "actual" = .00 then "remaining" computes to $1000.00 as it should.

My problem is that "actual" needs to be a Read Only field. "Actual" only gets changed via a posted transaction, creating a detailed transaction audit trail. If I allow an operator, when setting up a new account, access to the "actual' field and and she/he fails to enter ".00" (thus the "actual" would be NULL) the "remaining" amount will be incorrect.

Zero fill works, but it makes for sloppy looking reports etc.

Looking for help...

Buck

peebee
AppGini Super Hero
AppGini Super Hero
Posts: 352
Joined: 2013-03-21 04:37

Re: Simple math

Post by peebee » 2016-08-23 05:59

Have you tried making default value for "actual" as 0 (zero)?

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Simple math

Post by grimblefritz » 2016-08-23 12:31

Or, if you want actual to be null (and not a default of zero) until it is set via program, you could compute remaining in php and update using that result.

Code: Select all

$remaining = $data['budget'] - $data['actual'];

sql("UPDATE monthly_budget set remaining = {$remaining}", $eo);
PHP's data casting and manipulation is far superior to MySQL's.

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: Simple math

Post by buck1942 » 2016-08-23 13:57

Grimble, I'll try that, thanks.

Peedee, I was looking for a way to default to "0", but I did not see how to do it... I'll take another look, that would be my "druthers"...

Thanks too,

Buck

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: Simple math

Post by buck1942 » 2016-08-23 14:24

peedee... I set the default to ".00" and all is well and works like I want it to.

Sometimes, it is sooo easy to overlook the obvious... when defining fields, I just looked over the "Default" option (even tho' it is in the middle of the screen)... go figure

Again, thanks to both of you for your help...

Buck

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: Simple math

Post by grimblefritz » 2016-08-23 14:30

No worries. It took me quite some time to realize that the default window size on my system was showing me one line of the "Guidelines regarding the .... data type". Only when I eventually maximized the window, quite by accident, did I notice there was more than one line there :)

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Simple math

Post by Bertv » 2016-08-24 07:41

When 'budget' can be null you have the same problem....
You can always use 'ifnull' if it is not sure the field has a value:

sql("UPDATE monthly_budget set remaining = ifnull(budget,0) - ifnull(actual,0) ", $eo);
Bert
I am using Appgini 5.75

Post Reply