<?php
function Dealer_crearContacto($entity) {
	global $adb, $current_user;
	$data = $entity->data;
	$id = explode("x", $data['id'])[1];
	$assigned_user_id = explode("x", $data['assigned_user_id'])[1];
	$source = $data['source'];
	if ( $source == "CRM" ) {
		$source = "AUTOCREADO";
	}
	
	//var_dump($id);die();
	//$idEquipo = explode("x",$data['asset_no'])[1];
	$query = $adb-> pquery("SELECT dealer_tks_nombre, dealer_tks_apellidos FROM vtiger_dealer WHERE dealerid = ? ",array($id));

	$NombreContacto = $adb->query_result($query,0,'dealer_tks_nombre');
	$ApellidoContacto  = $adb->query_result($query,0,'dealer_tks_apellidos');
	$Movil  = 'POR COMPLETAR';
	$Correo  = 'POR COMPLETAR';
	
	$model = Vtiger_Record_Model::getCleanInstance('Dealercontact');
	$model->set('mode', '');
	$model->set('dealercontact_tks_nombre', $NombreContacto);
	$model->set('dealercontact_tks_apellidos', $ApellidoContacto);
	$model->set('dealercontact_tks_movil', $Movil );
	$model->set('dealercontact_tks_correo', $Correo );
	$model->set('assigned_user_id', $assigned_user_id);
	$model->set('cuenta', $id);
	$model->set('source', $source);
	
	try {
		$model->save();
	} catch (Exception $e) {
		print_r($e->getMessage()); die;
	}
		
}