fixed currency conversion

This commit is contained in:
denisdulici 2020-03-09 18:07:16 +03:00
parent bc421a5887
commit 08f349c42c
10 changed files with 24 additions and 49 deletions

View File

@ -98,7 +98,7 @@ abstract class DocumentModel extends Model
$default_model->currency_code = $item->currency_code; $default_model->currency_code = $item->currency_code;
$default_model->currency_rate = $currencies[$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 = new Transaction();
$convert_model->default_currency_code = $item->currency_code; $convert_model->default_currency_code = $item->currency_code;
@ -106,7 +106,7 @@ abstract class DocumentModel extends Model
$convert_model->currency_code = $this->currency_code; $convert_model->currency_code = $this->currency_code;
$convert_model->currency_rate = $currencies[$this->currency_code]; $convert_model->currency_rate = $currencies[$this->currency_code];
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault(); $amount = (double) $convert_model->getAmountConvertedFromDefault();
} }
$paid += $amount; $paid += $amount;

View File

@ -100,7 +100,7 @@ class Invoices extends Controller
if ($invoice->currency_code != $item->currency_code) { if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$amount = $item->getAmountConvertedFromCustomDefault(); $amount = $item->getAmountConvertedFromDefault();
} }
$paid += $amount; $paid += $amount;
@ -129,7 +129,7 @@ class Invoices extends Controller
if ($invoice->currency_code != $item->currency_code) { if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$amount = $item->getAmountConvertedFromCustomDefault(); $amount = $item->getAmountConvertedFromDefault();
} }
$paid += $amount; $paid += $amount;

View File

@ -371,7 +371,7 @@ class Bills extends Controller
if ($bill->currency_code != $item->currency_code) { if ($bill->currency_code != $item->currency_code) {
$item->default_currency_code = $bill->currency_code; $item->default_currency_code = $bill->currency_code;
$amount = $item->getAmountConvertedFromCustomDefault(); $amount = $item->getAmountConvertedFromDefault();
} }
$paid += $amount; $paid += $amount;

View File

@ -447,7 +447,7 @@ class Invoices extends Controller
if ($invoice->currency_code != $item->currency_code) { if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$amount = $item->getAmountConvertedFromCustomDefault(); $amount = $item->getAmountConvertedFromDefault();
} }
$paid += $amount; $paid += $amount;

View File

@ -92,7 +92,7 @@ class CreateDocumentTransaction extends Job
$default_amount_model->currency_code = $this->request['currency_code']; $default_amount_model->currency_code = $this->request['currency_code'];
$default_amount_model->currency_rate = $currencies[$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 = new Transaction();
$convert_amount_model->default_currency_code = $this->request['currency_code']; $convert_amount_model->default_currency_code = $this->request['currency_code'];
@ -100,7 +100,7 @@ class CreateDocumentTransaction extends Job
$convert_amount_model->currency_code = $this->model->currency_code; $convert_amount_model->currency_code = $this->model->currency_code;
$convert_amount_model->currency_rate = $currencies[$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; $total_amount -= $this->model->paid;
@ -126,7 +126,7 @@ class CreateDocumentTransaction extends Job
$error_amount_model->currency_code = $this->model->currency_code; $error_amount_model->currency_code = $this->model->currency_code;
$error_amount_model->currency_rate = $currencies[$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 = new Transaction();
$convert_amount_model->default_currency_code = $this->model->currency_code; $convert_amount_model->default_currency_code = $this->model->currency_code;
@ -134,7 +134,7 @@ class CreateDocumentTransaction extends Job
$convert_amount_model->currency_code = $this->request['currency_code']; $convert_amount_model->currency_code = $this->request['currency_code'];
$convert_amount_model->currency_rate = $currencies[$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)]); $message = trans('messages.error.over_payment', ['amount' => money($error_amount, $this->request['currency_code'], true)]);

View File

@ -64,7 +64,7 @@ class CreateTransfer extends Job
$default_amount_model->currency_code = $expense_currency_code; $default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$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(); $transfer_amount = new Transfer();
@ -74,7 +74,7 @@ class CreateTransfer extends Job
$transfer_amount->currency_code = $income_currency_code; $transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code]; $transfer_amount->currency_rate = $currencies[$income_currency_code];
$amount = $transfer_amount->getAmountConvertedFromCustomDefault(); $amount = $transfer_amount->getAmountConvertedFromDefault();
} else { } else {
$amount = $this->request->get('amount'); $amount = $this->request->get('amount');
} }

View File

@ -71,7 +71,7 @@ class UpdateTransfer extends Job
$default_amount_model->currency_code = $expense_currency_code; $default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$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(); $transfer_amount = new Transfer();
@ -81,7 +81,7 @@ class UpdateTransfer extends Job
$transfer_amount->currency_code = $income_currency_code; $transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code]; $transfer_amount->currency_rate = $currencies[$income_currency_code];
$amount = $transfer_amount->getAmountConvertedFromCustomDefault(); $amount = $transfer_amount->getAmountConvertedFromDefault();
} else { } else {
$amount = $this->request->get('amount'); $amount = $this->request->get('amount');
} }

View File

@ -211,9 +211,4 @@ class Transaction extends Model
return $this->getMedia('attachment')->last(); return $this->getMedia('attachment')->last();
} }
public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
}
} }

View File

@ -3,46 +3,31 @@
namespace App\Traits; namespace App\Traits;
use Akaunting\Money\Money; use Akaunting\Money\Money;
use Akaunting\Money\Currency;
trait Currencies 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 // No need to convert same currency
if ($from == $to) { if ($from == $to) {
return $format ? $money->format() : $money->getAmount(); 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(); 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) 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) 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) public function getAmountConvertedToDefault($format = false, $with_tax = true)
@ -55,18 +40,13 @@ trait Currencies
return $this->convertFromDefault($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format); return $this->convertFromDefault($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format);
} }
public function getAmountConvertedFromCustomDefault($format = false, $with_tax = true) public function getAmount($with_tax = true)
{ {
return $this->convert($this->getAmount($with_tax), $this->default_currency_code, $this->currency_code, $this->currency_rate, $format); return $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
} }
public function getAmountDivided($format = false, $with_tax = true) public function getDefaultCurrency()
{ {
return $this->divide($this->getAmount($with_tax), $this->currency_code, $this->currency_rate, $format); return !empty($this->default_currency_code) ? $this->default_currency_code : setting('default.currency', 'USD');
}
protected function getDefaultCurrency()
{
return setting('default.currency', 'USD');
} }
} }

View File

@ -18,7 +18,7 @@
"akaunting/language": "1.0.*", "akaunting/language": "1.0.*",
"akaunting/menu": "1.0.*", "akaunting/menu": "1.0.*",
"akaunting/module": "1.0.*", "akaunting/module": "1.0.*",
"akaunting/money": "1.0.*", "akaunting/money": "1.1.*",
"akaunting/setting": "1.1.*", "akaunting/setting": "1.1.*",
"akaunting/version": "1.0.*", "akaunting/version": "1.0.*",
"barryvdh/laravel-debugbar": "3.2.*", "barryvdh/laravel-debugbar": "3.2.*",