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
02cb64b4
Commit
02cb64b4
authored
Jul 27, 2016
by
Thorsten Buss
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FIX calculation with TaxedMoney are now correct
parent
bba9dff6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
19 deletions
+55
-19
readme.md
readme.md
+2
-0
Money.php
src/Money/Money.php
+19
-9
TaxedMoney.php
src/Money/TaxedMoney.php
+34
-10
No files found.
readme.md
View file @
02cb64b4
...
...
@@ -221,6 +221,8 @@ Examples:
## Changelog
**
0.1.5
-
FIX: calculation with TaxedMoney are now correct
**
0.1.4
-
add CurrencyUpdater to download and parse a file and save the data with a Closure
**
0.1.3
...
...
src/Money/Money.php
View file @
02cb64b4
...
...
@@ -8,6 +8,7 @@
namespace
Bnet\Money
;
use
Illuminate\Contracts\Support\Arrayable
;
use
Illuminate\Contracts\Support\Jsonable
;
use
Illuminate\Support\Str
;
...
...
@@ -18,7 +19,7 @@ use Illuminate\Support\Str;
* @method static Money EUR(int $amount)
* @method static Money USD(int $amount)
*/
class
Money
implements
\JsonSerializable
,
Jsonable
{
class
Money
implements
\JsonSerializable
,
Jsonable
,
Arrayable
{
/**
* @var int cents of currency
...
...
@@ -64,7 +65,7 @@ class Money implements \JsonSerializable, Jsonable{
* @return float
*/
public
function
normalize
()
{
return
bcdiv
(
$this
->
amount
(),
$this
->
currency
->
unit_factor
,
$this
->
currency
->
decimal_place
);
return
(
float
)
bcdiv
(
$this
->
amount
(),
$this
->
currency
->
unit_factor
,
$this
->
currency
->
decimal_place
);
}
/**
...
...
@@ -286,7 +287,7 @@ class Money implements \JsonSerializable, Jsonable{
*/
public
function
add
(
self
$addend
)
{
$this
->
assertSameCurrency
(
$addend
);
return
new
static
(
$this
->
amount
()
+
$addend
->
amount
(),
$this
->
currency
);
return
$this
->
dbl
(
$this
->
amount
()
+
$addend
->
amount
()
);
}
/**
...
...
@@ -300,7 +301,7 @@ class Money implements \JsonSerializable, Jsonable{
*/
public
function
subtract
(
self
$subtrahend
)
{
$this
->
assertSameCurrency
(
$subtrahend
);
return
new
static
(
$this
->
amount
()
-
$subtrahend
->
amount
(),
$this
->
currency
);
return
$this
->
dbl
(
$this
->
amount
()
-
$subtrahend
->
amount
()
);
}
/**
...
...
@@ -315,7 +316,7 @@ class Money implements \JsonSerializable, Jsonable{
* @throws \OutOfBoundsException
*/
public
function
multiply
(
$multiplier
,
$roundingMode
=
PHP_ROUND_HALF_UP
)
{
return
new
static
((
int
)
round
(
$this
->
amount
()
*
$multiplier
,
0
,
$roundingMode
),
$this
->
currency
);
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
()
*
$multiplier
,
0
,
$roundingMode
)
);
}
/**
...
...
@@ -347,7 +348,7 @@ class Money implements \JsonSerializable, Jsonable{
if
(
$divisor
==
0
)
{
throw
new
\InvalidArgumentException
(
'Division by zero'
);
}
return
new
static
((
int
)
round
(
$this
->
amount
()
/
$divisor
,
0
,
$roundingMode
),
$this
->
currency
);
return
$this
->
dbl
((
int
)
round
(
$this
->
amount
()
/
$divisor
,
0
,
$roundingMode
)
);
}
/**
...
...
@@ -372,7 +373,7 @@ class Money implements \JsonSerializable, Jsonable{
}
// generate MoneyObjects
foreach
(
$results
as
$k
=>
$v
)
{
$results
[
$k
]
=
new
static
(
$v
,
$this
->
currency
);
$results
[
$k
]
=
$this
->
dbl
(
$v
);
}
return
$results
;
}
...
...
@@ -464,8 +465,7 @@ class Money implements \JsonSerializable, Jsonable{
* @return Money
*/
public
static
function
__callStatic
(
$method
,
array
$arguments
)
{
$convert
=
(
isset
(
$arguments
[
1
])
&&
is_bool
(
$arguments
[
1
]))
?
(
bool
)
$arguments
[
1
]
:
false
;
return
new
static
(
$arguments
[
0
],
new
Currency
(
$method
),
$convert
);
return
new
static
(
$arguments
[
0
],
new
Currency
(
$method
));
}
/**
...
...
@@ -475,4 +475,14 @@ class Money implements \JsonSerializable, Jsonable{
public
function
hasTax
()
{
return
false
;
}
/**
* clone this MoneyObj with the given $amount and the currency of this obj
* @param $amount
* @param null $currency
* @return static
*/
protected
function
dbl
(
$amount
,
$currency
=
null
)
{
return
new
static
(
$amount
,
$currency
?:
$this
->
currency
());
}
}
\ No newline at end of file
src/Money/TaxedMoney.php
View file @
02cb64b4
...
...
@@ -111,18 +111,13 @@ class TaxedMoney extends Money {
*/
public
function
amount
(
$precision
=
0
)
{
if
(
$this
->
amount_type
==
$this
->
default_return_type
)
{
$amount
=
parent
::
amount
();
return
parent
::
amount
();
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_NET
)
{
$amount
=
$this
->
amountWithTax
(
$precision
);
return
$this
->
amountWithTax
(
$precision
);
}
elseif
(
$this
->
amount_type
==
self
::
TYPE_GROSS
)
{
$amount
=
$this
->
amountWithoutTax
(
$precision
);
}
else
{
throw
new
MoneyException
(
'Problems with defined types in TaxedMoney'
);
return
$this
->
amountWithoutTax
(
$precision
);
}
// cast to int if the precision is 0 for internal calculations that need and int
return
$precision
==
0
?
(
int
)
$amount
:
$amount
;
throw
new
MoneyException
(
'Problems with defined types in TaxedMoney'
);
}
/**
...
...
@@ -174,6 +169,35 @@ class TaxedMoney extends Money {
* @return float|int
*/
protected
function
round
(
$amount
,
$precision
=
0
)
{
return
round
(
$amount
,
$precision
);
$result
=
round
(
$amount
,
$precision
);
// cast to int if the precision is 0 for internal calculations that need an int
return
$precision
==
0
?
(
int
)
$result
:
$result
;
}
/**
* Get the instance as an array.
*
* @return array
*/
public
function
toArray
()
{
$arr
=
parent
::
toArray
();
$arr
[
'price_net'
]
=
$this
->
amountWithoutTax
();
$arr
[
'price_gross'
]
=
$this
->
amountWithTax
();
$arr
[
'tax'
]
=
$this
->
tax
;
return
$arr
;
}
/**
* clone this MoneyObj with the given $amount and the currency of this obj
* @param $amount
* @param null $currency
* @return static
*/
protected
function
dbl
(
$amount
,
$currency
=
null
)
{
return
new
static
(
$amount
,
$currency
?:
$this
->
currency
(),
$this
->
tax
,
$this
->
amount_type
,
$this
->
default_return_type
);
}
}
\ No newline at end of file
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