Files
firefly-iii/app/Services/Internal/Update/AccountUpdateService.php

151 lines
5.4 KiB
PHP
Raw Normal View History

2018-02-21 20:34:24 +01:00
<?php
/**
* AccountUpdateService.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-02-21 20:34:24 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-21 20:34:24 +01: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
*
* 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
* GNU Affero General Public License for more details.
2018-02-21 20:34:24 +01: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;
use FireflyIII\Models\Account;
2019-06-22 10:25:34 +02:00
use FireflyIII\Models\AccountType;
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;
use Log;
2018-02-21 20:34:24 +01:00
/**
* Class AccountUpdateService
*/
class AccountUpdateService
{
use AccountServiceTrait;
2018-12-21 16:38:10 +01:00
2019-06-22 10:25:34 +02:00
/** @var AccountRepositoryInterface */
protected $accountRepository;
/** @var array */
private $canHaveVirtual;
2019-07-31 16:53:09 +02:00
/** @var User */
private $user;
/** @var array */
protected $validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
/** @var array */
protected $validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
/** @var array */
protected $validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
2019-06-07 18:20:15 +02:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
2019-06-22 10:25:34 +02:00
// TODO move to configuration.
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
$this->accountRepository = app(AccountRepositoryInterface::class);
}
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
{
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;
2019-06-22 10:25:34 +02:00
2018-02-21 20:34:24 +01:00
// update the account itself:
$account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban;
// 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'];
}
2018-12-21 16:38:10 +01:00
$account->save();
2019-06-22 10:25:34 +02:00
// find currency, or use default currency instead.
2019-08-29 17:53:25 +02:00
if (isset($data['currency_id']) && (null !== $data['currency_id'] || null !== $data['currency_code'])) {
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
unset($data['currency_code']);
$data['currency_id'] = $currency->id;
}
2018-02-21 20:34:24 +01:00
// update all meta data:
$this->updateMetaData($account, $data);
// update, delete or create location:
$updateLocation = $data['update_location'] ?? false;
// location must be updated?
if (true === $updateLocation) {
// if all set to NULL, delete
2019-12-30 21:04:22 +01:00
if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) {
$account->locations()->delete();
}
// otherwise, update or create.
2019-12-30 21:04:22 +01:00
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);
}
$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();
}
}
2018-02-21 20:34:24 +01:00
// has valid initial balance (IB) data?
2019-06-22 10:25:34 +02:00
$type = $account->accountType;
// if it can have a virtual balance, it can also have an opening balance.
2019-07-31 16:53:09 +02:00
2019-06-22 10:25:34 +02:00
if (in_array($type->type, $this->canHaveVirtual, true)) {
2019-07-31 16:53:09 +02:00
2019-06-22 10:25:34 +02:00
if ($this->validOBData($data)) {
$this->updateOBGroup($account, $data);
}
2019-07-31 16:53:09 +02:00
2019-06-22 10:25:34 +02:00
if (!$this->validOBData($data)) {
$this->deleteOBGroup($account);
}
2018-02-21 20:34:24 +01:00
}
// update note:
if (isset($data['notes']) && null !== $data['notes']) {
2018-04-02 14:50:17 +02:00
$this->updateNote($account, (string)$data['notes']);
2018-02-21 20:34:24 +01:00
}
return $account;
}
2018-03-05 19:35:58 +01:00
}