Trouble Calculating field.

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
Stevo2112
Posts: 10
Joined: 2015-01-14 15:05

Trouble Calculating field.

Post by Stevo2112 » 2015-01-14 15:21

Hi, thanks in advance for any help. I am a noob and using the latest version of AppGini, I am trying to do a calculation in a table by place a hook in the before insert and the before update sections of the hooks file for that table. I used the example from the online tutorial, specifically

function tablename_before_insert(&$data, $memberInfo, &$args){

$data['total'] = $data['quantity'] * $data['unit_price'];

return TRUE;
}

My problem is the corresponding unit_price field is a auto-fill field from a parent table. The calculation is incorrect, it is not using the value from the field, but the id number of the parent table record.

To Clarify: let's say you have 20 quantity and the unit_price of Item No. 30 is $5.00 (which is auto-filled from an Items table), the total should be $100.00, but after the calculation is processed, total is equal to $600.00 which is the (quantity) * (id of the item from the parent table)

How can I fix this?

Thanks

Stevo2112
Posts: 10
Joined: 2015-01-14 15:05

Re: Trouble Calculating field.

Post by Stevo2112 » 2015-01-14 16:22

I found a snippet in another post that will work using after insert and update involving sql statements. Is there no way of having it calculate on the fly, with a simple expression? Seems to me if you can see the data in the web form, you should be able to manipulate it. Thanks again. This may be beyond the abilities of Appgini...

dallo
Posts: 8
Joined: 2014-05-19 14:43

Re: Trouble Calculating field.

Post by dallo » 2015-01-15 09:28

Hello steevo,

Please post you solution snippet, as this may help others.

thanks

Stevo2112
Posts: 10
Joined: 2015-01-14 15:05

Re: Trouble Calculating field.

Post by Stevo2112 » 2015-01-15 14:08

Here's my solution: it works perfectly... Placed in the table specific hooks file. Note I also put this function in *_after_update as well

function Table_after_insert($data, $memberInfo, &$args){

$id = makeSafe($data['selectedID']);
$item_id = makeSafe($data['{connected field in parent table}']);
$amt = sqlValue("SELECT {field that auto-fills field I originally wanted to calculate} FROM {parent table} WHERE id = '$item_id' ");
$quant= sqlValue("SELECT {field you want to use in calculation from your current table} FROM {current table} WHERE id = '$id' ");
$tot = $amt * $quant;
sql("UPDATE {current table} SET {result of calculation field, in current table} ='$tot' WHERE id ='$id' ", $eo);

return TRUE;


(result of calculation field, in current table) is also set to read only in Appgini. Must be placed in "after," because it acts on the database after the data has been added. The "$words" are variables and can be anything you want within reason, the {words} are field names from the two tables. I took a lot of swapping field names around until I realized that the variable statement "$item_id = makeSafe($data['{connected field in parent table}']);" was the one that controlled the whole thing... Hope this helps someone, I'm just spoiled from Ms Access, where all you had to do was pull up an expression builder...
Have a nice day! And thanks to the Original poster: wplim!!!!!

Post Reply