Merge branch 'master' of github.com:akaunting/akaunting
This commit is contained in:
commit
238f69b40a
@ -4,24 +4,18 @@ namespace App\Traits;
|
|||||||
|
|
||||||
use Akaunting\Money\Money;
|
use Akaunting\Money\Money;
|
||||||
use Akaunting\Money\Currency;
|
use Akaunting\Money\Currency;
|
||||||
use App\Models\Setting\Currency as CurrencyModel;
|
|
||||||
|
|
||||||
trait Currencies
|
trait Currencies
|
||||||
{
|
{
|
||||||
|
|
||||||
public function convert($amount, $code, $rate, $format = false)
|
public function convert($amount, $code, $rate, $format = false)
|
||||||
{
|
{
|
||||||
$default = new Currency(setting('general.default_currency', 'USD'));
|
$default = new Currency(setting('general.default_currency', 'USD'));
|
||||||
|
|
||||||
$defaultCurrency = CurrencyModel::where('code', '=', $default->getCurrency())
|
|
||||||
->first(['code', 'rate']);
|
|
||||||
|
|
||||||
$defaultRate = $defaultCurrency ? $defaultCurrency->rate : 1;
|
|
||||||
$unratedAmount = $amount / $rate;
|
|
||||||
|
|
||||||
if ($format) {
|
if ($format) {
|
||||||
$money = Money::$code($unratedAmount, true)->convert($default, (double)$defaultRate)->format();
|
$money = Money::$code($amount, true)->convert($default, (double) $rate)->format();
|
||||||
} else {
|
} else {
|
||||||
$money = Money::$code($unratedAmount)->convert($default, (double)$defaultRate)->getAmount();
|
$money = Money::$code($amount)->convert($default, (double) $rate)->getAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $money;
|
return $money;
|
||||||
@ -30,9 +24,9 @@ trait Currencies
|
|||||||
public function divide($amount, $code, $rate, $format = false)
|
public function divide($amount, $code, $rate, $format = false)
|
||||||
{
|
{
|
||||||
if ($format) {
|
if ($format) {
|
||||||
$money = Money::$code($amount, true)->divide((double)$rate)->format();
|
$money = Money::$code($amount, true)->divide((double) $rate)->format();
|
||||||
} else {
|
} else {
|
||||||
$money = Money::$code($amount)->divide((double)$rate)->getAmount();
|
$money = Money::$code($amount)->divide((double) $rate)->getAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $money;
|
return $money;
|
||||||
@ -45,9 +39,9 @@ trait Currencies
|
|||||||
$code = new Currency($code);
|
$code = new Currency($code);
|
||||||
|
|
||||||
if ($format) {
|
if ($format) {
|
||||||
$money = Money::$default($amount, true)->convert($code, (double)$rate)->format();
|
$money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
|
||||||
} else {
|
} else {
|
||||||
$money = Money::$default($amount)->convert($code, (double)$rate)->getAmount();
|
$money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $money;
|
return $money;
|
||||||
@ -58,9 +52,9 @@ trait Currencies
|
|||||||
$code = new Currency($code);
|
$code = new Currency($code);
|
||||||
|
|
||||||
if ($format) {
|
if ($format) {
|
||||||
$money = Money::$default($amount, true)->convert($code, (double)$rate)->format();
|
$money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
|
||||||
} else {
|
} else {
|
||||||
$money = Money::$default($amount)->convert($code, (double)$rate)->getAmount();
|
$money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $money;
|
return $money;
|
||||||
@ -68,37 +62,22 @@ trait Currencies
|
|||||||
|
|
||||||
public function getConvertedAmount($format = false, $with_tax = true)
|
public function getConvertedAmount($format = false, $with_tax = true)
|
||||||
{
|
{
|
||||||
$amount = $this->amount;
|
$amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
|
||||||
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
||||||
$amount = $this->amount_without_tax;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->convert($amount, $this->currency_code, $this->currency_rate, $format);
|
return $this->convert($amount, $this->currency_code, $this->currency_rate, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getReverseConvertedAmount($format = false, $with_tax = true)
|
public function getReverseConvertedAmount($format = false, $with_tax = true)
|
||||||
{
|
{
|
||||||
$amount = $this->amount;
|
$amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
|
||||||
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
||||||
$amount = $this->amount_without_tax;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->reverseConvert($amount, $this->currency_code, $this->currency_rate, $format);
|
return $this->reverseConvert($amount, $this->currency_code, $this->currency_rate, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDynamicConvertedAmount($format = false, $with_tax = true)
|
public function getDynamicConvertedAmount($format = false, $with_tax = true)
|
||||||
{
|
{
|
||||||
$amount = $this->amount;
|
$amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
|
||||||
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
||||||
$amount = $this->amount_without_tax;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->dynamicConvert(
|
return $this->dynamicConvert($this->default_currency_code, $amount, $this->currency_code, $this->currency_rate, $format);
|
||||||
$this->default_currency_code,
|
|
||||||
$amount,
|
|
||||||
$this->currency_code,
|
|
||||||
$this->currency_rate,
|
|
||||||
$format
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user