Skip to content

Commit

Permalink
dev/core#5079 Handle money localization before any
Browse files Browse the repository at this point in the history
thing else in formRule
  • Loading branch information
eileenmcnaughton committed Mar 18, 2024
1 parent 980f4d3 commit 3cae337
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 8 additions & 6 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,16 @@ private function buildRecur(): void {
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $self) {
foreach ($fields as $key => $field) {
$fields[$key] = $self->getUnLocalizedSubmittedValue($key, $field);
}
$self->resetOrder($fields);
$errors = array_filter(['auto_renew' => $self->getAutoRenewError($fields)]);
// @todo - should just be $this->getOrder()->getTotalAmount()
$amount = $self->computeAmount($fields, $self->_values);

if ((!empty($fields['selectMembership']) &&
$fields['selectMembership'] != 'no_thanks'
$fields['selectMembership'] !== 'no_thanks'
) ||
(!empty($fields['priceSetId']) &&
$self->_useForMember
Expand Down Expand Up @@ -862,7 +865,7 @@ public static function formRule($fields, $files, $self) {
$otherAmount = $priceField->id;
}
elseif (!empty($fields["price_{$priceField->id}"])) {
$otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]);
$otherAmountVal = $fields["price_{$priceField->id}"];
$min = $self->_values['min_amount'] ?? NULL;
$max = $self->_values['max_amount'] ?? NULL;
if ($min && $otherAmountVal < $min) {
Expand Down Expand Up @@ -1131,21 +1134,20 @@ public static function formRule($fields, $files, $self) {
*/
private function computeAmount($params, $formValues) {
$amount = 0;
// First clean up the other amount field if present.
if (isset($params['amount_other'])) {
$params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']);
}

if (($params['amount'] ?? NULL) == 'amount_other_radio' || !empty($params['amount_other'])) {
// @todo - probably unreachable - field would be (e.) price_12 now....
$amount = $params['amount_other'];
}
elseif (!empty($params['pledge_amount'])) {
foreach ($params['pledge_amount'] as $paymentId => $dontCare) {
// @todo - why would this be a good thing? Is it reachable.
$amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount');
}
}
else {
if (!empty($formValues['amount'])) {
// @todo - probably unreachable.
$amountID = $params['amount'] ?? NULL;

if ($amountID) {
Expand Down
5 changes: 3 additions & 2 deletions CRM/Financial/BAO/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,12 @@ protected function calculateLineItems(): array {
elseif ($this->getPriceFieldMetadata($lineItem['price_field_id'])['name'] === 'other_amount') {
// Other amount is a front end user entered form. It is reasonable to think it would be tax inclusive.
$lineItem['line_total_inclusive'] = $lineItem['line_total'];
$lineItem['line_total'] = $lineItem['line_total_inclusive'] / (1 + ($lineItem['tax_rate'] / 100));
$lineItem['line_total'] = $lineItem['line_total_inclusive'] ? $lineItem['line_total_inclusive'] / (1 + ($lineItem['tax_rate'] / 100)) : 0;
$lineItem['tax_amount'] = round($lineItem['line_total_inclusive'] - $lineItem['line_total'], 2);
// Make sure they still add up to each other afer the rounding.
$lineItem['line_total'] = $lineItem['line_total_inclusive'] - $lineItem['tax_amount'];
$lineItem['unit_price'] = $lineItem['line_total'] / $lineItem['qty'];
$lineItem['qty'] = 1;
$lineItem['unit_price'] = $lineItem['line_total'];

}
elseif ($taxRate) {
Expand Down

0 comments on commit 3cae337

Please sign in to comment.