mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Fix #1087
This commit is contained in:
@@ -45,8 +45,10 @@ class Amount implements ConverterInterface
|
|||||||
if (null === $value) {
|
if (null === $value) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
$value = strval($value);
|
|
||||||
Log::debug(sprintf('Start with amount "%s"', $value));
|
Log::debug(sprintf('Start with amount "%s"', $value));
|
||||||
|
$original = $value;
|
||||||
|
$value = strval($value);
|
||||||
|
$value = $this->stripAmount($value);
|
||||||
$len = strlen($value);
|
$len = strlen($value);
|
||||||
$decimalPosition = $len - 3;
|
$decimalPosition = $len - 3;
|
||||||
$altPosition = $len - 2;
|
$altPosition = $len - 2;
|
||||||
@@ -81,27 +83,43 @@ class Amount implements ConverterInterface
|
|||||||
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
|
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
|
||||||
if ('.' === $decimal) {
|
if ('.' === $decimal) {
|
||||||
$search = [',', ' '];
|
$search = [',', ' '];
|
||||||
$oldValue = $value;
|
|
||||||
$value = str_replace($search, '', $value);
|
$value = str_replace($search, '', $value);
|
||||||
Log::debug(sprintf('Converted amount from "%s" to "%s".', $oldValue, $value));
|
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
|
||||||
}
|
}
|
||||||
if (',' === $decimal) {
|
if (',' === $decimal) {
|
||||||
$search = ['.', ' '];
|
$search = ['.', ' '];
|
||||||
$oldValue = $value;
|
|
||||||
$value = str_replace($search, '', $value);
|
$value = str_replace($search, '', $value);
|
||||||
$value = str_replace(',', '.', $value);
|
$value = str_replace(',', '.', $value);
|
||||||
Log::debug(sprintf('Converted amount from "%s" to "%s".', $oldValue, $value));
|
Log::debug(sprintf('Converted amount from "%s" to "%s".', $original, $value));
|
||||||
}
|
}
|
||||||
if (null === $decimal) {
|
if (null === $decimal) {
|
||||||
// replace all:
|
// replace all:
|
||||||
$search = ['.', ' ', ','];
|
$search = ['.', ' ', ','];
|
||||||
$oldValue = $value;
|
|
||||||
$value = str_replace($search, '', $value);
|
$value = str_replace($search, '', $value);
|
||||||
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $oldValue, $value));
|
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));
|
||||||
}
|
}
|
||||||
|
|
||||||
$number = strval(number_format(round(floatval($value), 12), 12, '.', ''));
|
$number = strval(number_format(round(floatval($value), 12), 12, '.', ''));
|
||||||
|
|
||||||
return $number;
|
return $number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function stripAmount(string $value): string
|
||||||
|
{
|
||||||
|
$str = preg_replace('/[^\-\(\)\.\,0-9 ]/', '', $value);
|
||||||
|
$len = strlen($str);
|
||||||
|
if ($str{0} === '(' && $str{$len - 1} === ')') {
|
||||||
|
$str = '-' . substr($str, 1, ($len - 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::debug(sprintf('Stripped "%s" away to "%s"', $value, $str));
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -150,6 +150,11 @@ class AmountTest extends TestCase
|
|||||||
'0.115' => '0.115',
|
'0.115' => '0.115',
|
||||||
'-0.115' => '-0.115',
|
'-0.115' => '-0.115',
|
||||||
'1.33' => '1.33',
|
'1.33' => '1.33',
|
||||||
|
'$1.23' => '1.23',
|
||||||
|
'€1,44' => '1.44',
|
||||||
|
'(33.52)' => '-33.52',
|
||||||
|
'€(63.12)' => '-63.12',
|
||||||
|
'($182.77)' => '-182.77',
|
||||||
];
|
];
|
||||||
foreach ($values as $value => $expected) {
|
foreach ($values as $value => $expected) {
|
||||||
$converter = new Amount;
|
$converter = new Amount;
|
||||||
|
Reference in New Issue
Block a user