Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
M
money-datatype
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
package
money-datatype
Commits
69b85f28
Commit
69b85f28
authored
Jul 28, 2016
by
Thorsten Buss
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FIX calculations with TaxedMoney are now correct
parent
02cb64b4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
8 deletions
+48
-8
Money.php
src/Money/Money.php
+14
-6
TaxedMoney.php
src/Money/TaxedMoney.php
+20
-2
TaxedMoneyTest.php
tests/Money/TaxedMoneyTest.php
+14
-0
No files found.
src/Money/Money.php
View file @
69b85f28
...
...
@@ -60,6 +60,14 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
return
$this
->
amount
;
}
/**
* amount for internatl calculating - important for TaxedMoney
* @return int
*/
protected
function
amountToCalc
()
{
return
$this
->
amount
;
}
/**
* amount as decimal with . as decPoint
* @return float
...
...
@@ -287,7 +295,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
*/
public
function
add
(
self
$addend
)
{
$this
->
assertSameCurrency
(
$addend
);
return
$this
->
dbl
(
$this
->
amount
()
+
$addend
->
amount
());
return
$this
->
dbl
(
$this
->
amount
ToCalc
()
+
$addend
->
amountToCalc
());
}
/**
...
...
@@ -301,7 +309,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
*/
public
function
subtract
(
self
$subtrahend
)
{
$this
->
assertSameCurrency
(
$subtrahend
);
return
$this
->
dbl
(
$this
->
amount
()
-
$subtrahend
->
amount
());
return
$this
->
dbl
(
$this
->
amount
ToCalc
()
-
$subtrahend
->
amountToCalc
());
}
/**
...
...
@@ -316,7 +324,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
* @throws \OutOfBoundsException
*/
public
function
multiply
(
$multiplier
,
$roundingMode
=
PHP_ROUND_HALF_UP
)
{
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
()
*
$multiplier
,
0
,
$roundingMode
));
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
ToCalc
()
*
$multiplier
,
0
,
$roundingMode
));
}
/**
...
...
@@ -348,7 +356,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
if
(
$divisor
==
0
)
{
throw
new
\InvalidArgumentException
(
'Division by zero'
);
}
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
()
/
$divisor
,
0
,
$roundingMode
));
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
ToCalc
()
/
$divisor
,
0
,
$roundingMode
));
}
/**
...
...
@@ -359,11 +367,11 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
* @return array
*/
public
function
allocate
(
array
$ratios
)
{
$remainder
=
$this
->
amount
();
$remainder
=
$this
->
amount
ToCalc
();
$results
=
[];
$total
=
array_sum
(
$ratios
);
foreach
(
$ratios
as
$ratio
)
{
$share
=
(
int
)
floor
(
$this
->
amount
()
*
$ratio
/
$total
);
$share
=
(
int
)
floor
(
$this
->
amount
ToCalc
()
*
$ratio
/
$total
);
$results
[]
=
$share
;
$remainder
-=
$share
;
}
...
...
src/Money/TaxedMoney.php
View file @
69b85f28
...
...
@@ -108,14 +108,32 @@ class TaxedMoney extends Money {
* return the gross/net amount as defined in $this->default_return_type
* @param int $precision the number of precision positions for better calucations with the amount
* @return int
* @throws MoneyException
*/
public
function
amount
(
$precision
=
0
)
{
if
(
$this
->
amount_type
==
$this
->
default_return_type
)
{
return
parent
::
amount
();
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_NET
)
{
}
elseif
(
$this
->
default_return_type
==
self
::
TYPE_NET
)
{
return
$this
->
amountWithoutTax
(
$precision
);
}
elseif
(
$this
->
default_return_type
==
self
::
TYPE_GROSS
)
{
return
$this
->
amountWithTax
(
$precision
);
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_GROSS
)
{
}
throw
new
MoneyException
(
'Problems with defined types in TaxedMoney'
);
}
/**
* amount for internatl calculating - important for TaxedMoney
* @param int $precision
* @return int
* @throws MoneyException
*/
protected
function
amountToCalc
(
$precision
=
0
)
{
if
(
$this
->
amount_type
==
$this
->
default_return_type
)
{
return
parent
::
amount
();
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_NET
)
{
return
$this
->
amountWithoutTax
(
$precision
);
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_GROSS
)
{
return
$this
->
amountWithTax
(
$precision
);
}
throw
new
MoneyException
(
'Problems with defined types in TaxedMoney'
);
}
...
...
tests/Money/TaxedMoneyTest.php
View file @
69b85f28
...
...
@@ -112,4 +112,18 @@ class TaxedMoneyTest extends MoneyTest {
$this
->
assertEquals
(
1190
,
$m
->
amountWithTax
());
}
public
function
testMultiplication
()
{
$m1
=
MoneyGross
::
fromNet
(
150
,
10
);
$m2
=
MoneyGross
::
fromNet
(
10
,
10
);
$this
->
assertEquals
(
$m1
,
$m2
->
multiply
(
15
));
$this
->
assertNotEquals
(
$m1
,
$m2
->
multiply
(
10
));
}
public
function
testDivision
()
{
$m1
=
$this
->
money
(
3
,
new
\Bnet\Money\Currency
(
'EUR'
));
$m2
=
$this
->
money
(
10
,
new
\Bnet\Money\Currency
(
'EUR'
));
$this
->
assertEquals
(
$m1
,
$m2
->
divide
(
3
));
$this
->
assertNotEquals
(
$m1
,
$m2
->
divide
(
2
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment