public function process(Vtiger_Request $request)
 {
     $recordId = $request->get('record');
     $modules = $request->get('modules');
     $assignId = $request->get('assigned_user_id');
     $currentUser = Users_Record_Model::getCurrentUserModel();
     $entityValues = array();
     $entityValues['transferRelatedRecordsTo'] = $request->get('transferModule');
     $entityValues['assignedTo'] = vtws_getWebserviceEntityId(vtws_getOwnerType($assignId), $assignId);
     $entityValues['leadId'] = vtws_getWebserviceEntityId($request->getModule(), $recordId);
     $recordModel = Vtiger_Record_Model::getInstanceById($recordId, $request->getModule());
     $convertLeadFields = $recordModel->getConvertLeadFields();
     $availableModules = array('Accounts', 'Contacts', 'Potentials');
     foreach ($availableModules as $module) {
         if (vtlib_isModuleActive($module) && in_array($module, $modules)) {
             $entityValues['entities'][$module]['create'] = true;
             $entityValues['entities'][$module]['name'] = $module;
             $entityValues['entities'][$module]['create_always'] = Vtiger_Processes_Model::getConfig('marketing', 'conversion', 'create_always');
             foreach ($convertLeadFields[$module] as $fieldModel) {
                 $fieldName = $fieldModel->getName();
                 $fieldValue = $request->get($fieldName);
                 //Potential Amount Field value converting into DB format
                 if ($fieldModel->getFieldDataType() === 'currency') {
                     $fieldValue = Vtiger_Currency_UIType::convertToDBFormat($fieldValue);
                 } elseif ($fieldModel->getFieldDataType() === 'date') {
                     $fieldValue = DateTimeField::convertToDBFormat($fieldValue);
                 } elseif ($fieldModel->getFieldDataType() === 'reference' && $fieldValue) {
                     $ids = vtws_getIdComponents($fieldValue);
                     if (count($ids) === 1) {
                         $fieldValue = vtws_getWebserviceEntityId(getSalesEntityType($fieldValue), $fieldValue);
                     }
                 }
                 $entityValues['entities'][$module][$fieldName] = $fieldValue;
             }
         }
     }
     try {
         $result = vtws_convertlead($entityValues, $currentUser);
     } catch (Exception $e) {
         $this->showError($request, $e);
         exit;
     }
     if (!empty($result['Accounts'])) {
         $accountIdComponents = vtws_getIdComponents($result['Accounts']);
         $accountId = $accountIdComponents[1];
     }
     if (!empty($result['Contacts'])) {
         $contactIdComponents = vtws_getIdComponents($result['Contacts']);
         $contactId = $contactIdComponents[1];
     }
     if (!empty($accountId)) {
         ModTracker_Record_Model::addConvertToAccountRelation('Accounts', $accountId, $assignId);
         header("Location: index.php?view=Detail&module=Accounts&record={$accountId}");
     } elseif (!empty($contactId)) {
         header("Location: index.php?view=Detail&module=Contacts&record={$contactId}");
     } else {
         $this->showError($request);
         exit;
     }
 }
 function handleEvent($eventName, $entityData)
 {
     global $adb, $shared_owners;
     if ($eventName == 'vtiger.entity.aftersave.final' && $shared_owners == true) {
         $moduleName = $entityData->getModuleName();
         $recordId = $entityData->getId();
         $vtEntityDelta = new VTEntityDelta();
         $delta = $vtEntityDelta->getEntityDelta($moduleName, $recordId, true);
         $shownerids = $entityData->get('shownerid');
         $inheritsharing = $entityData->get('inheritsharing');
         if (array_key_exists("shownerid", $delta)) {
             $usersUpadated = TRUE;
             $oldValue = Vtiger_Functions::getArrayFromValue($delta['shownerid']['oldValue']);
             $currentValue = Vtiger_Functions::getArrayFromValue($delta['shownerid']['currentValue']);
             $addUsers = $currentValue;
             $removeUser = array_diff($oldValue, $currentValue);
         } else {
             $usersUpadated = FALSE;
         }
         $updateRelatedRecords = TRUE;
         if (array_key_exists("inheritsharing", $delta)) {
             $inheritsharing = $delta['inheritsharing']['currentValue'];
             $inheritsharingUpdated = TRUE;
             if ('0' === $delta['inheritsharing']['currentValue']) {
                 $updateRelatedRecords = FALSE;
                 $removedShared = TRUE;
             }
         } else {
             $inheritsharingUpdated = FALSE;
             $inheritsharing = $entityData->get('inheritsharing');
             if (!$inheritsharing) {
                 $updateRelatedRecords = FALSE;
             }
         }
         if ($usersUpadated || $inheritsharingUpdated) {
             if ($updateRelatedRecords) {
                 Users_Privileges_Model::setSharedOwnerRecursively($recordId, $addUsers, $removeUser, $moduleName);
             }
             if ($removedShared) {
                 Users_Privileges_Model::setSharedOwnerRecursively($recordId, [], $addUsers, $moduleName);
             }
         }
     }
     if ($eventName == 'vtiger.entity.link.after' && $shared_owners == true && Vtiger_Processes_Model::getConfig('sales', 'popup', 'update_shared_permissions') == 'true') {
         $destinationModule = array('Products', 'Services');
         if ($entityData['sourceModule'] == 'Potentials' && in_array($entityData['destinationModule'], $destinationModule)) {
             $adb = PearDatabase::getInstance();
             $result1 = $adb->pquery('SELECT smownerid, shownerid FROM vtiger_crmentity WHERE crmid = ?;', array($entityData['destinationRecordId']));
             $result2 = $adb->pquery('SELECT shownerid FROM vtiger_crmentity WHERE crmid = ?;', array($entityData['sourceRecordId']));
             if ($adb->num_rows($result1) == 1 && $adb->num_rows($result2) == 1) {
                 $smownerid = $adb->query_result($result1, 0, 'smownerid') . ',' . $adb->query_result($result1, 0, 'shownerid');
                 $shownerid = $adb->query_result($result2, 0, 'shownerid') . ',' . trim($smownerid, ',');
                 $adb->pquery("UPDATE vtiger_crmentity SET shownerid = ? WHERE crmid = ?;", [trim($shownerid, ','), $entityData['sourceRecordId']]);
             }
         }
     }
 }
 function handleEvent($eventName, $entityData)
 {
     if ($eventName == 'vtiger.entity.aftersave.final' && vglobal('shared_owners') == true) {
         $moduleName = $entityData->getModuleName();
         $recordId = $entityData->getId();
         $vtEntityDelta = new VTEntityDelta();
         $delta = $vtEntityDelta->getEntityDelta($moduleName, $recordId, true);
         if (array_key_exists('assigned_user_id', $delta)) {
             $usersUpadated = TRUE;
             $oldValue = Vtiger_Functions::getArrayFromValue($delta['assigned_user_id']['oldValue']);
             $currentValue = Vtiger_Functions::getArrayFromValue($delta['assigned_user_id']['currentValue']);
             $addUsers = $currentValue;
             $removeUser = array_diff($oldValue, $currentValue);
             Users_Privileges_Model::setSharedOwnerRecursively($recordId, $addUsers, $removeUser, $moduleName);
         }
     }
     if ($eventName == 'vtiger.entity.link.after' && vglobal('shared_owners') == true && Vtiger_Processes_Model::getConfig('sales', 'popup', 'update_shared_permissions') == 'true') {
         $destinationModule = ['Products', 'Services'];
         if ($entityData['sourceModule'] == 'Potentials' && in_array($entityData['destinationModule'], $destinationModule)) {
             $db = PearDatabase::getInstance();
             $sourceRecordId =& $entityData['sourceRecordId'];
             $destinationRecordId =& $entityData['destinationRecordId'];
             $recordMetaData = Vtiger_Functions::getCRMRecordMetadata($sourceRecordId);
             $shownerIds = Vtiger_SharedOwner_UIType::getSharedOwners($sourceRecordId, $entityData['sourceModule']);
             $shownerIds[] = $recordMetaData['smownerid'];
             $shownerIds = array_unique($shownerIds);
             $usersExist = [];
             $shownersTable = Vtiger_SharedOwner_UIType::getShownerTable($entityData['destinationModule']);
             $result = $db->pquery('SELECT crmid, userid FROM ' . $shownersTable . ' WHERE userid IN(' . implode(',', $shownerIds) . ') AND crmid = ?', [$destinationRecordId]);
             while ($row = $db->getRow($result)) {
                 $usersExist[$row['crmid']][$row['userid']] = true;
             }
             foreach ($shownerIds as $userId) {
                 if (!isset($usersExist[$destinationRecordId][$userId])) {
                     $db->insert($shownersTable, ['crmid' => $destinationRecordId, 'userid' => $userId]);
                 }
             }
         }
     }
 }
 public function process(Vtiger_Request $request)
 {
     $recordId = $request->get('record');
     $modules = $request->get('modules');
     $assignId = $request->get('assigned_user_id');
     $currentUser = Users_Record_Model::getCurrentUserModel();
     $entityValues = array();
     $entityValues['transferRelatedRecordsTo'] = $request->get('transferModule');
     $entityValues['assignedTo'] = vtws_getWebserviceEntityId(vtws_getOwnerType($assignId), $assignId);
     $entityValues['leadId'] = vtws_getWebserviceEntityId($request->getModule(), $recordId);
     $createAlways = Vtiger_Processes_Model::getConfig('marketing', 'conversion', 'create_always');
     $recordModel = Vtiger_Record_Model::getInstanceById($recordId, $request->getModule());
     $convertLeadFields = $recordModel->getConvertLeadFields();
     $availableModules = array('Accounts', 'Contacts', 'Potentials');
     foreach ($availableModules as $module) {
         if (vtlib_isModuleActive($module) && in_array($module, $modules)) {
             $entityValues['entities'][$module]['create'] = true;
             $entityValues['entities'][$module]['name'] = $module;
             foreach ($convertLeadFields[$module] as $fieldModel) {
                 $fieldName = $fieldModel->getName();
                 $fieldValue = $request->get($fieldName);
                 //Potential Amount Field value converting into DB format
                 if ($fieldModel->getFieldDataType() === 'currency') {
                     $fieldValue = Vtiger_Currency_UIType::convertToDBFormat($fieldValue);
                 } elseif ($fieldModel->getFieldDataType() === 'date') {
                     $fieldValue = DateTimeField::convertToDBFormat($fieldValue);
                 } elseif ($fieldModel->getFieldDataType() === 'reference' && $fieldValue) {
                     $ids = vtws_getIdComponents($fieldValue);
                     if (count($ids) === 1) {
                         $fieldValue = vtws_getWebserviceEntityId(getSalesEntityType($fieldValue), $fieldValue);
                     }
                 }
                 $entityValues['entities'][$module][$fieldName] = $fieldValue;
             }
         }
     }
     try {
         $results = true;
         if ($createAlways === true || $createAlways === 'true') {
             $leadModel = Vtiger_Module_Model::getCleanInstance($request->getModule());
             $results = $leadModel->searchAccountsToConvert($recordModel);
             $entityValues['entities']['Accounts']['convert_to_id'] = $results;
         }
         if (!$results) {
             $message = vtranslate('LBL_TOO_MANY_ACCOUNTS_TO_CONVERT', $request->getModule(), '');
             if ($currentUser->isAdminUser()) {
                 $message = vtranslate('LBL_TOO_MANY_ACCOUNTS_TO_CONVERT', $request->getModule(), '<a href="index.php?module=MarketingProcesses&view=Index&parent=Settings"><span class="glyphicon glyphicon-folder-open"></span></a>');
             }
             $this->showError($request, '', $message);
             exit;
         }
     } catch (Exception $e) {
         $this->showError($request, $e);
         exit;
     }
     try {
         $result = vtws_convertlead($entityValues, $currentUser);
     } catch (Exception $e) {
         $this->showError($request, $e);
         exit;
     }
     if (!empty($result['Accounts'])) {
         $accountIdComponents = vtws_getIdComponents($result['Accounts']);
         $accountId = $accountIdComponents[1];
     }
     if (!empty($result['Contacts'])) {
         $contactIdComponents = vtws_getIdComponents($result['Contacts']);
         $contactId = $contactIdComponents[1];
     }
     if (!empty($accountId)) {
         ModTracker_Record_Model::addConvertToAccountRelation('Accounts', $accountId, $assignId);
         header("Location: index.php?view=Detail&module=Accounts&record={$accountId}");
     } elseif (!empty($contactId)) {
         header("Location: index.php?view=Detail&module=Contacts&record={$contactId}");
     } else {
         $this->showError($request);
         exit;
     }
 }
Example #5
0
 public function searchAccountsToConvert($recordModel)
 {
     $log = vglobal('log');
     $log->debug('Start ' . __CLASS__ . ':' . __FUNCTION__);
     if ($recordModel) {
         $params = [];
         $db = PearDatabase::getInstance();
         $mappingFields = Vtiger_Processes_Model::getConfig('marketing', 'conversion', 'mapping');
         $mappingFields = Zend_Json::decode($mappingFields);
         $sql = "SELECT vtiger_account.accountid FROM vtiger_account " . "INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_account.accountid " . "INNER JOIN `vtiger_accountaddress` ON vtiger_accountaddress.accountaddressid=vtiger_account.accountid " . "INNER JOIN `vtiger_accountscf` ON vtiger_accountscf.accountid=vtiger_account.accountid " . "WHERE vtiger_crmentity.deleted=0";
         foreach ($mappingFields as $fields) {
             $sql .= ' AND `' . current($fields) . '` = ?';
             $params[] = $recordModel->get(key($fields));
         }
         $result = $db->pquery($sql, $params);
         $num = $db->num_rows($result);
         if ($num > 1) {
             $log->debug('End ' . __CLASS__ . ':' . __FUNCTION__);
             return false;
         } elseif ($num == 1) {
             $log->debug('End ' . __CLASS__ . ':' . __FUNCTION__);
             return (int) $db->query_result($result, 0, 'accountid');
         }
     }
     $log->debug('End ' . __CLASS__ . ':' . __FUNCTION__);
     return true;
 }