Using onAfterprocess to update a quantity from a related table

tnunaia

New Member
Please anyone who has a sample code for this need:

I got two related tables: inventory and inventory_movement.
A form of 'inventory_movement' is for the user to enter items to be either 'Out for use', 'Returned' and 'Non-returnable'.

I need to decrease the item 'quantity' from the main table 'inventory' once the table form of 'inventory_movement' is submitted only for 'Out for use' and 'Non-returnable' otherwise to increase.

I've tried with some sample PHP codes but still not working. Please if you could help me to solve the above to update 'quantity' of 'inventory' table using onAfterprocess.



Thanks a lot in advance.
 

Attachments

  • inventory and inventory_movement.jpg
    inventory and inventory_movement.jpg
    54.1 KB · Views: 61
Here's an example for increasing the inventory balance on item receival:
Code:
$mydb = JFactory::getDBO();

$inventory_id = '{inventory_movement___inventory_id_raw}'
$received_qty = '{inventory_movement___quantity}'

//get current amount from inventory table
$mydb->setQuery("SELECT quantity FROM inventory WHERE id = ".$mydb->Quote($inventory_id));
$cur_amount = $mydb->loadResult();

//calculate new amount
$new_amount = $cur_amount + $received_qty;

//update new amount to inventory table
$mydb->setQuery("UPDATE inventory SET quantity = ".$mydb->Quote($new_amount)." WHERE id = ".$mydb->Quote($inventory_id));
$mydb->execute();
You can modify it to decrease the quantity on the conditions you mentioned. This exact code haven't been tested, so you might have to make some corrections.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top