Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20899 New refund form: format amount up to 2 decimal places #10687

Merged
merged 1 commit into from
Nov 3, 2017

Conversation

pradpnayak
Copy link
Contributor



@pradpnayak pradpnayak changed the title CRM-20899, formatted number upto 2 decimal place [ready for core team to review]CRM-20899, formatted number upto 2 decimal place Jul 18, 2017
@@ -185,7 +185,7 @@ public function setDefaultValues() {
}

if ($this->_refund) {
$defaults['total_amount'] = abs($this->_refund);
$defaults['total_amount'] = number_format(abs($this->_refund), 2);
}
Copy link
Contributor

@mattwire mattwire Jul 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certain currencies support more than 2 decimal places (@eileenmcnaughton: eg. bitcoin) so not sure we should be hardcoding 2 here. See https://github.com/civicrm/civicrm-core/pull/10641/files#diff-9dacd15500c58030b03a70b7501fa5e5R87 or CRM_Utils_Money::format with onlyNumber=TRUE might be more appropriate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the goal seems to be to have AT LEAST 2 decimal places , rather than 'not more than 2 decimal places. In this case it seems our primary goal is to pad it

(aside: That function you have mentioned looks nasty. ie. the function has been hacked to be 'two functions in one' -
it does something different if $onlyNumber is passed in which looks like a code smell to me)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton so for now, should we allow the current fix that is formatting refund money up to 2 decimal place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be using CRM_Utils_Money::format function shouldn't we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton See the first comment :-) that you commented on. number_format($var,2) will round to 2decimal places - so will CRM_Utils_Money::format. I do agree that CRM_Utils_Money::format is a bit of a messy function, but that should probably be cleaned up as a separate exercise. All money amounts that are formatted for display should really go through that function so we have one place for formatting. Note that I can't use it when I want to write more than 2dp to the database eg. (https://github.com/civicrm/civicrm-core/pull/10641/files#diff-9bb0aa4010a43713521df361cca72fe6R841). Though it might make sense in that link to replace my filter_var with another CRM_Utils_money function!

In this case, as the value is ONLY destined for display, I think CRM_Utils_Money::format(...onlyNumber=TRUE...) is the right function to use.

@monishdeb monishdeb changed the title [ready for core team to review]CRM-20899, formatted number upto 2 decimal place CRM-20899, formatted number upto 2 decimal place Aug 7, 2017
@@ -184,7 +184,7 @@ public function setDefaultValues() {
}

if ($this->_refund) {
$defaults['total_amount'] = abs($this->_refund);
$defaults['total_amount'] = CRM_Utils_Money::format(abs($this->_refund), NULL, NULL, TRUE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton @mattwire I've made the changes. It's working fine on my local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monishdeb I haven't looked at this in detail but I don't think you need the abs() in there?

Copy link
Member

@monishdeb monishdeb Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so but $this->_refund holds negative amount and eventually, it results to (50.12) for -50.12 value, which is how the money_format(..) deals with negative amount (see here) So we need to use abs().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with this, I do think we need to look at refactoring that CRM_Utils_Money::format function - it's too vague with what it returns and we get wierd issues like this in different locales! See https://issues.civicrm.org/jira/browse/CRM-16460 and https://issues.civicrm.org/jira/browse/CRM-20772. But I agree this PR is ready to merge @JoeMurray

@monishdeb
Copy link
Member

Jenkins test this please

@pradpnayak
Copy link
Contributor Author

QA'd, Number is formatted correctly

@mlutfy
Copy link
Member

mlutfy commented Nov 3, 2017

I tested this patch and it works for me. Merging.

@mlutfy mlutfy merged commit 0bfbea1 into civicrm:master Nov 3, 2017
@mlutfy mlutfy changed the title CRM-20899, formatted number upto 2 decimal place CRM-20899 New refund form: format amount up to 2 decimal places Nov 3, 2017
@monishdeb monishdeb deleted the CRM-20898 branch December 14, 2017 14:22
sluc23 pushed a commit to ixiam/civicrm-core that referenced this pull request Jan 10, 2018
CRM-20899, formatted number upto 2 decimal place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants