| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * ReportSum.php | 
					
						
							| 
									
										
										
										
											2020-01-23 20:35:02 +01:00
										 |  |  |  * Copyright (c) 2020 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  | namespace FireflyIII\Console\Commands\Integrity; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  | use FireflyIII\Console\Commands\ShowsFriendlyMessages; | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-06-08 15:20:22 +02:00
										 |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 06:48:58 +01:00
										 |  |  | class ReportsSums extends Command | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |     use ShowsFriendlyMessages; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |     protected $description = 'Report on the total sum of transactions. Must be 0.'; | 
					
						
							| 
									
										
										
										
											2024-12-27 06:48:58 +01:00
										 |  |  |     protected $signature   = 'integrity:total-sums'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Execute the console command. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle(): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |         $this->reportSum(); | 
					
						
							| 
									
										
										
										
											2020-03-21 15:43:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |      * Reports for each user when the sum of their transactions is not zero. | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |     private function reportSum(): void | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |         /** @var UserRepositoryInterface $userRepository */ | 
					
						
							|  |  |  |         $userRepository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var User $user */ | 
					
						
							|  |  |  |         foreach ($userRepository->all() as $user) { | 
					
						
							| 
									
										
										
										
											2024-12-14 08:10:58 +01:00
										 |  |  |             $sum     = (string) $user->transactions()->selectRaw('SUM(amount) as total')->value('total'); | 
					
						
							|  |  |  |             $foreign = (string) $user->transactions()->selectRaw('SUM(foreign_amount) as total')->value('total'); | 
					
						
							|  |  |  |             $sum     = '' === $sum ? '0' : $sum; | 
					
						
							|  |  |  |             $foreign = '' === $foreign ? '0' : $foreign; | 
					
						
							| 
									
										
										
										
											2025-06-09 05:32:04 +02:00
										 |  |  |             $sum     = Steam::floatalize($sum); | 
					
						
							| 
									
										
										
										
											2025-06-08 15:20:22 +02:00
										 |  |  |             $foreign = Steam::floatalize($foreign); | 
					
						
							| 
									
										
										
										
											2024-12-14 08:10:58 +01:00
										 |  |  |             $total   = bcadd($sum, $foreign); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 08:10:58 +01:00
										 |  |  |             if (0 !== bccomp($total, '0')) { | 
					
						
							|  |  |  |                 $message = sprintf('Error: Transactions for user #%d (%s) are off by %s!', $user->id, $user->email, $total); | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |                 $this->friendlyError($message); | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-12-14 08:10:58 +01:00
										 |  |  |             if (0 === bccomp($total, '0')) { | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |                 $this->friendlyPositive(sprintf('Amount integrity OK for user #%d', $user->id)); | 
					
						
							| 
									
										
										
										
											2019-03-23 08:10:59 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |