diff --git a/app/Models/Client.php b/app/Models/Client.php index a72dd3344c29..8272e2fc10a5 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -812,6 +812,15 @@ class Client extends BaseModel implements HasLocalePreference return $defaults; } + public function setExchangeRate() + { + + $converter = new CurrencyApi(); + + return 1/$converter->convert(1, $this->currency()->id, $this->company->settings->currency_id); + + } + public function timezone_offset() :int { $offset = 0; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index e435263879b1..045282f41aaf 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -567,7 +567,7 @@ class InvoiceService /* If client currency differs from the company default currency, then insert the client exchange rate on the model.*/ if (! isset($this->invoice->exchange_rate) && $this->invoice->client->currency()->id != (int) $this->invoice->company->settings->currency_id) { - $this->invoice->exchange_rate = $this->invoice->client->currency()->exchange_rate; + $this->invoice->exchange_rate = $this->invoice->client->setExchangeRate(); } if ($this->invoice->client->getSetting('auto_bill_standard_invoices')) { diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index b09978d6af67..92632dad2380 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -11,25 +11,27 @@ namespace Tests\Feature; +use Tests\TestCase; +use App\Models\User; +use App\Models\Client; +use App\Models\Credit; +use App\Models\Account; +use App\Models\Company; +use App\Models\Currency; +use Tests\MockAccountData; +use Illuminate\Support\Str; +use App\Models\CompanyToken; +use App\Models\ClientContact; +use App\Utils\Traits\MakesHash; +use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; use App\Factory\InvoiceItemFactory; -use App\Models\Account; -use App\Models\Client; -use App\Models\ClientContact; -use App\Models\Company; -use App\Models\CompanyToken; -use App\Models\Credit; -use App\Models\User; -use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Session; -use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; -use Tests\MockAccountData; -use Tests\TestCase; +use Illuminate\Routing\Middleware\ThrottleRequests; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -65,6 +67,34 @@ class ClientTest extends TestCase $this->makeTestData(); } + public function testClientExchangeRateCalculation() + { + $settings = ClientSettings::defaults(); + $settings->currency_id = 12; + + $c = Client::factory() + ->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'settings' => $settings + ]); + + $settings = $this->company->settings; + $settings->currency_id = '3'; + + $this->company->saveSettings($settings, $this->company); + + $client_exchange_rate = round($c->setExchangeRate(),2); + + $aud_currency = Currency::find(12); + $eur_currency = Currency::find(3); + + $synthetic_exchange = $aud_currency->exchange_rate / $eur_currency->exchange_rate; + + $this->assertEquals($client_exchange_rate, round($synthetic_exchange,2)); + + } + public function testStoreClientFixes2() { $data = [ diff --git a/tests/Unit/CurrencyApiTest.php b/tests/Unit/CurrencyApiTest.php index d863dfac09e7..5db10d26ef9f 100644 --- a/tests/Unit/CurrencyApiTest.php +++ b/tests/Unit/CurrencyApiTest.php @@ -39,6 +39,7 @@ class CurrencyApiTest extends TestCase $converted_synthetic = 100 / ($aud_currency->exchange_rate / $eur_currency->exchange_rate); $this->assertEquals(round($converted_synthetic, 2), round($converted_amount, 2)); + } public function testCurrencyConversionWorking()