Merge pull request #6126 from rohankatkar/CRM-16777
[civicrm-core.git] / CRM / Utils / Money.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Money utilties
38 */
39class CRM_Utils_Money {
40 static $_currencySymbols = NULL;
41
42 /**
fe482240 43 * Format a monetary string.
6a488035
TO
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 *
77855840
TO
52 * @param float $amount
53 * The monetary amount to display (1234.56).
54 * @param string $currency
55 * The three-letter ISO currency code ('USD').
56 * @param string $format
57 * The desired currency format.
f4aaa82a 58 * @param bool $onlyNumber
77855840
TO
59 * @param string $valueFormat
60 * The desired monetary value display format (e.g. '%!i').
6a488035 61 *
a6c01b45
CW
62 * @return string
63 * formatted monetary string
6a488035 64 *
6a488035 65 */
00be9182 66 public static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE, $valueFormat = NULL) {
6a488035
TO
67
68 if (CRM_Utils_System::isNull($amount)) {
69 return '';
70 }
71
72 $config = CRM_Core_Config::singleton();
73
74 if (!$format) {
75 $format = $config->moneyformat;
76 }
f4aaa82a 77
ec4da14d
DG
78 if (!$valueFormat) {
79 $valueFormat = $config->moneyvalueformat;
80 }
f4aaa82a 81
6a488035
TO
82 if ($onlyNumber) {
83 // money_format() exists only in certain PHP install (CRM-650)
84 if (is_numeric($amount) and function_exists('money_format')) {
ec4da14d 85 $amount = money_format($valueFormat, $amount);
6a488035
TO
86 }
87 return $amount;
88 }
89
90 if (!self::$_currencySymbols) {
353ffa53
TO
91 self::$_currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array(
92 'keyColumn' => 'name',
7c550ca0 93 'labelColumn' => 'symbol',
353ffa53 94 ));
6a488035
TO
95 }
96
97 if (!$currency) {
98 $currency = $config->defaultCurrency;
99 }
100
6a488035
TO
101 // money_format() exists only in certain PHP install (CRM-650)
102 // setlocale() affects native gettext (CRM-11054, CRM-9976)
103 if (is_numeric($amount) && function_exists('money_format')) {
104 $lc = setlocale(LC_MONETARY, 0);
105 setlocale(LC_MONETARY, 'en_US.utf8', 'en_US', 'en_US.utf8', 'en_US', 'C');
ec4da14d 106 $amount = money_format($valueFormat, $amount);
6a488035
TO
107 setlocale(LC_MONETARY, $lc);
108 }
109
110 $rep = array(
111 ',' => $config->monetaryThousandSeparator,
112 '.' => $config->monetaryDecimalPoint,
113 );
114
115 // If it contains tags, means that HTML was passed and the
116 // amount is already converted properly,
117 // so don't mess with it again.
118 if (strpos($amount, '<') === FALSE) {
119 $amount = strtr($amount, $rep);
120 }
121
122 $replacements = array(
123 '%a' => $amount,
124 '%C' => $currency,
125 '%c' => CRM_Utils_Array::value($currency, self::$_currencySymbols, $currency),
126 );
127 return strtr($format, $replacements);
128 }
96025800 129
6a488035 130}