<?php
function Assets_CrearActualizarActivo($entity) {
	global $adb, $current_user;
	$data = $entity->data;
	
	//Obtenemos el id de la oportunidad 
	$idpotentials = explode("x",$data['cf_1098'])[1];

	//Obtenemos el los totales de los campos calculados 
	$totales_model = $adb->pquery("SELECT   SUM(cf.cf_1086) as 'totalprecio', 
											SUM(cf.cf_1092) as 'totalgasto', 
											SUM(cf.cf_1074) as 'totalvaf',
											SUM(cf.cf_1076) as 'totalcolneta' 
											
												FROM vtiger_assets a
								INNER JOIN vtiger_assetscf cf ON cf.assetsid = a.assetsid
								INNER JOIN vtiger_crmentity entity ON entity.crmid = a.assetsid AND entity.deleted = 0
								WHERE cf.cf_1150 = ?",array($idpotentials));

	//Creamos el array con los parametros de la oportunidad que actualizaremos o registraremos 

	$parameters = array(
					$adb->query_result($totales_model,0,'totalprecio'),
					$adb->query_result($totales_model,0,'totalgasto'),
					$adb->query_result($totales_model,0,'totalvaf'),
					$adb->query_result($totales_model,0,'totalcolneta'),
					$idpotentials
	);

	//Actualizamos los campos de la oportunidad 
	/*
		cf_1142 = total precio
		cf_1144 = total gasto
		cf_1146 = total VAF
		cf_1148 = total col neta
	*/
	$adb->pquery("UPDATE vtiger_potentialscf SET
											cf_1142 = ?, 
											cf_1144 = ?, 
											cf_1146 = ?, 
											cf_1148 = ? WHERE potentialid = ? ",$parameters);



	//var_dump($idpotentials);
	//var_dump($data);

	//die();
		
}