| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-12-27 07:29:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 06:48:58 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * ConvertsDatesToUTC.php | 
					
						
							|  |  |  |  * Copyright (c) 2024 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/. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 06:48:58 +01:00
										 |  |  | namespace FireflyIII\Console\Commands\Correction; | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Console\Commands\ShowsFriendlyMessages; | 
					
						
							| 
									
										
										
										
											2024-11-22 06:03:29 +01:00
										 |  |  | use FireflyIII\Support\Facades\FireflyConfig; | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | use Illuminate\Database\QueryException; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 06:56:08 +01:00
										 |  |  | class ConvertsDatesToUTC extends Command | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     use ShowsFriendlyMessages; | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     protected $description = 'Convert stored dates to UTC.'; | 
					
						
							| 
									
										
										
										
											2024-12-27 06:56:08 +01:00
										 |  |  |     protected $signature   = 'correction:convert-to-utc'; | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function handle(): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-03 15:04:15 +01:00
										 |  |  |         $this->friendlyWarning('Please do not use this command right now.'); | 
					
						
							| 
									
										
										
										
											2024-12-27 07:29:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 08:02:05 +01:00
										 |  |  |         // this variable is ALWAYS en_US.
 | 
					
						
							|  |  |  |         // stops phpstan complaining about dead code.
 | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  |         if ('en_US' === config('app.fallback_locale')) { | 
					
						
							| 
									
										
										
										
											2025-01-04 08:02:05 +01:00
										 |  |  |             return Command::SUCCESS; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-27 07:29:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * @var string $model | 
					
						
							|  |  |  |          * @var array  $fields | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         foreach (CorrectsTimezoneInformation::$models as $model => $fields) { | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |             $this->ConvertModeltoUTC($model, $fields); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-22 06:03:29 +01:00
										 |  |  |         // tell the system we are now in UTC mode.
 | 
					
						
							|  |  |  |         FireflyConfig::set('utc', true); | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return Command::SUCCESS; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function ConvertModeltoUTC(string $model, array $fields): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var string $field */ | 
					
						
							|  |  |  |         foreach ($fields as $field) { | 
					
						
							|  |  |  |             $this->convertFieldtoUTC($model, $field); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  |     private function convertFieldtoUTC(string $model, string $field): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |         $this->info(sprintf('Converting %s.%s to UTC', $model, $field)); | 
					
						
							|  |  |  |         $shortModel    = str_replace('FireflyIII\Models\\', '', $model); | 
					
						
							|  |  |  |         $timezoneField = sprintf('%s_tz', $field); | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  |         $items         = new Collection(); | 
					
						
							|  |  |  |         $timeZone      = config('app.timezone'); | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $items = $model::where($timezoneField, $timeZone)->get(); | 
					
						
							|  |  |  |         } catch (QueryException $e) { | 
					
						
							|  |  |  |             $this->friendlyError(sprintf('Cannot find timezone information to field "%s" of model "%s". Field does not exist.', $field, $shortModel)); | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (0 === $items->count()) { | 
					
						
							|  |  |  |             $this->friendlyPositive(sprintf('All timezone information is UTC in field "%s" of model "%s".', $field, $shortModel)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->friendlyInfo(sprintf('Converting field "%s" of model "%s" to UTC.', $field, $shortModel)); | 
					
						
							|  |  |  |         $items->each( | 
					
						
							| 
									
										
										
										
											2024-11-11 04:10:58 +01:00
										 |  |  |             function ($item) use ($field, $timezoneField): void { | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |                 /** @var Carbon $date */ | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  |                 $date                   = Carbon::parse($item->{$field}, $item->{$timezoneField}); // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |                 $date->setTimezone('UTC'); | 
					
						
							| 
									
										
										
										
											2025-01-03 15:04:15 +01:00
										 |  |  |                 $item->{$field}         = $date->format('Y-m-d H:i:s'); // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2025-01-04 08:02:05 +01:00
										 |  |  |                 $item->{$timezoneField} = 'UTC';                        // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2024-11-09 12:19:01 +01:00
										 |  |  |                 $item->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |