Skip to content

Commit

Permalink
Fix editing and saving a template contribution via form
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Feb 3, 2022
1 parent 8a96610 commit 68c7099
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public static function ensureTemplateContributionExists(int $id) {
$templateContributionParams['skipRecentView'] = TRUE;
$templateContributionParams['contribution_recur_id'] = $id;
$templateContributionParams['line_item'] = $mostRecentContribution['line_item'];
$templateContributionParams['status_id'] = 'Template';
$templateContributionParams['contribution_status_id'] = 'Template';
foreach (['contact_id', 'campaign_id', 'financial_type_id', 'currency', 'source', 'amount_level', 'address_id', 'on_behalf', 'source_contact_id', 'tax_amount', 'contribution_page_id', 'total_amount'] as $fieldName) {
if (isset($mostRecentContribution[$fieldName])) {
$templateContributionParams[$fieldName] = $mostRecentContribution[$fieldName];
Expand Down
24 changes: 21 additions & 3 deletions CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ public function preProcess() {
$this->applyCustomData('Contribution', $this->getFinancialTypeID(), $this->_id);
}

if (!empty($this->_values['is_template'])) {
if (empty($this->_values['is_template'])) {
$this->_values['is_template'] = FALSE;
}
if ($this->_values['is_template']) {
$this->assign('is_template', TRUE);
}

Expand All @@ -311,7 +314,7 @@ public function preProcess() {
$this->assign('payNow', $this->_payNow);
$this->setTitle(ts('Pay with Credit Card'));
}
elseif (!empty($this->_values['is_template'])) {
elseif ($this->_values['is_template']) {
$this->setPageTitle(ts('Template Contribution'));
}
elseif ($this->_mode) {
Expand Down Expand Up @@ -442,6 +445,12 @@ public function setDefaultValues() {
$defaults['refund_trxn_id'] = $defaults['trxn_id'] ?? NULL;
}

if (!empty($defaults['contribution_status_id'])
&& ('Template' === CRM_Contribute_PseudoConstant::contributionStatus($defaults['contribution_status_id'], 'name'))
) {
$this->getElement('contribution_status_id')->freeze();
}

if (!$this->_id && empty($defaults['receive_date'])) {
$defaults['receive_date'] = date('Y-m-d H:i:s');
}
Expand Down Expand Up @@ -695,8 +704,10 @@ public function buildQuickForm() {
}
}

// If contribution is a template receive date is not required
$receiveDateRequired = !$this->_values['is_template'];
// add various dates
$this->addField('receive_date', ['entity' => 'contribution'], !$this->_mode, FALSE);
$this->addField('receive_date', ['entity' => 'contribution'], $receiveDateRequired, FALSE);
$this->addField('receipt_date', ['entity' => 'contribution'], FALSE, FALSE);
$this->addField('cancel_date', ['entity' => 'contribution', 'label' => ts('Cancelled / Refunded Date')], FALSE, FALSE);

Expand Down Expand Up @@ -884,6 +895,7 @@ public static function formRule($fields, $files, $self) {
if (($self->_action & CRM_Core_Action::UPDATE)
&& $self->_id
&& $self->_values['contribution_status_id'] != $fields['contribution_status_id']
&& $self->_values['is_template'] != 1
) {
CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors);
}
Expand Down Expand Up @@ -943,6 +955,12 @@ public function postProcess() {
}
// Get the submitted form values.
$submittedValues = $this->controller->exportValues($this->_name);
if ($this->_values['is_template']) {
// If we are a template contribution we don't allow the contribution_status_id to be set
// on the form but we need it for the submit function.
$submittedValues['is_template'] = $this->_values['is_template'];
$submittedValues['contribution_status_id'] = $this->_values['contribution_status_id'];
}

try {
$contribution = $this->submit($submittedValues, $this->_action, $this->_ppID);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/ContributionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function preProcess() {
'icon' => 'fa-pencil',
'accesskey' => 'e',
];
if (!empty($paymentButtonName)) {
if (!empty($paymentButtonName) && empty($values['is_template'])) {
$linkButtons[] = [
'title' => $paymentButtonName,
'url' => 'civicrm/payment',
Expand Down

0 comments on commit 68c7099

Please sign in to comment.