2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Represents a decimal field containing a currency amount.
|
2011-10-29 01:11:27 +02:00
|
|
|
* The currency class only supports single currencies. For multi-currency support, use {@link Money}
|
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
*
|
|
|
|
* Example definition via {@link DataObject::$db}:
|
|
|
|
* <code>
|
|
|
|
* static $db = array(
|
|
|
|
* "Price" => "Currency",
|
|
|
|
* "Tax" => "Currency(5)",
|
|
|
|
* );
|
|
|
|
* </code>
|
2009-04-02 19:17:04 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Currency extends Decimal {
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $currency_symbol = '$';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0) {
|
2011-10-29 01:02:11 +02:00
|
|
|
parent::__construct($name, $wholeSize, $decimalSize, $defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-10-15 01:58:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the number as a currency, eg “$1,000.00”.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2007-07-19 12:40:28 +02:00
|
|
|
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
|
2013-03-21 19:48:54 +01:00
|
|
|
$val = $this->config()->currency_symbol . number_format(abs($this->value), 2);
|
2008-03-17 03:10:23 +01:00
|
|
|
if($this->value < 0) return "($val)";
|
|
|
|
else return $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 01:58:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the number as a whole-number currency, eg “$1,000”.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Whole() {
|
2013-03-21 19:48:54 +01:00
|
|
|
$val = $this->config()->currency_symbol . number_format(abs($this->value), 0);
|
2008-03-17 03:10:23 +01:00
|
|
|
if($this->value < 0) return "($val)";
|
|
|
|
else return $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value, $record = null) {
|
2008-03-17 04:03:43 +01:00
|
|
|
$matches = null;
|
|
|
|
if(is_numeric($value)) {
|
|
|
|
$this->value = $value;
|
|
|
|
|
|
|
|
} else if(preg_match('/-?\$?[0-9,]+(.[0-9]+)?([Ee][0-9]+)?/', $value, $matches)) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$this->value = str_replace(array('$',',',$this->config()->currency_symbol),'',$matches[0]);
|
2008-03-17 04:03:43 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->value = 0;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-04-22 04:12:26 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @deprecated 3.2 Use the "Currency.currency_symbol" config setting instead
|
|
|
|
* @param [type] $value [description]
|
|
|
|
*/
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function setCurrencySymbol($value) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "Currency.currency_symbol" config setting instead');
|
2013-03-26 10:13:00 +01:00
|
|
|
Currency::config()->currency_symbol = $value;
|
2008-04-22 04:12:26 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|