Currencies.php
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace App\Traits;
use Akaunting\Money\Money;
use Akaunting\Money\Currency;
trait Currencies
{
public function convert($amount, $code, $rate, $format = false)
{
$default = new Currency(setting('general.default_currency', 'USD'));
if ($format) {
$money = Money::$code($amount, true)->convert($default, (double) $rate)->format();
} else {
$money = Money::$code($amount)->convert($default, (double) $rate)->getAmount();
}
return $money;
}
public function divide($amount, $code, $rate, $format = false)
{
if ($format) {
$money = Money::$code($amount, true)->divide((double) $rate)->format();
} else {
$money = Money::$code($amount)->divide((double) $rate)->getAmount();
}
return $money;
}
public function reverseConvert($amount, $code, $rate, $format = false)
{
$default = setting('general.default_currency', 'USD');
$code = new Currency($code);
if ($format) {
$money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
} else {
$money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
}
return $money;
}
public function dynamicConvert($default, $amount, $code, $rate, $format = false)
{
$code = new Currency($code);
if ($format) {
$money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
} else {
$money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
}
return $money;
}
public function getConvertedAmount($format = false)
{
return $this->convert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
public function getReverseConvertedAmount($format = false)
{
return $this->reverseConvert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
public function getDynamicConvertedAmount($format = false)
{
return $this->dynamicConvert($this->default_currency_code, $this->amount, $this->currency_code, $this->currency_rate, $format);
}
}