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

322 lines
14 KiB
PHP
Raw Normal View History

2016-10-08 07:54:32 -05:00
<?php
/**
* AccountTasker.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2016-10-08 07:54:32 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2016-10-08 07:54:32 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-10-08 07:54:32 -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;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
2017-04-17 01:31:42 -05:00
use FireflyIII\Models\TransactionType;
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
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
2019-06-07 11:20:15 -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-08-21 10:27:59 -05:00
* @param Carbon $start
* @param Carbon $end
2016-10-08 09:04:05 -05:00
*
* @return array
2016-10-08 09:04:05 -05: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);
Log::debug('Start of accountreport');
/** @var AccountRepositoryInterface $repository */
2019-08-15 23:21:10 -05:00
$repository = app(AccountRepositoryInterface::class);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
$return = [
2019-08-15 23:21:10 -05:00
'accounts' => [],
'sums' => [],
];
2016-10-08 09:04:05 -05:00
/** @var Account $account */
2016-10-08 09:04:05 -05:00
foreach ($accounts as $account) {
2019-08-15 23:21:10 -05:00
$id = $account->id;
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$return['sums'][$currency->id] = $return['sums'][$currency->id] ?? [
'start' => '0',
'end' => '0',
'difference' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_name' => $currency->name,
'currency_decimal_places' => $currency->decimal_places,];
$entry = [
'name' => $account->name,
'id' => $account->id,
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_name' => $currency->name,
'currency_decimal_places' => $currency->decimal_places,
'start_balance' => '0',
'end_balance' => '0',
];
// get first journal date:
$first = $repository->oldestJournal($account);
$entry['start_balance'] = $startSet[$account->id] ?? '0';
$entry['end_balance'] = $endSet[$account->id] ?? '0';
// first journal exists, and is on start, then this is the actual opening balance:
2019-08-21 10:27:59 -05:00
if (null !== $first && $first->date->isSameDay($start) && TransactionType::OPENING_BALANCE === $first->transactionType->type) {
Log::debug(sprintf('Date of first journal for %s is %s', $account->name, $first->date->format('Y-m-d')));
$entry['start_balance'] = $first->transactions()->where('account_id', $account->id)->first()->amount;
Log::debug(sprintf('Account %s was opened on %s, so opening balance is %f', $account->name, $start->format('Y-m-d'), $entry['start_balance']));
}
2019-08-15 23:21:10 -05:00
$return['sums'][$currency->id]['start'] = bcadd($return['sums'][$currency->id]['start'], $entry['start_balance']);
$return['sums'][$currency->id]['end'] = bcadd($return['sums'][$currency->id]['end'], $entry['end_balance']);
$return['accounts'][$id] = $entry;
}
2016-10-08 09:04:05 -05:00
2019-08-15 23:21:10 -05:00
foreach (array_keys($return['sums']) as $index) {
$return['sums'][$index]['difference'] = bcsub($return['sums'][$index]['end'], $return['sums'][$index]['start']);
}
2016-10-08 09:04:05 -05: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-08-21 10:27:59 -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:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-14 12:06:05 -05:00
$collector->setSourceAccounts($accounts)->setRange($start, $end);
$collector->excludeDestinationAccounts($accounts);
2019-08-15 23:21:10 -05:00
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])->withAccountInformation();
$journals = $collector->getExtractedJournals();
2019-08-17 00:47:39 -05:00
2019-08-15 23:21:10 -05:00
$report = $this->groupExpenseByDestination($journals);
2019-08-17 01:04:41 -05:00
// sort the result
// Obtain a list of columns
$sum = [];
foreach ($report['accounts'] as $accountId => $row) {
$sum[$accountId] = (float)$row['sum'];
}
array_multisort($sum, SORT_ASC, $report['accounts']);
2019-08-15 23:21:10 -05:00
return $report;
2017-04-17 01:31:42 -05:00
}
2019-07-31 09:53:09 -05:00
/**
2019-08-21 10:27:59 -05:00
* @param Carbon $start
* @param Carbon $end
2019-07-31 09:53:09 -05:00
* @param Collection $accounts
*
* @return array
*/
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
{
2019-08-13 23:16:44 -05:00
// get all incomes for the given accounts in the given period!
2019-07-31 09:53:09 -05:00
// also transfers!
// get all transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-14 12:06:05 -05:00
$collector->setDestinationAccounts($accounts)->setRange($start, $end);
$collector->excludeSourceAccounts($accounts);
2019-08-15 23:21:10 -05:00
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])->withAccountInformation();
$report = $this->groupIncomeBySource($collector->getExtractedJournals());
2019-07-31 09:53:09 -05:00
// sort the result
// Obtain a list of columns
2019-08-17 01:04:41 -05:00
$sum = [];
foreach ($report['accounts'] as $accountId => $row) {
$sum[$accountId] = (float)$row['sum'];
}
array_multisort($sum, SORT_DESC, $report['accounts']);
2019-08-15 23:21:10 -05:00
return $report;
2019-07-31 09:53:09 -05:00
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
2017-04-17 01:31:42 -05:00
/**
* @param array $array
2017-04-17 01:31:42 -05:00
*
* @return array
*/
2019-08-14 12:06:05 -05:00
private function groupExpenseByDestination(array $array): array
2017-04-17 01:31:42 -05:00
{
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
2019-08-15 23:21:10 -05:00
$report = [
'accounts' => [],
'sums' => [],
];
/** @var array $journal */
foreach ($array as $journal) {
2019-08-21 10:27:59 -05:00
$sourceId = (int)$journal['destination_account_id'];
$currencyId = (int)$journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$report['accounts'][$key] = $report['accounts'][$key] ?? [
2019-08-15 23:21:10 -05:00
'id' => $sourceId,
'name' => $journal['destination_account_name'],
'sum' => '0',
'average' => '0',
'count' => 0,
'currency_id' => $currencies[$currencyId]->id,
'currency_name' => $currencies[$currencyId]->name,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_decimal_places' => $currencies[$currencyId]->decimal_places,
2017-04-17 01:31:42 -05:00
];
2019-08-15 23:21:10 -05:00
$report['accounts'][$key]['sum'] = bcadd($report['accounts'][$key]['sum'], $journal['amount']);
2019-08-17 00:47:39 -05:00
Log::debug(sprintf('Sum for %s is now %s', $journal['destination_account_name'], $report['accounts'][$key]['sum']));
2019-08-15 23:21:10 -05:00
++$report['accounts'][$key]['count'];
2017-04-17 01:31:42 -05:00
}
2019-08-14 12:06:05 -05:00
2019-08-15 23:21:10 -05:00
// do averages and sums.
foreach (array_keys($report['accounts']) as $key) {
if ($report['accounts'][$key]['count'] > 1) {
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string)$report['accounts'][$key]['count']);
}
2019-08-15 23:21:10 -05:00
$currencyId = $report['accounts'][$key]['currency_id'];
$report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
$report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $report['accounts'][$key]['sum']);
2017-04-17 01:31:42 -05:00
}
2019-08-21 10:27:59 -05:00
2019-08-15 23:21:10 -05:00
return $report;
2017-04-17 01:31:42 -05:00
}
2019-08-14 12:06:05 -05:00
/**
* @param array $array
*
* @return array
*/
private function groupIncomeBySource(array $array): array
{
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
2019-08-15 23:21:10 -05:00
$report = [
'accounts' => [],
'sums' => [],
];
2019-08-14 12:06:05 -05:00
/** @var array $journal */
foreach ($array as $journal) {
2019-08-21 10:27:59 -05:00
$sourceId = (int)$journal['source_account_id'];
$currencyId = (int)$journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
2019-08-15 23:21:10 -05:00
if (!isset($report['accounts'][$key])) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$report['accounts'][$key] = [
'id' => $sourceId,
'name' => $journal['source_account_name'],
'sum' => '0',
'average' => '0',
'count' => 0,
'currency_id' => $currencies[$currencyId]->id,
'currency_name' => $currencies[$currencyId]->name,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_decimal_places' => $currencies[$currencyId]->decimal_places,
2019-08-14 12:06:05 -05:00
];
}
2019-08-15 23:21:10 -05:00
$report['accounts'][$key]['sum'] = bcadd($report['accounts'][$key]['sum'], bcmul($journal['amount'], '-1'));
++$report['accounts'][$key]['count'];
2019-08-14 12:06:05 -05:00
}
2019-08-15 23:21:10 -05:00
// do averages and sums.
foreach (array_keys($report['accounts']) as $key) {
if ($report['accounts'][$key]['count'] > 1) {
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string)$report['accounts'][$key]['count']);
2019-08-14 12:06:05 -05:00
}
2019-08-15 23:21:10 -05:00
$currencyId = $report['accounts'][$key]['currency_id'];
$report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
$report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $report['accounts'][$key]['sum']);
2019-08-14 12:06:05 -05:00
}
2019-08-15 23:21:10 -05:00
return $report;
2019-08-14 12:06:05 -05:00
}
2016-10-23 05:42:44 -05:00
}