[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /* +******************************************************************************* 3 * The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 ******************************************************************************** */ 10 11 require_once 'include/Webservices/Retrieve.php'; 12 require_once 'include/Webservices/Create.php'; 13 require_once 'include/Webservices/Delete.php'; 14 require_once 'include/Webservices/DescribeObject.php'; 15 require_once 'includes/Loader.php'; 16 vimport ('includes.runtime.Globals'); 17 vimport ('includes.runtime.BaseModel'); 18 19 function vtws_convertlead($entityvalues, $user) { 20 21 global $adb, $log; 22 if (empty($entityvalues['assignedTo'])) { 23 $entityvalues['assignedTo'] = vtws_getWebserviceEntityId('Users', $user->id); 24 } 25 if (empty($entityvalues['transferRelatedRecordsTo'])) { 26 $entityvalues['transferRelatedRecordsTo'] = 'Contacts'; 27 } 28 29 30 $leadObject = VtigerWebserviceObject::fromName($adb, 'Leads'); 31 $handlerPath = $leadObject->getHandlerPath(); 32 $handlerClass = $leadObject->getHandlerClass(); 33 34 require_once $handlerPath; 35 36 $leadHandler = new $handlerClass($leadObject, $user, $adb, $log); 37 38 39 $leadInfo = vtws_retrieve($entityvalues['leadId'], $user); 40 $sql = "select converted from vtiger_leaddetails where converted = 1 and leadid=?"; 41 $leadIdComponents = vtws_getIdComponents($entityvalues['leadId']); 42 $result = $adb->pquery($sql, array($leadIdComponents[1])); 43 if ($result === false) { 44 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 45 vtws_getWebserviceTranslatedString('LBL_' . 46 WebServiceErrorCode::$DATABASEQUERYERROR)); 47 } 48 $rowCount = $adb->num_rows($result); 49 if ($rowCount > 0) { 50 throw new WebServiceException(WebServiceErrorCode::$LEAD_ALREADY_CONVERTED, 51 "Lead is already converted"); 52 } 53 54 $entityIds = array(); 55 56 $availableModules = array('Accounts', 'Contacts', 'Potentials'); 57 58 if (!(($entityvalues['entities']['Accounts']['create']) || ($entityvalues['entities']['Contacts']['create']))) { 59 return null; 60 } 61 62 foreach ($availableModules as $entityName) { 63 if ($entityvalues['entities'][$entityName]['create']) { 64 $entityvalue = $entityvalues['entities'][$entityName]; 65 $entityObject = VtigerWebserviceObject::fromName($adb, $entityvalue['name']); 66 $handlerPath = $entityObject->getHandlerPath(); 67 $handlerClass = $entityObject->getHandlerClass(); 68 69 require_once $handlerPath; 70 71 $entityHandler = new $handlerClass($entityObject, $user, $adb, $log); 72 73 $entityObjectValues = array(); 74 $entityObjectValues['assigned_user_id'] = $entityvalues['assignedTo']; 75 $entityObjectValues = vtws_populateConvertLeadEntities($entityvalue, $entityObjectValues, $entityHandler, $leadHandler, $leadInfo); 76 77 //update potential related to property 78 if ($entityvalue['name'] == 'Potentials') { 79 if (!empty($entityIds['Accounts'])) { 80 $entityObjectValues['related_to'] = $entityIds['Accounts']; 81 } 82 if (!empty($entityIds['Contacts'])) { 83 $entityObjectValues['contact_id'] = $entityIds['Contacts']; 84 } 85 } 86 87 //update the contacts relation 88 if ($entityvalue['name'] == 'Contacts') { 89 if (!empty($entityIds['Accounts'])) { 90 $entityObjectValues['account_id'] = $entityIds['Accounts']; 91 } 92 } 93 94 try { 95 $create = true; 96 if ($entityvalue['name'] == 'Accounts') { 97 $sql = "SELECT vtiger_account.accountid FROM vtiger_account,vtiger_crmentity WHERE vtiger_crmentity.crmid=vtiger_account.accountid AND vtiger_account.accountname=? AND vtiger_crmentity.deleted=0"; 98 $result = $adb->pquery($sql, array($entityvalue['accountname'])); 99 if ($adb->num_rows($result) > 0) { 100 $entityIds[$entityName] = vtws_getWebserviceEntityId('Accounts', $adb->query_result($result, 0, 'accountid')); 101 $create = false; 102 } 103 } 104 if ($create) { 105 $entityRecord = vtws_create($entityvalue['name'], $entityObjectValues, $user); 106 $entityIds[$entityName] = $entityRecord['id']; 107 } 108 } catch (Exception $e) { 109 throw new WebServiceException(WebServiceErrorCode::$UNKNOWNOPERATION, 110 $e->getMessage().' : '.$entityvalue['name']); 111 } 112 } 113 } 114 115 116 try { 117 $accountIdComponents = vtws_getIdComponents($entityIds['Accounts']); 118 $accountId = $accountIdComponents[1]; 119 120 $contactIdComponents = vtws_getIdComponents($entityIds['Contacts']); 121 $contactId = $contactIdComponents[1]; 122 123 if (!empty($accountId) && !empty($contactId) && !empty($entityIds['Potentials'])) { 124 $potentialIdComponents = vtws_getIdComponents($entityIds['Potentials']); 125 $potentialId = $potentialIdComponents[1]; 126 $sql = "insert into vtiger_contpotentialrel values(?,?)"; 127 $result = $adb->pquery($sql, array($contactId, $potentialIdComponents[1])); 128 if ($result === false) { 129 throw new WebServiceException(WebServiceErrorCode::$FAILED_TO_CREATE_RELATION, 130 "Failed to related Contact with the Potential"); 131 } 132 } 133 134 $transfered = vtws_convertLeadTransferHandler($leadIdComponents, $entityIds, $entityvalues); 135 136 $relatedIdComponents = vtws_getIdComponents($entityIds[$entityvalues['transferRelatedRecordsTo']]); 137 vtws_getRelatedActivities($leadIdComponents[1], $accountId, $contactId, $relatedIdComponents[1]); 138 vtws_updateConvertLeadStatus($entityIds, $entityvalues['leadId'], $user); 139 } catch (Exception $e) { 140 foreach ($entityIds as $entity => $id) { 141 vtws_delete($id, $user); 142 } 143 return null; 144 } 145 146 return $entityIds; 147 } 148 149 /* 150 * populate the entity fields with the lead info. 151 * if mandatory field is not provided populate with '????' 152 * returns the entity array. 153 */ 154 155 function vtws_populateConvertLeadEntities($entityvalue, $entity, $entityHandler, $leadHandler, $leadinfo) { 156 global $adb, $log; 157 $column; 158 $entityName = $entityvalue['name']; 159 $sql = "SELECT * FROM vtiger_convertleadmapping"; 160 $result = $adb->pquery($sql, array()); 161 if ($adb->num_rows($result)) { 162 switch ($entityName) { 163 case 'Accounts':$column = 'accountfid'; 164 break; 165 case 'Contacts':$column = 'contactfid'; 166 break; 167 case 'Potentials':$column = 'potentialfid'; 168 break; 169 default:$column = 'leadfid'; 170 break; 171 } 172 173 $leadFields = $leadHandler->getMeta()->getModuleFields(); 174 $entityFields = $entityHandler->getMeta()->getModuleFields(); 175 $row = $adb->fetch_array($result); 176 $count = 1; 177 do { 178 $entityField = vtws_getFieldfromFieldId($row[$column], $entityFields); 179 if ($entityField == null) { 180 //user doesn't have access so continue.TODO update even if user doesn't have access 181 continue; 182 } 183 $leadField = vtws_getFieldfromFieldId($row['leadfid'], $leadFields); 184 if ($leadField == null) { 185 //user doesn't have access so continue.TODO update even if user doesn't have access 186 continue; 187 } 188 $leadFieldName = $leadField->getFieldName(); 189 $entityFieldName = $entityField->getFieldName(); 190 $entity[$entityFieldName] = $leadinfo[$leadFieldName]; 191 $count++; 192 } while ($row = $adb->fetch_array($result)); 193 194 foreach ($entityvalue as $fieldname => $fieldvalue) { 195 if (!empty($fieldvalue)) { 196 $entity[$fieldname] = $fieldvalue; 197 } 198 } 199 200 $entity = vtws_validateConvertLeadEntityMandatoryValues($entity, $entityHandler, $leadinfo, $entityName); 201 } 202 return $entity; 203 } 204 205 function vtws_validateConvertLeadEntityMandatoryValues($entity, $entityHandler, $leadinfo, $module) { 206 207 $mandatoryFields = $entityHandler->getMeta()->getMandatoryFields(); 208 foreach ($mandatoryFields as $field) { 209 if (empty($entity[$field])) { 210 $fieldInfo = vtws_getConvertLeadFieldInfo($module, $field); 211 if (($fieldInfo['type']['name'] == 'picklist' || $fieldInfo['type']['name'] == 'multipicklist' 212 || $fieldInfo['type']['name'] == 'date' || $fieldInfo['type']['name'] == 'datetime') 213 && ($fieldInfo['editable'] == true)) { 214 $entity[$field] = $fieldInfo['default']; 215 } else { 216 $entity[$field] = '????'; 217 } 218 } 219 } 220 return $entity; 221 } 222 223 function vtws_getConvertLeadFieldInfo($module, $fieldname) { 224 global $adb, $log, $current_user; 225 $describe = vtws_describe($module, $current_user); 226 foreach ($describe['fields'] as $index => $fieldInfo) { 227 if ($fieldInfo['name'] == $fieldname) { 228 return $fieldInfo; 229 } 230 } 231 return false; 232 } 233 234 //function to handle the transferring of related records for lead 235 function vtws_convertLeadTransferHandler($leadIdComponents, $entityIds, $entityvalues) { 236 237 try { 238 $entityidComponents = vtws_getIdComponents($entityIds[$entityvalues['transferRelatedRecordsTo']]); 239 vtws_transferLeadRelatedRecords($leadIdComponents[1], $entityidComponents[1], $entityvalues['transferRelatedRecordsTo']); 240 } catch (Exception $e) { 241 return false; 242 } 243 244 return true; 245 } 246 247 function vtws_updateConvertLeadStatus($entityIds, $leadId, $user) { 248 global $adb, $log; 249 $leadIdComponents = vtws_getIdComponents($leadId); 250 if ($entityIds['Accounts'] != '' || $entityIds['Contacts'] != '') { 251 $sql = "UPDATE vtiger_leaddetails SET converted = 1 where leadid=?"; 252 $result = $adb->pquery($sql, array($leadIdComponents[1])); 253 if ($result === false) { 254 throw new WebServiceException(WebServiceErrorCode::$FAILED_TO_MARK_CONVERTED, 255 "Failed mark lead converted"); 256 } 257 //updating the campaign-lead relation --Minnie 258 $sql = "DELETE FROM vtiger_campaignleadrel WHERE leadid=?"; 259 $adb->pquery($sql, array($leadIdComponents[1])); 260 261 $sql = "DELETE FROM vtiger_tracker WHERE item_id=?"; 262 $adb->pquery($sql, array($leadIdComponents[1])); 263 264 //update the modifiedtime and modified by information for the record 265 $leadModifiedTime = $adb->formatDate(date('Y-m-d H:i:s'), true); 266 $crmentityUpdateSql = "UPDATE vtiger_crmentity SET modifiedtime=?, modifiedby=? WHERE crmid=?"; 267 $adb->pquery($crmentityUpdateSql, array($leadModifiedTime, $user->id, $leadIdComponents[1])); 268 } 269 $moduleArray = array('Accounts','Contacts','Potentials'); 270 271 foreach($moduleArray as $module){ 272 if(!empty($entityIds[$module])) { 273 $idComponents = vtws_getIdComponents($entityIds[$module]); 274 $id = $idComponents[1]; 275 $webserviceModule = vtws_getModuleHandlerFromName($module, $user); 276 $meta = $webserviceModule->getMeta(); 277 $fields = $meta->getModuleFields(); 278 $field = $fields['isconvertedfromlead']; 279 $tablename = $field->getTableName(); 280 $tableList = $meta->getEntityTableIndexList(); 281 $tableIndex = $tableList[$tablename]; 282 $adb->pquery("UPDATE $tablename SET isconvertedfromlead = ? WHERE $tableIndex = ?",array(1,$id)); 283 } 284 } 285 286 } 287 288 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |