2018-02-18 12:55:35 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AccountFactory.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2018-02-21 14:06:59 -06:00
|
|
|
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
|
2018-02-18 12:55:35 -06:00
|
|
|
use FireflyIII\User;
|
|
|
|
|
|
|
|
/**
|
2018-02-19 12:44:46 -06:00
|
|
|
* Factory to create or return accounts.
|
|
|
|
*
|
2018-02-18 12:55:35 -06:00
|
|
|
* Class AccountFactory
|
|
|
|
*/
|
|
|
|
class AccountFactory
|
|
|
|
{
|
2018-02-21 14:06:59 -06:00
|
|
|
use AccountServiceTrait;
|
2018-02-18 12:55:35 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public function create(array $data): Account
|
|
|
|
{
|
2018-02-21 14:06:59 -06:00
|
|
|
$type = $this->getAccountType($data['account_type_id'], $data['accountType']);
|
|
|
|
$data['iban'] = $this->filterIban($data['iban']);
|
|
|
|
|
|
|
|
|
|
|
|
// account may exist already:
|
|
|
|
$existingAccount = $this->find($data['name'], $type->type);
|
|
|
|
if (null !== $existingAccount) {
|
|
|
|
return $existingAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create it:
|
|
|
|
$databaseData
|
|
|
|
= [
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'account_type_id' => $type->id,
|
|
|
|
'name' => $data['name'],
|
|
|
|
'virtual_balance' => strlen(strval($data['virtualBalance'])) === 0 ? '0' : $data['virtualBalance'],
|
|
|
|
'active' => true === $data['active'] ? true : false,
|
|
|
|
'iban' => $data['iban'],
|
|
|
|
];
|
|
|
|
|
|
|
|
// remove virtual balance when not an asset account:
|
|
|
|
if ($type->type !== AccountType::ASSET) {
|
|
|
|
$databaseData['virtual_balance'] = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
$newAccount = Account::create($databaseData);
|
|
|
|
$this->updateMetadata($newAccount, $data);
|
|
|
|
|
2018-03-01 13:54:50 -06:00
|
|
|
if ($this->validIBData($data) && $type->type === AccountType::ASSET) {
|
2018-02-21 14:06:59 -06:00
|
|
|
$this->updateIB($newAccount, $data);
|
|
|
|
}
|
2018-03-01 13:54:50 -06:00
|
|
|
if (!$this->validIBData($data) && $type->type === AccountType::ASSET) {
|
2018-02-21 14:06:59 -06:00
|
|
|
$this->deleteIB($newAccount);
|
|
|
|
}
|
|
|
|
// update note:
|
|
|
|
if (isset($data['notes'])) {
|
|
|
|
$this->updateNote($newAccount, $data['notes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accountName
|
|
|
|
* @param string $accountType
|
|
|
|
*
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public function find(string $accountName, string $accountType): ?Account
|
|
|
|
{
|
|
|
|
$type = AccountType::whereType($accountType)->first();
|
|
|
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
|
|
|
|
|
|
|
|
/** @var Account $object */
|
|
|
|
foreach ($accounts as $object) {
|
|
|
|
if ($object->name === $accountName) {
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-02-18 12:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accountName
|
|
|
|
* @param string $accountType
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public function findOrCreate(string $accountName, string $accountType): Account
|
|
|
|
{
|
|
|
|
$type = AccountType::whereType($accountType)->first();
|
|
|
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
|
|
|
|
|
|
|
|
/** @var Account $object */
|
|
|
|
foreach ($accounts as $object) {
|
|
|
|
if ($object->name === $accountName) {
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
return $this->create(
|
2018-02-18 12:55:35 -06:00
|
|
|
[
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'name' => $accountName,
|
|
|
|
'account_type_id' => $type->id,
|
2018-02-21 14:06:59 -06:00
|
|
|
'accountType' => null,
|
2018-03-01 13:54:50 -06:00
|
|
|
'virtualBalance' => '0',
|
2018-02-18 12:55:35 -06:00
|
|
|
'iban' => null,
|
|
|
|
'active' => true,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2018-02-21 14:06:59 -06:00
|
|
|
/**
|
|
|
|
* @param int|null $accountTypeId
|
|
|
|
* @param null|string $accountType
|
|
|
|
*
|
|
|
|
* @return AccountType|null
|
|
|
|
*/
|
|
|
|
protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
|
|
|
|
{
|
|
|
|
$accountTypeId = intval($accountTypeId);
|
|
|
|
if ($accountTypeId > 0) {
|
|
|
|
return AccountType::find($accountTypeId);
|
|
|
|
}
|
|
|
|
$type = config('firefly.accountTypeByIdentifier.' . strval($accountType));
|
|
|
|
|
|
|
|
return AccountType::whereType($type)->first();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:55:35 -06:00
|
|
|
}
|