Exemplo n.º 1
0
    /**
     * Function perform two task.
     * 1. Merge two duplicate contacts cases - follow CRM-5758 rules.
     * 2. Merge two cases of same contact - follow CRM-5598 rules.
     *
     * @param int $mainContactId contact id of main contact record.
     * @param int $mainCaseId case id of main case record.
     * @param int $otherContactId contact id of record which is going to merge.
     * @param int $otherCaseId case id of record which is going to merge.
     *
     * @param bool $changeClient
     *
     * @return integer|NULL
     * @static
     */
    static function mergeCases($mainContactId, $mainCaseId = NULL, $otherContactId = NULL, $otherCaseId = NULL, $changeClient = FALSE)
    {
        $moveToTrash = TRUE;
        $duplicateContacts = FALSE;
        if ($mainContactId && $otherContactId && $mainContactId != $otherContactId) {
            $duplicateContacts = TRUE;
        }
        $duplicateCases = FALSE;
        if ($mainCaseId && $otherCaseId && $mainCaseId != $otherCaseId) {
            $duplicateCases = TRUE;
        }
        $mainCaseIds = array();
        if (!$duplicateContacts && !$duplicateCases) {
            return $mainCaseIds;
        }
        $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
        $activityStatuses = CRM_Core_PseudoConstant::activityStatus('name');
        $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
        $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
        $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
        $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
        $processCaseIds = array($otherCaseId);
        if ($duplicateContacts && !$duplicateCases) {
            if ($changeClient) {
                $processCaseIds = array($mainCaseId);
            } else {
                //get all case ids for other contact.
                $processCaseIds = self::retrieveCaseIdsByContactId($otherContactId, TRUE);
            }
            if (!is_array($processCaseIds)) {
                return;
            }
        }
        $session = CRM_Core_Session::singleton();
        $currentUserId = $session->get('userID');
        CRM_Utils_Hook::pre_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient);
        // copy all cases and connect to main contact id.
        foreach ($processCaseIds as $otherCaseId) {
            if ($duplicateContacts) {
                $mainCase = CRM_Core_DAO::copyGeneric('CRM_Case_DAO_Case', array('id' => $otherCaseId));
                $mainCaseId = $mainCase->id;
                if (!$mainCaseId) {
                    continue;
                }
                // CRM-11662 Copy Case custom data
                $extends = array('case');
                $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
                if ($groupTree) {
                    foreach ($groupTree as $groupID => $group) {
                        $table[$groupTree[$groupID]['table_name']] = array('entity_id');
                        foreach ($group['fields'] as $fieldID => $field) {
                            $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name'];
                        }
                    }
                    foreach ($table as $tableName => $tableColumns) {
                        $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') ';
                        $tableColumns[0] = $mainCaseId;
                        $select = 'SELECT ' . implode(', ', $tableColumns);
                        $from = ' FROM ' . $tableName;
                        $where = " WHERE {$tableName}.entity_id = {$otherCaseId}";
                        $query = $insert . $select . $from . $where;
                        $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
                    }
                }
                $mainCase->free();
                $mainCaseIds[] = $mainCaseId;
                //insert record for case contact.
                $otherCaseContact = new CRM_Case_DAO_CaseContact();
                $otherCaseContact->case_id = $otherCaseId;
                $otherCaseContact->find();
                while ($otherCaseContact->fetch()) {
                    $mainCaseContact = new CRM_Case_DAO_CaseContact();
                    $mainCaseContact->case_id = $mainCaseId;
                    $mainCaseContact->contact_id = $otherCaseContact->contact_id;
                    if ($mainCaseContact->contact_id == $otherContactId) {
                        $mainCaseContact->contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainCaseContact->find(TRUE)) {
                        $mainCaseContact->save();
                    }
                    $mainCaseContact->free();
                }
                $otherCaseContact->free();
            } elseif (!$otherContactId) {
                $otherContactId = $mainContactId;
            }
            if (!$mainCaseId || !$otherCaseId || !$mainContactId || !$otherContactId) {
                continue;
            }
            // get all activities for other case.
            $otherCaseActivities = array();
            CRM_Core_DAO::commonRetrieveAll('CRM_Case_DAO_CaseActivity', 'case_id', $otherCaseId, $otherCaseActivities);
            //for duplicate cases do not process singleton activities.
            $otherActivityIds = $singletonActivityIds = array();
            foreach ($otherCaseActivities as $caseActivityId => $otherIds) {
                $otherActId = CRM_Utils_Array::value('activity_id', $otherIds);
                if (!$otherActId || in_array($otherActId, $otherActivityIds)) {
                    continue;
                }
                $otherActivityIds[] = $otherActId;
            }
            if ($duplicateCases) {
                if ($openCaseType = array_search('Open Case', $activityTypes)) {
                    $sql = "\nSELECT  id\n  FROM  civicrm_activity\n WHERE  activity_type_id = {$openCaseType}\n   AND  id IN ( " . implode(',', array_values($otherActivityIds)) . ');';
                    $dao = CRM_Core_DAO::executeQuery($sql);
                    while ($dao->fetch()) {
                        $singletonActivityIds[] = $dao->id;
                    }
                    $dao->free();
                }
            }
            // migrate all activities and connect to main contact.
            $copiedActivityIds = $activityMappingIds = array();
            sort($otherActivityIds);
            foreach ($otherActivityIds as $otherActivityId) {
                //for duplicate cases -
                //do not migrate singleton activities.
                if (!$otherActivityId || in_array($otherActivityId, $singletonActivityIds)) {
                    continue;
                }
                //migrate activity record.
                $otherActivity = new CRM_Activity_DAO_Activity();
                $otherActivity->id = $otherActivityId;
                if (!$otherActivity->find(TRUE)) {
                    continue;
                }
                $mainActVals = array();
                $mainActivity = new CRM_Activity_DAO_Activity();
                CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
                $mainActivity->copyValues($mainActVals);
                $mainActivity->id = NULL;
                $mainActivity->activity_date_time = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
                $mainActivity->source_record_id = CRM_Utils_Array::value($mainActivity->source_record_id, $activityMappingIds);
                $mainActivity->original_id = CRM_Utils_Array::value($mainActivity->original_id, $activityMappingIds);
                $mainActivity->parent_id = CRM_Utils_Array::value($mainActivity->parent_id, $activityMappingIds);
                $mainActivity->save();
                $mainActivityId = $mainActivity->id;
                if (!$mainActivityId) {
                    continue;
                }
                $activityMappingIds[$otherActivityId] = $mainActivityId;
                // insert log of all activities
                CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
                $otherActivity->free();
                $mainActivity->free();
                $copiedActivityIds[] = $otherActivityId;
                //create case activity record.
                $mainCaseActivity = new CRM_Case_DAO_CaseActivity();
                $mainCaseActivity->case_id = $mainCaseId;
                $mainCaseActivity->activity_id = $mainActivityId;
                $mainCaseActivity->save();
                $mainCaseActivity->free();
                //migrate source activity.
                $otherSourceActivity = new CRM_Activity_DAO_ActivityContact();
                $otherSourceActivity->activity_id = $otherActivityId;
                $otherSourceActivity->record_type_id = $sourceID;
                $otherSourceActivity->find();
                while ($otherSourceActivity->fetch()) {
                    $mainActivitySource = new CRM_Activity_DAO_ActivityContact();
                    $mainActivitySource->record_type_id = $sourceID;
                    $mainActivitySource->activity_id = $mainActivityId;
                    $mainActivitySource->contact_id = $otherSourceActivity->contact_id;
                    if ($mainActivitySource->contact_id == $otherContactId) {
                        $mainActivitySource->contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainActivitySource->find(TRUE)) {
                        $mainActivitySource->save();
                    }
                    $mainActivitySource->free();
                }
                $otherSourceActivity->free();
                //migrate target activities.
                $otherTargetActivity = new CRM_Activity_DAO_ActivityContact();
                $otherTargetActivity->activity_id = $otherActivityId;
                $otherTargetActivity->record_type_id = $targetID;
                $otherTargetActivity->find();
                while ($otherTargetActivity->fetch()) {
                    $mainActivityTarget = new CRM_Activity_DAO_ActivityContact();
                    $mainActivityTarget->record_type_id = $targetID;
                    $mainActivityTarget->activity_id = $mainActivityId;
                    $mainActivityTarget->contact_id = $otherTargetActivity->contact_id;
                    if ($mainActivityTarget->contact_id == $otherContactId) {
                        $mainActivityTarget->contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainActivityTarget->find(TRUE)) {
                        $mainActivityTarget->save();
                    }
                    $mainActivityTarget->free();
                }
                $otherTargetActivity->free();
                //migrate assignee activities.
                $otherAssigneeActivity = new CRM_Activity_DAO_ActivityContact();
                $otherAssigneeActivity->activity_id = $otherActivityId;
                $otherAssigneeActivity->record_type_id = $assigneeID;
                $otherAssigneeActivity->find();
                while ($otherAssigneeActivity->fetch()) {
                    $mainAssigneeActivity = new CRM_Activity_DAO_ActivityContact();
                    $mainAssigneeActivity->activity_id = $mainActivityId;
                    $mainAssigneeActivity->record_type_id = $assigneeID;
                    $mainAssigneeActivity->contact_id = $otherAssigneeActivity->contact_id;
                    if ($mainAssigneeActivity->contact_id == $otherContactId) {
                        $mainAssigneeActivity->contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainAssigneeActivity->find(TRUE)) {
                        $mainAssigneeActivity->save();
                    }
                    $mainAssigneeActivity->free();
                }
                $otherAssigneeActivity->free();
                // copy custom fields and attachments
                $aparams = array('activityID' => $otherActivityId, 'mainActivityId' => $mainActivityId);
                CRM_Activity_BAO_Activity::copyExtendedActivityData($aparams);
            }
            //copy case relationship.
            if ($duplicateContacts) {
                //migrate relationship records.
                $otherRelationship = new CRM_Contact_DAO_Relationship();
                $otherRelationship->case_id = $otherCaseId;
                $otherRelationship->find();
                $otherRelationshipIds = array();
                while ($otherRelationship->fetch()) {
                    $otherRelVals = array();
                    $updateOtherRel = FALSE;
                    CRM_Core_DAO::storeValues($otherRelationship, $otherRelVals);
                    $mainRelationship = new CRM_Contact_DAO_Relationship();
                    $mainRelationship->copyValues($otherRelVals);
                    $mainRelationship->id = NULL;
                    $mainRelationship->case_id = $mainCaseId;
                    if ($mainRelationship->contact_id_a == $otherContactId) {
                        $updateOtherRel = TRUE;
                        $mainRelationship->contact_id_a = $mainContactId;
                    }
                    //case creator change only when we merge user contact.
                    if ($mainRelationship->contact_id_b == $otherContactId) {
                        //do not change creator for change client.
                        if (!$changeClient) {
                            $updateOtherRel = TRUE;
                            $mainRelationship->contact_id_b = $currentUserId ? $currentUserId : $mainContactId;
                        }
                    }
                    $mainRelationship->end_date = CRM_Utils_Date::isoToMysql($otherRelationship->end_date);
                    $mainRelationship->start_date = CRM_Utils_Date::isoToMysql($otherRelationship->start_date);
                    //avoid duplicate object.
                    if (!$mainRelationship->find(TRUE)) {
                        $mainRelationship->save();
                    }
                    $mainRelationship->free();
                    //get the other relationship ids to update end date.
                    if ($updateOtherRel) {
                        $otherRelationshipIds[$otherRelationship->id] = $otherRelationship->id;
                    }
                }
                $otherRelationship->free();
                //update other relationships end dates
                if (!empty($otherRelationshipIds)) {
                    $sql = 'UPDATE  civicrm_relationship
                               SET  end_date = CURDATE()
                             WHERE  id IN ( ' . implode(',', $otherRelationshipIds) . ')';
                    CRM_Core_DAO::executeQuery($sql);
                }
            }
            //move other case to trash.
            $mergeCase = self::deleteCase($otherCaseId, $moveToTrash);
            if (!$mergeCase) {
                continue;
            }
            $mergeActSubject = $mergeActSubjectDetails = $mergeActType = '';
            if ($changeClient) {
                $mainContactDisplayName = CRM_Contact_BAO_Contact::displayName($mainContactId);
                $otherContactDisplayName = CRM_Contact_BAO_Contact::displayName($otherContactId);
                $mergeActType = array_search('Reassigned Case', $activityTypes);
                $mergeActSubject = ts("Case %1 reassigned client from %2 to %3. New Case ID is %4.", array(1 => $otherCaseId, 2 => $otherContactDisplayName, 3 => $mainContactDisplayName, 4 => $mainCaseId));
            } elseif ($duplicateContacts) {
                $mergeActType = array_search('Merge Case', $activityTypes);
                $mergeActSubject = ts("Case %1 copied from contact id %2 to contact id %3 via merge. New Case ID is %4.", array(1 => $otherCaseId, 2 => $otherContactId, 3 => $mainContactId, 4 => $mainCaseId));
            } else {
                $mergeActType = array_search('Merge Case', $activityTypes);
                $mergeActSubject = ts("Case %1 merged into case %2", array(1 => $otherCaseId, 2 => $mainCaseId));
                if (!empty($copiedActivityIds)) {
                    $sql = '
SELECT id, subject, activity_date_time, activity_type_id
FROM civicrm_activity
WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
                    $dao = CRM_Core_DAO::executeQuery($sql);
                    while ($dao->fetch()) {
                        $mergeActSubjectDetails .= "{$dao->activity_date_time} :: {$activityTypes[$dao->activity_type_id]}";
                        if ($dao->subject) {
                            $mergeActSubjectDetails .= " :: {$dao->subject}";
                        }
                        $mergeActSubjectDetails .= "<br />";
                    }
                }
            }
            //create merge activity record.
            $activityParams = array('subject' => $mergeActSubject, 'details' => $mergeActSubjectDetails, 'status_id' => array_search('Completed', $activityStatuses), 'activity_type_id' => $mergeActType, 'source_contact_id' => $mainContactId, 'activity_date_time' => date('YmdHis'));
            $mergeActivity = CRM_Activity_BAO_Activity::create($activityParams);
            $mergeActivityId = $mergeActivity->id;
            if (!$mergeActivityId) {
                continue;
            }
            $mergeActivity->free();
            //connect merge activity to case.
            $mergeCaseAct = array('case_id' => $mainCaseId, 'activity_id' => $mergeActivityId);
            self::processCaseActivity($mergeCaseAct);
        }
        CRM_Utils_Hook::post_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient);
        return $mainCaseIds;
    }
Exemplo n.º 2
0
 static function convertToCaseActivity()
 {
     $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
     foreach ($params as $param) {
         ${$param} = CRM_Utils_Array::value($param, $_POST);
     }
     if (!$activityID || !$caseID) {
         echo json_encode(array('error_msg' => 'required params missing.'));
         CRM_Utils_System::civiExit();
     }
     require_once "CRM/Activity/DAO/Activity.php";
     $otherActivity = new CRM_Activity_DAO_Activity();
     $otherActivity->id = $activityID;
     if (!$otherActivity->find(true)) {
         echo json_encode(array('error_msg' => 'activity record is missing.'));
         CRM_Utils_System::civiExit();
     }
     $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
     //create new activity record.
     $mainActivity = new CRM_Activity_DAO_Activity();
     $mainActVals = array();
     CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
     //get new activity subject.
     if (!empty($newSubject)) {
         $mainActVals['subject'] = $newSubject;
     }
     $mainActivity->copyValues($mainActVals);
     $mainActivity->id = null;
     $mainActivity->activity_date_time = $actDateTime;
     //make sure this is current revision.
     $mainActivity->is_current_revision = true;
     //drop all relations.
     $mainActivity->parent_id = $mainActivity->original_id = null;
     $mainActivity->save();
     $mainActivityId = $mainActivity->id;
     require_once 'CRM/Activity/BAO/Activity.php';
     CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
     $mainActivity->free();
     //mark previous activity as deleted.
     if (in_array($mode, array('move', 'file'))) {
         $otherActivity->activity_date_time = $actDateTime;
         $otherActivity->is_deleted = 1;
         $otherActivity->save();
     }
     $otherActivity->free();
     require_once "CRM/Activity/BAO/Activity.php";
     $targetContacts = array();
     if (!empty($targetContactIds)) {
         $targetContacts = array_unique(explode(',', $targetContactIds));
     }
     foreach ($targetContacts as $key => $value) {
         $params = array('activity_id' => $mainActivityId, 'target_contact_id' => $value);
         CRM_Activity_BAO_Activity::createActivityTarget($params);
     }
     //attach newly created activity to case.
     require_once "CRM/Case/DAO/CaseActivity.php";
     $caseActivity = new CRM_Case_DAO_CaseActivity();
     $caseActivity->case_id = $caseID;
     $caseActivity->activity_id = $mainActivityId;
     $caseActivity->save();
     $error_msg = $caseActivity->_lastError;
     $caseActivity->free();
     echo json_encode(array('error_msg' => $error_msg));
     CRM_Utils_System::civiExit();
 }
Exemplo n.º 3
0
 /**
  * @param array $params
  *
  * @return array
  */
 public static function _convertToCaseActivity($params)
 {
     if (!$params['activityID'] || !$params['caseID']) {
         return array('error_msg' => 'required params missing.');
     }
     $otherActivity = new CRM_Activity_DAO_Activity();
     $otherActivity->id = $params['activityID'];
     if (!$otherActivity->find(TRUE)) {
         return array('error_msg' => 'activity record is missing.');
     }
     $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
     // Create new activity record.
     $mainActivity = new CRM_Activity_DAO_Activity();
     $mainActVals = array();
     CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
     // Get new activity subject.
     if (!empty($params['newSubject'])) {
         $mainActVals['subject'] = $params['newSubject'];
     }
     $mainActivity->copyValues($mainActVals);
     $mainActivity->id = NULL;
     $mainActivity->activity_date_time = $actDateTime;
     // Make sure this is current revision.
     $mainActivity->is_current_revision = TRUE;
     // Drop all relations.
     $mainActivity->parent_id = $mainActivity->original_id = NULL;
     $mainActivity->save();
     $mainActivityId = $mainActivity->id;
     CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
     $mainActivity->free();
     // Mark previous activity as deleted. If it was a non-case activity
     // then just change the subject.
     if (in_array($params['mode'], array('move', 'file'))) {
         $caseActivity = new CRM_Case_DAO_CaseActivity();
         $caseActivity->case_id = $params['caseID'];
         $caseActivity->activity_id = $otherActivity->id;
         if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) {
             $otherActivity->is_deleted = 1;
         } else {
             $otherActivity->subject = ts('(Filed on case %1)', array(1 => $params['caseID'])) . ' ' . $otherActivity->subject;
         }
         $otherActivity->activity_date_time = $actDateTime;
         $otherActivity->save();
         $caseActivity->free();
     }
     $otherActivity->free();
     $targetContacts = array();
     if (!empty($params['targetContactIds'])) {
         $targetContacts = array_unique(explode(',', $params['targetContactIds']));
     }
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
     $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
     $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']);
     $src_params = array('activity_id' => $mainActivityId, 'contact_id' => $sourceContactID, 'record_type_id' => $sourceID);
     CRM_Activity_BAO_ActivityContact::create($src_params);
     foreach ($targetContacts as $key => $value) {
         $targ_params = array('activity_id' => $mainActivityId, 'contact_id' => $value, 'record_type_id' => $targetID);
         CRM_Activity_BAO_ActivityContact::create($targ_params);
     }
     // typically this will be empty, since assignees on another case may be completely different
     $assigneeContacts = array();
     if (!empty($params['assigneeContactIds'])) {
         $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
     }
     foreach ($assigneeContacts as $key => $value) {
         $assigneeParams = array('activity_id' => $mainActivityId, 'contact_id' => $value, 'record_type_id' => $assigneeID);
         CRM_Activity_BAO_ActivityContact::create($assigneeParams);
     }
     // Attach newly created activity to case.
     $caseActivity = new CRM_Case_DAO_CaseActivity();
     $caseActivity->case_id = $params['caseID'];
     $caseActivity->activity_id = $mainActivityId;
     $caseActivity->save();
     $error_msg = $caseActivity->_lastError;
     $caseActivity->free();
     $params['mainActivityId'] = $mainActivityId;
     CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
     return array('error_msg' => $error_msg, 'newId' => $mainActivity->id);
 }
Exemplo n.º 4
0
    /**
     * Function perform two task.
     * 1. Merge two duplicate contacts cases - follow CRM-5758 rules.
     * 2. Merge two cases of same contact - follow CRM-5598 rules.
     *
     * @param int $mainContactId    contact id of main contact record.
     * @param int $mainCaseId       case id of main case record.
     * @param int $otherContactId   contact id of record which is going to merge.
     * @param int $otherCaseId      case id of record which is going to merge.
     *
     * @return void.
     * @static
     */
    function mergeCases($mainContactId, $mainCaseId = null, $otherContactId = null, $otherCaseId = null, $changeClient = false)
    {
        $moveToTrash = true;
        $duplicateContacts = false;
        if ($mainContactId && $otherContactId && $mainContactId != $otherContactId) {
            $duplicateContacts = true;
        }
        $duplicateCases = false;
        if ($mainCaseId && $otherCaseId && $mainCaseId != $otherCaseId) {
            $duplicateCases = true;
        }
        $mainCaseIds = array();
        if (!$duplicateContacts && !$duplicateCases) {
            return $mainCaseIds;
        }
        require_once 'CRM/Core/PseudoConstant.php';
        $activityTypes = CRM_Core_PseudoConstant::activityType(true, true, false, 'name');
        $activityStatuses = CRM_Core_PseudoConstant::activityStatus('name');
        $processCaseIds = array($otherCaseId);
        if ($duplicateContacts && !$duplicateCases) {
            if ($changeClient) {
                $processCaseIds = array($mainCaseId);
            } else {
                //get all case ids for other contact.
                $processCaseIds = self::retrieveCaseIdsByContactId($otherContactId, true);
            }
            if (!is_array($processCaseIds)) {
                return;
            }
        }
        require_once 'CRM/Case/DAO/CaseContact.php';
        require_once 'CRM/Activity/DAO/Activity.php';
        require_once 'CRM/Case/DAO/CaseActivity.php';
        require_once 'CRM/Contact/DAO/Relationship.php';
        require_once 'CRM/Activity/DAO/ActivityTarget.php';
        require_once 'CRM/Activity/DAO/ActivityAssignment.php';
        require_once 'CRM/Activity/BAO/Activity.php';
        $session = CRM_Core_Session::singleton();
        $currentUserId = $session->get('userID');
        // copy all cases and connect to main contact id.
        foreach ($processCaseIds as $otherCaseId) {
            if ($duplicateContacts) {
                $mainCase = CRM_Core_DAO::copyGeneric('CRM_Case_DAO_Case', array('id' => $otherCaseId));
                $mainCaseId = $mainCase->id;
                if (!$mainCaseId) {
                    continue;
                }
                $mainCase->free();
                $mainCaseIds[] = $mainCaseId;
                //insert record for case contact.
                $otherCaseContact = new CRM_Case_DAO_CaseContact();
                $otherCaseContact->case_id = $otherCaseId;
                $otherCaseContact->find();
                while ($otherCaseContact->fetch()) {
                    $mainCaseContact = new CRM_Case_DAO_CaseContact();
                    $mainCaseContact->case_id = $mainCaseId;
                    $mainCaseContact->contact_id = $otherCaseContact->contact_id;
                    if ($mainCaseContact->contact_id == $otherContactId) {
                        $mainCaseContact->contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainCaseContact->find(true)) {
                        $mainCaseContact->save();
                    }
                    $mainCaseContact->free();
                }
                $otherCaseContact->free();
            } else {
                if (!$otherContactId) {
                    $otherContactId = $mainContactId;
                }
            }
            if (!$mainCaseId || !$otherCaseId || !$mainContactId || !$otherContactId) {
                continue;
            }
            // get all activities for other case.
            $otherCaseActivities = array();
            CRM_Core_DAO::commonRetrieveAll('CRM_Case_DAO_CaseActivity', 'case_id', $otherCaseId, $otherCaseActivities);
            //for duplicate cases do not process singleton activities.
            $otherActivityIds = $singletonActivityIds = array();
            foreach ($otherCaseActivities as $caseActivityId => $otherIds) {
                $otherActId = CRM_Utils_Array::value('activity_id', $otherIds);
                if (!$otherActId || in_array($otherActId, $otherActivityIds)) {
                    continue;
                }
                $otherActivityIds[] = $otherActId;
            }
            if ($duplicateCases) {
                if ($openCaseType = array_search('Open Case', $activityTypes)) {
                    $sql = "\nSELECT  id\n  FROM  civicrm_activity \n WHERE  activity_type_id = {$openCaseType} \n   AND  id IN ( " . implode(',', array_values($otherActivityIds)) . ');';
                    $dao = CRM_Core_DAO::executeQuery($sql);
                    while ($dao->fetch()) {
                        $singletonActivityIds[] = $dao->id;
                    }
                    $dao->free();
                }
            }
            // migrate all activities and connect to main contact.
            $copiedActivityIds = $activityMappingIds = array();
            sort($otherActivityIds);
            foreach ($otherActivityIds as $otherActivityId) {
                //for duplicate cases -
                //do not migrate singleton activities.
                if (!$otherActivityId || in_array($otherActivityId, $singletonActivityIds)) {
                    continue;
                }
                //migrate activity record.
                $otherActivity = new CRM_Activity_DAO_Activity();
                $otherActivity->id = $otherActivityId;
                if (!$otherActivity->find(true)) {
                    continue;
                }
                $mainActVals = array();
                $mainActivity = new CRM_Activity_DAO_Activity();
                CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
                $mainActivity->copyValues($mainActVals);
                $mainActivity->id = null;
                $mainActivity->activity_date_time = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
                //do check for merging contact,
                if ($mainActivity->source_contact_id == $otherContactId) {
                    $mainActivity->source_contact_id = $mainContactId;
                }
                $mainActivity->source_record_id = CRM_Utils_Array::value($mainActivity->source_record_id, $activityMappingIds);
                $mainActivity->original_id = CRM_Utils_Array::value($mainActivity->original_id, $activityMappingIds);
                $mainActivity->parent_id = CRM_Utils_Array::value($mainActivity->parent_id, $activityMappingIds);
                $mainActivity->save();
                $mainActivityId = $mainActivity->id;
                if (!$mainActivityId) {
                    continue;
                }
                $activityMappingIds[$otherActivityId] = $mainActivityId;
                // insert log of all activites
                CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
                $otherActivity->free();
                $mainActivity->free();
                $copiedActivityIds[] = $otherActivityId;
                //create case activity record.
                $mainCaseActivity = new CRM_Case_DAO_CaseActivity();
                $mainCaseActivity->case_id = $mainCaseId;
                $mainCaseActivity->activity_id = $mainActivityId;
                $mainCaseActivity->save();
                $mainCaseActivity->free();
                //migrate target activities.
                $otherTargetActivity = new CRM_Activity_DAO_ActivityTarget();
                $otherTargetActivity->activity_id = $otherActivityId;
                $otherTargetActivity->find();
                while ($otherTargetActivity->fetch()) {
                    $mainActivityTarget = new CRM_Activity_DAO_ActivityTarget();
                    $mainActivityTarget->activity_id = $mainActivityId;
                    $mainActivityTarget->target_contact_id = $otherTargetActivity->target_contact_id;
                    if ($mainActivityTarget->target_contact_id == $otherContactId) {
                        $mainActivityTarget->target_contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainActivityTarget->find(true)) {
                        $mainActivityTarget->save();
                    }
                    $mainActivityTarget->free();
                }
                $otherTargetActivity->free();
                //migrate assignee activities.
                $otherAssigneeActivity = new CRM_Activity_DAO_ActivityAssignment();
                $otherAssigneeActivity->activity_id = $otherActivityId;
                $otherAssigneeActivity->find();
                while ($otherAssigneeActivity->fetch()) {
                    $mainAssigneeActivity = new CRM_Activity_DAO_ActivityAssignment();
                    $mainAssigneeActivity->activity_id = $mainActivityId;
                    $mainAssigneeActivity->assignee_contact_id = $otherAssigneeActivity->assignee_contact_id;
                    if ($mainAssigneeActivity->assignee_contact_id == $otherContactId) {
                        $mainAssigneeActivity->assignee_contact_id = $mainContactId;
                    }
                    //avoid duplicate object.
                    if (!$mainAssigneeActivity->find(true)) {
                        $mainAssigneeActivity->save();
                    }
                    $mainAssigneeActivity->free();
                }
                $otherAssigneeActivity->free();
            }
            //copy case relationship.
            if ($duplicateContacts) {
                //migrate relationship records.
                $otherRelationship = new CRM_Contact_DAO_Relationship();
                $otherRelationship->case_id = $otherCaseId;
                $otherRelationship->find();
                $otherRelationshipIds = array();
                while ($otherRelationship->fetch()) {
                    $otherRelVals = array();
                    $updateOtherRel = false;
                    CRM_Core_DAO::storeValues($otherRelationship, $otherRelVals);
                    $mainRelationship = new CRM_Contact_DAO_Relationship();
                    $mainRelationship->copyValues($otherRelVals);
                    $mainRelationship->id = null;
                    $mainRelationship->case_id = $mainCaseId;
                    if ($mainRelationship->contact_id_a == $otherContactId) {
                        $updateOtherRel = true;
                        $mainRelationship->contact_id_a = $mainContactId;
                    }
                    //case creator change only when we merge user contact.
                    if ($mainRelationship->contact_id_b == $otherContactId) {
                        //do not change creator for change client.
                        if (!$changeClient) {
                            $updateOtherRel = true;
                            $mainRelationship->contact_id_b = $currentUserId ? $currentUserId : $mainContactId;
                        }
                    }
                    $mainRelationship->end_date = CRM_Utils_Date::isoToMysql($otherRelationship->end_date);
                    $mainRelationship->start_date = CRM_Utils_Date::isoToMysql($otherRelationship->start_date);
                    //avoid duplicate object.
                    if (!$mainRelationship->find(true)) {
                        $mainRelationship->save();
                    }
                    $mainRelationship->free();
                    //get the other relationship ids to update end date.
                    if ($updateOtherRel) {
                        $otherRelationshipIds[$otherRelationship->id] = $otherRelationship->id;
                    }
                }
                $otherRelationship->free();
                //update other relationships end dates
                if (!empty($otherRelationshipIds)) {
                    $sql = 'UPDATE  civicrm_relationship 
                               SET  end_date = CURDATE() 
                             WHERE  id IN ( ' . implode(',', $otherRelationshipIds) . ')';
                    CRM_Core_DAO::executeQuery($sql);
                }
            }
            //move other case to trash.
            $mergeCase = self::deleteCase($otherCaseId, $moveToTrash);
            if (!$mergeCase) {
                continue;
            }
            $mergeActSubject = $mergeActSubjectDetails = $mergeActType = '';
            if ($changeClient) {
                require_once 'CRM/Contact/BAO/Contact.php';
                $mainContactDisplayName = CRM_Contact_BAO_Contact::displayName($mainContactId);
                $otherContactDisplayName = CRM_Contact_BAO_Contact::displayName($otherContactId);
                $mergeActType = array_search('Reassigned Case', $activityTypes);
                $mergeActSubject = ts("Case %1 reassigned client from %2 to %3. New Case ID is %4.", array(1 => $otherCaseId, 2 => $otherContactDisplayName, 3 => $mainContactDisplayName, 4 => $mainCaseId));
            } else {
                if ($duplicateContacts) {
                    $mergeActType = array_search('Merge Case', $activityTypes);
                    $mergeActSubject = ts("Case %1 copied from contact id %2 to contact id %3 via merge. New Case ID is %4.", array(1 => $otherCaseId, 2 => $otherContactId, 3 => $mainContactId, 4 => $mainCaseId));
                } else {
                    $mergeActType = array_search('Merge Case', $activityTypes);
                    $mergeActSubject = ts("Case %1 merged into case %2", array(1 => $otherCaseId, 2 => $mainCaseId));
                    if (!empty($copiedActivityIds)) {
                        $sql = '
SELECT id, subject, activity_date_time, activity_type_id
FROM civicrm_activity
WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
                        $dao = CRM_Core_DAO::executeQuery($sql);
                        while ($dao->fetch()) {
                            $mergeActSubjectDetails .= "{$dao->activity_date_time} :: {$activityTypes[$dao->activity_type_id]}";
                            if ($dao->subject) {
                                $mergeActSubjectDetails .= " :: {$dao->subject}";
                            }
                            $mergeActSubjectDetails .= "<br />";
                        }
                    }
                }
            }
            //create merge activity record.
            $activityParams = array('subject' => $mergeActSubject, 'details' => $mergeActSubjectDetails, 'status_id' => array_search('Completed', $activityStatuses), 'activity_type_id' => $mergeActType, 'source_contact_id' => $mainContactId, 'activity_date_time' => date('YmdHis'));
            $mergeActivity = CRM_Activity_BAO_Activity::create($activityParams);
            $mergeActivityId = $mergeActivity->id;
            if (!$mergeActivityId) {
                continue;
            }
            $mergeActivity->free();
            //connect merge activity to case.
            $mergeCaseAct = array('case_id' => $mainCaseId, 'activity_id' => $mergeActivityId);
            self::processCaseActivity($mergeCaseAct);
        }
        return $mainCaseIds;
    }