| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * JournalServiceTrait.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:35 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01: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/>. | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Internal\Support; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-10-13 11:50:04 +02:00
										 |  |  | use FireflyIII\Factory\AccountMetaFactory; | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  | use FireflyIII\Factory\TagFactory; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Support\NullArrayObject; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2018-02-23 15:12:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait JournalServiceTrait | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait JournalServiceTrait | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-26 07:57:48 +02:00
										 |  |  |     private AccountRepositoryInterface  $accountRepository; | 
					
						
							|  |  |  |     private BudgetRepositoryInterface   $budgetRepository; | 
					
						
							|  |  |  |     private CategoryRepositoryInterface $categoryRepository; | 
					
						
							|  |  |  |     private TagFactory                  $tagFactory; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 17:26:49 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param string $transactionType | 
					
						
							|  |  |  |      * @param string $direction | 
					
						
							|  |  |  |      * @param array  $data | 
					
						
							| 
									
										
										
										
											2019-03-05 17:26:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-10-30 06:45:56 +01:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2020-10-04 17:11:59 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-03-05 17:26:49 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-30 06:45:56 +01:00
										 |  |  |     protected function getAccount(string $transactionType, string $direction, array $data): ?Account | 
					
						
							| 
									
										
										
										
											2019-03-05 17:26:49 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |         // some debug logging:
 | 
					
						
							| 
									
										
										
										
											2019-10-13 11:50:04 +02:00
										 |  |  |         Log::debug(sprintf('Now in getAccount(%s)', $direction), $data); | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // expected type of source account, in order of preference
 | 
					
						
							|  |  |  |         /** @var array $array */ | 
					
						
							|  |  |  |         $array         = config('firefly.expected_source_types'); | 
					
						
							|  |  |  |         $expectedTypes = $array[$direction]; | 
					
						
							|  |  |  |         unset($array); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // and now try to find it, based on the type of transaction.
 | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         $message = 'Transaction = %s, %s account should be in: %s. Direction is %s.'; | 
					
						
							| 
									
										
										
										
											2021-07-21 06:23:42 +02:00
										 |  |  |         Log::debug(sprintf($message, $transactionType, $direction, implode(', ', $expectedTypes[$transactionType] ?? ['UNKNOWN']), $direction)); | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         $result       = $this->findAccountById($data, $expectedTypes[$transactionType]); | 
					
						
							|  |  |  |         $result       = $this->findAccountByIban($result, $data, $expectedTypes[$transactionType]); | 
					
						
							|  |  |  |         $ibanResult   = $result; | 
					
						
							| 
									
										
										
										
											2023-01-11 18:02:24 +01:00
										 |  |  |         $result       = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]); | 
					
						
							|  |  |  |         $numberResult = $result; | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         $result       = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]); | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |         $nameResult   = $result; | 
					
						
							| 
									
										
										
										
											2023-01-11 18:02:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-18 06:37:05 +01:00
										 |  |  |         // if $result (find by name) is NULL, but IBAN is set, any result of the search by NAME can't overrule
 | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         // this account. In such a case, the name search must be retried with a new name.
 | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |         if (null !== $nameResult && null === $numberResult && null === $ibanResult && '' !== (string)$data['iban'] && '' !== (string)$nameResult->iban) { | 
					
						
							| 
									
										
										
										
											2023-01-11 18:02:24 +01:00
										 |  |  |             $data['name'] = sprintf('%s (%s)', $data['name'], $data['iban']); | 
					
						
							|  |  |  |             Log::debug(sprintf('Search again using the new name, "%s".', $data['name'])); | 
					
						
							|  |  |  |             $result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // the account that Firefly III creates must be "creatable", aka select the one we can create from the list just in case
 | 
					
						
							|  |  |  |         $creatableType = $this->getCreatableType($expectedTypes[$transactionType]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         // if the result is NULL but the ID is set, an account could exist of the wrong type.
 | 
					
						
							|  |  |  |         // that data can be used to create a new account of the right type.
 | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |         if (null === $result && null !== $data['id'] && null !== $creatableType) { | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |             Log::debug(sprintf('Account #%d may exist and be of the wrong type, use data to create one of the right type.', $data['id'])); | 
					
						
							|  |  |  |             $temp = $this->findAccountById(['id' => $data['id']], []); | 
					
						
							|  |  |  |             if (null !== $temp) { | 
					
						
							|  |  |  |                 $tempData = [ | 
					
						
							|  |  |  |                     'name'   => $temp->name, | 
					
						
							|  |  |  |                     'iban'   => $temp->iban, | 
					
						
							|  |  |  |                     'number' => null, | 
					
						
							|  |  |  |                     'bic'    => null, | 
					
						
							|  |  |  |                 ]; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |                 $result   = $this->createAccount(null, $tempData, $creatableType); | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |         if (null === $result && null !== $creatableType) { | 
					
						
							| 
									
										
										
										
											2023-02-18 06:37:05 +01:00
										 |  |  |             Log::debug('If nothing is found, create it.'); | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |             $result = $this->createAccount($result, $data, $creatableType); | 
					
						
							| 
									
										
										
										
											2023-02-18 06:37:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (null === $result) { | 
					
						
							|  |  |  |             Log::debug('If cant be created, return cash account.'); | 
					
						
							|  |  |  |             $result = $this->getCashAccount($result, $data, $expectedTypes[$transactionType]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2019-03-05 17:26:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param array $data | 
					
						
							|  |  |  |      * @param array $types | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function findAccountById(array $data, array $types): ?Account | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // first attempt, find by ID.
 | 
					
						
							|  |  |  |         if (null !== $data['id']) { | 
					
						
							|  |  |  |             $search = $this->accountRepository->find((int)$data['id']); | 
					
						
							|  |  |  |             if (null !== $search && in_array($search->accountType->type, $types, true)) { | 
					
						
							|  |  |  |                 Log::debug( | 
					
						
							|  |  |  |                     sprintf('Found "account_id" object: #%d, "%s" of type %s (1)', $search->id, $search->name, $search->accountType->type) | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |                 return $search; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $search && 0 === count($types)) { | 
					
						
							|  |  |  |                 Log::debug( | 
					
						
							|  |  |  |                     sprintf('Found "account_id" object: #%d, "%s" of type %s (2)', $search->id, $search->name, $search->accountType->type) | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |                 return $search; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug(sprintf('Found no account by ID #%d of types', $data['id']), $types); | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account|null $account | 
					
						
							|  |  |  |      * @param array        $data | 
					
						
							|  |  |  |      * @param array        $types | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function findAccountByIban(?Account $account, array $data, array $types): ?Account | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null !== $account) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name)); | 
					
						
							|  |  |  |             return $account; | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $data['iban'] || '' === $data['iban']) { | 
					
						
							|  |  |  |             Log::debug('IBAN is empty, will not search for IBAN.'); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // find by preferred type.
 | 
					
						
							|  |  |  |         $source = $this->accountRepository->findByIbanNull($data['iban'], [$types[0]]); | 
					
						
							|  |  |  |         // or any expected type.
 | 
					
						
							|  |  |  |         $source = $source ?? $this->accountRepository->findByIbanNull($data['iban'], $types); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null !== $source) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Found "account_iban" object: #%d, %s', $source->id, $source->name)); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return $source; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Found no account with IBAN "%s" of expected types', $data['iban']), $types); | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account|null $account | 
					
						
							|  |  |  |      * @param array        $data | 
					
						
							|  |  |  |      * @param array        $types | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function findAccountByNumber(?Account $account, array $data, array $types): ?Account | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null !== $account) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name)); | 
					
						
							|  |  |  |             return $account; | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $data['number'] || '' === $data['number']) { | 
					
						
							|  |  |  |             Log::debug('Account number is empty, will not search for account number.'); | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // find by preferred type.
 | 
					
						
							|  |  |  |         $source = $this->accountRepository->findByAccountNumber((string)$data['number'], [$types[0]]); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // or any expected type.
 | 
					
						
							|  |  |  |         $source = $source ?? $this->accountRepository->findByAccountNumber((string)$data['number'], $types); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null !== $source) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Found account: #%d, %s', $source->id, $source->name)); | 
					
						
							| 
									
										
										
										
											2021-04-22 06:18:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return $source; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Found no account with account number "%s" of expected types', $data['number']), $types); | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-22 06:18:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account|null $account | 
					
						
							|  |  |  |      * @param array        $data | 
					
						
							|  |  |  |      * @param array        $types | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function findAccountByName(?Account $account, array $data, array $types): ?Account | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null !== $account) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name)); | 
					
						
							|  |  |  |             return $account; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $data['name'] || '' === $data['name']) { | 
					
						
							|  |  |  |             Log::debug('Account name is empty, will not search for account name.'); | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // find by preferred type.
 | 
					
						
							|  |  |  |         $source = $this->accountRepository->findByName($data['name'], [$types[0]]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // or any expected type.
 | 
					
						
							|  |  |  |         $source = $source ?? $this->accountRepository->findByName($data['name'], $types); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $source) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Found "account_name" object: #%d, %s', $source->id, $source->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $source; | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug(sprintf('Found no account with account name "%s" of expected types', $data['name']), $types); | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2021-04-22 06:18:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param array $types | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return null|string | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getCreatableType(array $types): ?string | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $result = null; | 
					
						
							|  |  |  |         $list   = config('firefly.dynamic_creation_allowed'); | 
					
						
							|  |  |  |         /** @var string $type */ | 
					
						
							|  |  |  |         foreach ($types as $type) { | 
					
						
							|  |  |  |             if (true === in_array($type, $list, true)) { | 
					
						
							|  |  |  |                 $result = $type; | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:02:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 06:18:46 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account|null $account | 
					
						
							|  |  |  |      * @param array        $data | 
					
						
							|  |  |  |      * @param string       $preferredType | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:54:58 +02:00
										 |  |  |      * @return Account|null | 
					
						
							| 
									
										
										
										
											2020-10-04 17:11:59 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-08 11:21:20 +02:00
										 |  |  |     private function createAccount(?Account $account, array $data, string $preferredType): ?Account | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-14 17:23:58 +02:00
										 |  |  |         Log::debug('Now in createAccount()', $data); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |         // return new account.
 | 
					
						
							| 
									
										
										
										
											2020-05-09 18:03:32 +02:00
										 |  |  |         if (null !== $account) { | 
					
						
							|  |  |  |             Log::debug( | 
					
						
							|  |  |  |                 sprintf( | 
					
						
							| 
									
										
										
										
											2021-12-31 06:13:05 +01:00
										 |  |  |                     'Was given %s account #%d ("%s") so will simply return that.', | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |                     $account->accountType->type, | 
					
						
							|  |  |  |                     $account->id, | 
					
						
							|  |  |  |                     $account->name | 
					
						
							| 
									
										
										
										
											2020-05-09 18:03:32 +02:00
										 |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |         if (null === $account) { | 
					
						
							|  |  |  |             // final attempt, create it.
 | 
					
						
							|  |  |  |             if (AccountType::ASSET === $preferredType) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |                 throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', json_encode($data))); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-09 18:03:32 +02:00
										 |  |  |             // fix name of account if only IBAN is given:
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             if ('' === (string)$data['name'] && '' !== (string)$data['iban']) { | 
					
						
							| 
									
										
										
										
											2020-05-09 18:03:32 +02:00
										 |  |  |                 Log::debug(sprintf('Account name is now IBAN ("%s")', $data['iban'])); | 
					
						
							|  |  |  |                 $data['name'] = $data['iban']; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-04-08 11:21:20 +02:00
										 |  |  |             // fix name of account if only number is given:
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             if ('' === (string)$data['name'] && '' !== (string)$data['number']) { | 
					
						
							| 
									
										
										
										
											2021-04-08 11:21:20 +02:00
										 |  |  |                 Log::debug(sprintf('Account name is now account number ("%s")', $data['number'])); | 
					
						
							|  |  |  |                 $data['name'] = $data['number']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // if name is still NULL, return NULL.
 | 
					
						
							| 
									
										
										
										
											2022-10-30 06:45:56 +01:00
										 |  |  |             if ('' === (string)$data['name']) { | 
					
						
							|  |  |  |                 Log::debug('Account name is still NULL, return NULL.'); | 
					
						
							| 
									
										
										
										
											2021-04-08 11:21:20 +02:00
										 |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-10-30 06:45:56 +01:00
										 |  |  |             //$data['name'] = $data['name'] ?? '(no name)';
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 09:35:25 +01:00
										 |  |  |             $account = $this->accountRepository->store( | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-04-05 21:52:55 +02:00
										 |  |  |                     'account_type_id'   => null, | 
					
						
							|  |  |  |                     'account_type_name' => $preferredType, | 
					
						
							|  |  |  |                     'name'              => $data['name'], | 
					
						
							|  |  |  |                     'virtual_balance'   => null, | 
					
						
							|  |  |  |                     'active'            => true, | 
					
						
							|  |  |  |                     'iban'              => $data['iban'], | 
					
						
							|  |  |  |                     'currency_id'       => $data['currency_id'] ?? null, | 
					
						
							|  |  |  |                     'order'             => $this->accountRepository->maxOrder($preferredType), | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |                 ] | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |             // store BIC
 | 
					
						
							|  |  |  |             if (null !== $data['bic']) { | 
					
						
							|  |  |  |                 /** @var AccountMetaFactory $metaFactory */ | 
					
						
							|  |  |  |                 $metaFactory = app(AccountMetaFactory::class); | 
					
						
							| 
									
										
										
										
											2020-04-10 07:29:15 +02:00
										 |  |  |                 $metaFactory->create(['account_id' => $account->id, 'name' => 'BIC', 'data' => $data['bic']]); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             // store account number
 | 
					
						
							|  |  |  |             if (null !== $data['number']) { | 
					
						
							|  |  |  |                 /** @var AccountMetaFactory $metaFactory */ | 
					
						
							|  |  |  |                 $metaFactory = app(AccountMetaFactory::class); | 
					
						
							| 
									
										
										
										
											2021-03-30 06:32:42 +02:00
										 |  |  |                 $metaFactory->create(['account_id' => $account->id, 'name' => 'account_number', 'data' => $data['number']]); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:38 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account|null $account | 
					
						
							|  |  |  |      * @param array        $data | 
					
						
							|  |  |  |      * @param array        $types | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Account|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getCashAccount(?Account $account, array $data, array $types): ?Account | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // return cash account.
 | 
					
						
							|  |  |  |         if (null === $account && '' === (string)$data['name'] | 
					
						
							|  |  |  |             && in_array(AccountType::CASH, $types, true)) { | 
					
						
							|  |  |  |             $account = $this->accountRepository->getCashAccount(); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Cannot return cash account, return input instead.'); | 
					
						
							|  |  |  |         return $account; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param string $amount | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function getAmount(string $amount): string | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if ('' === $amount) { | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount)); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug(sprintf('Now in getAmount("%s")', $amount)); | 
					
						
							|  |  |  |         if (0 === bccomp('0', $amount)) { | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param string|null $amount | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return string|null | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function getForeignAmount(?string $amount): ?string | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $amount) { | 
					
						
							|  |  |  |             Log::debug('No foreign amount info in array. Return NULL'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if ('' === $amount) { | 
					
						
							|  |  |  |             Log::debug('Foreign amount is empty string, return NULL.'); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (0 === bccomp('0', $amount)) { | 
					
						
							|  |  |  |             Log::debug('Foreign amount is 0.0, return NULL.'); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug(sprintf('Foreign amount is %s', $amount)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $amount; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param NullArrayObject    $data | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function storeBudget(TransactionJournal $journal, NullArrayObject $data): void | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) { | 
					
						
							|  |  |  |             $journal->budgets()->sync([]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $budget = $this->budgetRepository->findBudget($data['budget_id'], $data['budget_name']); | 
					
						
							|  |  |  |         if (null !== $budget) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Link budget #%d to journal #%d', $budget->id, $journal->id)); | 
					
						
							|  |  |  |             $journal->budgets()->sync([$budget->id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // if the budget is NULL, sync empty.
 | 
					
						
							|  |  |  |         $journal->budgets()->sync([]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param NullArrayObject    $data | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:23:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function storeCategory(TransactionJournal $journal, NullArrayObject $data): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $category = $this->categoryRepository->findCategory($data['category_id'], $data['category_name']); | 
					
						
							|  |  |  |         if (null !== $category) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Link category #%d to journal #%d', $category->id, $journal->id)); | 
					
						
							|  |  |  |             $journal->categories()->sync([$category->id]); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // if the category is NULL, sync empty.
 | 
					
						
							|  |  |  |         $journal->categories()->sync([]); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param string|null        $notes | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function storeNotes(TransactionJournal $journal, ?string $notes): void | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $notes = (string)$notes; | 
					
						
							|  |  |  |         $note  = $journal->notes()->first(); | 
					
						
							|  |  |  |         if ('' !== $notes) { | 
					
						
							|  |  |  |             if (null === $note) { | 
					
						
							|  |  |  |                 $note = new Note(); | 
					
						
							|  |  |  |                 $note->noteable()->associate($journal); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $note->text = $notes; | 
					
						
							|  |  |  |             $note->save(); | 
					
						
							|  |  |  |             Log::debug(sprintf('Stored notes for journal #%d', $journal->id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('' === $notes && null !== $note) { | 
					
						
							|  |  |  |             // try to delete existing notes.
 | 
					
						
							|  |  |  |             $note->delete(); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Link tags to journal. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * @param array|null         $tags | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function storeTags(TransactionJournal $journal, ?array $tags): void | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Now in storeTags()', $tags ?? []); | 
					
						
							|  |  |  |         $this->tagFactory->setUser($journal->user); | 
					
						
							|  |  |  |         $set = []; | 
					
						
							|  |  |  |         if (!is_array($tags)) { | 
					
						
							|  |  |  |             Log::debug('Tags is not an array, break.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug('Start of loop.'); | 
					
						
							|  |  |  |         foreach ($tags as $string) { | 
					
						
							|  |  |  |             $string = (string)$string; | 
					
						
							|  |  |  |             Log::debug(sprintf('Now at tag "%s"', $string)); | 
					
						
							|  |  |  |             if ('' !== $string) { | 
					
						
							|  |  |  |                 $tag = $this->tagFactory->findOrCreate($string); | 
					
						
							|  |  |  |                 if (null !== $tag) { | 
					
						
							| 
									
										
										
										
											2023-07-29 06:17:09 +02:00
										 |  |  |                     $set[] = (int)$tag->id; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-07-29 06:17:09 +02:00
										 |  |  |         $set = array_unique($set); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('End of loop.'); | 
					
						
							|  |  |  |         Log::debug(sprintf('Total nr. of tags: %d', count($tags)), $tags); | 
					
						
							|  |  |  |         $journal->tags()->sync($set); | 
					
						
							| 
									
										
										
										
											2021-03-15 10:31:11 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |