2020-05-12 11:56:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-05-12 11:56:30 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-05-12 11:56:30 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-05-12 11:56:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Currency;
|
2020-10-28 11:10:49 +01:00
|
|
|
use GuzzleHttp\Client;
|
2020-05-12 11:56:30 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
class UpdateExchangeRates implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2020-05-12 11:56:30 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function handle(): void
|
2020-05-12 11:56:30 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
info('updating currencies');
|
|
|
|
|
|
|
|
if (empty(config('ninja.currency_converter_api_key'))) {
|
2020-05-12 11:56:30 +02:00
|
|
|
return;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-12 11:56:30 +02:00
|
|
|
|
|
|
|
$cc_endpoint = sprintf('https://openexchangerates.org/api/latest.json?app_id=%s', config('ninja.currency_converter_api_key'));
|
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
if (config('ninja.db.multi_db_enabled')) {
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
$client = new Client();
|
|
|
|
$response = $client->get($cc_endpoint);
|
2020-05-12 11:56:30 +02:00
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
$currency_api = json_decode($response->getBody());
|
2020-05-12 11:56:30 +02:00
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
/* Update all currencies */
|
|
|
|
Currency::all()->each(function ($currency) use ($currency_api) {
|
|
|
|
$currency->exchange_rate = $currency_api->rates->{$currency->code};
|
|
|
|
$currency->save();
|
|
|
|
});
|
2020-05-12 11:56:30 +02:00
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
/* Rebuild the cache */
|
|
|
|
$currencies = Currency::orderBy('name')->get();
|
2020-05-12 11:56:30 +02:00
|
|
|
|
2022-11-26 21:52:49 +01:00
|
|
|
Cache::forever('currencies', $currencies);
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-16 02:36:09 +01:00
|
|
|
$client = new Client();
|
|
|
|
$response = $client->get($cc_endpoint);
|
2022-11-26 21:52:49 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$currency_api = json_decode($response->getBody());
|
2022-11-26 21:52:49 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
/* Update all currencies */
|
|
|
|
Currency::all()->each(function ($currency) use ($currency_api) {
|
|
|
|
$currency->exchange_rate = $currency_api->rates->{$currency->code};
|
|
|
|
$currency->save();
|
|
|
|
});
|
2022-11-26 21:52:49 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
/* Rebuild the cache */
|
|
|
|
$currencies = Currency::orderBy('name')->get();
|
2022-11-26 21:52:49 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
Cache::forever('currencies', $currencies);
|
2022-11-26 21:52:49 +01:00
|
|
|
}
|
2020-05-12 11:56:30 +02:00
|
|
|
}
|
|
|
|
}
|