From 467fe956772964566d33a95eb0583d46d8fca30c Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Wed, 16 Dec 2015 18:26:47 +0000 Subject: [PATCH] CRM-17718 add Financial type to recur form This has only been added for those with sites with no ACLs as the financial acl code is quite new & raised questions when I looked. Further the option is only editable if there is a single line item. Multiple line item recurrings cannot be overridden from here Contribution Recur Edit should not require installments Leaving installments empty is valid - ie. it could be null for unlimited (there is an implication that it should be '0' rather than null but null 'works' & is set in some cases and we permit the DB field to be null - which we should not do if null is not OK) --- CRM/Contribute/BAO/Contribution.php | 25 +++++----- CRM/Contribute/BAO/ContributionRecur.php | 48 ++++++++++++++++++- CRM/Contribute/Form/UpdateSubscription.php | 12 +++-- CRM/Contribute/Page/ContributionRecur.php | 11 +++-- .../Contribute/Form/UpdateSubscription.tpl | 1 + .../CRM/Contribute/Page/ContributionRecur.tpl | 2 + 6 files changed, 77 insertions(+), 22 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e42aff2cc5..f0248e0eef 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2036,10 +2036,7 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ if (!empty($input['amount'])) { $contribution->total_amount = $contributionParams['total_amount'] = $input['amount']; } - $templateContribution = civicrm_api3('Contribution', 'getsingle', array( - 'contribution_recur_id' => $contributionParams['contribution_recur_id'], - 'options' => array('limit' => 1), - )); + $templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionParams['contribution_recur_id']); if (!empty($contributionParams['contribution_recur_id'])) { $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', array( 'id' => $contributionParams['contribution_recur_id'], @@ -4188,7 +4185,7 @@ WHERE con.id = {$contributionId} * @throws \CiviCRM_API3_Exception */ public static function isSingleLineItem($id) { - $lineItemCount = civicrm_api3('LineItem', 'getcount', array('id' => $id)); + $lineItemCount = civicrm_api3('LineItem', 'getcount', array('contribution_id' => $id)); return ($lineItemCount == 1); } @@ -4634,14 +4631,14 @@ LIMIT 1;"; } /** - * Get the description (source field) for the recurring contribution. - * - * @param CRM_Contribute_BAO_Contribution $contribution - * @param CRM_Event_DAO_Event|null $event - * - * @return array - * @throws \CiviCRM_API3_Exception - */ + * Get the description (source field) for the recurring contribution. + * + * @param CRM_Contribute_BAO_Contribution $contribution + * @param CRM_Event_DAO_Event|null $event + * + * @return array + * @throws \CiviCRM_API3_Exception + */ protected static function getRecurringContributionDescription($contribution, $event) { if (!empty($contribution->contribution_page_id)) { $contributionPageTitle = civicrm_api3('ContributionPage', 'getvalue', array( @@ -4650,7 +4647,7 @@ LIMIT 1;"; )); return ts('Online Contribution') . ': ' . $contributionPageTitle; } - .elseif ($event) { + elseif ($event) { return ts('Online Event Registration') . ': ' . $event->title; } return 'recurring contribution'; diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index b5a94192c4..c627aa7869 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -379,7 +379,8 @@ SELECT rec.id as recur_id, rec.auto_renew, rec.currency, rec.campaign_id, - con.id as contribution_id, + rec.financial_type_id, + con.id as contribution_id, con.contribution_page_id, rec.contact_id, mp.membership_id"; @@ -413,10 +414,53 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) return $dao; } else { - return CRM_Core_DAO::$_nullObject; + return NULL; } } + /** + * Does the recurring contribution support financial type change. + * + * This is conditional on there being only one line item or if there are no contributions as yet. + * + * (This second is a bit of an unusual condition but might occur in the context of a + * + * @param int $id + * + * @return bool + */ + public static function supportsFinancialTypeChange($id) { + // At this stage only sites with no Financial ACLs will have the opportunity to edit the financial type. + // this is to limit the scope of the change and because financial ACLs are still fairly new & settling down. + if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { + return FALSE; + } + $contribution = self::getTemplateContribution($id); + return CRM_Contribute_BAO_Contribution::isSingleLineItem($contribution['id']); + } + + /** + * Get the contribution to be used as the template for later contributions. + * + * Later we might merge in data stored against the contribution recur record rather than just return the contribution. + * + * @param int $id + * + * @return array + * @throws \CiviCRM_API3_Exception + */ + public static function getTemplateContribution($id) { + $templateContribution = civicrm_api3('Contribution', 'get', array( + 'contribution_recur_id' => $id, + 'options' => array('limit' => 1, 'sort' => array('id DESC')), + 'sequential' => 1, + )); + if ($templateContribution['count']) { + return $templateContribution['values'][0]; + } + return array(); + } + public static function setSubscriptionContext() { // handle context redirection for subscription url $session = CRM_Core_Session::singleton(); diff --git a/CRM/Contribute/Form/UpdateSubscription.php b/CRM/Contribute/Form/UpdateSubscription.php index 3c871634a3..e5dbe81dcf 100644 --- a/CRM/Contribute/Form/UpdateSubscription.php +++ b/CRM/Contribute/Form/UpdateSubscription.php @@ -78,12 +78,13 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Core_Form { $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'info'); $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj'); $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_coid, 'contribution'); + $this->_crid = $this->_subscriptionDetails->recur_id; } elseif ($this->_crid) { $this->_coid = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_crid, 'id', 'contribution_recur_id'); } - if ((!$this->_crid && !$this->_coid) || + if ((!$this->_crid) || ($this->_subscriptionDetails == CRM_Core_DAO::$_nullObject) ) { CRM_Core_Error::fatal('Required information missing.'); @@ -133,6 +134,7 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Core_Form { $this->_defaults['amount'] = $this->_subscriptionDetails->amount; $this->_defaults['installments'] = $this->_subscriptionDetails->installments; $this->_defaults['campaign_id'] = $this->_subscriptionDetails->campaign_id; + $this->_defaults['financial_type_id'] = $this->_subscriptionDetails->financial_type_id; $this->_defaults['is_notify'] = 1; return $this->_defaults; @@ -152,14 +154,18 @@ class CRM_Contribute_Form_UpdateSubscription extends CRM_Core_Form { TRUE, 'currency', $this->_subscriptionDetails->currency, TRUE ); - $this->add('text', 'installments', ts('Number of Installments'), array('size' => 20), TRUE); + $this->add('text', 'installments', ts('Number of Installments'), array('size' => 20), FALSE); if ($this->_donorEmail) { $this->add('checkbox', 'is_notify', ts('Notify Contributor?')); } if (CRM_Core_Permission::check('edit contributions')) { - CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); + CRM_Campaign_BAO_Campaign::addCampaign($this); + } + + if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($this->_crid)) { + $this->addEntityRef('financial_type_id', ts('Financial Type'), array('entity' => 'FinancialType'), TRUE); } $type = 'next'; diff --git a/CRM/Contribute/Page/ContributionRecur.php b/CRM/Contribute/Page/ContributionRecur.php index 8acb3bfc74..7071b5cb77 100644 --- a/CRM/Contribute/Page/ContributionRecur.php +++ b/CRM/Contribute/Page/ContributionRecur.php @@ -57,9 +57,14 @@ class CRM_Contribute_Page_ContributionRecur extends CRM_Core_Page { 'name' ); } - // get contribution status label - if (!empty($values['contribution_status_id'])) { - $values['contribution_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $values['contribution_status_id']); + $idFields = array('contribution_status_id', 'campaign_id'); + if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($values['id'])) { + $idFields[] = 'financial_type_id'; + } + foreach ($idFields as $idField) { + if (!empty($values[$idField])) { + $values[substr($idField, 0, -3)] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', $idField, $values[$idField]); + } } $this->assign('recur', $values); diff --git a/templates/CRM/Contribute/Form/UpdateSubscription.tpl b/templates/CRM/Contribute/Form/UpdateSubscription.tpl index 35a7ec926b..2eb92a6b33 100644 --- a/templates/CRM/Contribute/Form/UpdateSubscription.tpl +++ b/templates/CRM/Contribute/Form/UpdateSubscription.tpl @@ -40,6 +40,7 @@ {if !$self_service} {$form.is_notify.label}{$form.is_notify.html} {$form.campaign_id.label}{$form.campaign_id.html} + {$form.financial_type_id.label}{$form.financial_type_id.html} {/if} {* Currently changes to interval, unit and cycle day are not supported. *} diff --git a/templates/CRM/Contribute/Page/ContributionRecur.tpl b/templates/CRM/Contribute/Page/ContributionRecur.tpl index 32f70ebf4b..8f433506a6 100644 --- a/templates/CRM/Contribute/Page/ContributionRecur.tpl +++ b/templates/CRM/Contribute/Page/ContributionRecur.tpl @@ -49,6 +49,8 @@ {if $recur.invoice_id}{ts}Failure Retry Date{/ts}{$recur.next_sched_contribution_date|crmDate}{/if} {ts}Auto Renew?{/ts}{if $recur.auto_renew}{ts}Yes{/ts}{else}{ts}No{/ts}{/if} {if $recur.payment_processor}{ts}Payment Processor{/ts}{$recur.payment_processor}{/if} + {if $recur.financial_type}{ts}Financial Type{/ts}{$recur.financial_type}{/if} + {if $recur.campaign}{ts}Campaign{/ts}{$recur.campaign}{/if}
{ts}Done{/ts}
-- 2.25.1