Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Improve amount conversion code.
James Colee2ec9ca5fbdf
diff --git a/app/Import/Converter/Amount.php b/app/Import/Converter/Amount.phpindex c9317f2045..070842a050 100644--- a/app/Import/Converter/Amount.php+++ b/app/Import/Converter/Amount.php@@ -98,8 +98,31 @@ class Amount implements ConverterInterface$value = str_replace($search, '', $value);Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));}+ if ($value{0} === '.') {+ $value = '0' . $value;+ }++ if (is_numeric($value)) {+ Log::debug(sprintf('Final NUMERIC value is: "%s"', $value));++ return $value;+ }+ Log::debug(sprintf('Final value is: "%s"', $value));+ $formatted = sprintf('%01.12f', $value);+ Log::debug(sprintf('Is formatted to : "%s"', $formatted));++ return $formatted;+ }++ private function bcround($number, $scale = 0)+ {+ $fix = "5";+ for ($i = 0; $i < $scale; $i++) {+ $fix = "0$fix";+ }+ $number = bcadd($number, "0.$fix", $scale + 1);- return number_format(round((float)$value, 12), 12, '.', '');+ return bcdiv($number, "1.0", $scale);}/**diff --git a/tests/Unit/Import/Converter/AmountCreditTest.php b/tests/Unit/Import/Converter/AmountCreditTest.phpindex d6161fc30e..7bcde6b83a 100644--- a/tests/Unit/Import/Converter/AmountCreditTest.php+++ b/tests/Unit/Import/Converter/AmountCreditTest.php@@ -36,16 +36,17 @@ class AmountCreditTest extends TestCasepublic function testConvert(): void{$values = [+'0' => '0',- '0.0' => '0',+ '0.0' => '0.0','0.1' => '0.1','.2' => '0.2','0.01' => '0.01','1' => '1',- '1.0' => '1',+ '1.0' => '1.0','1.1' => '1.1','1.12' => '1.12',- '1.10' => '1.1',+ '1.10' => '1.10','12' => '12','12.3' => '12.3','12.34' => '12.34',@@ -62,14 +63,14 @@ class AmountCreditTest extends TestCase'1,234.5' => '1234.5','1,234.56' => '1234.56','123,456,789' => '123456789',- '0,0' => '0',+ '0,0' => '0.0','0,1' => '0.1',',2' => '0.2','0,01' => '0.01',- '1,0' => '1',+ '1,0' => '1.0','1,1' => '1.1','1,12' => '1.12',- '1,10' => '1.1',+ '1,10' => '1.10','12,3' => '12.3','12,34' => '12.34','123,4' => '123.4',@@ -82,17 +83,17 @@ class AmountCreditTest extends TestCase'1.234,5' => '1234.5','1.234,56' => '1234.56',// many decimals- '2.00' => '2',- '3.000' => '3',- '4.0000' => '4',- '5.000' => '5',- '6.0000' => '6',- '7.200' => '7.2',- '8.2000' => '8.2',- '9.330' => '9.33',- '10.3300' => '10.33',+ '2.00' => '2.00',+ '3.000' => '3.000',+ '4.0000' => '4.0000',+ '5.000' => '5.000',+ '6.0000' => '6.0000',+ '7.200' => '7.200',+ '8.2000' => '8.2000',+ '9.330' => '9.330',+ '10.3300' => '10.3300','11.444' => '11.444',- '12.4440' => '12.444',+ '12.4440' => '12.4440','13.5555' => '13.5555','14.45678' => '14.45678','15.456789' => '15.456789',@@ -103,18 +104,19 @@ class AmountCreditTest extends TestCase'20.16334567898' => '20.16334567898','21.16364567898' => '21.16364567898','22.163644567898' => '22.163644567898',+ '22.1636445670069' => '22.1636445670069',// many decimals, mixed, large numbers- '63522.00' => '63522',- '63523.000' => '63523',- '63524.0000' => '63524',- '63525.000' => '63525',- '63526.0000' => '63526',- '63527.200' => '63527.2',- '63528.2000' => '63528.2',- '63529.330' => '63529.33',- '635210.3300' => '635210.33',+ '63522.00' => '63522.00',+ '63523.000' => '63523.000',+ '63524.0000' => '63524.0000',+ '63525.000' => '63525.000',+ '63526.0000' => '63526.0000',+ '63527.200' => '63527.200',+ '63528.2000' => '63528.2000',+ '63529.330' => '63529.330',+ '635210.3300' => '635210.3300','635211.444' => '635211.444',- '635212.4440' => '635212.444',+ '635212.4440' => '635212.4440','635213.5555' => '635213.5555','635214.45678' => '635214.45678','635215.456789' => '635215.456789',@@ -126,17 +128,17 @@ class AmountCreditTest extends TestCase'635221.16364567898' => '635221.16364567898','635222.163644567898' => '635222.163644567898',// many decimals, mixed, also mixed thousands separators- '63 522.00' => '63522',- '63 523.000' => '63523',- '63,524.0000' => '63524',- '63 525.000' => '63525',- '63,526.0000' => '63526',- '63 527.200' => '63527.2',- '63 528.2000' => '63528.2',- '63 529.330' => '63529.33',- '63,5210.3300' => '635210.33',+ '63 522.00' => '63522.00',+ '63 523.000' => '63523.000',+ '63,524.0000' => '63524.0000',+ '63 525.000' => '63525.000',+ '63,526.0000' => '63526.0000',+ '63 527.200' => '63527.200',+ '63 528.2000' => '63528.2000',+ '63 529.330' => '63529.330',+ '63,5210.3300' => '635210.3300','63,5211.444' => '635211.444',- '63 5212.4440' => '635212.444',+ '63 5212.4440' => '635212.4440','163 5219.1634567898' => '1635219.1634567898','444 163 5219.1634567898' => '4441635219.1634567898','-0.34918323' => '0.34918323',@@ -150,6 +152,19 @@ class AmountCreditTest extends TestCase'0.115' => '0.115','-0.115' => '0.115','1.33' => '1.33',+ '$1.23' => '1.23',+ '€1,44' => '1.44',+ '(33.52)' => '33.52',+ '€(63.12)' => '63.12',+ '($182.77)' => '182.77',++ // double minus because why the hell not+ '--0.03881677' => '0.03881677',+ '--0.33' => '0.33',+ '--$1.23' => '1.23',+ '--63 5212.4440' => '635212.4440',+ '--,2' => '0.2',+];foreach ($values as $value => $expected) {$converter = new AmountCredit;diff --git a/tests/Unit/Import/Converter/AmountDebitTest.php b/tests/Unit/Import/Converter/AmountDebitTest.phpindex 815678c5ca..c75a998ccf 100644--- a/tests/Unit/Import/Converter/AmountDebitTest.php+++ b/tests/Unit/Import/Converter/AmountDebitTest.php@@ -36,16 +36,17 @@ class AmountDebitTest extends TestCasepublic function testConvert(): void{$values = [+'0' => '0',- '0.0' => '0',+ '0.0' => '0.0','0.1' => '-0.1','.2' => '-0.2','0.01' => '-0.01','1' => '-1',- '1.0' => '-1',+ '1.0' => '-1.0','1.1' => '-1.1','1.12' => '-1.12',- '1.10' => '-1.1',+ '1.10' => '-1.10','12' => '-12','12.3' => '-12.3','12.34' => '-12.34',@@ -62,14 +63,14 @@ class AmountDebitTest extends TestCase'1,234.5' => '-1234.5','1,234.56' => '-1234.56','123,456,789' => '-123456789',- '0,0' => '0',+ '0,0' => '0.0','0,1' => '-0.1',',2' => '-0.2','0,01' => '-0.01',- '1,0' => '-1',+ '1,0' => '-1.0','1,1' => '-1.1','1,12' => '-1.12',- '1,10' => '-1.1',+ '1,10' => '-1.10','12,3' => '-12.3','12,34' => '-12.34','123,4' => '-123.4',@@ -82,17 +83,17 @@ class AmountDebitTest extends TestCase'1.234,5' => '-1234.5','1.234,56' => '-1234.56',// many decimals- '2.00' => '-2',- '3.000' => '-3',- '4.0000' => '-4',- '5.000' => '-5',- '6.0000' => '-6',- '7.200' => '-7.2',- '8.2000' => '-8.2',- '9.330' => '-9.33',- '10.3300' => '-10.33',+ '2.00' => '-2.00',+ '3.000' => '-3.000',+ '4.0000' => '-4.0000',+ '5.000' => '-5.000',+ '6.0000' => '-6.0000',+ '7.200' => '-7.200',+ '8.2000' => '-8.2000',+ '9.330' => '-9.330',+ '10.3300' => '-10.3300','11.444' => '-11.444',- '12.4440' => '-12.444',+ '12.4440' => '-12.4440','13.5555' => '-13.5555','14.45678' => '-14.45678','15.456789' => '-15.456789',@@ -103,18 +104,19 @@ class AmountDebitTest extends TestCase'20.16334567898' => '-20.16334567898','21.16364567898' => '-21.16364567898','22.163644567898' => '-22.163644567898',+ '22.1636445670069' => '-22.163644567006',// many decimals, mixed, large numbers- '63522.00' => '-63522',- '63523.000' => '-63523',- '63524.0000' => '-63524',- '63525.000' => '-63525',- '63526.0000' => '-63526',- '63527.200' => '-63527.2',- '63528.2000' => '-63528.2',- '63529.330' => '-63529.33',- '635210.3300' => '-635210.33',+ '63522.00' => '-63522.00',+ '63523.000' => '-63523.000',+ '63524.0000' => '-63524.0000',+ '63525.000' => '-63525.000',+ '63526.0000' => '-63526.0000',+ '63527.200' => '-63527.200',+ '63528.2000' => '-63528.2000',+ '63529.330' => '-63529.330',+ '635210.3300' => '-635210.3300','635211.444' => '-635211.444',- '635212.4440' => '-635212.444',+ '635212.4440' => '-635212.4440','635213.5555' => '-635213.5555','635214.45678' => '-635214.45678','635215.456789' => '-635215.456789',@@ -126,17 +128,17 @@ class AmountDebitTest extends TestCase'635221.16364567898' => '-635221.16364567898','635222.163644567898' => '-635222.163644567898',// many decimals, mixed, also mixed thousands separators- '63 522.00' => '-63522',- '63 523.000' => '-63523',- '63,524.0000' => '-63524',- '63 525.000' => '-63525',- '63,526.0000' => '-63526',- '63 527.200' => '-63527.2',- '63 528.2000' => '-63528.2',- '63 529.330' => '-63529.33',- '63,5210.3300' => '-635210.33',+ '63 522.00' => '-63522.00',+ '63 523.000' => '-63523.000',+ '63,524.0000' => '-63524.0000',+ '63 525.000' => '-63525.000',+ '63,526.0000' => '-63526.0000',+ '63 527.200' => '-63527.200',+ '63 528.2000' => '-63528.2000',+ '63 529.330' => '-63529.330',+ '63,5210.3300' => '-635210.3300','63,5211.444' => '-635211.444',- '63 5212.4440' => '-635212.444',+ '63 5212.4440' => '-635212.4440','163 5219.1634567898' => '-1635219.1634567898','444 163 5219.1634567898' => '-4441635219.1634567898','-0.34918323' => '-0.34918323',@@ -150,11 +152,23 @@ class AmountDebitTest extends TestCase'0.115' => '-0.115','-0.115' => '-0.115','1.33' => '-1.33',+ '$1.23' => '-1.23',+ '€1,44' => '-1.44',+ '(33.52)' => '-33.52',+ '€(63.12)' => '-63.12',+ '($182.77)' => '-182.77',++ // double minus because why the hell not+ '--0.03881677' => '-0.03881677',+ '--0.33' => '-0.33',+ '--$1.23' => '-1.23',+ '--63 5212.4440' => '-635212.4440',+ '--,2' => '-0.2',];foreach ($values as $value => $expected) {$converter = new AmountDebit;$result = $converter->convert($value);- //$this->assertEquals($expected, $result, sprintf('The original value was %s', $value));+ $this->assertEquals($expected, $result, sprintf('The original value was %s', $value));}}diff --git a/tests/Unit/Import/Converter/AmountTest.php b/tests/Unit/Import/Converter/AmountTest.phpindex 8211f2539a..cb78114cf7 100644--- a/tests/Unit/Import/Converter/AmountTest.php+++ b/tests/Unit/Import/Converter/AmountTest.php@@ -38,15 +38,15 @@ class AmountTest extends TestCase{$values = ['0' => '0',- '0.0' => '0',+ '0.0' => '0.0','0.1' => '0.1','.2' => '0.2','0.01' => '0.01','1' => '1',- '1.0' => '1',+ '1.0' => '1.0','1.1' => '1.1','1.12' => '1.12',- '1.10' => '1.1',+ '1.10' => '1.10','12' => '12','12.3' => '12.3','12.34' => '12.34',@@ -63,14 +63,14 @@ class AmountTest extends TestCase'1,234.5' => '1234.5','1,234.56' => '1234.56','123,456,789' => '123456789',- '0,0' => '0',+ '0,0' => '0.0','0,1' => '0.1',',2' => '0.2','0,01' => '0.01',- '1,0' => '1',+ '1,0' => '1.0','1,1' => '1.1','1,12' => '1.12',- '1,10' => '1.1',+ '1,10' => '1.10','12,3' => '12.3','12,34' => '12.34','123,4' => '123.4',@@ -83,17 +83,17 @@ class AmountTest extends TestCase'1.234,5' => '1234.5','1.234,56' => '1234.56',// many decimals- '2.00' => '2',- '3.000' => '3',- '4.0000' => '4',- '5.000' => '5',- '6.0000' => '6',- '7.200' => '7.2',- '8.2000' => '8.2',- '9.330' => '9.33',- '10.3300' => '10.33',+ '2.00' => '2.00',+ '3.000' => '3.000',+ '4.0000' => '4.0000',+ '5.000' => '5.000',+ '6.0000' => '6.0000',+ '7.200' => '7.200',+ '8.2000' => '8.2000',+ '9.330' => '9.330',+ '10.3300' => '10.3300','11.444' => '11.444',- '12.4440' => '12.444',+ '12.4440' => '12.4440','13.5555' => '13.5555','14.45678' => '14.45678','15.456789' => '15.456789',@@ -104,18 +104,19 @@ class AmountTest extends TestCase'20.16334567898' => '20.16334567898','21.16364567898' => '21.16364567898','22.163644567898' => '22.163644567898',+ '22.1636445670069' => '22.1636445670069',// many decimals, mixed, large numbers- '63522.00' => '63522',- '63523.000' => '63523',- '63524.0000' => '63524',- '63525.000' => '63525',- '63526.0000' => '63526',- '63527.200' => '63527.2',- '63528.2000' => '63528.2',- '63529.330' => '63529.33',- '635210.3300' => '635210.33',+ '63522.00' => '63522.00',+ '63523.000' => '63523.000',+ '63524.0000' => '63524.0000',+ '63525.000' => '63525.000',+ '63526.0000' => '63526.0000',+ '63527.200' => '63527.200',+ '63528.2000' => '63528.2000',+ '63529.330' => '63529.330',+ '635210.3300' => '635210.3300','635211.444' => '635211.444',- '635212.4440' => '635212.444',+ '635212.4440' => '635212.4440','635213.5555' => '635213.5555','635214.45678' => '635214.45678','635215.456789' => '635215.456789',@@ -127,17 +128,17 @@ class AmountTest extends TestCase'635221.16364567898' => '635221.16364567898','635222.163644567898' => '635222.163644567898',// many decimals, mixed, also mixed thousands separators- '63 522.00' => '63522',- '63 523.000' => '63523',- '63,524.0000' => '63524',- '63 525.000' => '63525',- '63,526.0000' => '63526',- '63 527.200' => '63527.2',- '63 528.2000' => '63528.2',- '63 529.330' => '63529.33',- '63,5210.3300' => '635210.33',+ '63 522.00' => '63522.00',+ '63 523.000' => '63523.000',+ '63,524.0000' => '63524.0000',+ '63 525.000' => '63525.000',+ '63,526.0000' => '63526.0000',+ '63 527.200' => '63527.200',+ '63 528.2000' => '63528.2000',+ '63 529.330' => '63529.330',+ '63,5210.3300' => '635210.3300','63,5211.444' => '635211.444',- '63 5212.4440' => '635212.444',+ '63 5212.4440' => '635212.4440','163 5219.1634567898' => '1635219.1634567898','444 163 5219.1634567898' => '4441635219.1634567898','-0.34918323' => '-0.34918323',@@ -161,13 +162,13 @@ class AmountTest extends TestCase'--0.03881677' => '0.03881677','--0.33' => '0.33','--$1.23' => '1.23',- '--63 5212.4440' => '635212.444',+ '--63 5212.4440' => '635212.4440','--,2' => '0.2',];foreach ($values as $value => $expected) {$converter = new Amount;$result = $converter->convert($value);- $this->assertEquals($expected, $result, sprintf('The original value was %s', $value));+ $this->assertEquals($expected, $result, sprintf('The original value was "%s", returned was "%s"', $value, $result));}}