Skip to content

Commit

Permalink
[REF] Simplify isRenew handling on batch for membership
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
eileenmcnaughton committed Jul 7, 2021
1 parent 44f9625 commit d6dd2be
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions CRM/Batch/Form/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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',
Expand All @@ -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;
}

}

0 comments on commit d6dd2be

Please sign in to comment.