From f8f771fec243c495d6f6be63006b3b92419ca08d Mon Sep 17 00:00:00 2001 From: Luiz Fernando Vid Date: Tue, 15 Jan 2019 14:46:05 -0200 Subject: [PATCH 1/2] =?UTF-8?q?Added=20debugbar=20folder=E2=80=99s=20conte?= =?UTF-8?q?nt=20to=20gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bcb3d0e9f..3e1ce5cd5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ npm-debug.log .env robots.txt _ide_helper.php -.phpstorm.meta.php \ No newline at end of file +.phpstorm.meta.php +/storage/debugbar/* \ No newline at end of file From 80ff42cd7c9266a9cff1b4556536a08522b1d065 Mon Sep 17 00:00:00 2001 From: Luiz Fernando Vid Date: Tue, 15 Jan 2019 14:46:14 -0200 Subject: [PATCH 2/2] Fixed displaying wrong values when different currencies are used and some changes to conform with psr-2 --- app/Traits/Currencies.php | 47 ++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/app/Traits/Currencies.php b/app/Traits/Currencies.php index 6e8bbe69b..9f52ada56 100644 --- a/app/Traits/Currencies.php +++ b/app/Traits/Currencies.php @@ -4,18 +4,24 @@ namespace App\Traits; use Akaunting\Money\Money; use Akaunting\Money\Currency; +use App\Models\Setting\Currency as CurrencyModel; trait Currencies { - public function convert($amount, $code, $rate, $format = false) { $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) { - $money = Money::$code($amount, true)->convert($default, (double) $rate)->format(); + $money = Money::$code($unratedAmount, true)->convert($default, (double)$defaultRate)->format(); } else { - $money = Money::$code($amount)->convert($default, (double) $rate)->getAmount(); + $money = Money::$code($unratedAmount)->convert($default, (double)$defaultRate)->getAmount(); } return $money; @@ -24,9 +30,9 @@ trait Currencies public function divide($amount, $code, $rate, $format = false) { if ($format) { - $money = Money::$code($amount, true)->divide((double) $rate)->format(); + $money = Money::$code($amount, true)->divide((double)$rate)->format(); } else { - $money = Money::$code($amount)->divide((double) $rate)->getAmount(); + $money = Money::$code($amount)->divide((double)$rate)->getAmount(); } return $money; @@ -39,9 +45,9 @@ trait Currencies $code = new Currency($code); if ($format) { - $money = Money::$default($amount, true)->convert($code, (double) $rate)->format(); + $money = Money::$default($amount, true)->convert($code, (double)$rate)->format(); } else { - $money = Money::$default($amount)->convert($code, (double) $rate)->getAmount(); + $money = Money::$default($amount)->convert($code, (double)$rate)->getAmount(); } return $money; @@ -52,9 +58,9 @@ trait Currencies $code = new Currency($code); if ($format) { - $money = Money::$default($amount, true)->convert($code, (double) $rate)->format(); + $money = Money::$default($amount, true)->convert($code, (double)$rate)->format(); } else { - $money = Money::$default($amount)->convert($code, (double) $rate)->getAmount(); + $money = Money::$default($amount)->convert($code, (double)$rate)->getAmount(); } return $money; @@ -62,22 +68,37 @@ trait Currencies public function getConvertedAmount($format = false, $with_tax = true) { - $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount); + $amount = $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); } public function getReverseConvertedAmount($format = false, $with_tax = true) { - $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount); + $amount = $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); } public function getDynamicConvertedAmount($format = false, $with_tax = true) { - $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount); + $amount = $this->amount; + if (! $with_tax && isset($this->amount_without_tax)) { + $amount = $this->amount_without_tax; + } - return $this->dynamicConvert($this->default_currency_code, $amount, $this->currency_code, $this->currency_rate, $format); + return $this->dynamicConvert( + $this->default_currency_code, + $amount, + $this->currency_code, + $this->currency_rate, + $format + ); } } \ No newline at end of file