Skip to content

Commit

Permalink
Merge pull request #28731 from eileenmcnaughton/amount_conf
Browse files Browse the repository at this point in the history
Extract chunk of code to function
  • Loading branch information
eileenmcnaughton authored Dec 20, 2023
2 parents 9dff56a + 0a4c595 commit 1bd9ce9
Showing 1 changed file with 64 additions and 52 deletions.
116 changes: 64 additions & 52 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,59 +203,10 @@ public function buildQuickForm() {
($this->_params[0]['amount'] || $this->_params[0]['amount'] == 0) &&
!$this->_requireApproval
) {
$this->_amount = [];

$taxAmount = 0;
foreach ($this->_params as $k => $v) {
if ($v === 'skip') {
continue;
}
$individualTaxAmount = 0;
$append = '';
//display tax amount on confirmation page
$taxAmount += $v['tax_amount'];
if (is_array($v)) {
$this->cleanMoneyFields($v);
foreach (['first_name', 'last_name'] as $name) {
if (isset($v['billing_' . $name]) &&
!isset($v[$name])
) {
$v[$name] = $v['billing_' . $name];
}
}
[$taxAmount, $participantDetails, $individual, $amountArray] = $this->calculateAmounts();

if (!empty($v['first_name']) && !empty($v['last_name'])) {
$append = $v['first_name'] . ' ' . $v['last_name'];
}
else {
//use an email if we have one
foreach ($v as $v_key => $v_val) {
if (str_starts_with($v_key, 'email-')) {
$append = $v[$v_key];
}
}
}

$this->_amount[$k]['amount'] = $v['amount'];
if (!empty($v['discountAmount'])) {
$this->_amount[$k]['amount'] -= $v['discountAmount'];
}

$this->_amount[$k]['label'] = preg_replace('//', '', $v['amount_level']) . ' - ' . $append;
$participantDetails[$k]['info'] = ($v['first_name'] ?? '') . ' ' . ($v['last_name'] ?? '');
if (empty($v['first_name'])) {
$participantDetails[$k]['info'] = $append;
}

/*CRM-16320 */
$individual[$k]['totalAmtWithTax'] = $this->_amount[$k]['amount'];
$individual[$k]['totalTaxAmt'] = $individualTaxAmount + $v['tax_amount'];
$this->_totalAmount = $this->_totalAmount + $this->_amount[$k]['amount'];
if (!empty($v['is_primary'])) {
$this->set('primaryParticipantAmount', $this->_amount[$k]['amount']);
}
}
}
$this->_amount = $amountArray;

if (\Civi::settings()->get('invoicing')) {
$this->assign('totalTaxAmount', $taxAmount);
Expand All @@ -266,7 +217,7 @@ public function buildQuickForm() {

$this->assign('part', $participantDetails);
$this->set('part', $participantDetails);
$this->assign('amounts', $this->_amount);
$this->assign('amounts', $amountArray);
$this->assign('totalAmount', $this->_totalAmount);
$this->set('totalAmount', $this->_totalAmount);
// This use of the ts function uses the legacy interpolation of the button name to avoid translations having to be re-done.
Expand Down Expand Up @@ -1313,4 +1264,65 @@ protected function cleanMoneyFields(&$params) {
}
}

/**
* Interim refactoring extraction.
*
* @return array
*/
private function calculateAmounts(): array {
$taxAmount = 0;
$amountArray = [];
foreach ($this->_params as $k => $v) {
if ($v === 'skip') {
continue;
}
$individualTaxAmount = 0;
$append = '';
//display tax amount on confirmation page
$taxAmount += $v['tax_amount'];
if (is_array($v)) {
$this->cleanMoneyFields($v);
foreach (['first_name', 'last_name'] as $name) {
if (isset($v['billing_' . $name]) &&
!isset($v[$name])
) {
$v[$name] = $v['billing_' . $name];
}
}

if (!empty($v['first_name']) && !empty($v['last_name'])) {
$append = $v['first_name'] . ' ' . $v['last_name'];
}
else {
//use an email if we have one
foreach ($v as $v_key => $v_val) {
if (str_starts_with($v_key, 'email-')) {
$append = $v[$v_key];
}
}
}

$amountArray[$k]['amount'] = $v['amount'];
if (!empty($v['discountAmount'])) {
$amountArray[$k]['amount'] -= $v['discountAmount'];
}

$amountArray[$k]['label'] = preg_replace('//', '', $v['amount_level']) . ' - ' . $append;
$participantDetails[$k]['info'] = ($v['first_name'] ?? '') . ' ' . ($v['last_name'] ?? '');
if (empty($v['first_name'])) {
$participantDetails[$k]['info'] = $append;
}

/*CRM-16320 */
$individual[$k]['totalAmtWithTax'] = $amountArray[$k]['amount'];
$individual[$k]['totalTaxAmt'] = $individualTaxAmount + $v['tax_amount'];
$this->_totalAmount = $this->_totalAmount + $amountArray[$k]['amount'];
if (!empty($v['is_primary'])) {
$this->set('primaryParticipantAmount', $amountArray[$k]['amount']);
}
}
}
return [$taxAmount, $participantDetails, $individual, $amountArray];
}

}

0 comments on commit 1bd9ce9

Please sign in to comment.