| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2025-09-25 19:11:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * PeriodStatisticRepository.php | 
					
						
							|  |  |  |  * Copyright (c) 2025 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\PeriodStatistic; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Models\PeriodStatistic; | 
					
						
							| 
									
										
										
										
											2025-10-03 05:53:50 +02:00
										 |  |  | use FireflyIII\Models\UserGroup; | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; | 
					
						
							|  |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2025-09-26 19:32:53 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2025-09-29 05:22:25 +02:00
										 |  |  | use Override; | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  | class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, UserGroupInterface | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |     use UserGroupTrait; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |     public function findPeriodStatistics(Model $model, Carbon $start, Carbon $end, array $types): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $model->primaryPeriodStatistics() | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |             ->where('start', $start) | 
					
						
							|  |  |  |             ->where('end', $end) | 
					
						
							|  |  |  |             ->whereIn('type', $types) | 
					
						
							|  |  |  |             ->get() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $model->primaryPeriodStatistics() | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |             ->where('start', $start) | 
					
						
							|  |  |  |             ->where('end', $end) | 
					
						
							|  |  |  |             ->where('type', $type) | 
					
						
							|  |  |  |             ->get() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function saveStatistic(Model $model, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |         $stat                          = new PeriodStatistic(); | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |         $stat->primaryStatable()->associate($model); | 
					
						
							|  |  |  |         $stat->transaction_currency_id = $currencyId; | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |         $stat->user_group_id           = $this->getUserGroup()->id; | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |         $stat->start                   = $start; | 
					
						
							|  |  |  |         $stat->start_tz                = $start->format('e'); | 
					
						
							|  |  |  |         $stat->end                     = $end; | 
					
						
							|  |  |  |         $stat->end_tz                  = $end->format('e'); | 
					
						
							|  |  |  |         $stat->amount                  = $amount; | 
					
						
							|  |  |  |         $stat->count                   = $count; | 
					
						
							|  |  |  |         $stat->type                    = $type; | 
					
						
							|  |  |  |         $stat->save(); | 
					
						
							| 
									
										
										
										
											2025-09-25 19:11:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-26 19:43:39 +02:00
										 |  |  |         Log::debug(sprintf( | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |             'Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.', | 
					
						
							|  |  |  |             $stat->id, | 
					
						
							|  |  |  |             $model::class, | 
					
						
							|  |  |  |             $model->id, | 
					
						
							|  |  |  |             $stat->transaction_currency_id, | 
					
						
							|  |  |  |             $stat->start->toW3cString(), | 
					
						
							|  |  |  |             $stat->end->toW3cString(), | 
					
						
							|  |  |  |             $count, | 
					
						
							|  |  |  |             $amount | 
					
						
							|  |  |  |         )); | 
					
						
							| 
									
										
										
										
											2025-09-26 19:32:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  |         return $stat; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function allInRangeForModel(Model $model, Carbon $start, Carbon $end): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-26 19:32:53 +02:00
										 |  |  |         return $model->primaryPeriodStatistics()->where('start', '>=', $start)->where('end', '<=', $end)->get(); | 
					
						
							| 
									
										
										
										
											2025-09-26 06:05:37 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-09-26 19:38:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function deleteStatisticsForModel(Model $model, Carbon $date): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $model->primaryPeriodStatistics()->where('start', '<=', $date)->where('end', '>=', $date)->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-29 05:22:25 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |     public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->userGroup->periodStatistics() | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |             ->where('type', 'LIKE', sprintf('%s%%', $prefix)) | 
					
						
							|  |  |  |             ->where('start', '>=', $start)->where('end', '<=', $end)->get() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-29 05:22:25 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |     public function savePrefixedStatistic(string $prefix, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-29 05:22:25 +02:00
										 |  |  |         $stat                          = new PeriodStatistic(); | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |         $stat->transaction_currency_id = $currencyId; | 
					
						
							|  |  |  |         $stat->user_group_id           = $this->getUserGroup()->id; | 
					
						
							|  |  |  |         $stat->start                   = $start; | 
					
						
							|  |  |  |         $stat->start_tz                = $start->format('e'); | 
					
						
							|  |  |  |         $stat->end                     = $end; | 
					
						
							|  |  |  |         $stat->end_tz                  = $end->format('e'); | 
					
						
							|  |  |  |         $stat->amount                  = $amount; | 
					
						
							|  |  |  |         $stat->count                   = $count; | 
					
						
							| 
									
										
										
										
											2025-09-29 05:22:25 +02:00
										 |  |  |         $stat->type                    = sprintf('%s_%s', $prefix, $type); | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  |         $stat->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf( | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |             'Saved #%d [currency #%d, type "%s", %s to %s, %d, %s] as new statistic.', | 
					
						
							|  |  |  |             $stat->id, | 
					
						
							|  |  |  |             $stat->transaction_currency_id, | 
					
						
							|  |  |  |             $stat->type, | 
					
						
							|  |  |  |             $stat->start->toW3cString(), | 
					
						
							|  |  |  |             $stat->end->toW3cString(), | 
					
						
							|  |  |  |             $count, | 
					
						
							|  |  |  |             $amount | 
					
						
							|  |  |  |         )); | 
					
						
							| 
									
										
										
										
											2025-09-27 16:07:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $stat; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-03 05:53:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-03 06:04:16 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2025-10-03 05:53:50 +02:00
										 |  |  |     public function deleteStatisticsForPrefix(UserGroup $userGroup, string $prefix, Carbon $date): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $userGroup->periodStatistics()->where('start', '<=', $date)->where('end', '>=', $date)->where('type', 'LIKE', sprintf('%s%%', $prefix))->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-09-25 19:07:02 +02:00
										 |  |  | } |