Revamp multi-account report.

This commit is contained in:
James Cole
2019-09-03 22:35:41 +02:00
parent 4743230136
commit 47de2fec28
29 changed files with 2286 additions and 628 deletions

View File

@@ -88,6 +88,41 @@ class AccountRepository implements AccountRepositoryInterface
return true;
}
/**
* Find account with same name OR same IBAN or both, but not the same type or ID.
*
* @param Collection $accounts
*
* @return Collection
*/
public function expandWithDoubles(Collection $accounts): Collection
{
$result = new Collection;
/** @var Account $account */
foreach ($accounts as $account) {
$byName = $this->user->accounts()->where('name', $account->name)
->where('id', '!=', $account->id)->first();
if (null !== $byName) {
$result->push($account);
$result->push($byName);
continue;
}
if (null !== $account->iban) {
$byIban = $this->user->accounts()->where('iban', $account->iban)
->where('id', '!=', $account->id)->first();
if (null !== $byIban) {
$result->push($account);
$result->push($byIban);
continue;
}
}
$result->push($account);
}
return $result;
}
/**
* @param string $number
* @param array $types

View File

@@ -37,21 +37,6 @@ use Illuminate\Support\Collection;
*/
interface AccountRepositoryInterface
{
/**
* @param Account $account
*
* @return TransactionJournal|null
*
*/
public function getOpeningBalance(Account $account): ?TransactionJournal;
/**
* @param Account $account
* @return TransactionGroup|null
*/
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup;
/**
* Moved here from account CRUD.
*
@@ -64,18 +49,27 @@ interface AccountRepositoryInterface
/**
* Moved here from account CRUD.
*
* @param Account $account
* @param Account $account
* @param Account|null $moveTo
*
* @return bool
*/
public function destroy(Account $account, ?Account $moveTo): bool;
/**
* Find account with same name OR same IBAN or both, but not the same type or ID.
*
* @param Collection $accounts
*
* @return Collection
*/
public function expandWithDoubles(Collection $accounts): Collection;
/**
* Find by account number. Is used.
*
* @param string $number
* @param array $types
* @param array $types
*
* @return Account|null
*/
@@ -83,7 +77,7 @@ interface AccountRepositoryInterface
/**
* @param string $iban
* @param array $types
* @param array $types
*
* @return Account|null
*/
@@ -91,7 +85,7 @@ interface AccountRepositoryInterface
/**
* @param string $name
* @param array $types
* @param array $types
*
* @return Account|null
*/
@@ -157,7 +151,7 @@ interface AccountRepositoryInterface
* Return meta value for account. Null if not found.
*
* @param Account $account
* @param string $field
* @param string $field
*
* @return null|string
*/
@@ -172,6 +166,14 @@ interface AccountRepositoryInterface
*/
public function getNoteText(Account $account): ?string;
/**
* @param Account $account
*
* @return TransactionJournal|null
*
*/
public function getOpeningBalance(Account $account): ?TransactionJournal;
/**
* Returns the amount of the opening balance for this account.
*
@@ -190,6 +192,13 @@ interface AccountRepositoryInterface
*/
public function getOpeningBalanceDate(Account $account): ?string;
/**
* @param Account $account
*
* @return TransactionGroup|null
*/
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup;
/**
* @param Account $account
*
@@ -235,7 +244,7 @@ interface AccountRepositoryInterface
/**
* @param string $query
* @param array $types
* @param array $types
*
* @return Collection
*/
@@ -255,7 +264,7 @@ interface AccountRepositoryInterface
/**
* @param Account $account
* @param array $data
* @param array $data
*
* @return Account
*/

View File

@@ -0,0 +1,189 @@
<?php
/**
* OperationsRepository.php
* Copyright (c) 2019 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\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
*
* Class OperationsRepository
*/
class OperationsRepository implements OperationsRepositoryInterface
{
/** @var User */
private $user;
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function listExpenses(Carbon $start, Carbon $end, Collection $accounts): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]);
$collector->setBothAccounts($accounts);
$collector->withCategoryInformation()->withAccountInformation()->withBudgetInformation()->withTagInformation();
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'transaction_journals' => [],
];
$journalId = (int)$journal['transaction_journal_id'];
$array[$currencyId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'date' => $journal['date'],
'transaction_journal_id' => $journalId,
'budget_name' => $journal['budget_name'],
'category_name' => $journal['category_name'],
'source_account_id' => $journal['source_account_id'],
'source_account_name' => $journal['source_account_name'],
'source_account_iban' => $journal['source_account_iban'],
'destination_account_id' => $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'destination_account_iban' => $journal['destination_account_iban'],
'tags' => $journal['tags'],
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
];
}
return $array;
}
/**
* This method returns a list of all the deposit transaction journals (as arrays) set in that period
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT]);
$collector->setBothAccounts($accounts);
$collector->withCategoryInformation()->withAccountInformation()->withBudgetInformation()->withTagInformation();
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'transaction_journals' => [],
];
$journalId = (int)$journal['transaction_journal_id'];
$array[$currencyId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->positive($journal['amount']),
'date' => $journal['date'],
'transaction_journal_id' => $journalId,
'budget_name' => $journal['budget_name'],
'tags' => $journal['tags'],
'category_name' => $journal['category_name'],
'source_account_id' => $journal['source_account_id'],
'source_account_name' => $journal['source_account_name'],
'source_account_iban' => $journal['source_account_iban'],
'destination_account_id' => $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'destination_account_iban' => $journal['destination_account_iban'],
'description' => $journal['description'],
'transaction_group_id' => $journal['transaction_group_id'],
];
}
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Sum of withdrawal journals in period for a set of accounts, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
throw new FireflyException(sprintf('%s is not yet implemented', __METHOD__));
}
/**
* Sum of income journals in period for a set of accounts, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
throw new FireflyException(sprintf('%s is not yet implemented', __METHOD__));
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* OperationsRepositoryInterface.php
* Copyright (c) 2019 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\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
* Interface OperationsRepositoryInterface
*/
interface OperationsRepositoryInterface
{
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function listExpenses(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* This method returns a list of all the deposit transaction journals (as arrays) set in that period
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* @param User $user
*/
public function setUser(User $user): void;
/**
* Sum of withdrawal journals in period for a set of accounts, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* Sum of income journals in period for a set of accounts, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
}