From d6dd2beea01ad598272ab0009b6689f8134f3351 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 7 Jul 2021 17:29:04 +1200 Subject: [PATCH] [REF] Simplify isRenew handling on batch for membership This starts the process of using getters to get data from the row and also marks the no-longer shared emailReceipt function as no longer deprecated (since it was copied back to this form early). Clean Money moved to 'stdiseRow' --- CRM/Batch/Form/Entry.php | 52 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index ff306545cf55..ba6277b8902d 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -62,6 +62,15 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ protected $_preserveDefault = TRUE; + /** + * Renew option for current row. + * + * This is as set on the form. + * + * @var int + */ + protected $currentRowIsRenewOption; + /** * Contact fields. * @@ -86,6 +95,13 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ protected $currentRowContributionID; + /** + * Row being processed. + * + * @var array + */ + protected $currentRow = []; + /** * Get the contribution id for the current row. * @@ -733,12 +749,9 @@ private function processMembership(array $params) { continue; } $value['contact_id'] = $params['primary_contact_id'][$key]; - foreach ($value as $fieldKey => $fieldValue) { - if (isset($this->_fields[$fieldKey]) && $this->_fields[$fieldKey]['data_type'] === 'Money') { - $value[$fieldKey] = CRM_Utils_Rule::cleanMoney($fieldValue); - } - } $value = $this->standardiseRow($value); + $this->currentRow = $value; + $this->currentRowIsRenewOption = (int) ($params['member_option'][$key] ?? 1); // update contact information $this->updateContactInfo($value); @@ -789,12 +802,10 @@ private function processMembership(array $params) { 'join_date' => $value['membership_join_date'] ?? NULL, 'campaign_id' => $value['member_campaign_id'] ?? NULL, ]; - $value['is_renew'] = FALSE; - if (!empty($params['member_option']) && CRM_Utils_Array::value($key, $params['member_option']) == 2) { + if ($this->currentRowIsRenew()) { // The following parameter setting may be obsolete. $this->_params = $params; - $value['is_renew'] = TRUE; $formDates = [ 'end_date' => $value['membership_end_date'] ?? NULL, @@ -865,10 +876,6 @@ private function processMembership(array $params) { * true if mail was sent successfully * @throws \CRM_Core_Exception|\API_Exception * - * @deprecated - * This function is shared with Batch_Entry which has limited overlap - * & needs rationalising. - * */ protected function emailReceipt($form, &$formValues, $membership): bool { // @todo figure out how much of the stuff below is genuinely shared with the batch form & a logical shared place. @@ -888,12 +895,7 @@ protected function emailReceipt($form, &$formValues, $membership): bool { $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name')); } - if (!empty($formValues['is_renew'])) { - $form->assign('receiptType', 'membership renewal'); - } - else { - $form->assign('receiptType', 'membership signup'); - } + $form->assign('receiptType', $this->currentRowIsRenew() ? 'membership renewal' : 'membership signup'); $form->assign('receive_date', CRM_Utils_Array::value('receive_date', $formValues)); $form->assign('formValues', $formValues); @@ -1220,6 +1222,11 @@ private function getFromEmailAddress(): string { * @return array */ private function standardiseRow(array $row): array { + foreach ($row as $fieldKey => $fieldValue) { + if (isset($this->_fields[$fieldKey]) && $this->_fields[$fieldKey]['data_type'] === 'Money') { + $row[$fieldKey] = CRM_Utils_Rule::cleanMoney($fieldValue); + } + } $renameFieldMapping = [ 'financial_type' => 'financial_type_id', 'payment_instrument' => 'payment_instrument_id', @@ -1242,4 +1249,13 @@ private function standardiseRow(array $row): array { return $row; } + /** + * Is the current row a renewal. + * + * @return bool + */ + private function currentRowIsRenew(): bool { + return $this->currentRowIsRenewOption === 2; + } + }