akaunting/app/Traits/Currencies.php

86 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Traits;
2017-11-27 01:41:51 +03:00
use Akaunting\Money\Money;
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);
2021-06-08 23:33:17 +03:00
} catch (\Throwable $e) {
report($e);
2020-03-15 10:31:49 +03:00
return 0;
}
2019-11-16 10:21:14 +03:00
return $format ? $money->format() : $money->getAmount();
2017-09-14 22:21:00 +03:00
}
2020-07-06 09:54:18 +03:00
public function convertToDefault($amount, $from, $rate, $format = false, $default = null)
2017-09-14 22:21:00 +03:00
{
2020-07-06 09:54:18 +03:00
$default_currency = $default ?? $this->getDefaultCurrency();
return $this->convert('divide', $amount, $from, $default_currency, $rate, $format);
2017-09-14 22:21:00 +03:00
}
2020-07-06 09:54:18 +03:00
public function convertFromDefault($amount, $to, $rate, $format = false, $default = null)
2017-09-14 22:21:00 +03:00
{
2020-07-06 09:54:18 +03:00
$default_currency = $default ?? $this->getDefaultCurrency();
return $this->convert('multiply', $amount, $default_currency, $to, $rate, $format);
}
public function convertBetween($amount, $from_code, $from_rate, $to_code, $to_rate)
{
$default_amount = $amount;
if ($from_code != default_currency()) {
2020-07-06 09:54:18 +03:00
$default_amount = $this->convertToDefault($amount, $from_code, $from_rate);
}
$converted_amount = $this->convertFromDefault($default_amount, $to_code, $to_rate, false, $from_code);
return $converted_amount;
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
{
return !empty($this->default_currency_code) ? $this->default_currency_code : default_currency();
2017-09-14 22:21:00 +03:00
}
2020-07-06 09:54:18 +03:00
public function setDefaultCurrency($code)
{
$this->default_currency_code = $code;
}
public function unsetDefaultCurrency()
{
unset($this->default_currency_code);
}
}