Skip to content
Snippets Groups Projects
Commit afecf695 authored by mattwire's avatar mattwire
Browse files

Fix 'Recur next scheduled date' trigger not working

parent 349fcbe0
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ class CRM_CivirulesCronTrigger_Form_NextContributionDate extends CRM_CivirulesTr
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_id');
$this->add('select', 'interval_unit', ts('Interval'), CRM_CivirulesCronTrigger_MembershipEndDate::intervals(), TRUE);
$this->add('select', 'interval_unit', ts('Interval'), CRM_CivirulesCronTrigger_NextContributionDate::intervals(), TRUE);
$this->add('text', 'interval', ts('Interval'), [], TRUE);
$this->addRule('interval', ts('Interval should be a numeric value'), 'numeric');
$this->addButtons([
......
......@@ -113,7 +113,12 @@ class CRM_CivirulesCronTrigger_MembershipEndDate extends CRM_Civirules_Trigger_C
$sql = "SELECT m.*
FROM `civicrm_membership` `m`
LEFT JOIN `civirule_rule_log` `rule_log` ON `rule_log`.entity_table = 'civicrm_membership' AND `rule_log`.entity_id = m.id AND `rule_log`.`contact_id` = `m`.`contact_id` AND DATE(`rule_log`.`log_date`) = DATE(NOW()) AND `rule_log`.`rule_id` = %3
LEFT JOIN `civirule_rule_log` `rule_log`
ON `rule_log`.entity_table = 'civicrm_membership'
AND `rule_log`.entity_id = m.id
AND `rule_log`.`contact_id` = `m`.`contact_id`
AND DATE(`rule_log`.`log_date`) = DATE(NOW())
AND `rule_log`.`rule_id` = %3
WHERE `m`.`membership_type_id` IN (%1)
AND `rule_log`.`id` IS NULL
{$end_date_statement}
......
......@@ -9,7 +9,7 @@ use CRM_Civirules_ExtensionUtil as E;
class CRM_CivirulesCronTrigger_NextContributionDate extends CRM_Civirules_Trigger_Cron {
/**
* @var \CRM_Member_DAO_Membership $dao
* @var \CRM_Contribute_DAO_ContributionRecur $dao
*/
private $_dao = NULL;
......@@ -57,47 +57,57 @@ class CRM_CivirulesCronTrigger_NextContributionDate extends CRM_Civirules_Trigge
* Method to query trigger entities
*/
private function queryForTriggerEntities() {
$nextDateStatement = "AND DATE(r.next_sched_contribution_date) = CURRENT_DATE()";
$params = [1 => [0, "Integer"]];
switch ($this->triggerParams['interval_unit']) {
case '-days':
$nextDateStatement = "AND DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 DAY) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 DAY)';
break;
case '-weeks':
$nextDateStatement = "AND DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 WEEK) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 WEEK)';
break;
case '-months':
$nextDateStatement = "AND DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 MONTH) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_SUB(r.next_sched_contribution_date, INTERVAL %2 MONTH)';
break;
case '+days':
$nextDateStatement = "AND DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 DAY) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 DAY)';
break;
case '+weeks':
$nextDateStatement = "AND DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 WEEK) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 WEEK)';
break;
case '+months':
$nextDateStatement = "AND DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 MONTH) = CURRENT_DATE()";
$params[2] = [$this->triggerParams['interval'], 'Integer'];
$dateCalcStatement = 'DATE_ADD(r.next_sched_contribution_date, INTERVAL %2 MONTH)';
break;
default:
throw new CRM_Core_Exception('Unknown interval_unit: ' . $this->triggerParams['interval_unit']);
}
$nextDateStatement = "AND DATE({$dateCalcStatement}) = CURRENT_DATE()";
$params = [
// is_test
1 => [0, 'Integer'],
// date numeric interval (eg. 2)
2 => [$this->triggerParams['interval'], 'Integer'],
// The rule ID
3 => [$this->ruleId, 'Integer'],
];
$sql = "SELECT r.id AS `contribution_recur_id`, r.*
FROM `civicrm_contribution_recur` `r`
LEFT JOIN `civirule_rule_log` `rule_log` ON `rule_log`.entity_table = 'civicrm_contribution_recur' AND `rule_log`.entity_id = r.id AND `rule_log`.`contact_id` = `r`.`contact_id` AND DATE(`rule_log`.`log_date`) = DATE(NOW()) AND `rule_log`.`rule_id` = %3
WHERE `r`.`is_test` = %1 AND (`r`.`next_sched_contribution_date` > CURRENT_DATE() OR `r`.`next_sched_contribution_date` IS NULL)
AND `rule_log`.`id` IS NULL
{$nextDateStatement}
AND `r`.`contact_id` NOT IN (
SELECT `rule_log2`.`contact_id`
FROM `civirule_rule_log` `rule_log2`
WHERE `rule_log2`.`rule_id` = %3 AND DATE(`rule_log2`.`log_date`) = DATE(NOW()) and `rule_log2`.`entity_table` IS NULL AND `rule_log2`.`entity_id` IS NULL
LEFT JOIN `civirule_rule_log` `rule_log`
ON `rule_log`.entity_table = 'civicrm_contribution_recur'
AND `rule_log`.entity_id = r.id
AND `rule_log`.`contact_id` = `r`.`contact_id`
AND DATE(`rule_log`.`log_date`) = DATE(NOW())
AND `rule_log`.`rule_id` = %3
WHERE `r`.`is_test` = %1
AND `rule_log`.`id` IS NULL
{$nextDateStatement}
AND `r`.`contact_id` NOT IN (
SELECT `rule_log2`.`contact_id`
FROM `civirule_rule_log` `rule_log2`
WHERE `rule_log2`.`rule_id` = %3 AND DATE(`rule_log2`.`log_date`) = DATE(NOW()) and `rule_log2`.`entity_table` IS NULL AND `rule_log2`.`entity_id` IS NULL
)";
$params[3] = [$this->ruleId, 'Integer'];
$this->_dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Contribute_DAO_ContributionRecur');
return TRUE;
}
......@@ -122,7 +132,7 @@ class CRM_CivirulesCronTrigger_NextContributionDate extends CRM_Civirules_Trigge
public function getTriggerDescription(): string {
$intervalUnits = self::intervals();
$intervalUnitLabel = $intervalUnits[$this->triggerParams['interval_unit']];
return E::ts('Next Scheduled Contribution Date %1 - %2', [
return E::ts('Next Scheduled Contribution Date: %1 %2', [
1 => $this->triggerParams['interval'],
2 => $intervalUnitLabel,
]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment