From 6357981e1e797d7332ecd46767a92b7ad30c44a8 Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Fri, 24 May 2013 17:59:15 +0530 Subject: [PATCH] CRM-12524-fix : handled custom fields carry forward for recurring contributions --- CRM/Core/Payment/BaseIPN.php | 59 ++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/CRM/Core/Payment/BaseIPN.php b/CRM/Core/Payment/BaseIPN.php index accdaf5153..cf001e435e 100644 --- a/CRM/Core/Payment/BaseIPN.php +++ b/CRM/Core/Payment/BaseIPN.php @@ -164,6 +164,11 @@ class CRM_Core_Payment_BaseIPN { $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); } + //copy initial contribution custom fields for recurring contributions + if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id) { + $this->copyCustomValues($objects['contributionRecur']->id, $contribution->id); + } + if (!CRM_Utils_Array::value('skipComponentSync', $input)) { if (!empty($memberships)) { foreach ($memberships as $membership) { @@ -222,6 +227,11 @@ class CRM_Core_Payment_BaseIPN { $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); } + //copy initial contribution custom fields for recurring contributions + if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id) { + $this->copyCustomValues($objects['contributionRecur']->id, $contribution->id); + } + if (!CRM_Utils_Array::value('skipComponentSync', $input)) { if (!empty($memberships)) { foreach ($memberships as $membership) { @@ -468,6 +478,11 @@ LIMIT 1;"; $this->addrecurLineItems($objects['contributionRecur']->id, $contribution->id); } + //copy initial contribution custom fields for recurring contributions + if ($recurContrib && $recurContrib->id) { + $this->copyCustomValues($recurContrib->id, $contribution->id); + } + // next create the transaction record $paymentProcessor = $paymentProcessorId = ''; if (isset($objects['paymentProcessor'])) { @@ -496,7 +511,7 @@ LIMIT 1;"; $input['participant_id'] = $contribution->_relatedObjects['participant']->id; $input['skipLineItem'] = 1; } - + CRM_Contribute_BAO_Contribution::recordFinancialAccounts($input, NULL); } @@ -768,5 +783,45 @@ LIMIT 1;"; CRM_Price_BAO_LineItem::processPriceSet($contributionId, $lineSets); } } -} + // 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); + } + } + } + } +} \ No newline at end of file -- 2.25.1