2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
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}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
*
|
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
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBCurrency extends DBDecimal {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $currency_symbol = '$';
|
2014-08-15 08:53:05 +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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
2008-03-17 04:03:43 +01:00
|
|
|
$matches = null;
|
|
|
|
if(is_numeric($value)) {
|
|
|
|
$this->value = $value;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
} 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]);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 04:03:43 +01:00
|
|
|
} else {
|
|
|
|
$this->value = 0;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|