| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * DownloadExchangeRates.php | 
					
						
							|  |  |  |  * Copyright (c) 2022 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | namespace FireflyIII\Jobs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							|  |  |  | use GuzzleHttp\Client; | 
					
						
							| 
									
										
										
										
											2024-05-06 06:33:12 +02:00
										 |  |  | use GuzzleHttp\Exception\ConnectException; | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  | use GuzzleHttp\Exception\GuzzleException; | 
					
						
							| 
									
										
										
										
											2023-03-09 08:14:30 +01:00
										 |  |  | use GuzzleHttp\Exception\RequestException; | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +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\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class DownloadExchangeRates | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class DownloadExchangeRates implements ShouldQueue | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |     use Dispatchable; | 
					
						
							|  |  |  |     use InteractsWithQueue; | 
					
						
							|  |  |  |     use Queueable; | 
					
						
							|  |  |  |     use SerializesModels; | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     private array                       $active; | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |     private Carbon                      $date; | 
					
						
							|  |  |  |     private CurrencyRepositoryInterface $repository; | 
					
						
							|  |  |  |     private Collection                  $users; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new job instance. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(?Carbon $date) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->active     = []; | 
					
						
							|  |  |  |         $this->repository = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get all users:
 | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $userRepository */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $userRepository   = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->users      = $userRepository->all(); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $date) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $newDate    = clone $date; | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             $newDate->startOfDay(); | 
					
						
							|  |  |  |             $this->date = $newDate; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Created new DownloadExchangeRates("%s")', $this->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Execute the job. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in handle()'); | 
					
						
							| 
									
										
										
										
											2023-10-23 20:09:10 +02:00
										 |  |  |         $currencies = $this->repository->getCompleteSet(); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |         foreach ($currencies as $currency) { | 
					
						
							|  |  |  |             $this->downloadRates($currency); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws GuzzleException | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function downloadRates(TransactionCurrency $currency): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now downloading new exchange rates for currency %s.', $currency->code)); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $base       = sprintf('%s/%s/%s', (string) config('cer.url'), $this->date->year, $this->date->isoWeek); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $client     = new Client(); | 
					
						
							|  |  |  |         $url        = sprintf('%s/%s.json', $base, $currency->code); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 08:14:30 +01:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $res = $client->get($url); | 
					
						
							| 
									
										
										
										
											2024-05-09 05:10:13 +02:00
										 |  |  |         } catch (ConnectException|RequestException $e) { | 
					
						
							| 
									
										
										
										
											2024-12-27 05:44:03 +01:00
										 |  |  |             app('log')->warning(sprintf('Trying to grab "%s" resulted in error "%s".', $url, $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 08:14:30 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         $statusCode = $res->getStatusCode(); | 
					
						
							|  |  |  |         if (200 !== $statusCode) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning(sprintf('Trying to grab "%s" resulted in status code %d.', $url, $statusCode)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $body       = (string) $res->getBody(); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $json       = json_decode($body, true); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         if (false === $json || null === $json) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning(sprintf('Trying to grab "%s" resulted in bad JSON.', $url)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $date       = Carbon::createFromFormat('Y-m-d', $json['date'], config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-04-02 15:40:33 +02:00
										 |  |  |         if (null === $date) { | 
					
						
							| 
									
										
										
										
											2023-11-28 04:45:07 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         $this->saveRates($currency, $date, $json['rates']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function saveRates(TransactionCurrency $currency, Carbon $date, array $rates): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($rates as $code => $rate) { | 
					
						
							|  |  |  |             $to = $this->getCurrency($code); | 
					
						
							|  |  |  |             if (null === $to) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Currency %s is not in use, do not save rate.', $code)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Currency %s is in use.', $code)); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $this->saveRate($currency, $to, $date, $rate); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |     private function getCurrency(string $code): ?TransactionCurrency | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // if we have it already, don't bother searching for it again.
 | 
					
						
							|  |  |  |         if (array_key_exists($code, $this->active)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Already know what the result is of searching for %s', $code)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             return $this->active[$code]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // find it in the database.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $currency            = $this->repository->findByCode($code); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         if (null === $currency) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Did not find currency %s.', $code)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             $this->active[$code] = null; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (false === $currency->enabled) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Currency %s is not enabled.', $code)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             $this->active[$code] = null; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Currency %s is enabled.', $code)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         $this->active[$code] = $currency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $currency; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function saveRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date, float $rate): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($this->users as $user) { | 
					
						
							|  |  |  |             $this->repository->setUser($user); | 
					
						
							|  |  |  |             $existing = $this->repository->getExchangeRate($from, $to, $date); | 
					
						
							|  |  |  |             if (null === $existing) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Saved rate from %s to %s for user #%d.', $from->code, $to->code, $user->id)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |                 $this->repository->setExchangeRate($from, $to, $date, $rate); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function setDate(Carbon $date): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newDate    = clone $date; | 
					
						
							|  |  |  |         $newDate->startOfDay(); | 
					
						
							|  |  |  |         $this->date = $newDate; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | } |