Simple inventory on aos_products related to aos_quotes and aos_products_quotes

Hi,

SuiteCRM 7.10.10

I am trying to make simple inventory on custom field ‘Availability’ added to aos_products,
Logics:

  1. 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.
  2. 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.
  3. 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.

Some addition.
If you delete whole quote, the method beforeDelete works fine with $bean->product_qty, but still if you want to delete the line item during quote edition, it does not work.

I figured it out.

Instead of before_delete hook, I applied before_relationship_delete.
logic_hooks.php and AOS_Products_Quotes_Hook.php are looked now as followed:
----logic_hooks.php—


<?php
$hook_version = 1;
$hook_array = Array();
$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', 'beforeSave');
$hook_array['before_relationship_delete'] = Array ();
$hook_array['before_relationship_delete'][] = Array (2, 'AOS_Products_Quotes_Hook_BRD', 'custom/modules/AOS_Products_Quotes/AOS_Products_Quotes_Hook.php', 'AOS_Products_Quotes_Hook', 'beforeRelationshipDelete');
?>

—AOS_Products_Quotes_Hook.php—


<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class AOS_Products_Quotes_Hook {
	function beforeSave($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;
			}
		}
	}	
	
	function beforeRelationshipDelete ($bean, $event, $arguments) {
		if(!empty($bean)) {
				if(isset($arguments['related_bean']->lineItems) && $arguments['related_bean']->lineItems > 0) {			
					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();					
						}
					}			
				}
		} 
		
	}
	
}

?>