Skip to content

Commit

Permalink
Merged in atrium-4003 (pull request civicrm#25)
Browse files Browse the repository at this point in the history
Atrium 4003
  • Loading branch information
andrew-cormick-dockery committed Sep 6, 2016
2 parents 7c0165e + bdeecaf commit 3ec4b39
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
*/

/**
Expand Down Expand Up @@ -1220,10 +1218,10 @@ public static function buildRecipientContacts($mappingID, $now, $params = array(
$select[] = 'e.id as entity_id';
$select[] = "'{$mapping->entity}' as entity_table";
$select[] = "{$actionSchedule->id} as action_schedule_id";
$reminderJoinClause = "civicrm_action_log reminder ON reminder.contact_id = {$contactField} AND
reminder.entity_id = e.id AND
reminder.entity_table = '{$mapping->entity}' AND
reminder.action_schedule_id = %1";
$reminderJoinClause = "civicrm_action_log reminder ON reminder.contact_id = {$contactField}
AND reminder.entity_id = e.id
AND reminder.entity_table = '{$mapping->entity}'
AND reminder.action_schedule_id = %1";

if ($anniversary) {
// only consider reminders less than 11 months ago
Expand Down Expand Up @@ -1254,7 +1252,7 @@ public static function buildRecipientContacts($mappingID, $now, $params = array(
}

// ( now >= date_built_from_start_time ) OR ( now = absolute_date )
$dateClause = "reminder.id IS NULL AND {$startDate}";
$dateClause = $startDate ? " AND {$startDate} " : '';

// start composing query
$selectClause = 'SELECT ' . implode(', ', $select);
Expand All @@ -1272,26 +1270,38 @@ public static function buildRecipientContacts($mappingID, $now, $params = array(
{$fromClause}
{$joinClause}
LEFT JOIN {$reminderJoinClause}
{$whereClause} {$limitWhereClause} AND {$dateClause} {$notINClause}
{$whereClause} AND reminder.id IS NULL {$limitWhereClause} {$dateClause} {$notINClause}
";

// In some cases reference_date got outdated due to many reason e.g. In Membership renewal end_date got extended
// which means reference date mismatches with the end_date where end_date may be used as the start_action_date
// criteria for some schedule reminder so in order to send new reminder we INSERT new reminder with new reference_date
// value via UNION operation
// We need to add in reminders that
// have not already had a reminder for the current end date and HAVE had a reminder for a different
// end date for the same reminder. These will have been excluded earlier, on the basis of a reminder having gone out
// so we want to selectively re-add them.
if (strpos($selectColumns, 'reference_date') !== FALSE) {
$dateClause = str_replace('reminder.id IS NULL', 'reminder.id IS NOT NULL', $dateClause);
$referenceQuery = "
INSERT INTO civicrm_action_log ({$selectColumns})
{$selectClause}
{$fromClause}
{$joinClause}
LEFT JOIN {$reminderJoinClause}
{$whereClause} {$limitWhereClause} {$notINClause} AND {$dateClause} AND
reminder.action_date_time IS NOT NULL AND
reminder.reference_date IS NOT NULL
GROUP BY reminder.id, reminder.reference_date
HAVING reminder.id = MAX(reminder.id) AND reminder.reference_date <> {$dateField}
LEFT JOIN (
SELECT entity_id, entity_table
{$fromClause}
{$joinClause}
LEFT JOIN {$reminderJoinClause}
{$whereClause} {$limitWhereClause} {$dateClause} {$notINClause}
AND reminder.reference_date = $dateField
) as already_sent ON e.id = already_sent.entity_id AND already_sent.entity_table = '{$mapping->entity}'
{$whereClause} {$limitWhereClause} {$notINClause} {$dateClause}
AND reminder.id IS NOT NULL
AND reminder.reference_date IS NOT NULL
AND reminder.reference_date <> $dateField
AND already_sent.entity_table IS NULL
GROUP BY e.id
";
}

Expand Down

0 comments on commit 3ec4b39

Please sign in to comment.