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

360 lines
11 KiB
PHP
Raw Normal View History

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