+ // function to copy custom data of the
+ // initial contribution into its recurring contributions
+ function copyCustomValues($recurId, $targetContributionId) {
+ if ($recurId && $targetContributionId) {
+ // get the initial contribution id of recur id
+ $sourceContributionId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $recurId, 'id', 'contribution_recur_id');
+
+ // if the same contribution is being proccessed then return
+ if ($sourceContributionId == $targetContributionId) {
+ return;
+ }
+ // check if proper recurring contribution record is being processed
+ $targetConRecurId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $targetContributionId, 'contribution_recur_id');
+ if ($targetConRecurId != $recurId) {
+ return;
+ }
+
+ // copy custom data
+ $extends = array('Contribution');
+ $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] = $targetContributionId;
+ $select = 'SELECT ' . implode(', ', $tableColumns);
+ $from = ' FROM ' . $tableName;
+ $where = " WHERE {$tableName}.entity_id = {$sourceContributionId}";
+ $query = $insert . $select . $from . $where;
+ $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
+ }
+ }
+ }
+ }
+}