Merge pull request #21471 from mattwire/edittemplatecontributionform
[civicrm-core.git] / CRM / Contribute / Form / Contribution.php
index 7a58a71eb2b19cffa33a5adc85aedb964056c474..dc89c47f074dee9188336079ed3f75c851aa5209 100644 (file)
@@ -280,16 +280,16 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $this->assignPremiumProduct($this->_id);
       $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values);
     }
+    if (!isset($this->_values['is_template'])) {
+      $this->_values['is_template'] = FALSE;
+    }
+    $this->assign('is_template', $this->_values['is_template']);
 
     // when custom data is included in this page
     if (!empty($_POST['hidden_custom'])) {
       $this->applyCustomData('Contribution', $this->getFinancialTypeID(), $this->_id);
     }
 
-    if (!empty($this->_values['is_template'])) {
-      $this->assign('is_template', TRUE);
-    }
-
     $this->_lineItems = [];
     if ($this->_id) {
       if (!empty($this->_compId) && $this->_compContext === 'participant') {
@@ -311,7 +311,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $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) {
@@ -442,6 +442,12 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $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');
     }
@@ -608,9 +614,9 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     $this->assign('allPanes', $allPanes);
 
     $this->addFormRule(['CRM_Contribute_Form_Contribution', 'formRule'], $this);
+    $this->assign('formType', $this->_formType);
 
     if ($this->_formType) {
-      $this->assign('formType', $this->_formType);
       return;
     }
 
@@ -695,8 +701,10 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       }
     }
 
+    // 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);
 
@@ -884,6 +892,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     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);
     }
@@ -943,6 +952,12 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     }
     // 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);
@@ -2052,7 +2067,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       return CRM_Contribute_BAO_Contribution_Utils::getPendingCompleteFailedAndCancelledStatuses();
     }
     $statusNames = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate');
-    $statusNamesToUnset = [
+    $statusNamesToUnset = array_diff([
       // For records which represent a data template for a recurring
       // contribution that may not yet have a payment. This status should not
       // be available from forms. 'Template' contributions should only be created
@@ -2060,15 +2075,15 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       // is_template field set to 1. This status excludes them from reports
       // that are still ignorant of the is_template field.
       'Template',
-    ];
+      'Partially paid',
+      'Pending refund',
+    ], [$this->getPreviousContributionStatus()]);
     switch ($this->getPreviousContributionStatus()) {
       case 'Completed':
         // [CRM-17498] Removing unsupported status change options.
         $statusNamesToUnset = array_merge($statusNamesToUnset, [
           'Pending',
           'Failed',
-          'Partially paid',
-          'Pending refund',
         ]);
         break;