Skip to content
Open
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
42 changes: 21 additions & 21 deletions custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
'trigger_sql' => '(SELECT IF(SUM(line_total) IS NULL, 0, SUM(line_total))
FROM civicrm_contribution t1 JOIN
civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
WHERE t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t1.is_test = 0 AND
t2.financial_type_id IN (%financial_type_ids))',
'trigger_table' => 'civicrm_line_item',
Expand All @@ -103,7 +103,7 @@
'weight' => '10',
'text_length' => '32',
'trigger_sql' => '(SELECT IF(SUM(total_amount) IS NULL, 0, SUM(total_amount))
FROM civicrm_contribution t1 WHERE t1.contact_id = NEW.contact_id AND t1.is_test = 0
FROM civicrm_contribution t1 WHERE t1.contact_id = NEW.contact_id AND t1.is_test = 0
AND t1.contribution_status_id = 1 AND t1.financial_type_id IN (%financial_type_ids))',
'trigger_table' => 'civicrm_contribution',
'optgroup' => 'fundraising',
Expand All @@ -118,7 +118,7 @@
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%current_fiscal_year_begin"
AND "%current_fiscal_year_end" AND t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
AND "%current_fiscal_year_end" AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -146,7 +146,7 @@
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN DATE_SUB(NOW(), INTERVAL 12 MONTH) AND NOW()
AND t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -175,7 +175,7 @@
t1.financial_type_id = t2.id AND is_deductible = 1
JOIN civicrm_line_item t3 ON t1.id = t3.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%current_fiscal_year_begin" AND
"%current_fiscal_year_end" AND t1.contact_id IN (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
"%current_fiscal_year_end" AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t3.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -205,7 +205,7 @@
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%last_fiscal_year_begin"
AND "%last_fiscal_year_end" AND t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
AND "%last_fiscal_year_end" AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -234,7 +234,7 @@
t1.financial_type_id = t2.id AND is_deductible = 1
JOIN civicrm_line_item t3 ON t1.id = t3.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%last_fiscal_year_begin" AND
"%last_fiscal_year_end" AND t1.contact_id IN (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
"%last_fiscal_year_end" AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t3.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -264,7 +264,7 @@
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%year_before_last_fiscal_year_begin"
AND "%year_before_last_fiscal_year_end" AND t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
AND "%year_before_last_fiscal_year_end" AND t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -293,7 +293,7 @@
t1.financial_type_id = t2.id AND is_deductible = 1
JOIN civicrm_line_item t3 ON t1.id = t3.contribution_id
WHERE CAST(receive_date AS DATE) BETWEEN "%year_before_last_fiscal_year_begin" AND
"%year_before_last_fiscal_year_end" AND t1.contact_id IN (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
"%year_before_last_fiscal_year_end" AND t1.contact_id IN calculated_contact_id AND
t1.contribution_status_id = 1 AND t3.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -322,7 +322,7 @@
'trigger_sql' => '(SELECT COALESCE(total_amount,0)
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id)
WHERE t1.contact_id = calculated_contact_id
AND t1.contribution_status_id = 1 AND t2.financial_type_id IN
(%financial_type_ids) AND t1.is_test = 0 ORDER BY t1.receive_date DESC LIMIT 1)',
'trigger_table' => 'civicrm_line_item',
Expand All @@ -349,7 +349,7 @@
'text_length' => '32',
'trigger_sql' => '(SELECT MAX(receive_date) FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.contribution_status_id = 1 AND
WHERE t1.contact_id = calculated_contact_id AND t1.contribution_status_id = 1 AND
t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand All @@ -375,7 +375,7 @@
'trigger_sql' => '(SELECT COALESCE(total_amount,0)
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id)
WHERE t1.contact_id = calculated_contact_id
AND t1.contribution_status_id = 1 AND t2.financial_type_id IN
(%financial_type_ids) AND t1.is_test = 0 ORDER BY t1.receive_date ASC LIMIT 1)',
'trigger_table' => 'civicrm_line_item',
Expand All @@ -402,7 +402,7 @@
'text_length' => '32',
'trigger_sql' => '(SELECT MIN(receive_date) FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.contribution_status_id = 1 AND
WHERE t1.contact_id = calculated_contact_id AND t1.contribution_status_id = 1 AND
t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand All @@ -428,7 +428,7 @@
'trigger_sql' => '(SELECT COALESCE(MAX(total_amount), 0)
FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND
WHERE t1.contact_id = calculated_contact_id AND
t1.contribution_status_id = 1 AND t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand All @@ -453,7 +453,7 @@
'text_length' => '32',
'trigger_sql' => '(SELECT COALESCE(COUNT(DISTINCT t1.id), 0) FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.contribution_status_id = 1 AND
WHERE t1.contact_id = calculated_contact_id AND t1.contribution_status_id = 1 AND
t2.financial_type_id IN (%financial_type_ids) AND t1.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand All @@ -477,12 +477,12 @@
'weight' => '50',
'text_length' => '32',
'trigger_sql' => '(SELECT COALESCE(SUM(line_total),0) / (SELECT NULLIF(COUNT(DISTINCT SUBSTR(receive_date, 1, 4)), 0)
FROM civicrm_contribution t0
FROM civicrm_contribution t0
JOIN civicrm_line_item t1 ON t0.id = t1.contribution_id
WHERE t0.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.financial_type_id
WHERE t0.contact_id = calculated_contact_id AND t1.financial_type_id
IN (%financial_type_ids) AND t0.contribution_status_id = 1 AND is_test = 0) FROM civicrm_contribution t2
JOIN civicrm_line_item t3 ON t2.id = t3.contribution_id
WHERE t2.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t3.financial_type_id IN (%financial_type_ids)
WHERE t2.contact_id = calculated_contact_id AND t3.financial_type_id IN (%financial_type_ids)
AND t2.contribution_status_id = 1 AND t2.is_test = 0)',
'trigger_table' => 'civicrm_line_item',
'optgroup' => 'fundraising',
Expand Down Expand Up @@ -551,7 +551,7 @@
'text_length' => '32',
'trigger_sql' => '(SELECT MAX(receive_date) FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.contribution_status_id = 1 AND
WHERE t1.contact_id = calculated_contact_id AND t1.contribution_status_id = 1 AND
t2.financial_type_id IN (%membership_financial_type_ids) AND is_test = 0 ORDER BY
receive_date DESC LIMIT 1)',
'trigger_table' => 'civicrm_line_item',
Expand All @@ -578,7 +578,7 @@
'text_length' => '32',
'trigger_sql' => '(SELECT total_amount FROM civicrm_contribution t1
JOIN civicrm_line_item t2 ON t1.id = t2.contribution_id
WHERE t1.contact_id = (SELECT contact_id FROM civicrm_contribution cc WHERE cc.id = NEW.contribution_id) AND t1.contribution_status_id = 1 AND
WHERE t1.contact_id = calculated_contact_id AND t1.contribution_status_id = 1 AND
t2.financial_type_id IN (%membership_financial_type_ids) AND t1.is_test = 0 ORDER BY
receive_date DESC LIMIT 1)',
'trigger_table' => 'civicrm_line_item',
Expand All @@ -592,7 +592,7 @@
'text_length' => '32',
'trigger_sql' =>'(SELECT total_amount FROM civicrm_contribution t1 WHERE
t1.contact_id = NEW.contact_id AND t1.contribution_status_id = 1 AND
t1.financial_type_id IN (%membership_financial_type_ids) AND t1.is_test = 0 ORDER BY
t1.financial_type_id IN (%membership_financial_type_ids) AND t1.is_test = 0 ORDER BY
receive_date DESC LIMIT 1)',
'trigger_table' => 'civicrm_contribution',
'optgroup' => 'membership',
Expand Down
9 changes: 8 additions & 1 deletion sumfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,25 @@ function sumfields_civicrm_triggerInfo(&$info, $tableName) {
$extra_sql = implode(',', $parts);
$sql = $pre_sql . $generic_sql . $extra_sql . ' ON DUPLICATE KEY UPDATE ' . $extra_sql . ';' . $post_sql;

$variables = ["
DECLARE calculated_contact_id integer;
SET calculated_contact_id := (SELECT contact_id FROM civicrm_contribution WHERE id = NEW.contribution_id);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monishdeb aren't SQL variables normally set to start with an @

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 actually there are two types of variable declaration, the one which starts with @ is a user-defined loosely typed variable (by loosely means can be directly assigned with a value without declaring the type) and can be re-used in other Triggers/Procedure in a session and without @ is a local variable which is a valid in particular trigger and need to mention their type. For more detail please check here.

In this case as there are three CREATE/UPDATE/DELETE event trigger, so I thought its better to have a there respective local variable.

"];

// We want to fire this trigger on insert, update and delete.
$info[] = array(
'table' => $table,
'when' => 'AFTER',
'event' => 'INSERT',
'sql' => $sql,
'variables' => $variables,
);
$info[] = array(
'table' => $table,
'when' => 'AFTER',
'event' => 'UPDATE',
'sql' => $sql,
'variables' => $variables,
);
// For delete, we reference OLD.field instead of NEW.field
$sql = str_replace('NEW.', 'OLD.', $sql);
Expand All @@ -406,6 +413,7 @@ function sumfields_civicrm_triggerInfo(&$info, $tableName) {
'when' => 'AFTER',
'event' => 'DELETE',
'sql' => $sql,
'variables' => $variables,
);
}

Expand Down Expand Up @@ -1431,4 +1439,3 @@ function sumfields_civicrm_alterLogTables(&$logTableSpec) {
unset($logTableSpec[$tableName]);
}
}