| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankRepository.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\PiggyBank; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use FireflyIII\Events\Model\PiggyBank\ChangedAmount; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2024-12-04 06:38:47 +01:00
										 |  |  | use FireflyIII\Factory\PiggyBankFactory; | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2020-05-07 06:44:01 +02:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  | use FireflyIII\Models\PiggyBankRepetition; | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-25 19:09:05 +01:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2024-01-05 10:55:46 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\Storage; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use Override; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class PiggyBankRepository. | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  | class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInterface | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-21 14:20:40 +01:00
										 |  |  |     use ModifiesPiggyBanks; | 
					
						
							| 
									
										
										
										
											2020-09-07 13:03:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  |     use UserGroupTrait; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |     public function destroyAll(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-05 10:55:46 +01:00
										 |  |  |         Log::channel('audit')->info('Delete all piggy banks through destroyAll'); | 
					
						
							| 
									
										
										
										
											2025-01-03 04:32:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |             ->where('accounts.user_id', $this->user->id) | 
					
						
							|  |  |  |             ->delete() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-16 13:16:04 +02:00
										 |  |  |     public function findPiggyBank(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug('Searching for piggy information.'); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $piggyBankId) { | 
					
						
							| 
									
										
										
										
											2023-11-05 08:15:17 +01:00
										 |  |  |             $searchResult = $this->find($piggyBankId); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if ($searchResult instanceof PiggyBank) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |                 Log::debug(sprintf('Found piggy based on #%d, will return it.', $piggyBankId)); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $searchResult; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $piggyBankName) { | 
					
						
							| 
									
										
										
										
											2023-11-05 08:15:17 +01:00
										 |  |  |             $searchResult = $this->findByName($piggyBankName); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if ($searchResult instanceof PiggyBank) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |                 Log::debug(sprintf('Found piggy based on "%s", will return it.', $piggyBankName)); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $searchResult; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug('Found no piggy bank.'); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function find(int $piggyBankId): ?PiggyBank | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-18 16:18:06 +01:00
										 |  |  |         return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |             ->where('accounts.user_id', $this->user->id) | 
					
						
							|  |  |  |             ->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Find by name or return NULL. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findByName(string $name): ?PiggyBank | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-18 16:18:06 +01:00
										 |  |  |         return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |             ->where('accounts.user_id', $this->user->id) | 
					
						
							|  |  |  |             ->where('piggy_banks.name', $name)->first(['piggy_banks.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |     public function getAttachments(PiggyBank $piggyBank): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $set  = $piggyBank->attachments()->get(); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $disk = Storage::disk('upload'); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $set->each( | 
					
						
							| 
									
										
										
										
											2025-01-04 09:15:39 +01:00
										 |  |  |             static function (Attachment $attachment) use ($disk) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |                 $notes                   = $attachment->notes()->first(); | 
					
						
							|  |  |  |                 $attachment->file_exists = $disk->exists($attachment->fileName()); | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |                 $attachment->notes_text  = null !== $notes ? $notes->text : ''; | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $attachment; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get current amount saved in piggy bank. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-08-03 07:13:57 +02:00
										 |  |  |     public function getCurrentPrimaryCurrencyAmount(PiggyBank $piggyBank, ?Account $account = null): string | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $sum = '0'; | 
					
						
							|  |  |  |         foreach ($piggyBank->accounts as $current) { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if ($account instanceof Account && $account->id !== $current->id) { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $amount = (string) $current->pivot->native_current_amount; | 
					
						
							|  |  |  |             $amount = '' === $amount ? '0' : $amount; | 
					
						
							|  |  |  |             $sum    = bcadd($sum, $amount); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $sum; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 09:27:45 +02:00
										 |  |  |     public function getEvents(PiggyBank $piggyBank): Collection | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-13 20:43:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Used for connecting to a piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-12-22 13:19:23 +01:00
										 |  |  |     public function getExactAmount(PiggyBank $piggyBank, TransactionJournal $journal): string | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug(sprintf('Now in getExactAmount(%d, %d)', $piggyBank->id, $journal->id)); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $operator        = null; | 
					
						
							|  |  |  |         $currency        = null; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         /** @var JournalRepositoryInterface $journalRepost */ | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $journalRepost   = app(JournalRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         $journalRepost->setUser($this->user); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepos */ | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $accountRepos    = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         $accountRepos->setUser($this->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |         $primaryCurrency = app('amount')->getPrimaryCurrencyByUserGroup($this->user->userGroup); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBank->transactionCurrency->code)); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var Transaction $source */ | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $source          = $journal->transactions()->with(['account'])->where('amount', '<', 0)->first(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         /** @var Transaction $destination */ | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $destination     = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first(); | 
					
						
							|  |  |  |         $hits            = 0; | 
					
						
							| 
									
										
										
										
											2025-01-03 07:01:02 +01:00
										 |  |  |         foreach ($piggyBank->accounts as $account) { | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // matches source, which means amount will be removed from piggy:
 | 
					
						
							| 
									
										
										
										
											2025-01-03 07:01:02 +01:00
										 |  |  |             if ($account->id === $source->account_id) { | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  |                 $operator = 'negative'; | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |                 $currency = $accountRepos->getAccountCurrency($source->account) ?? $primaryCurrency; | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |                 Log::debug(sprintf('Currency will draw money out of piggy bank. Source currency is %s', $currency->code)); | 
					
						
							| 
									
										
										
										
											2025-01-03 07:01:02 +01:00
										 |  |  |                 ++$hits; | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             // matches destination, which means amount will be added to piggy.
 | 
					
						
							|  |  |  |             if ($account->id === $destination->account_id) { | 
					
						
							|  |  |  |                 $operator = 'positive'; | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |                 $currency = $accountRepos->getAccountCurrency($destination->account) ?? $primaryCurrency; | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |                 Log::debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code)); | 
					
						
							| 
									
										
										
										
											2025-01-03 07:01:02 +01:00
										 |  |  |                 ++$hits; | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  |         if ($hits > 1) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug(sprintf('Transaction journal is related to %d of the accounts, cannot determine what to do. Return "0".', $hits)); | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  |             return '0'; | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-02 04:54:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         if (null === $operator || null === $currency) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug('Currency is NULL and operator is NULL, return "0".'); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |             return '0'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // currency of the account + the piggy bank currency are almost the same.
 | 
					
						
							|  |  |  |         // which amount from the transaction matches?
 | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $amount          = null; | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         if ((int) $source->transaction_currency_id === $currency->id) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug('Use normal amount'); | 
					
						
							|  |  |  |             $amount = Steam::{$operator}($source->amount); // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         if ((int) $source->foreign_currency_id === $currency->id) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug('Use foreign amount'); | 
					
						
							|  |  |  |             $amount = Steam::{$operator}($source->foreign_amount); // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (null === $amount) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug('No match on currency, so amount remains null, return "0".'); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 18:02:24 +02:00
										 |  |  |             return '0'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount)); | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $currentAmount   = $this->getCurrentAmount($piggyBank); | 
					
						
							|  |  |  |         $room            = bcsub($piggyBank->target_amount, $currentAmount); | 
					
						
							|  |  |  |         $compare         = bcmul($currentAmount, '-1'); | 
					
						
							| 
									
										
										
										
											2022-05-04 20:27:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 16:02:30 +01:00
										 |  |  |         if (0 === bccomp($piggyBank->target_amount, '0')) { | 
					
						
							| 
									
										
										
										
											2022-05-04 20:27:18 +02:00
										 |  |  |             // amount is zero? then the "room" is positive amount of we wish to add or remove.
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             $room = Steam::positive($amount); | 
					
						
							|  |  |  |             Log::debug(sprintf('Room is now %s', $room)); | 
					
						
							| 
									
										
										
										
											2022-05-04 20:27:18 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Log::debug(sprintf('Will add/remove %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // if the amount is positive, make sure it fits in piggy bank:
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:56:10 +02:00
										 |  |  |         if (1 === bccomp($amount, '0') && -1 === bccomp($room, $amount)) { | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             // amount is positive and $room is smaller than $amount
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug(sprintf('Room in piggy bank for extra money is %f', $room)); | 
					
						
							|  |  |  |             Log::debug(sprintf('There is NO room to add %f to piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							|  |  |  |             Log::debug(sprintf('New amount is %f', $room)); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             return $room; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-21 18:35:05 +01:00
										 |  |  |         // amount is negative and $currentAmount is smaller than $amount
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (-1 === bccomp($amount, '0') && 1 === bccomp($compare, $amount)) { | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |             Log::debug(sprintf('Max amount to remove is %f', $currentAmount)); | 
					
						
							|  |  |  |             Log::debug(sprintf('Cannot remove %f from piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); | 
					
						
							|  |  |  |             Log::debug(sprintf('New amount is %f', $compare)); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |             return $compare; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:56:10 +02:00
										 |  |  |         return $amount; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get current amount saved in piggy bank. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCurrentAmount(PiggyBank $piggyBank, ?Account $account = null): string | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |         $sum = '0'; | 
					
						
							|  |  |  |         foreach ($piggyBank->accounts as $current) { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if ($account instanceof Account && $account->id !== $current->id) { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $amount = (string) $current->pivot->current_amount; | 
					
						
							|  |  |  |             $amount = '' === $amount ? '0' : $amount; | 
					
						
							|  |  |  |             $sum    = bcadd($sum, $amount); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-06-07 06:31:14 +02:00
										 |  |  |         Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum)); | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $sum; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return note for piggy bank. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getNoteText(PiggyBank $piggyBank): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|Note $note */ | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |         $note = $piggyBank->notes()->first(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         return (string) $note?->text; | 
					
						
							| 
									
										
										
										
											2018-12-20 05:46:05 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Also add amount in name. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-04 18:02:19 +01:00
										 |  |  |     public function getPiggyBanksWithAmount(): Collection | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-15 16:16:13 +01:00
										 |  |  |         $set = $this->getPiggyBanks(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 21:12:48 +02:00
										 |  |  |         /** @var PiggyBank $piggy */ | 
					
						
							|  |  |  |         foreach ($set as $piggy) { | 
					
						
							| 
									
										
										
										
											2024-12-15 16:11:48 +01:00
										 |  |  |             $currentAmount = $this->getCurrentAmount($piggy); | 
					
						
							| 
									
										
										
										
											2024-12-18 16:18:06 +01:00
										 |  |  |             $piggy->name   = sprintf('%s (%s)', $piggy->name, app('amount')->formatAnything($piggy->transactionCurrency, $currentAmount, false)); | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 12:08:41 +02:00
										 |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function getPiggyBanks(): Collection | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-03-09 09:53:36 +01:00
										 |  |  |         $query = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$this->user instanceof User) { | 
					
						
							| 
									
										
										
										
											2025-03-09 09:53:36 +01:00
										 |  |  |             $query->where('accounts.user_group_id', $this->userGroup->id); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ($this->user instanceof User) { | 
					
						
							| 
									
										
										
										
											2025-03-09 09:53:36 +01:00
										 |  |  |             $query->where('accounts.user_id', $this->user->id); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-09 09:53:36 +01:00
										 |  |  |         return $query | 
					
						
							|  |  |  |             ->with( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'objectGroups', | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->orderBy('piggy_banks.order', 'ASC')->distinct()->get(['piggy_banks.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     public function getRepetition(PiggyBank $piggyBank, bool $overrule = false): ?PiggyBankRepetition | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (false === $overrule) { | 
					
						
							|  |  |  |             throw new FireflyException('[b] Piggy bank repetitions are EOL.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::warning('Piggy bank repetitions are EOL.'); | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|PiggyBankRepetition */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         return $piggyBank->piggyBankRepetitions()->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 07:48:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the suggested amount the user should save per month, or "". | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |         $savePerMonth  = '0'; | 
					
						
							|  |  |  |         $currentAmount = $this->getCurrentAmount($piggyBank); | 
					
						
							|  |  |  |         if (null !== $piggyBank->target_date && $currentAmount < $piggyBank->target_amount) { | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $now             = today(config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-11-30 16:02:30 +01:00
										 |  |  |             $startDate       = null !== $piggyBank->start_date && $piggyBank->start_date->gte($now) ? $piggyBank->start_date : $now; | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |             $diffInMonths    = (int) $startDate->diffInMonths($piggyBank->target_date); | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |             $remainingAmount = bcsub((string) $piggyBank->target_amount, $currentAmount); | 
					
						
							| 
									
										
										
										
											2018-05-26 07:48:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // more than 1 month to go and still need money to save:
 | 
					
						
							|  |  |  |             if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) { | 
					
						
							| 
									
										
										
										
											2024-12-01 18:32:05 +01:00
										 |  |  |                 $savePerMonth = bcdiv($remainingAmount, (string) $diffInMonths); | 
					
						
							| 
									
										
										
										
											2018-05-26 07:48:49 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // less than 1 month to go but still need money to save:
 | 
					
						
							|  |  |  |             if (0 === $diffInMonths && 1 === bccomp($remainingAmount, '0')) { | 
					
						
							|  |  |  |                 $savePerMonth = $remainingAmount; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $savePerMonth; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get for piggy account what is left to put in piggies. | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |     public function leftOnAccount(PiggyBank $piggyBank, Account $account, Carbon $date): string | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |         Log::debug(sprintf('leftOnAccount("%s","%s","%s")', $piggyBank->name, $account->name, $date->format('Y-m-d H:i:s'))); | 
					
						
							| 
									
										
										
										
											2025-02-10 16:47:59 +01:00
										 |  |  |         Log::debug(sprintf('leftOnAccount: Call finalAccountBalance with date/time "%s"', $date->toIso8601String())); | 
					
						
							| 
									
										
										
										
											2025-10-08 07:09:42 +02:00
										 |  |  |         // 2025-10-08 replace finalAccountBalance with accountsBalancesOptimized.
 | 
					
						
							|  |  |  |         $balance = Steam::accountsBalancesOptimized(new Collection()->push($account), $date)[$account->id]; | 
					
						
							|  |  |  |         //$balance = Steam::finalAccountBalance($account, $date)['balance'];
 | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |         Log::debug(sprintf('Balance is: %s', $balance)); | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var Collection $piggies */ | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |         $piggies = $account->piggyBanks; | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var PiggyBank $current */ | 
					
						
							|  |  |  |         foreach ($piggies as $current) { | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  |             $amount  = $this->getCurrentAmount($current, $account); | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |             $balance = bcsub((string) $balance, $amount); | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |             Log::debug(sprintf('Piggy bank: #%d with amount %s, balance is now %s', $current->id, $amount, $balance)); | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |         Log::debug(sprintf('Final balance is: %s', $balance)); | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |         return $balance; | 
					
						
							| 
									
										
										
										
											2017-04-28 07:51:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |     public function purgeAll(): void | 
					
						
							| 
									
										
										
										
											2024-12-01 18:16:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-18 16:13:32 +01:00
										 |  |  |         PiggyBank::withTrashed() | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->whereNotNull('piggy_banks.deleted_at') | 
					
						
							|  |  |  |             ->leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							|  |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |             ->where('accounts.user_id', $this->user->id) | 
					
						
							|  |  |  |             ->with( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'objectGroups', | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             ->delete() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-12-01 18:16:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-04 06:38:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |     public function resetOrder(): void | 
					
						
							| 
									
										
										
										
											2024-12-04 06:38:47 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-06 08:10:31 +01:00
										 |  |  |         $factory       = new PiggyBankFactory(); | 
					
						
							|  |  |  |         $factory->user = $this->user; | 
					
						
							| 
									
										
										
										
											2024-12-04 06:38:47 +01:00
										 |  |  |         $factory->resetOrder(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function searchPiggyBank(string $query, int $limit): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $search = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							|  |  |  |             ->where('accounts.user_id', $this->user->id) | 
					
						
							|  |  |  |             ->with( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'objectGroups', | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             ->orderBy('piggy_banks.order', 'ASC')->distinct() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if ('' !== $query) { | 
					
						
							|  |  |  |             $search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $search->orderBy('piggy_banks.order', 'ASC') | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->orderBy('piggy_banks.name', 'ASC') | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $search->take($limit)->get(['piggy_banks.*']); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-06-04 15:40:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function resetHistory(PiggyBank $piggyBank): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $piggyBank->piggyBankEvents()->delete(); | 
					
						
							| 
									
										
										
										
											2025-06-04 19:18:26 +02:00
										 |  |  |         foreach ($piggyBank->accounts as $account) { | 
					
						
							| 
									
										
										
										
											2025-06-14 10:18:58 +02:00
										 |  |  |             if (0 !== bccomp('0', (string) $account->pivot->current_amount)) { | 
					
						
							| 
									
										
										
										
											2025-06-04 15:40:29 +02:00
										 |  |  |                 event(new ChangedAmount($piggyBank, $account->pivot->current_amount, null, null)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |