Hi,
SuiteCRM 7.10.10
I am trying to make simple inventory on custom field ‘Availability’ added to aos_products,
Logics:
- one creates a quote and adds a line item to the quote, so the quantity of the line item is subtracted from the available amount of aos_product correspondingly – done.
- one edits the quote and changes (or not) the quantity of the item, so the available amount of aos_product is changed (or not) correspondingly – done.
- one deletes the line item on the quote (or delete the quote), so the quantity of the deleted line item is added to the available amount of aos_product correspondingly – NOT done.
The first and second logics are made by the logic hooks of the AOS_Products_Quotes
logic_hooks.php
<?php
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['before_save'] = Array ();
$hook_array['before_save'][] = Array (1, 'AOS_Products_Quotes_Hook_BS', 'custom/modules/AOS_Products_Quotes/AOS_Products_Quotes_Hook.php', 'AOS_Products_Quotes_Hook', 'beforeSafe');
?>
AOS_Products_Quotes_Hook.php:
<?php
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
class AOS_Products_Quotes_Hook {
function beforeSafe($bean, $event, $arguments) {
$old_qty = $bean->fetched_row['product_qty'];
if(!empty($old_qty)) {
$quantity = $bean->product_qty - $old_qty;
} else {
$quantity = $bean->product_qty;
}
if ($bean->load_relationship('aos_products')) {
$relatedProduct = $bean->aos_products->getBeans();
$parentProduct = false;
if (!empty($relatedProduct)) {
reset($relatedProduct);
$parentProduct = current($relatedProduct);
}
if(($parentProduct->availability_c - $quantity) > -1) {
$parentProduct->availability_c -= $quantity;
$parentProduct->save();
} else {
$bean->product_qty = 0;
return null;
}
}
}
}
?>
I tried to get the third logic done with before_save hook, but without success.
Code added to logic_hooks.php
$hook_array['before_delete'] = Array ();
$hook_array['before_delete'][] = Array (1, 'AOS_Products_Quotes_Hook_BD', 'custom/modules/AOS_Products_Quotes/AOS_Products_Quotes_Hook.php', 'AOS_Products_Quotes_Hook', 'beforeDelete'
Code I have tried to add to AOS_Products_Quotes_Hook.php as now:
function beforeDelete($bean, $event, $arguments) {
if(!empty($bean->fetched_row)) {
if ($bean->load_relationship('aos_products')) {
$relatedProduct = $bean->aos_products->getBeans();
$parentProduct = false;
if (!empty($relatedProduct)) {
reset($relatedProduct);
$parentProduct = current($relatedProduct);
}
if($parentProduct->availability_c += $bean->fetched_row['product_qty']) {
$parentProduct->save();
}
}
}
}
or
function beforeDelete($bean, $event, $arguments) {
if(!empty($bean)) {
if ($bean->load_relationship('aos_products')) {
$relatedProduct = $bean->aos_products->getBeans();
$parentProduct = false;
if (!empty($relatedProduct)) {
reset($relatedProduct);
$parentProduct = current($relatedProduct);
}
if($parentProduct->availability_c += $bean->product_qty) {
$parentProduct->save();
}
}
}
}
When checking before_save hook, the $bean as well as $bean->fetched_row are set (isset() function) and not empty (!empty()), but the fields within $bean or $bean->fetched_row are empty. So, logic_hook is not able to add deleted line item quantity ($bean->product_qty or $bean->fetched_row[‘product_quantity’]) to the available amount of aos_product correspondingly.
Any advice and suggestions will be greatly appreciated.