2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2017-11-27 01:41:51 +03:00
|
|
|
use Akaunting\Money\Money;
|
2020-03-15 10:31:49 +03:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use OutOfBoundsException;
|
|
|
|
use UnexpectedValueException;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
trait Currencies
|
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
public function convert($method, $amount, $from, $to, $rate, $format = false)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
$money = Money::$to($amount, $format);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// No need to convert same currency
|
|
|
|
if ($from == $to) {
|
|
|
|
return $format ? $money->format() : $money->getAmount();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-03-15 10:31:49 +03:00
|
|
|
try {
|
|
|
|
$money = $money->$method((double) $rate);
|
|
|
|
} catch (InvalidArgumentException | OutOfBoundsException | UnexpectedValueException $e) {
|
|
|
|
logger($e->getMessage());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return $format ? $money->format() : $money->getAmount();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function convertToDefault($amount, $from, $rate, $format = false)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
return $this->convert('divide', $amount, $from, $this->getDefaultCurrency(), $rate, $format);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function convertFromDefault($amount, $to, $rate, $format = false)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
return $this->convert('multiply', $amount, $this->getDefaultCurrency(), $to, $rate, $format);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function getAmountConvertedToDefault($format = false, $with_tax = true)
|
|
|
|
{
|
|
|
|
return $this->convertToDefault($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function getAmountConvertedFromDefault($format = false, $with_tax = true)
|
2017-10-04 01:30:11 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->convertFromDefault($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format);
|
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
2020-03-09 18:07:16 +03:00
|
|
|
public function getAmount($with_tax = true)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
return $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2018-12-22 14:59:58 +03:00
|
|
|
|
2020-03-09 18:07:16 +03:00
|
|
|
public function getDefaultCurrency()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-03-09 18:07:16 +03:00
|
|
|
return !empty($this->default_currency_code) ? $this->default_currency_code : setting('default.currency', 'USD');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2019-02-01 16:27:45 +03:00
|
|
|
}
|