Merge pull request #4806 from civicrm/4.5
[civicrm-core.git] / CRM / Utils / Money.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Money utilties
38 */
39 class CRM_Utils_Money {
40 static $_currencySymbols = NULL;
41
42 /**
43 * Format a monetary string
44 *
45 * Format a monetary string basing on the amount provided,
46 * ISO currency code provided and a format string consisting of:
47 *
48 * %a - the formatted amount
49 * %C - the currency ISO code (e.g., 'USD') if provided
50 * %c - the currency symbol (e.g., '$') if available
51 *
52 * @param float $amount the monetary amount to display (1234.56)
53 * @param string $currency the three-letter ISO currency code ('USD')
54 * @param string $format the desired currency format
55 * @param bool $onlyNumber
56 * @param string $valueFormat the desired monetary value display format (e.g. '%!i')
57 *
58 * @return string formatted monetary string
59 *
60 * @static
61 */
62 static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE, $valueFormat = NULL) {
63
64 if (CRM_Utils_System::isNull($amount)) {
65 return '';
66 }
67
68 $config = CRM_Core_Config::singleton();
69
70 if (!$format) {
71 $format = $config->moneyformat;
72 }
73
74 if (!$valueFormat) {
75 $valueFormat = $config->moneyvalueformat;
76 }
77
78 if ($onlyNumber) {
79 // money_format() exists only in certain PHP install (CRM-650)
80 if (is_numeric($amount) and function_exists('money_format')) {
81 $amount = money_format($valueFormat, $amount);
82 }
83 return $amount;
84 }
85
86 if (!self::$_currencySymbols) {
87 self::$_currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('keyColumn' => 'name', 'labelColumn' => 'symbol'));
88 }
89
90 if (!$currency) {
91 $currency = $config->defaultCurrency;
92 }
93
94 // money_format() exists only in certain PHP install (CRM-650)
95 // setlocale() affects native gettext (CRM-11054, CRM-9976)
96 if (is_numeric($amount) && function_exists('money_format')) {
97 $lc = setlocale(LC_MONETARY, 0);
98 setlocale(LC_MONETARY, 'en_US.utf8', 'en_US', 'en_US.utf8', 'en_US', 'C');
99 $amount = money_format($valueFormat, $amount);
100 setlocale(LC_MONETARY, $lc);
101 }
102
103 $rep = array(
104 ',' => $config->monetaryThousandSeparator,
105 '.' => $config->monetaryDecimalPoint,
106 );
107
108 // If it contains tags, means that HTML was passed and the
109 // amount is already converted properly,
110 // so don't mess with it again.
111 if (strpos($amount, '<') === FALSE) {
112 $amount = strtr($amount, $rep);
113 }
114
115 $replacements = array(
116 '%a' => $amount,
117 '%C' => $currency,
118 '%c' => CRM_Utils_Array::value($currency, self::$_currencySymbols, $currency),
119 );
120 return strtr($format, $replacements);
121 }
122 }
123