| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +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-18 19:55:35 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +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-18 19:55:35 +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-18 19:55:35 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  | use FireflyIII\Events\StoredAccount; | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  | use FireflyIII\Services\Internal\Support\AccountServiceTrait; | 
					
						
							| 
									
										
										
										
											2020-01-01 14:27:43 +01:00
										 |  |  | use FireflyIII\Services\Internal\Support\LocationServiceTrait; | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  | use FireflyIII\Services\Internal\Update\AccountUpdateService; | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:47 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |  * Factory to create or return accounts. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  |  * Class AccountFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-01 14:27:43 +01:00
										 |  |  |     use AccountServiceTrait, LocationServiceTrait; | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |     protected AccountRepositoryInterface $accountRepository; | 
					
						
							|  |  |  |     protected array                      $validAssetFields; | 
					
						
							|  |  |  |     protected array                      $validCCFields; | 
					
						
							|  |  |  |     protected array                      $validFields; | 
					
						
							|  |  |  |     private array                        $canHaveVirtual; | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |     private array                        $canHaveOpeningBalance; | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |     private User                         $user; | 
					
						
							| 
									
										
										
										
											2019-08-24 08:22:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-12-21 16:38:10 +01:00
										 |  |  |      * AccountFactory constructor. | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-12-21 16:38:10 +01:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         $this->accountRepository     = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->canHaveVirtual        = config('firefly.can_have_virtual_amounts'); | 
					
						
							|  |  |  |         $this->canHaveOpeningBalance = config('firefly.can_have_opening_balance'); | 
					
						
							|  |  |  |         $this->validAssetFields      = config('firefly.valid_asset_fields'); | 
					
						
							|  |  |  |         $this->validCCFields         = config('firefly.valid_cc_fields'); | 
					
						
							|  |  |  |         $this->validFields           = config('firefly.valid_account_fields'); | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $accountName | 
					
						
							|  |  |  |      * @param string $accountType | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findOrCreate(string $accountName, string $accountType): Account | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         Log::debug(sprintf('findOrCreate("%s", "%s")', $accountName, $accountType)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $type = $this->accountRepository->getAccountTypeByType($accountType); | 
					
						
							|  |  |  |         if (null === $type) { | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('Cannot find account type "%s"', $accountType)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $return = $this->user->accounts->where('account_type_id', $type->id)->where('name', $accountName)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null === $return) { | 
					
						
							|  |  |  |             Log::debug('Found nothing. Will create a new one.'); | 
					
						
							|  |  |  |             $return = $this->create( | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-04-05 21:52:55 +02:00
										 |  |  |                     'user_id'           => $this->user->id, | 
					
						
							|  |  |  |                     'name'              => $accountName, | 
					
						
							|  |  |  |                     'account_type_id'   => $type->id, | 
					
						
							|  |  |  |                     'account_type_name' => null, | 
					
						
							|  |  |  |                     'virtual_balance'   => '0', | 
					
						
							|  |  |  |                     'iban'              => null, | 
					
						
							|  |  |  |                     'active'            => true, | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2020-08-01 05:32:38 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-18 19:55:35 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create(array $data): Account | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         $type         = $this->getAccountType($data); | 
					
						
							| 
									
										
										
										
											2019-06-16 13:15:32 +02:00
										 |  |  |         $data['iban'] = $this->filterIban($data['iban'] ?? null); | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // account may exist already:
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         $return = $this->find($data['name'], $type->type); | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         if (null !== $return) { | 
					
						
							|  |  |  |             return $return; | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         $return = $this->createAccount($type, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         event(new StoredAccount($return)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return AccountType|null | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |     protected function getAccountType(array $data): ?AccountType | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         $accountTypeId   = array_key_exists('account_type_id', $data) ? (int)$data['account_type_id'] : 0; | 
					
						
							|  |  |  |         $accountTypeName = array_key_exists('account_type_name', $data) ? $data['account_type_name'] : null; | 
					
						
							|  |  |  |         $result          = null; | 
					
						
							|  |  |  |         // find by name or ID
 | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |         if ($accountTypeId > 0) { | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |             $result = AccountType::find($accountTypeId); | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         if (null !== $accountTypeName) { | 
					
						
							|  |  |  |             $result = $this->accountRepository->getAccountTypeByType($accountTypeName); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // try with type:
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:06:08 +02:00
										 |  |  |         if (null === $result) { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |             $types = config(sprintf('firefly.accountTypeByIdentifier.%s', $accountTypeName)) ?? []; | 
					
						
							| 
									
										
										
										
											2021-06-13 07:04:18 +02:00
										 |  |  |             if (!empty($types)) { | 
					
						
							| 
									
										
										
										
											2018-08-05 20:42:45 +02:00
										 |  |  |                 $result = AccountType::whereIn('type', $types)->first(); | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:47 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-24 18:55:02 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |         if (null === $result) { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |             Log::warning(sprintf('Found NO account type based on %d and "%s"', $accountTypeId, $accountTypeName)); | 
					
						
							|  |  |  |             throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $accountTypeId, $accountTypeName)); | 
					
						
							| 
									
										
										
										
											2019-03-17 17:05:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         Log::debug(sprintf('Found account type based on %d and "%s": "%s"', $accountTypeId, $accountTypeName, $result->type)); | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 18:55:02 +01:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $accountName | 
					
						
							|  |  |  |      * @param string $accountType | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Account|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function find(string $accountName, string $accountType): ?Account | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = AccountType::whereType($accountType)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @param AccountType $type | 
					
						
							|  |  |  |      * @param array       $data | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @throws \JsonException | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function createAccount(AccountType $type, array $data): Account | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->accountRepository->resetAccountOrder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create it:
 | 
					
						
							|  |  |  |         $virtualBalance = array_key_exists('virtual_balance', $data) ? $data['virtual_balance'] : null; | 
					
						
							|  |  |  |         $active         = array_key_exists('active', $data) ? $data['active'] : true; | 
					
						
							|  |  |  |         $databaseData   = ['user_id'         => $this->user->id, | 
					
						
							|  |  |  |                            'account_type_id' => $type->id, | 
					
						
							|  |  |  |                            'name'            => $data['name'], | 
					
						
							|  |  |  |                            'order'           => 25000, | 
					
						
							|  |  |  |                            'virtual_balance' => $virtualBalance, | 
					
						
							|  |  |  |                            'active'          => $active, | 
					
						
							|  |  |  |                            'iban'            => $data['iban'], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         // fix virtual balance when it's empty
 | 
					
						
							|  |  |  |         if ('' === (string)$databaseData['virtual_balance']) { | 
					
						
							|  |  |  |             $databaseData['virtual_balance'] = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // remove virtual balance when not an asset account or a liability
 | 
					
						
							|  |  |  |         if (!in_array($type->type, $this->canHaveVirtual, true)) { | 
					
						
							|  |  |  |             $databaseData['virtual_balance'] = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // create account!
 | 
					
						
							|  |  |  |         $account = Account::create($databaseData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update meta data:
 | 
					
						
							|  |  |  |         $data = $this->cleanMetaDataArray($account, $data); | 
					
						
							|  |  |  |         $this->storeMetaData($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create opening balance
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $this->storeOpeningBalance($account, $data); | 
					
						
							|  |  |  |         } catch (FireflyException $e) { | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create credit liability data (if relevant)
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $this->storeCreditLiability($account, $data); | 
					
						
							|  |  |  |         } catch (FireflyException $e) { | 
					
						
							|  |  |  |             Log::error($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // create notes
 | 
					
						
							|  |  |  |         $notes = array_key_exists('notes', $data) ? $data['notes'] : ''; | 
					
						
							|  |  |  |         $this->updateNote($account, $notes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create location
 | 
					
						
							|  |  |  |         $this->storeNewLocation($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // set order
 | 
					
						
							|  |  |  |         $this->storeOrder($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // refresh and return
 | 
					
						
							|  |  |  |         $account->refresh(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @throws \JsonException | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function cleanMetaDataArray(Account $account, array $data): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $currencyId   = array_key_exists('currency_id', $data) ? (int)$data['currency_id'] : 0; | 
					
						
							|  |  |  |         $currencyCode = array_key_exists('currency_code', $data) ? (string)$data['currency_code'] : ''; | 
					
						
							|  |  |  |         $accountRole  = array_key_exists('account_role', $data) ? (string)$data['account_role'] : null; | 
					
						
							|  |  |  |         $currency     = $this->getCurrency($currencyId, $currencyCode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // only asset account may have a role:
 | 
					
						
							|  |  |  |         if ($account->accountType->type !== AccountType::ASSET) { | 
					
						
							|  |  |  |             $accountRole = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-10 07:57:04 +02:00
										 |  |  |         // only liability may have direction:
 | 
					
						
							|  |  |  |         if (array_key_exists('liability_direction', $data) && !in_array($account->accountType->type, config('firefly.valid_liabilities'), true)) { | 
					
						
							|  |  |  |             $data['liability_direction'] = null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         $data['account_role'] = $accountRole; | 
					
						
							|  |  |  |         $data['currency_id']  = $currency->id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function storeMetaData(Account $account, array $data): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $fields = $this->validFields; | 
					
						
							|  |  |  |         if ($account->accountType->type === AccountType::ASSET) { | 
					
						
							|  |  |  |             $fields = $this->validAssetFields; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) { | 
					
						
							| 
									
										
										
										
											2021-04-10 07:57:04 +02:00
										 |  |  |             $fields = $this->validCCFields; | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountMetaFactory $factory */ | 
					
						
							|  |  |  |         $factory = app(AccountMetaFactory::class); | 
					
						
							|  |  |  |         foreach ($fields as $field) { | 
					
						
							|  |  |  |             // if the field is set but NULL, skip it.
 | 
					
						
							|  |  |  |             // if the field is set but "", update it.
 | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             if (array_key_exists($field, $data) && null !== $data[$field]) { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // convert boolean value:
 | 
					
						
							|  |  |  |                 if (is_bool($data[$field]) && false === $data[$field]) { | 
					
						
							| 
									
										
										
										
											2021-04-10 07:57:04 +02:00
										 |  |  |                     $data[$field] = 0; | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (is_bool($data[$field]) && true === $data[$field]) { | 
					
						
							| 
									
										
										
										
											2021-04-10 07:57:04 +02:00
										 |  |  |                     $data[$field] = 1; | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $factory->crud($account, $field, (string)$data[$field]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function storeOpeningBalance(Account $account, array $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountType = $account->accountType->type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         if (in_array($accountType, $this->canHaveOpeningBalance, true)) { | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |             if ($this->validOBData($data)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $openingBalance     = $data['opening_balance']; | 
					
						
							|  |  |  |                 $openingBalanceDate = $data['opening_balance_date']; | 
					
						
							|  |  |  |                 $this->updateOBGroupV2($account, $openingBalance, $openingBalanceDate); | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!$this->validOBData($data)) { | 
					
						
							|  |  |  |                 $this->deleteOBGroup($account); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function storeCreditLiability(Account $account, array $data) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |         Log::debug('storeCreditLiability'); | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         $account->refresh(); | 
					
						
							|  |  |  |         $accountType = $account->accountType->type; | 
					
						
							|  |  |  |         $direction   = $this->accountRepository->getMetaValue($account, 'liability_direction'); | 
					
						
							|  |  |  |         $valid       = config('firefly.valid_liabilities'); | 
					
						
							|  |  |  |         if (in_array($accountType, $valid, true) && 'credit' === $direction) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |             Log::debug('Is a liability with credit direction.'); | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |             if ($this->validOBData($data)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |                 Log::debug('Has valid CL data.'); | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $openingBalance     = $data['opening_balance']; | 
					
						
							|  |  |  |                 $openingBalanceDate = $data['opening_balance_date']; | 
					
						
							|  |  |  |                 $this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!$this->validOBData($data)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |                 Log::debug('Has NOT valid CL data.'); | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $this->deleteCreditTransaction($account); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function storeOrder(Account $account, array $data): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountType = $account->accountType->type; | 
					
						
							|  |  |  |         $maxOrder    = $this->accountRepository->maxOrder($accountType); | 
					
						
							|  |  |  |         $order       = null; | 
					
						
							|  |  |  |         if (!array_key_exists('order', $data)) { | 
					
						
							|  |  |  |             $order = $maxOrder + 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (array_key_exists('order', $data)) { | 
					
						
							|  |  |  |             $order = (int)($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']); | 
					
						
							|  |  |  |             $order = 0 === $order ? $maxOrder + 1 : $order; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $updateService = app(AccountUpdateService::class); | 
					
						
							|  |  |  |         $updateService->setUser($account->user); | 
					
						
							|  |  |  |         $updateService->update($account, ['order' => $order]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |         $this->accountRepository->setUser($user); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-05 10:56:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |