2016-10-08 07:54:32 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AccountTasker.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-10-08 07:54:32 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-08 07:54:32 -05:00
|
|
|
*
|
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/>.
|
2016-10-08 07:54:32 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-10-08 07:54:32 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Account;
|
|
|
|
|
2016-10-08 09:04:05 -05:00
|
|
|
use Carbon\Carbon;
|
2019-05-30 05:31:19 -05:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2018-09-06 12:47:29 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2017-04-17 01:31:42 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-09-06 12:47:29 -05:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2016-10-08 08:59:58 -05:00
|
|
|
use FireflyIII\User;
|
2016-10-08 09:04:05 -05:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Log;
|
2016-10-08 08:59:58 -05:00
|
|
|
|
2016-10-08 07:54:32 -05:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AccountTasker.
|
2016-10-08 07:54:32 -05:00
|
|
|
*/
|
2016-10-09 00:58:27 -05:00
|
|
|
class AccountTasker implements AccountTaskerInterface
|
2016-10-08 07:54:32 -05:00
|
|
|
{
|
2016-10-08 08:59:58 -05:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2016-10-08 07:54:32 -05:00
|
|
|
|
2018-09-03 01:41:03 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2018-09-03 01:41:03 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 09:04:05 -05:00
|
|
|
/**
|
2016-12-06 01:59:08 -06:00
|
|
|
* @param Collection $accounts
|
2019-05-30 05:31:19 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-10-08 09:04:05 -05:00
|
|
|
*
|
2017-01-19 14:54:27 -06:00
|
|
|
* @return array
|
2018-07-22 11:50:27 -05:00
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
2016-10-08 09:04:05 -05:00
|
|
|
*/
|
2017-01-19 14:54:27 -06:00
|
|
|
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
|
2016-10-08 09:04:05 -05:00
|
|
|
{
|
2017-01-30 09:42:58 -06:00
|
|
|
$yesterday = clone $start;
|
2016-10-08 09:04:05 -05:00
|
|
|
$yesterday->subDay();
|
2018-07-13 08:50:42 -05:00
|
|
|
$startSet = app('steam')->balancesByAccounts($accounts, $yesterday);
|
|
|
|
$endSet = app('steam')->balancesByAccounts($accounts, $end);
|
2016-10-08 09:04:05 -05:00
|
|
|
|
2017-01-19 14:54:27 -06:00
|
|
|
Log::debug('Start of accountreport');
|
|
|
|
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
|
2018-09-06 12:47:29 -05:00
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepository */
|
|
|
|
$currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
|
|
|
|
2017-01-19 14:54:27 -06:00
|
|
|
$return = [
|
2018-09-06 12:47:29 -05:00
|
|
|
'currencies' => [],
|
2017-01-19 14:54:27 -06:00
|
|
|
'start' => '0',
|
|
|
|
'end' => '0',
|
|
|
|
'difference' => '0',
|
|
|
|
'accounts' => [],
|
|
|
|
];
|
2016-10-08 09:04:05 -05:00
|
|
|
|
2018-09-06 12:47:29 -05:00
|
|
|
/** @var Account $account */
|
2016-10-08 09:04:05 -05:00
|
|
|
foreach ($accounts as $account) {
|
2018-09-06 12:47:29 -05:00
|
|
|
$id = $account->id;
|
|
|
|
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
|
|
|
|
$currency = $currencyRepository->findNull($currencyId);
|
|
|
|
$return['currencies'][] = $currencyId;
|
|
|
|
$entry = [
|
2017-01-19 14:54:27 -06:00
|
|
|
'name' => $account->name,
|
|
|
|
'id' => $account->id,
|
2018-09-06 12:47:29 -05:00
|
|
|
'currency' => $currency ?? $defaultCurrency,
|
2017-01-19 14:54:27 -06:00
|
|
|
'start_balance' => '0',
|
|
|
|
'end_balance' => '0',
|
|
|
|
];
|
|
|
|
|
|
|
|
// get first journal date:
|
2017-04-09 00:44:22 -05:00
|
|
|
$first = $repository->oldestJournal($account);
|
2017-01-19 14:54:27 -06:00
|
|
|
$entry['start_balance'] = $startSet[$account->id] ?? '0';
|
|
|
|
$entry['end_balance'] = $endSet[$account->id] ?? '0';
|
2017-02-12 03:58:37 -06:00
|
|
|
|
|
|
|
// first journal exists, and is on start, then this is the actual opening balance:
|
2018-07-24 13:30:52 -05:00
|
|
|
if (null !== $first && $first->date->isSameDay($start)) {
|
2017-03-02 09:42:33 -06:00
|
|
|
Log::debug(sprintf('Date of first journal for %s is %s', $account->name, $first->date->format('Y-m-d')));
|
2017-01-19 14:54:27 -06:00
|
|
|
$entry['start_balance'] = $first->transactions()->where('account_id', $account->id)->first()->amount;
|
2017-02-12 03:58:37 -06:00
|
|
|
Log::debug(sprintf('Account %s was opened on %s, so opening balance is %f', $account->name, $start->format('Y-m-d'), $entry['start_balance']));
|
2017-01-19 14:54:27 -06:00
|
|
|
}
|
|
|
|
$return['start'] = bcadd($return['start'], $entry['start_balance']);
|
|
|
|
$return['end'] = bcadd($return['end'], $entry['end_balance']);
|
2016-10-08 09:04:05 -05:00
|
|
|
|
2017-01-19 14:54:27 -06:00
|
|
|
$return['accounts'][$id] = $entry;
|
|
|
|
}
|
2018-09-06 12:47:29 -05:00
|
|
|
$return['currencies'] = count(array_unique($return['currencies']));
|
2017-01-19 14:54:27 -06:00
|
|
|
$return['difference'] = bcsub($return['end'], $return['start']);
|
2016-10-08 09:04:05 -05:00
|
|
|
|
2017-01-19 14:54:27 -06:00
|
|
|
return $return;
|
2016-10-08 09:04:05 -05:00
|
|
|
}
|
2016-10-09 00:58:27 -05:00
|
|
|
|
2017-04-17 01:31:42 -05:00
|
|
|
/**
|
2019-05-30 05:31:19 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2017-04-17 01:31:42 -05:00
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array
|
|
|
|
{
|
|
|
|
// get all expenses for the given accounts in the given period!
|
|
|
|
// also transfers!
|
|
|
|
// get all transactions:
|
2019-05-30 05:31:19 -05:00
|
|
|
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
|
2017-04-17 01:31:42 -05:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
|
|
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
2019-05-30 05:31:19 -05:00
|
|
|
->withAccountInformation();
|
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
$expenses = $this->groupByDestination($journals);
|
2017-04-17 01:31:42 -05:00
|
|
|
|
|
|
|
// sort the result
|
|
|
|
// Obtain a list of columns
|
|
|
|
$sum = [];
|
|
|
|
foreach ($expenses as $accountId => $row) {
|
2018-04-02 08:10:40 -05:00
|
|
|
$sum[$accountId] = (float)$row['sum'];
|
2017-04-17 01:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($sum, SORT_ASC, $expenses);
|
|
|
|
|
|
|
|
return $expenses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-30 05:31:19 -05:00
|
|
|
* @param array $array
|
2017-04-17 01:31:42 -05:00
|
|
|
*
|
|
|
|
* @return array
|
2018-07-22 11:50:27 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2017-04-17 01:31:42 -05:00
|
|
|
*/
|
2019-05-30 05:31:19 -05:00
|
|
|
private function groupByDestination(array $array): array
|
2017-04-17 01:31:42 -05:00
|
|
|
{
|
2018-09-06 12:47:29 -05:00
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$currencies = [$defaultCurrency->id => $defaultCurrency,];
|
|
|
|
$expenses = [];
|
|
|
|
$countAccounts = []; // if count remains 0 use original name, not the name with the currency.
|
|
|
|
|
|
|
|
|
2019-05-30 05:31:19 -05:00
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($array as $journal) {
|
|
|
|
$opposingId = (int)$journal['destination_account_id'];
|
|
|
|
$currencyId = (int)$journal['currency_id'];
|
2018-09-06 12:47:29 -05:00
|
|
|
$key = sprintf('%s-%s', $opposingId, $currencyId);
|
2019-05-30 05:31:19 -05:00
|
|
|
$name = sprintf('%s (%s)', $journal['destination_account_name'], $journal['currency_name']);
|
2018-09-06 12:47:29 -05:00
|
|
|
$countAccounts[$opposingId] = isset($countAccounts[$opposingId]) ? $countAccounts[$opposingId] + 1 : 1;
|
|
|
|
if (!isset($expenses[$key])) {
|
|
|
|
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
|
|
|
|
$expenses[$key] = [
|
|
|
|
'id' => $opposingId,
|
|
|
|
'name' => $name,
|
2019-05-30 05:31:19 -05:00
|
|
|
'original' => $journal['destination_account_name'],
|
2018-09-06 12:47:29 -05:00
|
|
|
'sum' => '0',
|
|
|
|
'average' => '0',
|
|
|
|
'currencies' => [],
|
|
|
|
'single_currency' => $currencies[$currencyId],
|
|
|
|
'count' => 0,
|
2017-04-17 01:31:42 -05:00
|
|
|
];
|
|
|
|
}
|
2019-05-30 05:31:19 -05:00
|
|
|
$expenses[$key]['currencies'][] = (int)$journal['currency_id'];
|
|
|
|
$expenses[$key]['sum'] = bcadd($expenses[$key]['sum'], $journal['amount']);
|
2018-09-06 12:47:29 -05:00
|
|
|
++$expenses[$key]['count'];
|
2017-04-17 01:31:42 -05:00
|
|
|
}
|
|
|
|
// do averages:
|
2017-08-12 03:27:45 -05:00
|
|
|
$keys = array_keys($expenses);
|
|
|
|
foreach ($keys as $key) {
|
2018-09-06 12:47:29 -05:00
|
|
|
$opposingId = $expenses[$key]['id'];
|
2019-02-13 10:38:41 -06:00
|
|
|
if (1 === $countAccounts[$opposingId]) {
|
2018-09-06 12:47:29 -05:00
|
|
|
$expenses[$key]['name'] = $expenses[$key]['original'];
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:31:42 -05:00
|
|
|
if ($expenses[$key]['count'] > 1) {
|
2018-04-02 08:10:40 -05:00
|
|
|
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], (string)$expenses[$key]['count']);
|
2017-04-17 01:31:42 -05:00
|
|
|
}
|
2019-05-30 05:31:19 -05:00
|
|
|
$expenses[$key]['currencies'] = count(array_unique($expenses[$key]['currencies']));
|
|
|
|
$expenses[$key]['all_currencies'] = count($currencies);
|
2017-04-17 01:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $expenses;
|
|
|
|
}
|
2019-05-30 05:31:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
|
|
|
|
{
|
|
|
|
// get all expenses for the given accounts in the given period!
|
|
|
|
// also transfers!
|
|
|
|
// get all transactions:
|
|
|
|
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
|
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
|
|
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
|
|
|
->withAccountInformation();
|
2019-05-31 06:35:33 -05:00
|
|
|
$income = $this->groupByDestination($collector->getExtractedJournals());
|
2019-05-30 05:31:19 -05:00
|
|
|
|
|
|
|
// sort the result
|
|
|
|
// Obtain a list of columns
|
|
|
|
$sum = [];
|
|
|
|
foreach ($income as $accountId => $row) {
|
|
|
|
$sum[$accountId] = (float)$row['sum'];
|
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($sum, SORT_DESC, $income);
|
|
|
|
|
|
|
|
return $income;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2016-10-23 05:42:44 -05:00
|
|
|
}
|