Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20546: Multiple issues with creation of membership activities #10324

Merged
merged 6 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 75 additions & 49 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2041,57 +2041,22 @@ public static function getContactActivity($contactId) {
* @param string $activityType
* For Membership Signup or Renewal.
* @param int $targetContactID
* @param array $params
* Activity params to override.
*
* @return bool|NULL
*/
public static function addActivity(
&$activity,
$activityType = 'Membership Signup',
$targetContactID = NULL
$targetContactID = NULL,
$params = array()
) {
$date = date('YmdHis');
if ($activity->__table == 'civicrm_membership') {
$membershipType = CRM_Member_PseudoConstant::membershipType($activity->membership_type_id);

if (!$membershipType) {
$membershipType = ts('Membership');
}

$subject = "{$membershipType}";

if (!empty($activity->source) && $activity->source != 'null') {
$subject .= " - {$activity->source}";
}

if ($activity->owner_membership_id) {
$query = "
SELECT display_name
FROM civicrm_contact, civicrm_membership
WHERE civicrm_contact.id = civicrm_membership.contact_id
AND civicrm_membership.id = $activity->owner_membership_id
";
$displayName = CRM_Core_DAO::singleValueQuery($query);
$subject .= " (by {$displayName})";
}

$subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $activity->status_id, 'label');
// CRM-72097 changed from start date to today
$date = date('YmdHis');
$component = 'Membership';
}
elseif ($activity->__table == 'civicrm_participant') {
$event = CRM_Event_BAO_Event::getEvents(1, $activity->event_id, TRUE, FALSE);

$roles = CRM_Event_PseudoConstant::participantRole();
$status = CRM_Event_PseudoConstant::participantStatus();

$subject = $event[$activity->event_id];
if (!empty($roles[$activity->role_id])) {
$subject .= ' - ' . $roles[$activity->role_id];
}
if (!empty($status[$activity->status_id])) {
$subject .= ' - ' . $status[$activity->status_id];
}
$date = date('YmdHis');
if ($activityType != 'Email') {
$activityType = 'Event Registration';
}
Expand All @@ -2102,27 +2067,38 @@ public static function addActivity(
if ($activity->contribution_status_id != 1) {
return NULL;
}
$activityType = $component = 'Contribution';

$subject = NULL;

$subject .= CRM_Utils_Money::format($activity->total_amount, $activity->currency);
if (!empty($activity->source) && $activity->source != 'null') {
$subject .= " - {$activity->source}";
// retrieve existing activity based on source_record_id and activity_type
if (empty($params['id'])) {
$params['id'] = CRM_Utils_Array::value('id', civicrm_api3('Activity', 'Get', array(
'source_record_id' => $activity->id,
'activity_type_id' => $activityType,
)));
}
if (!empty($params['id'])) {
// CRM-13237 : if activity record found, update it with campaign id of contribution
$params['campaign_id'] = $activity->campaign_id;
}

$date = CRM_Utils_Date::isoToMysql($activity->receive_date);
$activityType = $component = 'Contribution';
}

$activityParams = array(
'source_contact_id' => $activity->contact_id,
'source_record_id' => $activity->id,
'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $activityType),
'subject' => $subject,
'activity_date_time' => $date,
'is_test' => $activity->is_test,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
'skipRecentView' => TRUE,
'campaign_id' => $activity->campaign_id,
);
$activityParams = array_merge($activityParams, $params);

if (empty($activityParams['subject'])) {
$activityParams['subject'] = self::getActivitySubject($activity);
}

if (!empty($activity->activity_id)) {
$activityParams['id'] = $activity->activity_id;
Expand All @@ -2131,7 +2107,6 @@ public static function addActivity(
$id = CRM_Core_Session::getLoggedInContactID();
if ($id) {
$activityParams['source_contact_id'] = $id;
$activityParams['target_contact_id'][] = $activity->contact_id;
}

// CRM-14945
Expand All @@ -2140,7 +2115,7 @@ public static function addActivity(
}
//CRM-4027
if ($targetContactID) {
$activityParams['target_contact_id'][] = $targetContactID;
$activityParams['target_contact_id'] = $targetContactID;
}
// @todo - use api - remove lots of wrangling above. Remove deprecated fatal & let form layer
// deal with any exceptions.
Expand All @@ -2150,6 +2125,57 @@ public static function addActivity(
}
}

/**
* Get activity subject on basis of component object.
*
* @param object $entityObj
* particular component object.
*
* @return string
*/
public static function getActivitySubject($entityObj) {
switch ($entityObj->__table) {
case 'civicrm_membership':
$membershipType = CRM_Member_PseudoConstant::membershipType($entityObj->membership_type_id);
$subject = $membershipType ? $membershipType : ts('Membership');

if (!CRM_Utils_System::isNull($entityObj->source)) {
$subject .= " - {$entityObj->source}";
}

if ($entityObj->owner_membership_id) {
list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $entityObj->owner_membership_id, 'contact_id'));
$subject .= sprintf(' (by %s)', $displayName);
}

$subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $entityObj->status_id, 'label');
return $subject;

case 'civicrm_participant':
$event = CRM_Event_BAO_Event::getEvents(1, $entityObj->event_id, TRUE, FALSE);
$roles = CRM_Event_PseudoConstant::participantRole();
$status = CRM_Event_PseudoConstant::participantStatus();
$subject = $event[$entityObj->event_id];

if (!empty($roles[$entityObj->role_id])) {
$subject .= ' - ' . $roles[$entityObj->role_id];
}
if (!empty($status[$entityObj->status_id])) {
$subject .= ' - ' . $status[$entityObj->status_id];
}

return $subject;

case 'civicrm_contribution':
$subject = CRM_Utils_Money::format($entityObj->total_amount, $entityObj->currency);
if (!CRM_Utils_System::isNull($entityObj->source)) {
$subject .= " - {$entityObj->source}";
}

return $subject;
}
}

/**
* Get Parent activity for currently viewed activity.
*
Expand Down
43 changes: 38 additions & 5 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,11 @@ public static function create(&$params, $ids = array()) {
CRM_Core_DAO::setFieldValue('CRM_Activity_BAO_Activity', $activity['id'], 'campaign_id', $contribution->campaign_id);
$contribution->activity_id = $activity['id'];
}

if (empty($contribution->contact_id)) {
$contribution->find(TRUE);
}
CRM_Activity_BAO_Activity::addActivity($contribution, 'Offline');
CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution');

// do not add to recent items for import, CRM-4399
if (empty($params['skipRecentView'])) {
Expand Down Expand Up @@ -1867,6 +1868,7 @@ public static function transitionComponents($params, $processContributionObject

// CRM-15735-to update the membership status as per the contribution receive date
$joinDate = NULL;
$oldStatus = $membership->status_id;
if (!empty($params['receive_date'])) {
$joinDate = $params['receive_date'];
$status = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($membership->start_date,
Expand Down Expand Up @@ -1931,8 +1933,41 @@ public static function transitionComponents($params, $processContributionObject
//update related Memberships.
CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formattedParams);

//CRM-19678: No Membership Renewal Activity is created when a Pay Later is set to Completed
CRM_Activity_BAO_Activity::addActivity($membership, 'Membership Renewal', $membership->contact_id);
foreach (array('Membership Signup', 'Membership Renewal') as $activityType) {
$scheduledActivityID = CRM_Utils_Array::value('id',
civicrm_api3('Activity', 'Get',
array(
'source_record_id' => $membership->id,
'activity_type_id' => $activityType,
'status_id' => 'Scheduled',
'options' => array(
'limit' => 1,
'sort' => 'id DESC',
),
)
)
);
// 1. Update Schedule Membership Signup/Renewal activity to completed on successful payment of pending membership
// 2. OR Create renewal activity scheduled if its membership renewal will be paid later
if ($scheduledActivityID) {
CRM_Activity_BAO_Activity::addActivity($membership, $activityType, $membership->contact_id, array('id' => $scheduledActivityID));
break;
}
}

// track membership status change if any
if (!empty($oldStatus) && $membership->status_id != $oldStatus) {
$allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get');
CRM_Activity_BAO_Activity::addActivity($membership,
'Change Membership Status',
NULL,
array(
'subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$membership->status_id]}",
'source_contact_id' => $membershipLog['modified_id'],
'priority_id' => 'Normal',
)
);
}

$updateResult['membership_end_date'] = CRM_Utils_Date::customFormat($dates['end_date'],
'%B %E%f, %Y'
Expand Down Expand Up @@ -4608,8 +4643,6 @@ public static function completeOrder(&$input, &$ids, $objects, $transaction, $re
$var = TRUE;
CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE);
civicrm_api3('Membership', 'create', $membershipParams);
//CRM-19600: Add Membership Renewal activity
CRM_Activity_BAO_Activity::addActivity($membership, 'Membership Renewal', $membership->contact_id);
}
}
}
Expand Down
Loading