Skip to content

Commit

Permalink
fixed currency conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Mar 9, 2020
1 parent bc421a5 commit 08f349c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 49 deletions.
4 changes: 2 additions & 2 deletions app/Abstracts/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public function getPaidAttribute()
$default_model->currency_code = $item->currency_code;
$default_model->currency_rate = $currencies[$item->currency_code];

$default_amount = (double) $default_model->getDivideConvertedAmount();
$default_amount = (double) $default_model->getAmountConvertedToDefault();

$convert_model = new Transaction();
$convert_model->default_currency_code = $item->currency_code;
$convert_model->amount = $default_amount;
$convert_model->currency_code = $this->currency_code;
$convert_model->currency_rate = $currencies[$this->currency_code];

$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
$amount = (double) $convert_model->getAmountConvertedFromDefault();
}

$paid += $amount;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Portal/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function prepareInvoice(Invoice $invoice)
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;

$amount = $item->getAmountConvertedFromCustomDefault();
$amount = $item->getAmountConvertedFromDefault();
}

$paid += $amount;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function signed(Invoice $invoice)
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;

$amount = $item->getAmountConvertedFromCustomDefault();
$amount = $item->getAmountConvertedFromDefault();
}

$paid += $amount;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Purchases/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected function prepareBill(Bill $bill)
if ($bill->currency_code != $item->currency_code) {
$item->default_currency_code = $bill->currency_code;

$amount = $item->getAmountConvertedFromCustomDefault();
$amount = $item->getAmountConvertedFromDefault();
}

$paid += $amount;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Sales/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected function prepareInvoice(Invoice $invoice)
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;

$amount = $item->getAmountConvertedFromCustomDefault();
$amount = $item->getAmountConvertedFromDefault();
}

$paid += $amount;
Expand Down
8 changes: 4 additions & 4 deletions app/Jobs/Banking/CreateDocumentTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ protected function checkAmount()
$default_amount_model->currency_code = $this->request['currency_code'];
$default_amount_model->currency_rate = $currencies[$this->request['currency_code']];

$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
$default_amount = (double) $default_amount_model->getAmountConvertedToDefault();

$convert_amount_model = new Transaction();
$convert_amount_model->default_currency_code = $this->request['currency_code'];
$convert_amount_model->amount = $default_amount;
$convert_amount_model->currency_code = $this->model->currency_code;
$convert_amount_model->currency_rate = $currencies[$this->model->currency_code];

$amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault();
$amount = (double) $convert_amount_model->getAmountConvertedFromDefault();
}

$total_amount -= $this->model->paid;
Expand All @@ -126,15 +126,15 @@ protected function checkAmount()
$error_amount_model->currency_code = $this->model->currency_code;
$error_amount_model->currency_rate = $currencies[$this->model->currency_code];

$error_amount = (double) $error_amount_model->getDivideConvertedAmount();
$error_amount = (double) $error_amount_model->getAmountConvertedToDefault();

$convert_amount_model = new Transaction();
$convert_amount_model->default_currency_code = $this->model->currency_code;
$convert_amount_model->amount = $error_amount;
$convert_amount_model->currency_code = $this->request['currency_code'];
$convert_amount_model->currency_rate = $currencies[$this->request['currency_code']];

$error_amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault();
$error_amount = (double) $convert_amount_model->getAmountConvertedFromDefault();
}

$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $this->request['currency_code'], true)]);
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/Banking/CreateTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function handle()
$default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code];

$default_amount = $default_amount_model->getAmountDivided();
$default_amount = $default_amount_model->getAmountConvertedToDefault();
}

$transfer_amount = new Transfer();
Expand All @@ -74,7 +74,7 @@ public function handle()
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];

$amount = $transfer_amount->getAmountConvertedFromCustomDefault();
$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
}
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/Banking/UpdateTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function handle()
$default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code];

$default_amount = $default_amount_model->getDivideConvertedAmount();
$default_amount = $default_amount_model->getAmountConvertedToDefault();
}

$transfer_amount = new Transfer();
Expand All @@ -81,7 +81,7 @@ public function handle()
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];

$amount = $transfer_amount->getAmountConvertedFromCustomDefault();
$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
}
Expand Down
5 changes: 0 additions & 5 deletions app/Models/Banking/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,4 @@ public function getAttachmentAttribute($value)

return $this->getMedia('attachment')->last();
}

public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}
38 changes: 9 additions & 29 deletions app/Traits/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,31 @@
namespace App\Traits;

use Akaunting\Money\Money;
use Akaunting\Money\Currency;

trait Currencies
{
public function convert($amount, $from, $to, $rate, $format = false)
public function convert($method, $amount, $from, $to, $rate, $format = false)
{
$money = Money::$from($amount, $format);
$money = Money::$to($amount, $format);

// No need to convert same currency
if ($from == $to) {
return $format ? $money->format() : $money->getAmount();
}

$money = $money->convert(new Currency($to), (double) $rate);
$money = $money->$method((double) $rate);

return $format ? $money->format() : $money->getAmount();
}

public function divide($amount, $code, $rate, $format = false)
{
$money = Money::$code($amount, $format);

$money = $money->divide((double) $rate);

return $format ? $money->format() : $money->getAmount();
}

public function getAmount($with_tax = true)
{
return $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
}

public function convertToDefault($amount, $from, $rate, $format = false)
{
return $this->convert($amount, $from, $this->getDefaultCurrency(), $rate, $format);
return $this->convert('divide', $amount, $from, $this->getDefaultCurrency(), $rate, $format);
}

public function convertFromDefault($amount, $to, $rate, $format = false)
{
return $this->convert($amount, $this->getDefaultCurrency(), $to, $rate, $format);
return $this->convert('multiply', $amount, $this->getDefaultCurrency(), $to, $rate, $format);
}

public function getAmountConvertedToDefault($format = false, $with_tax = true)
Expand All @@ -55,18 +40,13 @@ public function getAmountConvertedFromDefault($format = false, $with_tax = true)
return $this->convertFromDefault($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format);
}

public function getAmountConvertedFromCustomDefault($format = false, $with_tax = true)
{
return $this->convert($this->getAmount($with_tax), $this->default_currency_code, $this->currency_code, $this->currency_rate, $format);
}

public function getAmountDivided($format = false, $with_tax = true)
public function getAmount($with_tax = true)
{
return $this->divide($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format);
return $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
}

protected function getDefaultCurrency()
public function getDefaultCurrency()
{
return setting('default.currency', 'USD');
return !empty($this->default_currency_code) ? $this->default_currency_code : setting('default.currency', 'USD');
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"akaunting/language": "1.0.*",
"akaunting/menu": "1.0.*",
"akaunting/module": "1.0.*",
"akaunting/money": "1.0.*",
"akaunting/money": "1.1.*",
"akaunting/setting": "1.1.*",
"akaunting/version": "1.0.*",
"barryvdh/laravel-debugbar": "3.2.*",
Expand Down

0 comments on commit 08f349c

Please sign in to comment.