Files
firefly-iii/app/Repositories/Account/AccountRepository.php

360 lines
11 KiB
PHP
Raw Normal View History

2015-02-09 07:23:39 +01:00
<?php
/**
* AccountRepository.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
2015-02-09 07:23:39 +01:00
namespace FireflyIII\Repositories\Account;
2015-02-21 12:16:41 +01:00
use Carbon\Carbon;
use DB;
use FireflyIII\Factory\AccountFactory;
2015-02-09 07:23:39 +01:00
use FireflyIII\Models\Account;
2016-10-10 08:03:03 +02:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
2017-12-30 21:04:04 +01:00
use FireflyIII\Models\Note;
use FireflyIII\Models\Tag;
2015-02-09 07:56:24 +01:00
use FireflyIII\Models\Transaction;
2016-10-10 08:03:03 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2018-02-21 18:42:15 +01:00
use FireflyIII\Services\Internal\Destroy\AccountDestroyService;
2018-02-21 20:34:24 +01:00
use FireflyIII\Services\Internal\Update\AccountUpdateService;
2016-03-20 11:47:10 +01:00
use FireflyIII\User;
2016-10-10 07:20:49 +02:00
use Log;
2015-02-09 07:23:39 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class AccountRepository.
2015-02-09 07:23:39 +01:00
*/
class AccountRepository implements AccountRepositoryInterface
{
2018-02-16 15:19:19 +01:00
2016-03-20 11:47:10 +01:00
/** @var User */
private $user;
2016-10-10 08:03:03 +02:00
/** @var array */
2018-02-13 18:24:06 +01:00
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
2018-02-16 15:19:19 +01:00
use FindAccountsTrait;
2018-02-13 18:24:06 +01:00
/** @var array */
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
private $validFields = ['accountNumber', 'currency_id', 'BIC'];
2016-03-30 17:47:13 +02:00
/**
2017-11-15 12:25:49 +01:00
* Moved here from account CRUD.
*
* @param array $types
*
* @return int
*/
2016-11-28 18:55:56 +01:00
public function count(array $types): int
{
$count = $this->user->accounts()->accountTypeIn($types)->count();
return $count;
}
/**
* Moved here from account CRUD.
*
* @param Account $account
* @param Account $moveTo
*
* @return bool
2017-12-22 18:32:43 +01:00
*
2017-12-17 14:30:53 +01:00
* @throws \Exception
*/
public function destroy(Account $account, Account $moveTo): bool
{
2018-02-21 18:42:15 +01:00
/** @var AccountDestroyService $service */
$service = app(AccountDestroyService::class);
$service->destroy($account, $moveTo);
return true;
}
2018-02-16 15:19:19 +01:00
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
/**
* Return account type by string.
*
* @param string $type
*
* @return AccountType|null
*/
public function getAccountType(string $type): ?AccountType
{
return AccountType::whereType($type)->first();
}
2017-12-31 10:40:27 +01:00
/**
* @param Account $account
*
* @return Note|null
*/
public function getNote(Account $account): ?Note
{
return $account->notes()->first();
}
2018-02-16 22:14:53 +01:00
/**
* Returns the amount of the opening balance for this account.
*
* @return string
*/
public function getOpeningBalanceAmount(Account $account): ?string
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (null === $journal) {
return null;
}
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
if (null === $transaction) {
return null;
}
return strval($transaction->amount);
}
/**
* Return date of opening balance as string or null.
*
* @param Account $account
*
* @return null|string
*/
public function getOpeningBalanceDate(Account $account): ?string
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (null === $journal) {
return null;
}
return $journal->date->format('Y-m-d');
}
/**
2016-11-21 20:23:25 +01:00
* Returns the date of the very last transaction in this account.
*
* @param Account $account
*
* @return Carbon
*/
2016-11-21 20:23:25 +01:00
public function newestJournalDate(Account $account): Carbon
{
2016-11-21 20:23:25 +01:00
$last = new Carbon;
$date = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
2016-11-21 20:23:25 +01:00
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->first(['transaction_journals.date']);
2017-11-15 12:25:49 +01:00
if (null !== $date) {
2016-11-21 20:23:25 +01:00
$last = new Carbon($date->date);
}
return $last;
}
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return TransactionJournal
2016-11-21 20:23:25 +01:00
*/
public function oldestJournal(Account $account): TransactionJournal
2016-11-21 20:23:25 +01:00
{
$first = $account->transactions()
2016-11-21 20:23:25 +01:00
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
->orderBy('transaction_journals.order', 'DESC')
->where('transaction_journals.user_id', $this->user->id)
2016-11-21 20:23:25 +01:00
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']);
2017-11-15 12:25:49 +01:00
if (null !== $first) {
return TransactionJournal::find(intval($first->id));
}
return new TransactionJournal();
}
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return Carbon
*/
public function oldestJournalDate(Account $account): Carbon
{
$journal = $this->oldestJournal($account);
2017-11-15 12:25:49 +01:00
if (null === $journal->id) {
return new Carbon;
}
return $journal->date;
}
2017-01-30 16:42:58 +01:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
2016-10-10 08:03:03 +02:00
/**
* @param array $data
*
* @return Account
*/
public function store(array $data): Account
{
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
$account = $factory->create($data);
2017-12-30 21:04:04 +01:00
return $account;
2016-10-10 08:03:03 +02:00
}
/**
* @param Account $account
* @param array $data
*
* @return Account
*/
public function update(Account $account, array $data): Account
{
2018-02-21 20:34:24 +01:00
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
2018-02-21 21:11:44 +01:00
$account = $service->update($account, $data);
2016-10-10 08:03:03 +02:00
return $account;
}
/**
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
*/
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');
2017-12-22 18:32:43 +01:00
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']);
}
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;
}
2015-03-29 08:14:32 +02:00
}