mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-28 20:29:15 +02:00
BUGFIX Fixed Money->writeToManipulation() to correctly overwrite with NULL values
ENHANCEMENT Added Money->NiceWithName() ENHANCEMENT Changed Money->NiceWithShortname() to use Zend_Currency options BUGFIX Setting isChanged flag on Money individual setters MINOR Added Money->__toString() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77548 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
7cb259f051
commit
a2a507f73c
@ -81,8 +81,17 @@ class Money extends DBField implements CompositeDBField {
|
||||
}
|
||||
|
||||
function writeToManipulation(&$manipulation) {
|
||||
$manipulation['fields'][$this->name.'Currency'] = $this->prepValueForDB($this->getCurrency());
|
||||
$manipulation['fields'][$this->name.'Amount'] = $this->getAmount();
|
||||
if($this->getCurrency()) {
|
||||
$manipulation['fields'][$this->name.'Currency'] = $this->prepValueForDB($this->getCurrency());
|
||||
} else {
|
||||
$manipulation['fields'][$this->name.'Currency'] = DBField::create('Varchar', $this->getCurrency())->nullValue();
|
||||
}
|
||||
|
||||
if($this->getAmount()) {
|
||||
$manipulation['fields'][$this->name.'Amount'] = $this->getAmount();
|
||||
} else {
|
||||
$manipulation['fields'][$this->name.'Amount'] = DBField::create('Decimal', $this->getAmount())->nullValue();
|
||||
}
|
||||
}
|
||||
|
||||
function addToQuery(&$query) {
|
||||
@ -105,31 +114,40 @@ class Money extends DBField implements CompositeDBField {
|
||||
} else if (is_array($value)) {
|
||||
if (array_key_exists('Currency', $value)) {
|
||||
$this->setCurrency($value['Currency']);
|
||||
$this->isChanged = true;
|
||||
}
|
||||
if (array_key_exists('Amount', $value)) {
|
||||
$this->setAmount($value['Amount']);
|
||||
$this->isChanged = true;
|
||||
}
|
||||
} else {
|
||||
user_error('Invalid value in Money->setValue()', E_USER_ERROR);
|
||||
// @todo Allow to reset a money value by passing in NULL
|
||||
//user_error('Invalid value in Money->setValue()', E_USER_ERROR);
|
||||
}
|
||||
|
||||
$this->isChanged = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function Nice($options = array()) {
|
||||
return $this->currencyLib->toCurrency($this->getAmount(), $options);
|
||||
$amount = $this->getAmount();
|
||||
return (is_numeric($amount)) ? $this->currencyLib->toCurrency($amount, $options) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function NiceWithShortname($options = array()){
|
||||
$shortName = $this->getShortName();
|
||||
$symbol = $this->getSymbol();
|
||||
return $shortName."(".$symbol.")"." ".$this->getAmount();
|
||||
$options['display'] = Zend_Currency::USE_SHORTNAME;
|
||||
return $this->Nice($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function NiceWithName($options = array()){
|
||||
$options['display'] = Zend_Currency::USE_NAME;
|
||||
return $this->Nice($options);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,6 +162,7 @@ class Money extends DBField implements CompositeDBField {
|
||||
*/
|
||||
function setCurrency($currency) {
|
||||
$this->currency = $currency;
|
||||
$this->isChanged = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -160,6 +179,7 @@ class Money extends DBField implements CompositeDBField {
|
||||
*/
|
||||
function setAmount($amount) {
|
||||
$this->amount = (float)$amount;
|
||||
$this->isChanged = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -232,13 +252,6 @@ class Money extends DBField implements CompositeDBField {
|
||||
function getAllowedCurrencies() {
|
||||
return $this->allowedCurrencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Implement this
|
||||
*/
|
||||
function toString() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a CompositeField instance used as a default
|
||||
@ -272,5 +285,15 @@ class Money extends DBField implements CompositeDBField {
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* For backwards compatibility reasons
|
||||
* (mainly with ecommerce module),
|
||||
* this returns the amount value of the field,
|
||||
* rather than a {@link Nice()} formatting.
|
||||
*/
|
||||
function __toString() {
|
||||
return $this->getAmount();
|
||||
}
|
||||
}
|
||||
?>
|
@ -15,6 +15,27 @@ class MoneyTest extends SapphireTest {
|
||||
|
||||
static $fixture_file = 'sapphire/tests/model/MoneyTest.yml';
|
||||
|
||||
function testCanOverwriteSettersWithNull() {
|
||||
$obj = new MoneyTest_DataObject();
|
||||
|
||||
$m1 = new Money();
|
||||
$m1->setAmount(987.65);
|
||||
$m1->setCurrency('USD');
|
||||
$obj->MyMoney = $m1;
|
||||
$obj->write();
|
||||
|
||||
$m2 = new Money();
|
||||
$m2->setAmount(null);
|
||||
$m2->setCurrency(null);
|
||||
$obj->MyMoney = $m2;
|
||||
$obj->write();
|
||||
|
||||
$moneyTest = DataObject::get_by_id('MoneyTest_DataObject',$obj->ID);
|
||||
$this->assertTrue($moneyTest instanceof MoneyTest_DataObject);
|
||||
$this->assertEquals('', $moneyTest->MyMoneyCurrency);
|
||||
$this->assertEquals(0.0000, $moneyTest->MyMoneyAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a Money object to the database, then re-read it to ensure it
|
||||
* is re-read properly.
|
||||
|
Loading…
Reference in New Issue
Block a user