| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AccountUpdateService.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:35 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +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-21 20:34:24 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +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-21 20:34:24 +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-21 20:34:24 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Internal\Update; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  | use FireflyIII\Events\UpdatedAccount; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2019-12-30 12:12:08 +01:00
										 |  |  | use FireflyIII\Models\Location; | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | use FireflyIII\Services\Internal\Support\AccountServiceTrait; | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountUpdateService | 
					
						
							| 
									
										
										
										
											2021-03-08 17:56:21 +01:00
										 |  |  |  * TODO this is a mess. | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class AccountUpdateService | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use AccountServiceTrait; | 
					
						
							| 
									
										
										
										
											2018-12-21 16:38:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 16:41:58 +02: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; | 
					
						
							| 
									
										
										
										
											2020-07-24 16:41:58 +02:00
										 |  |  |     private User                         $user; | 
					
						
							| 
									
										
										
										
											2019-08-24 08:22:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         $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'); | 
					
						
							|  |  |  |         $this->accountRepository     = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-21 21:06:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update account data. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Account $account, array $data): Account | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         Log::debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  |         $this->accountRepository->setUser($account->user); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |         $this->user = $account->user; | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  |         $account    = $this->updateAccount($account, $data); | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  |         $account    = $this->updateAccountOrder($account, $data); | 
					
						
							| 
									
										
										
										
											2019-08-24 07:56:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  |         // find currency, or use default currency instead.
 | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  |         if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) { | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  |             $currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)); | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  |             unset($data['currency_code'], $data['currency_id']); | 
					
						
							| 
									
										
										
										
											2019-08-24 07:56:08 +02:00
										 |  |  |             $data['currency_id'] = $currency->id; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-22 08:07:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |         // update all meta data:
 | 
					
						
							|  |  |  |         $this->updateMetaData($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-30 12:12:08 +01:00
										 |  |  |         // update, delete or create location:
 | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |         $this->updateLocation($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update opening balance.
 | 
					
						
							|  |  |  |         $this->updateOpeningBalance($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         // update opening balance.
 | 
					
						
							|  |  |  |         $this->updateCreditLiability($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |         // update note:
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists('notes', $data) && null !== $data['notes']) { | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |             $this->updateNote($account, (string)$data['notes']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update preferences if inactive:
 | 
					
						
							| 
									
										
										
										
											2021-04-04 07:25:52 +02:00
										 |  |  |         $this->updatePreferences($account); | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         event(new UpdatedAccount($account)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |     private function updateAccount(Account $account, array $data): Account | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         // update the account itself:
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (array_key_exists('name', $data)) { | 
					
						
							|  |  |  |             $account->name = $data['name']; | 
					
						
							| 
									
										
										
										
											2021-03-20 07:02:06 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (array_key_exists('active', $data)) { | 
					
						
							| 
									
										
										
										
											2021-03-20 07:02:06 +01:00
										 |  |  |             $account->active = $data['active']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (array_key_exists('iban', $data)) { | 
					
						
							|  |  |  |             $account->iban = $data['iban']; | 
					
						
							| 
									
										
										
										
											2021-03-20 07:02:06 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // set liability, but account must already be a liability.
 | 
					
						
							|  |  |  |         //$liabilityType = $data['liability_type'] ?? '';
 | 
					
						
							|  |  |  |         if ($this->isLiability($account) && array_key_exists('liability_type', $data)) { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             $type = $this->getAccountType($data['liability_type']); | 
					
						
							|  |  |  |             if (null !== $type) { | 
					
						
							| 
									
										
										
										
											2021-03-20 07:02:06 +01:00
										 |  |  |                 $account->account_type_id = $type->id; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-08 17:56:36 +02:00
										 |  |  |         // set liability, alternative method used in v1 layout:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->isLiability($account) && array_key_exists('account_type_id', $data)) { | 
					
						
							|  |  |  |             $type = AccountType::find((int)$data['account_type_id']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (null !== $type && in_array($type->type, config('firefly.valid_liabilities'), true)) { | 
					
						
							|  |  |  |                 $account->account_type_id = $type->id; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         // update virtual balance (could be set to zero if empty string).
 | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |         if (array_key_exists('virtual_balance', $data) && null !== $data['virtual_balance']) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance']; | 
					
						
							| 
									
										
										
										
											2019-12-30 12:12:08 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $account->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-19 20:13:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function isLiability(Account $account): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = $account->accountType->type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return in_array($type, [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE], true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 17:56:21 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getAccountType(string $type): AccountType | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-13 13:26:45 +01:00
										 |  |  |         return AccountType::whereType(ucfirst($type))->first(); | 
					
						
							| 
									
										
										
										
											2020-02-19 20:13:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Account | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |     public function updateAccountOrder(Account $account, array $data): Account | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         // skip if no order info
 | 
					
						
							|  |  |  |         if (!array_key_exists('order', $data) || $data['order'] === $account->order) { | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |             Log::debug(sprintf('Account order will not be touched because its not set or already at %d.', $account->order)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             return $account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // skip if not of orderable type.
 | 
					
						
							|  |  |  |         $type = $account->accountType->type; | 
					
						
							|  |  |  |         if (!in_array($type, [AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT], true)) { | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |             Log::debug('Will not change order of this account.'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             return $account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // get account type ID's because a join and an update is hard:
 | 
					
						
							|  |  |  |         $oldOrder = (int)$account->order; | 
					
						
							|  |  |  |         $newOrder = $data['order']; | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         Log::debug(sprintf('Order is set to be updated from %s to %s', $oldOrder, $newOrder)); | 
					
						
							|  |  |  |         $list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         if (in_array($type, [AccountType::ASSET], true)) { | 
					
						
							|  |  |  |             $list = $this->getTypeIds([AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         if ($newOrder > $oldOrder) { | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |             $this->user->accounts()->where('accounts.order', '<=', $newOrder)->where('accounts.order', '>', $oldOrder) | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |                        ->where('accounts.id', '!=', $account->id) | 
					
						
							|  |  |  |                        ->whereIn('accounts.account_type_id', $list) | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |                        ->decrement('order', 1); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $account->order = $newOrder; | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |             Log::debug(sprintf('Order of account #%d ("%s") is now %d', $account->id, $account->name, $newOrder)); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             $account->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $account; | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 12:01:01 +01:00
										 |  |  |         $this->user->accounts()->where('accounts.order', '>=', $newOrder)->where('accounts.order', '<', $oldOrder) | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |                    ->where('accounts.id', '!=', $account->id) | 
					
						
							|  |  |  |                    ->whereIn('accounts.account_type_id', $list) | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |                    ->increment('order', 1); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         $account->order = $newOrder; | 
					
						
							| 
									
										
										
										
											2021-03-13 14:33:48 +01:00
										 |  |  |         Log::debug(sprintf('Order of account #%d ("%s") is now %d', $account->id, $account->name, $newOrder)); | 
					
						
							| 
									
										
										
										
											2020-03-17 14:46:17 +01:00
										 |  |  |         $account->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |     private function getTypeIds(array $array): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = []; | 
					
						
							|  |  |  |         /** @var string $type */ | 
					
						
							|  |  |  |         foreach ($array as $type) { | 
					
						
							|  |  |  |             /** @var AccountType $type */ | 
					
						
							|  |  |  |             $type     = AccountType::whereType($type)->first(); | 
					
						
							|  |  |  |             $return[] = (int)$type->id; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |     private function updateLocation(Account $account, array $data): void | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |         $updateLocation = $data['update_location'] ?? false; | 
					
						
							|  |  |  |         // location must be updated?
 | 
					
						
							|  |  |  |         if (true === $updateLocation) { | 
					
						
							|  |  |  |             // if all set to NULL, delete
 | 
					
						
							|  |  |  |             if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) { | 
					
						
							|  |  |  |                 $account->locations()->delete(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // otherwise, update or create.
 | 
					
						
							|  |  |  |             if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) { | 
					
						
							|  |  |  |                 $location = $this->accountRepository->getLocation($account); | 
					
						
							|  |  |  |                 if (null === $location) { | 
					
						
							|  |  |  |                     $location = new Location; | 
					
						
							|  |  |  |                     $location->locatable()->associate($account); | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |                 $location->latitude   = $data['latitude'] ?? config('firefly.default_location.latitude'); | 
					
						
							|  |  |  |                 $location->longitude  = $data['longitude'] ?? config('firefly.default_location.longitude'); | 
					
						
							|  |  |  |                 $location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level'); | 
					
						
							|  |  |  |                 $location->save(); | 
					
						
							| 
									
										
										
										
											2020-10-21 06:29:25 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function updateOpeningBalance(Account $account, array $data): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // has valid initial balance (IB) data?
 | 
					
						
							|  |  |  |         $type = $account->accountType; | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         if (in_array($type->type, $this->canHaveOpeningBalance, true)) { | 
					
						
							|  |  |  |             // check if is submitted as empty, that makes it valid:
 | 
					
						
							|  |  |  |             if ($this->validOBData($data) && !$this->isEmptyOBData($data)) { | 
					
						
							|  |  |  |                 $openingBalance     = $data['opening_balance']; | 
					
						
							|  |  |  |                 $openingBalanceDate = $data['opening_balance_date']; | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $this->updateOBGroupV2($account, $openingBalance, $openingBalanceDate); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$this->validOBData($data) && $this->isEmptyOBData($data)) { | 
					
						
							|  |  |  |                 $this->deleteOBGroup($account); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param array   $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function updateCreditLiability(Account $account, array $data): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |         $type  = $account->accountType; | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |         $valid = config('firefly.valid_liabilities'); | 
					
						
							|  |  |  |         if (in_array($type->type, $valid, true)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |             $direction = array_key_exists('liability_direction', $data) ? $data['liability_direction'] : 'empty'; | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |             // check if is submitted as empty, that makes it valid:
 | 
					
						
							|  |  |  |             if ($this->validOBData($data) && !$this->isEmptyOBData($data)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $openingBalance     = $data['opening_balance']; | 
					
						
							|  |  |  |                 $openingBalanceDate = $data['opening_balance_date']; | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |                 if ('credit' === $data['liability_direction']) { | 
					
						
							|  |  |  |                     $this->updateCreditTransaction($account, $openingBalance, $openingBalanceDate); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$this->validOBData($data) && $this->isEmptyOBData($data)) { | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:36 +02:00
										 |  |  |                 $this->deleteCreditTransaction($account); | 
					
						
							| 
									
										
										
										
											2021-04-10 17:56:09 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if ($this->validOBData($data) && !$this->isEmptyOBData($data) && 'credit' !== $direction) { | 
					
						
							|  |  |  |                 $this->deleteCreditTransaction($account); | 
					
						
							| 
									
										
										
										
											2020-10-23 18:26:18 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-04 07:25:52 +02:00
										 |  |  |     private function updatePreferences(Account $account): void | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-04 07:25:52 +02:00
										 |  |  |         $account->refresh(); | 
					
						
							|  |  |  |         if (true === $account->active) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:31:19 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-04 07:25:52 +02:00
										 |  |  |         $preference = app('preferences')->getForUser($account->user, 'frontpageAccounts'); | 
					
						
							|  |  |  |         if (null === $preference) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $array = $preference->data; | 
					
						
							|  |  |  |         Log::debug('Old array is: ', $array); | 
					
						
							|  |  |  |         Log::debug(sprintf('Must remove : %d', $account->id)); | 
					
						
							|  |  |  |         $removeAccountId = (int)$account->id; | 
					
						
							|  |  |  |         $new             = []; | 
					
						
							|  |  |  |         foreach ($array as $value) { | 
					
						
							|  |  |  |             if ((int)$value !== $removeAccountId) { | 
					
						
							|  |  |  |                 Log::debug(sprintf('Will include: %d', $value)); | 
					
						
							|  |  |  |                 $new[] = (int)$value; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug('Final new array is', $new); | 
					
						
							|  |  |  |         app('preferences')->setForUser($account->user, 'frontpageAccounts', $new); | 
					
						
							| 
									
										
										
										
											2021-03-07 16:19:14 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |