2018-02-21 13:34:24 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AccountUpdateService.php
|
2020-02-16 06:56:35 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-21 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-21 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-21 13:34:24 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-21 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 13:34:24 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Services\Internal\Update;
|
|
|
|
|
2021-03-08 02:56:40 -06:00
|
|
|
use DB;
|
2018-02-21 13:34:24 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2019-06-22 03:25:34 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2019-12-30 05:12:08 -06:00
|
|
|
use FireflyIII\Models\Location;
|
2019-06-22 03:25:34 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2018-02-21 13:34:24 -06:00
|
|
|
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
|
2019-07-31 09:53:09 -05:00
|
|
|
use FireflyIII\User;
|
2018-09-06 05:29:32 -05:00
|
|
|
use Log;
|
2018-02-21 13:34:24 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountUpdateService
|
2021-03-08 10:56:21 -06:00
|
|
|
* TODO this is a mess.
|
2018-02-21 13:34:24 -06:00
|
|
|
*/
|
|
|
|
class AccountUpdateService
|
|
|
|
{
|
|
|
|
use AccountServiceTrait;
|
2018-12-21 09:38:10 -06:00
|
|
|
|
2020-07-24 09:41:58 -05:00
|
|
|
protected AccountRepositoryInterface $accountRepository;
|
|
|
|
protected array $validAssetFields;
|
|
|
|
protected array $validCCFields;
|
|
|
|
protected array $validFields;
|
|
|
|
private array $canHaveVirtual;
|
|
|
|
private User $user;
|
2019-08-24 01:22:46 -05:00
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 11:20:15 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
2019-06-22 03:25:34 -05:00
|
|
|
// TODO move to configuration.
|
|
|
|
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
|
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class);
|
2020-07-24 09:41:58 -05:00
|
|
|
$this->validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
|
|
|
|
$this->validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
|
|
|
|
$this->validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
2018-02-21 14:06:59 -06:00
|
|
|
|
2018-02-21 13:34:24 -06:00
|
|
|
/**
|
|
|
|
* Update account data.
|
|
|
|
*
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public function update(Account $account, array $data): Account
|
|
|
|
{
|
2019-06-22 03:25:34 -05:00
|
|
|
$this->accountRepository->setUser($account->user);
|
2019-07-31 09:53:09 -05:00
|
|
|
$this->user = $account->user;
|
2020-03-17 08:46:17 -05:00
|
|
|
$account = $this->updateAccount($account, $data);
|
2021-03-07 09:19:14 -06:00
|
|
|
$account = $this->updateAccountOrder($account, $data);
|
2019-08-24 00:56:08 -05:00
|
|
|
|
2019-06-22 03:25:34 -05:00
|
|
|
// find currency, or use default currency instead.
|
2021-03-09 23:34:03 -06:00
|
|
|
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
|
2020-10-20 23:29:25 -05:00
|
|
|
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
|
2021-03-09 23:34:03 -06:00
|
|
|
unset($data['currency_code'], $data['currency_id']);
|
2019-08-24 00:56:08 -05:00
|
|
|
$data['currency_id'] = $currency->id;
|
|
|
|
}
|
2018-04-22 01:07:33 -05:00
|
|
|
|
2018-02-21 13:34:24 -06:00
|
|
|
// update all meta data:
|
|
|
|
$this->updateMetaData($account, $data);
|
|
|
|
|
2019-12-30 05:12:08 -06:00
|
|
|
// update, delete or create location:
|
2020-10-23 11:26:18 -05:00
|
|
|
$this->updateLocation($account, $data);
|
|
|
|
|
|
|
|
// update opening balance.
|
|
|
|
$this->updateOpeningBalance($account, $data);
|
|
|
|
|
|
|
|
// update note:
|
|
|
|
if (isset($data['notes']) && null !== $data['notes']) {
|
|
|
|
$this->updateNote($account, (string)$data['notes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update preferences if inactive:
|
|
|
|
$this->updatePreferences($account, $data);
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
2021-03-12 11:31:19 -06:00
|
|
|
*
|
|
|
|
* @return Account
|
2020-10-23 11:26:18 -05:00
|
|
|
*/
|
2021-03-12 11:31:19 -06:00
|
|
|
private function updateAccount(Account $account, array $data): Account
|
2021-03-07 09:19:14 -06:00
|
|
|
{
|
2021-03-12 11:31:19 -06:00
|
|
|
// update the account itself:
|
|
|
|
$account->name = $data['name'] ?? $account->name;
|
|
|
|
$account->active = $data['active'] ?? $account->active;
|
|
|
|
$account->iban = $data['iban'] ?? $account->iban;
|
2019-12-30 13:44:52 -06:00
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
// liability stuff:
|
|
|
|
$liabilityType = $data['liability_type'] ?? '';
|
|
|
|
if ($this->isLiability($account) && $this->isLiabilityType($liabilityType)) {
|
|
|
|
$type = $this->getAccountType($liabilityType);
|
|
|
|
$account->account_type_id = $type->id;
|
|
|
|
}
|
2019-12-30 13:44:52 -06:00
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
// update virtual balance (could be set to zero if empty string).
|
|
|
|
if (null !== $data['virtual_balance']) {
|
|
|
|
$account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
|
2019-12-30 05:12:08 -06:00
|
|
|
}
|
2021-03-12 11:31:19 -06:00
|
|
|
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
return $account;
|
2018-02-21 13:34:24 -06:00
|
|
|
}
|
2020-02-19 13:13:09 -06: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 10:56:21 -06:00
|
|
|
* @param string $type
|
2020-02-19 13:13:09 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-08 10:56:21 -06:00
|
|
|
private function isLiabilityType(string $type): bool
|
2020-02-19 13:13:09 -06:00
|
|
|
{
|
2021-03-08 10:56:21 -06:00
|
|
|
if ('' === $type) {
|
2020-02-19 13:13:09 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:56:21 -06:00
|
|
|
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('type', ucfirst($type))->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
*/
|
|
|
|
private function getAccountType(string $type): AccountType
|
|
|
|
{
|
|
|
|
return AccountType::whereType($type)->first();
|
2020-02-19 13:13:09 -06:00
|
|
|
}
|
2020-03-17 08:46:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
2021-03-12 11:31:19 -06:00
|
|
|
private function updateAccountOrder(Account $account, array $data): Account
|
2020-03-17 08:46:17 -05:00
|
|
|
{
|
2021-03-12 11:31:19 -06:00
|
|
|
// skip if no order info
|
|
|
|
if (!array_key_exists('order', $data) || $data['order'] === $account->order) {
|
|
|
|
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)) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
// get account type ID's because a join and an update is hard:
|
|
|
|
$oldOrder = (int)$account->order;
|
|
|
|
$newOrder = $data['order'];
|
|
|
|
$list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]);
|
|
|
|
if (in_array($type, [AccountType::ASSET], true)) {
|
|
|
|
$list = $this->getTypeIds([AccountType::ASSET]);
|
2020-03-17 08:46:17 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
if ($newOrder > $oldOrder) {
|
|
|
|
$this->user->accounts()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
|
|
|
|
->where('accounts.id', '!=', $account->id)
|
|
|
|
->whereIn('accounts.account_type_id', $list)
|
|
|
|
->update(['order' => DB::raw('accounts.order-1')]);
|
|
|
|
$account->order = $newOrder;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
return $account;
|
2020-03-17 08:46:17 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
$this->user->accounts()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
|
|
|
|
->where('accounts.id', '!=', $account->id)
|
|
|
|
->whereIn('accounts.account_type_id', $list)
|
|
|
|
->update(['order' => DB::raw('accounts.order+1')]);
|
|
|
|
$account->order = $newOrder;
|
2020-03-17 08:46:17 -05:00
|
|
|
$account->save();
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
2020-10-20 23:29:25 -05:00
|
|
|
|
2021-03-12 11:31:19 -06: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-20 23:29:25 -05:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2021-03-12 11:31:19 -06:00
|
|
|
private function updateLocation(Account $account, array $data): void
|
2020-10-20 23:29:25 -05:00
|
|
|
{
|
2021-03-12 11:31:19 -06: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-20 23:29:25 -05:00
|
|
|
}
|
2021-03-07 09:19:14 -06:00
|
|
|
|
2021-03-12 11:31:19 -06: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-20 23:29:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 11:26:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
private function updateOpeningBalance(Account $account, array $data): void
|
|
|
|
{
|
|
|
|
|
|
|
|
// has valid initial balance (IB) data?
|
|
|
|
$type = $account->accountType;
|
|
|
|
// if it can have a virtual balance, it can also have an opening balance.
|
|
|
|
|
|
|
|
if (in_array($type->type, $this->canHaveVirtual, true)) {
|
|
|
|
|
|
|
|
// check if is submitted as empty, that makes it valid:
|
|
|
|
if ($this->validOBData($data) && !$this->isEmptyOBData($data)) {
|
|
|
|
$this->updateOBGroup($account, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->validOBData($data) && $this->isEmptyOBData($data)) {
|
|
|
|
$this->deleteOBGroup($account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 09:19:14 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2021-03-12 11:31:19 -06:00
|
|
|
private function updatePreferences(Account $account, array $data): void
|
2021-03-07 09:19:14 -06:00
|
|
|
{
|
2021-03-12 11:31:19 -06:00
|
|
|
Log::debug(sprintf('Now in updatePreferences(#%d)', $account->id));
|
|
|
|
if (array_key_exists('active', $data) && (false === $data['active'] || 0 === $data['active'])) {
|
|
|
|
Log::debug('Account was marked as inactive.');
|
|
|
|
$preference = app('preferences')->getForUser($account->user, 'frontpageAccounts');
|
|
|
|
if (null !== $preference) {
|
|
|
|
$removeAccountId = (int)$account->id;
|
|
|
|
$array = $preference->data;
|
|
|
|
Log::debug('Current list of accounts: ', $array);
|
|
|
|
Log::debug(sprintf('Going to remove account #%d', $removeAccountId));
|
|
|
|
$filtered = array_filter(
|
|
|
|
$array, function ($accountId) use ($removeAccountId) {
|
|
|
|
return (int)$accountId !== $removeAccountId;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Log::debug('Left with accounts', array_values($filtered));
|
|
|
|
app('preferences')->setForUser($account->user, 'frontpageAccounts', array_values($filtered));
|
|
|
|
app('preferences')->forget($account->user, 'frontpageAccounts');
|
2021-03-07 09:19:14 -06:00
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::debug("Found no frontpageAccounts preference, do nothing.");
|
2021-03-07 09:19:14 -06:00
|
|
|
|
2021-03-12 11:31:19 -06:00
|
|
|
return;
|
2021-03-07 09:19:14 -06:00
|
|
|
}
|
2021-03-12 11:31:19 -06:00
|
|
|
Log::debug('Account was not marked as inactive, do nothing.');
|
2021-03-07 09:19:14 -06:00
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|