Code removal. The code removed from these classes must move to respective services.

This commit is contained in:
James Cole
2018-02-22 20:07:14 +01:00
parent 4e923057ae
commit b627d42160
16 changed files with 431 additions and 665 deletions

View File

@@ -23,42 +23,29 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\Note;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\AccountDestroyService;
use FireflyIII\Services\Internal\Update\AccountUpdateService;
use FireflyIII\Services\Internal\Update\JournalUpdateService;
use FireflyIII\User;
use Log;
/**
* Class AccountRepository.
*/
class AccountRepository implements AccountRepositoryInterface
{
use FindAccountsTrait;
/** @var User */
private $user;
/** @var array */
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
use FindAccountsTrait;
/** @var array */
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
private $validFields = ['accountNumber', 'currency_id', 'BIC'];
/**
* Moved here from account CRUD.
*
* @param array $types
*
* @return int
@@ -239,6 +226,8 @@ class AccountRepository implements AccountRepositoryInterface
* @param array $data
*
* @return Account
* @throws FireflyException
* @throws Exception
*/
public function store(array $data): Account
{
@@ -254,6 +243,8 @@ class AccountRepository implements AccountRepositoryInterface
* @param Account $account
* @param array $data
*
* @throws FireflyException
* @throws Exception
* @return Account
*/
public function update(Account $account, array $data): Account
@@ -270,90 +261,16 @@ class AccountRepository implements AccountRepositoryInterface
* @param array $data
*
* @return TransactionJournal
* @throws FireflyException
* @throws Exception
*/
public function updateReconciliation(TransactionJournal $journal, array $data): TransactionJournal
{
// update journal
// update actual journal:
$data['amount'] = strval($data['amount']);
// unlink all categories, recreate them:
$journal->categories()->detach();
$this->storeCategoryWithJournal($journal, strval($data['category']));
// update amounts
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$transaction->amount = bcmul($data['amount'], '-1');
if (AccountType::ASSET === $transaction->account->accountType->type) {
$transaction->amount = $data['amount'];
}
$transaction->save();
}
$journal->save();
// update tags:
if (isset($data['tags']) && is_array($data['tags'])) {
$this->updateTags($journal, $data['tags']);
}
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$journal = $service->update($journal, $data);
return $journal;
}
/**
* @param TransactionJournal $journal
* @param string $category
*/
protected function storeCategoryWithJournal(TransactionJournal $journal, string $category)
{
if (strlen($category) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $category, 'user_id' => $journal->user_id]);
$journal->categories()->save($category);
}
}
/**
* @param TransactionJournal $journal
* @param array $array
*
* @return bool
*/
protected function updateTags(TransactionJournal $journal, array $array): bool
{
// create tag repository
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
// find or create all tags:
$tags = [];
$ids = [];
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
$tags[] = $tag;
$ids[] = $tag->id;
}
}
// delete all tags connected to journal not in this array:
if (count($ids) > 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
}
// if count is zero, delete them all:
if (0 === count($ids)) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
}
// connect each tag to journal (if not yet connected):
/** @var Tag $tag */
foreach ($tags as $tag) {
Log::debug(sprintf('Will try to connect tag #%d to journal #%d.', $tag->id, $journal->id));
$tagRepository->connect($journal, $tag);
}
return true;
}
}

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\User;
@@ -44,6 +45,7 @@ trait FindAccountsTrait
*/
public function find(int $accountId): Account
{
/** @var Account $account */
$account = $this->user->accounts()->find($accountId);
if (null === $account) {
return new Account;
@@ -56,6 +58,7 @@ trait FindAccountsTrait
* @param string $number
* @param array $types
*
* @deprecated
* @return Account
*/
public function findByAccountNumber(string $number, array $types): Account
@@ -83,6 +86,7 @@ trait FindAccountsTrait
* @param string $iban
* @param array $types
*
* @deprecated
* @return Account
*/
public function findByIban(string $iban, array $types): Account
@@ -213,15 +217,15 @@ trait FindAccountsTrait
* @return Account
*
* @throws FireflyException
* @throws \Exception
*/
public function getCashAccount(): Account
{
$type = AccountType::where('type', AccountType::CASH)->first();
$account = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $type->id, 'name' => 'Cash account']
);
$account->active = true;
$account->save();
$type = AccountType::where('type', AccountType::CASH)->first();
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
$account = $factory->findOrCreate('Cash account', $type->type);
return $account;
}
@@ -232,6 +236,7 @@ trait FindAccountsTrait
* @return Account|null
*
* @throws FireflyException
* @throws \Exception
*/
public function getReconciliation(Account $account): ?Account
{
@@ -247,23 +252,12 @@ trait FindAccountsTrait
return $account;
}
}
// assume nothing was found. create it!
$data = [
'accountType' => 'reconcile',
'name' => $name,
'iban' => null,
'virtualBalance' => '0',
'active' => true,
];
$account = $this->storeAccount($data);
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($account->user);
$account = $factory->findOrCreate($name, $type->type);
return $account;
}
/**
* @param array $data
*
* @return Account
*/
abstract protected function storeAccount(array $data): Account;
}