Problem with after insert hook

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
vikeh
Posts: 7
Joined: 2014-07-30 14:47

Problem with after insert hook

Post by vikeh » 2014-08-02 12:35

Hi,

I'm having a problem with a simple multiplication.

I have a table invoice_items. When I select the item which is a lookup from another table, the price automatically will be shown as it is an autofill.

After I save it suppose to calculate the Item_Price * Quantity which is not working due to the Price field is an autofill. When I remove the autofill option of the Price field and input it manually, the calculation works.

How can I make it work while the price field is autofill please?


Thanks in advance.

udayvatturi
AppGini Super Hero
AppGini Super Hero
Posts: 85
Joined: 2014-06-14 03:08
Location: India
Contact:

Re: Problem with after insert hook

Post by udayvatturi » 2014-08-14 04:08

There are many ways you can achieve this.
As per my knowledge, If the field is auto fill then you will not get that value in $data variable.


1. In hooks you need to write sqlValue to get price from other table using productId (or what ever it is) and then do Item_Price * Quantity

2. Remove autofill,
Do the way you did.
Use magic hooks to make the field readonly,

proque_cu
Posts: 3
Joined: 2016-02-02 04:43

Re: Problem with after insert hook

Post by proque_cu » 2016-02-02 04:49

Hi guys,

I'm quite green to the product.... :(
Are you able to share how do you get around doing this?
I have an orders table in which I want to calculate the price and for more than I read about the hooks not too sure where to start from.

- do I just add the field to the table and head over to the hooks folder?
- or... is there something that I may need to do over at the table side of things before creating a hook...

At the moment I cannot see my "Price" field within the orders.php file.

Thanks,

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

Re: Problem with after insert hook

Post by grimblefritz » 2016-02-02 23:46

Add the field to the table and make it read-only (but don't make it required!)

In the hooks folder, find the 'tablename.php' file and edit it.

In that file, find the 'tablename_after_insert' function and add the php code to calculate and update your price field.

Find the 'tablename_after_update' function and add the same code (or call the 'tablename_after_insert' function and pass along the function arguments.

Or, what I normally do, is add a 'tablename_update_blah" function, and then call that from the other two (insert/update) functions.

The update function would be something like this:

Code: Select all

function tablename_update_fieldname($data) {
    $id = $data['id'];
    $price = $data['cost'] * $data['qty'];
    sql("update tablename set price='{$price}' where id={$id}", $eo);
}

proque_cu
Posts: 3
Joined: 2016-02-02 04:43

Re: Problem with after insert hook

Post by proque_cu » 2016-02-03 02:25

Awesome, thank you very much!!! :)

Post Reply