I have created a logic hook to calculate a comission when the quotes is saved. But I realized that when the record is created event if the hook run after_save there are not data in the quote line products, but if once the records is created and I edit it when I save the hook work well.
Below the code
logic_hook.php under quotes module
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'Commissions', 'custom/modules/AOS_Quotes/CommissionLoad.php','UpdateCommissionClass', 'UpdateCommissionFunction');
CommissionLoad.php
ini_set('display_errors', 1);
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class UpdateCommissionClass
{
static $already_ran = false;
function UpdateCommissionFunction($bean, $event, $arguments)
{
if(self::$already_ran == true) return;
self::$already_ran = true;
$GLOBALS['log']->fatal('UpdateCommisionEntry called for Loads: '.$bean->id);
$loadId = $bean->id;
$sqlf = "SELECT d.parent_id, d.product_id, d.product_list_price, d.deleted, c.fee_c FROM aos_products_quotes d JOIN aos_products_cstm c ON d.product_id = c.id_c and d.parent_id = '$loadId' and c.fee_c = '1' and d.deleted = 0";
$products = $bean->db->query($sqlf, true);
$suma_total = 0;
if ($products->num_rows <= 0)
{
echo 'No hay prodcutos';
}else{
$suma_total = 0;
while ($prd = $products->fetch_array(MYSQLI_BOTH))
{
$suma_total = $prd['product_list_price'] + $suma_total;
}
}
$driverId = $bean->assigned_user_id;
$sql = "SELECT commission_c FROM users_cstm WHERE id_c = '$driverId'";
$results = $bean->db->query($sql, true);
$row = $bean->db->fetchByAssoc($results);
$comision = $row['commission_c'];
$bean->fee_porcentage_c = $comision;
$bean->comission_c = $suma_total * $comision / 100;
// write in log for verification *** temporal
$GLOBALS['log']->fatal('UpdateCommisionEntry called for Loads: '.$bean->comission_c);
$GLOBALS['log']->fatal('UpdateCommisionEntry Valor Loads: '.$suma_total);
$GLOBALS['log']->fatal('UpdateCommisionEntry Comission dvr: '.$comision);
}
}
?>
LOG when teh record is new
Tue Aug 2 16:09:31 2022 [3143519][1][FATAL] UpdateCommisionEntry called for Loads: e3a94152-e429-d20d-0eff-62e94ccc93b6
Tue Aug 2 16:09:31 2022 [3143519][1][FATAL] UpdateCommisionEntry called for Loads: 0
Tue Aug 2 16:09:31 2022 [3143519][1][FATAL] UpdateCommisionEntry Valor Loads: 0
Tue Aug 2 16:09:31 2022 [3143519][1][FATAL] UpdateCommisionEntry Comission dvr: 5.00
log when the recors is created and then updated
Tue Aug 2 16:10:54 2022 [3145117][1][FATAL] UpdateCommisionEntry called for Loads: e3a94152-e429-d20d-0eff-62e94ccc93b6
Tue Aug 2 16:10:54 2022 [3145117][1][FATAL] UpdateCommisionEntry called for Loads: 29
Tue Aug 2 16:10:54 2022 [3145117][1][FATAL] UpdateCommisionEntry Valor Loads: 580
Tue Aug 2 16:10:54 2022 [3145117][1][FATAL] UpdateCommisionEntry Comission dvr: 5.00
at this time the valor is updated.