2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2017-11-27 01:41:51 +03:00
|
|
|
use Akaunting\Money\Money;
|
|
|
|
use Akaunting\Money\Currency;
|
2019-01-15 14:46:14 -02:00
|
|
|
use App\Models\Setting\Currency as CurrencyModel;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
trait Currencies
|
|
|
|
{
|
|
|
|
public function convert($amount, $code, $rate, $format = false)
|
|
|
|
{
|
|
|
|
$default = new Currency(setting('general.default_currency', 'USD'));
|
|
|
|
|
2019-01-15 14:46:14 -02:00
|
|
|
$defaultCurrency = CurrencyModel::where('code', '=', $default->getCurrency())
|
|
|
|
->first(['code', 'rate']);
|
|
|
|
|
|
|
|
$defaultRate = $defaultCurrency ? $defaultCurrency->rate : 1;
|
|
|
|
$unratedAmount = $amount / $rate;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
if ($format) {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$code($unratedAmount, true)->convert($default, (double)$defaultRate)->format();
|
2017-09-14 22:21:00 +03:00
|
|
|
} else {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$code($unratedAmount)->convert($default, (double)$defaultRate)->getAmount();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $money;
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:58:24 +03:00
|
|
|
public function divide($amount, $code, $rate, $format = false)
|
|
|
|
{
|
|
|
|
if ($format) {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$code($amount, true)->divide((double)$rate)->format();
|
2018-08-07 19:58:24 +03:00
|
|
|
} else {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$code($amount)->divide((double)$rate)->getAmount();
|
2018-08-07 19:58:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $money;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function reverseConvert($amount, $code, $rate, $format = false)
|
|
|
|
{
|
|
|
|
$default = setting('general.default_currency', 'USD');
|
|
|
|
|
|
|
|
$code = new Currency($code);
|
|
|
|
|
|
|
|
if ($format) {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$default($amount, true)->convert($code, (double)$rate)->format();
|
2017-09-14 22:21:00 +03:00
|
|
|
} else {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$default($amount)->convert($code, (double)$rate)->getAmount();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $money;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dynamicConvert($default, $amount, $code, $rate, $format = false)
|
|
|
|
{
|
|
|
|
$code = new Currency($code);
|
|
|
|
|
|
|
|
if ($format) {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$default($amount, true)->convert($code, (double)$rate)->format();
|
2017-09-14 22:21:00 +03:00
|
|
|
} else {
|
2019-01-15 14:46:14 -02:00
|
|
|
$money = Money::$default($amount)->convert($code, (double)$rate)->getAmount();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $money;
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:59:58 +03:00
|
|
|
public function getConvertedAmount($format = false, $with_tax = true)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-01-15 14:46:14 -02:00
|
|
|
$amount = $this->amount;
|
|
|
|
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
|
|
$amount = $this->amount_without_tax;
|
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
|
|
|
return $this->convert($amount, $this->currency_code, $this->currency_rate, $format);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 14:59:58 +03:00
|
|
|
public function getReverseConvertedAmount($format = false, $with_tax = true)
|
2017-10-04 01:30:11 +03:00
|
|
|
{
|
2019-01-15 14:46:14 -02:00
|
|
|
$amount = $this->amount;
|
|
|
|
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
|
|
$amount = $this->amount_without_tax;
|
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
|
|
|
return $this->reverseConvert($amount, $this->currency_code, $this->currency_rate, $format);
|
2017-10-04 01:30:11 +03:00
|
|
|
}
|
|
|
|
|
2018-12-22 14:59:58 +03:00
|
|
|
public function getDynamicConvertedAmount($format = false, $with_tax = true)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-01-15 14:46:14 -02:00
|
|
|
$amount = $this->amount;
|
|
|
|
if (! $with_tax && isset($this->amount_without_tax)) {
|
|
|
|
$amount = $this->amount_without_tax;
|
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
2019-01-15 14:46:14 -02:00
|
|
|
return $this->dynamicConvert(
|
|
|
|
$this->default_currency_code,
|
|
|
|
$amount,
|
|
|
|
$this->currency_code,
|
|
|
|
$this->currency_rate,
|
|
|
|
$format
|
|
|
|
);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|