Attempt to cover all cases in amount converter, this fixes #840

This commit is contained in:
James Cole
2017-10-02 16:25:48 +02:00
parent 03f33c5e7e
commit 2dfe2ddaf3
4 changed files with 152 additions and 7 deletions

View File

@@ -36,13 +36,14 @@ class Amount implements ConverterInterface
*/
public function convert($value): string
{
if(is_null($value)) {
if (is_null($value)) {
return '0';
}
$value = strval($value);
Log::debug(sprintf('Start with amount "%s"', $value));
$len = strlen($value);
$decimalPosition = $len - 3;
$altPosition = $len - 2;
$decimal = null;
if (($len > 2 && $value{$decimalPosition} === '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
@@ -53,6 +54,11 @@ class Amount implements ConverterInterface
$decimal = ',';
Log::debug(sprintf('Decimal character in "%s" seems to be a comma.', $value));
}
// decimal character is null? find out if "0.1" or ".1" or "0,1" or ",1"
if ($len > 1 && ($value{$altPosition} === '.' || $value{$altPosition} === ',')) {
$decimal = $value{$altPosition};
Log::debug(sprintf('Alternate search resulted in "%s" for decimal sign.', $decimal));
}
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
if ($decimal === '.') {

View File

@@ -32,13 +32,13 @@ use Watson\Validating\ValidatingTrait;
* @property int $foreign_currency_dp
*
* @property int $account_id
* @property-read string $account_name
* @property string $account_name
* @property string $account_iban
* @property string $account_number
* @property string $account_bic
* @property string $account_currency_code
*
* @property-read int $opposing_account_id
* @property int $opposing_account_id
* @property string $opposing_account_name
* @property string $opposing_account_iban
* @property string $opposing_account_number
@@ -46,10 +46,10 @@ use Watson\Validating\ValidatingTrait;
* @property string $opposing_currency_code
*
*
* @property-read int $transaction_budget_id
* @property-read string $transaction_budget_name
* @property-read int $transaction_journal_budget_id
* @property-read string $transaction_journal_budget_name
* @property int $transaction_budget_id
* @property string $transaction_budget_name
* @property int $transaction_journal_budget_id
* @property string $transaction_journal_budget_name
*
* @property-read int $transaction_category_id
* @property-read string $transaction_category_name