1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Libraries/Currency/Conversion/CurrencyApi.php
David Bomba 6d0d6c10cd
Working on OAuth (#3686)
* Update Exchange rate Data once a day

* Tests for currency conversions

* Fixes for tests

* Fix for adding blank product keys

* Class for logging emails sent

* fixes for oauth
2020-05-12 19:56:30 +10:00

73 lines
1.7 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Libraries\Currency\Conversion;
use App\Models\Currency;
use Illuminate\Support\Carbon;
class CurrencyApi implements CurrencyConversionInterface
{
public function convert($amount, $from_currency_id, $to_currency_id, $date = null)
{
if(!$date)
$date = Carbon::now();
$from_currency = Currency::find($from_currency_id);
$to_currency = Currency::find($to_currency_id);
$usd_amount = $this->convertToUsd($amount, $from_currency);
return $this->convertFromUsdToCurrency($usd_amount, $to_currency);
}
public function exchangeRate($from_currency_id, $to_currency_id, $date = null)
{
$from_currency = Currency::find($from_currency_id);
$to_currency = Currency::find($to_currency_id);
$usd_amount = $this->convertToUsd(1, $from_currency);
return $this->convertFromUsdToCurrency($usd_amount, $to_currency);
}
/**
* Converts a currency amount to USD
*
* @param float $amount amount
* @param object $currency currency object
* @return float USD Amount
*/
private function convertToUsd($amount, $currency)
{
return $amount / $currency->exchange_rate;
}
/**
* Converts USD to any other denomination
*
* @param float $amount amount
* @param object $currency destination currency
* @return float the converted amount
*/
private function convertFromUsdToCurrency($amount, $currency)
{
return $amount * $currency->exchange_rate;
}
}