2019-08-29 14:33:12 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OperationsRepository.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-08-29 14:33:12 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-08-29 14:33:12 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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.
|
2019-08-29 14:33:12 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-08-29 14:33:12 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-08-29 14:33:12 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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/>.
|
2019-08-29 14:33:12 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Budget;
|
|
|
|
|
2019-08-29 14:42:55 -05:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2019-08-29 14:33:12 -05:00
|
|
|
use FireflyIII\Models\Budget;
|
2019-08-30 01:19:55 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2019-08-31 02:35:35 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-08-29 14:33:12 -05:00
|
|
|
use FireflyIII\User;
|
2019-08-29 14:42:55 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2019-08-29 14:33:12 -05:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class OperationsRepository
|
|
|
|
*/
|
|
|
|
class OperationsRepository implements OperationsRepositoryInterface
|
|
|
|
{
|
2020-09-18 09:14:17 -05:00
|
|
|
private User $user;
|
2019-08-29 14:33:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A method that returns the amount of money budgeted per day for this budget,
|
|
|
|
* on average.
|
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function budgetedPerDay(Budget $budget): string
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Now with budget #%d "%s"', $budget->id, $budget->name));
|
|
|
|
$total = '0';
|
|
|
|
$count = 0;
|
|
|
|
foreach ($budget->budgetlimits as $limit) {
|
|
|
|
$diff = $limit->start_date->diffInDays($limit->end_date);
|
|
|
|
$diff = 0 === $diff ? 1 : $diff;
|
2021-03-11 23:20:01 -06:00
|
|
|
$amount = (string)$limit->amount;
|
|
|
|
$perDay = bcdiv($amount, (string)$diff);
|
2019-08-29 14:33:12 -05:00
|
|
|
$total = bcadd($total, $perDay);
|
|
|
|
$count++;
|
|
|
|
Log::debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
|
|
|
|
}
|
|
|
|
$avg = $total;
|
|
|
|
if ($count > 0) {
|
2021-03-11 23:20:01 -06:00
|
|
|
$avg = bcdiv($total, (string)$count);
|
2019-08-29 14:33:12 -05:00
|
|
|
}
|
|
|
|
Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
|
|
|
|
|
|
|
|
return $avg;
|
|
|
|
}
|
|
|
|
|
2019-08-30 01:12:15 -05:00
|
|
|
/**
|
|
|
|
* This method is being used to generate the budget overview in the year/multi-year report. Its used
|
|
|
|
* in both the year/multi-year budget overview AND in the accompanying chart.
|
|
|
|
*
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$carbonFormat = app('navigation')->preferredCarbonFormat($start, $end);
|
|
|
|
$data = [];
|
|
|
|
// get all transactions:
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
|
|
$collector->setBudgets($budgets);
|
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
|
|
|
|
// loop transactions:
|
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
// prep data array for currency:
|
2021-03-11 23:20:01 -06:00
|
|
|
$budgetId = (int)$journal['budget_id'];
|
2019-08-30 01:12:15 -05:00
|
|
|
$budgetName = $journal['budget_name'];
|
2021-03-11 23:20:01 -06:00
|
|
|
$currencyId = (int)$journal['currency_id'];
|
2019-08-30 01:12:15 -05:00
|
|
|
$key = sprintf('%d-%d', $budgetId, $currencyId);
|
|
|
|
|
|
|
|
$data[$key] = $data[$key] ?? [
|
|
|
|
'id' => $budgetId,
|
|
|
|
'name' => sprintf('%s (%s)', $budgetName, $journal['currency_name']),
|
|
|
|
'sum' => '0',
|
|
|
|
'currency_id' => $currencyId,
|
|
|
|
'currency_code' => $journal['currency_code'],
|
|
|
|
'currency_name' => $journal['currency_name'],
|
|
|
|
'currency_symbol' => $journal['currency_symbol'],
|
|
|
|
'currency_decimal_places' => $journal['currency_decimal_places'],
|
|
|
|
'entries' => [],
|
|
|
|
];
|
|
|
|
$date = $journal['date']->format($carbonFormat);
|
|
|
|
$data[$key]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $journal['amount']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:41:57 -05:00
|
|
|
/**
|
|
|
|
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
|
|
|
|
* which have the specified budget set to them. 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|null $accounts
|
|
|
|
* @param Collection|null $budgets
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array
|
|
|
|
{
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]);
|
|
|
|
if (null !== $accounts && $accounts->count() > 0) {
|
|
|
|
$collector->setAccounts($accounts);
|
|
|
|
}
|
|
|
|
if (null !== $budgets && $budgets->count() > 0) {
|
|
|
|
$collector->setBudgets($budgets);
|
|
|
|
}
|
|
|
|
if (null === $budgets || (null !== $budgets && 0 === $budgets->count())) {
|
|
|
|
$collector->setBudgets($this->getBudgets());
|
|
|
|
}
|
2019-09-02 13:30:47 -05:00
|
|
|
$collector->withBudgetInformation()->withAccountInformation()->withCategoryInformation();
|
2019-09-01 11:41:57 -05:00
|
|
|
$journals = $collector->getExtractedJournals();
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
foreach ($journals as $journal) {
|
2021-03-11 23:20:01 -06:00
|
|
|
$currencyId = (int)$journal['currency_id'];
|
|
|
|
$budgetId = (int)$journal['budget_id'];
|
|
|
|
$budgetName = (string)$journal['budget_name'];
|
2019-09-01 11:41:57 -05:00
|
|
|
|
|
|
|
// catch "no category" entries.
|
|
|
|
if (0 === $budgetId) {
|
2019-09-01 12:08:10 -05:00
|
|
|
continue;
|
2019-09-01 11:41:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// info about the currency:
|
|
|
|
$array[$currencyId] = $array[$currencyId] ?? [
|
|
|
|
'budgets' => [],
|
|
|
|
'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'],
|
|
|
|
];
|
|
|
|
|
|
|
|
// info about the categories:
|
|
|
|
$array[$currencyId]['budgets'][$budgetId] = $array[$currencyId]['budgets'][$budgetId] ?? [
|
|
|
|
'id' => $budgetId,
|
|
|
|
'name' => $budgetName,
|
|
|
|
'transaction_journals' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
// add journal to array:
|
|
|
|
// only a subset of the fields.
|
2021-08-20 13:49:42 -05:00
|
|
|
$journalId = (int)$journal['transaction_journal_id'];
|
2019-09-01 11:41:57 -05:00
|
|
|
$array[$currencyId]['budgets'][$budgetId]['transaction_journals'][$journalId] = [
|
2019-09-02 13:30:47 -05:00
|
|
|
'amount' => app('steam')->negative($journal['amount']),
|
|
|
|
'destination_account_id' => $journal['destination_account_id'],
|
|
|
|
'destination_account_name' => $journal['destination_account_name'],
|
|
|
|
'source_account_id' => $journal['source_account_id'],
|
|
|
|
'source_account_name' => $journal['source_account_name'],
|
|
|
|
'category_name' => $journal['category_name'],
|
|
|
|
'description' => $journal['description'],
|
|
|
|
'transaction_group_id' => $journal['transaction_group_id'],
|
|
|
|
'date' => $journal['date'],
|
2019-09-01 11:41:57 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:33:12 -05:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2019-08-29 14:42:55 -05:00
|
|
|
|
2019-09-01 11:41:57 -05:00
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection */
|
|
|
|
|
2019-08-30 01:19:55 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user);
|
|
|
|
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
|
|
|
|
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$collector->setAccounts($accounts);
|
|
|
|
}
|
2021-08-20 13:49:42 -05:00
|
|
|
// See reference nr. 13
|
2019-08-30 01:19:55 -05:00
|
|
|
$set = $collector->getGroups();
|
|
|
|
$return = [];
|
|
|
|
$total = [];
|
|
|
|
$currencies = [];
|
|
|
|
/** @var array $group */
|
|
|
|
foreach ($set as $group) {
|
|
|
|
/** @var array $transaction */
|
|
|
|
foreach ($group['transactions'] as $transaction) {
|
|
|
|
$code = $transaction['currency_code'];
|
2021-04-07 00:28:43 -05:00
|
|
|
if (!array_key_exists($code, $currencies)) {
|
2019-08-30 01:19:55 -05:00
|
|
|
$currencies[$code] = [
|
|
|
|
'id' => $transaction['currency_id'],
|
|
|
|
'decimal_places' => $transaction['currency_decimal_places'],
|
|
|
|
'code' => $transaction['currency_code'],
|
|
|
|
'name' => $transaction['currency_name'],
|
|
|
|
'symbol' => $transaction['currency_symbol'],
|
|
|
|
];
|
|
|
|
}
|
2021-04-07 00:28:43 -05:00
|
|
|
$total[$code] = array_key_exists($code, $total) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount'];
|
2019-08-30 01:19:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @var string $code
|
|
|
|
* @var string $spent
|
|
|
|
*/
|
|
|
|
foreach ($total as $code => $spent) {
|
|
|
|
/** @var TransactionCurrency $currency */
|
|
|
|
$currency = $currencies[$code];
|
|
|
|
$return[] = [
|
2021-03-06 00:20:49 -06:00
|
|
|
'currency_id' => (string)$currency['id'],
|
2019-08-30 01:19:55 -05:00
|
|
|
'currency_code' => $code,
|
|
|
|
'currency_name' => $currency['name'],
|
|
|
|
'currency_symbol' => $currency['symbol'],
|
|
|
|
'currency_decimal_places' => $currency['decimal_places'],
|
2021-03-11 23:20:01 -06:00
|
|
|
'amount' => number_format((float)$spent, $currency['decimal_places'], '.', ''),
|
2019-08-30 01:19:55 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-08-31 02:35:35 -05:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection|null $accounts
|
|
|
|
* @param Collection|null $budgets
|
|
|
|
* @param TransactionCurrency|null $currency
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sumExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null, ?TransactionCurrency $currency = null
|
2021-03-11 23:20:01 -06:00
|
|
|
): array {
|
2020-10-01 05:48:27 -05:00
|
|
|
Log::debug(sprintf('Now in %s', __METHOD__));
|
2020-11-08 07:06:49 -06:00
|
|
|
$start->startOfDay();
|
|
|
|
$end->endOfDay();
|
2019-08-31 02:35:35 -05:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]);
|
|
|
|
|
2020-10-01 05:48:27 -05:00
|
|
|
if (null !== $accounts) {
|
2019-08-31 02:35:35 -05:00
|
|
|
$collector->setAccounts($accounts);
|
|
|
|
}
|
2020-10-01 05:48:27 -05:00
|
|
|
if (null === $budgets) {
|
2019-08-31 02:35:35 -05:00
|
|
|
$budgets = $this->getBudgets();
|
|
|
|
}
|
|
|
|
if (null !== $currency) {
|
|
|
|
$collector->setCurrency($currency);
|
|
|
|
}
|
|
|
|
$collector->setBudgets($budgets);
|
|
|
|
$journals = $collector->getExtractedJournals();
|
2020-10-01 05:48:27 -05:00
|
|
|
|
|
|
|
// same but for foreign currencies:
|
|
|
|
if (null !== $currency) {
|
|
|
|
Log::debug(sprintf('Currency is "%s".', $currency->name));
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->setForeignCurrency($currency)->setBudgets($budgets);
|
|
|
|
|
|
|
|
if (null !== $accounts) {
|
|
|
|
$collector->setAccounts($accounts);
|
|
|
|
}
|
|
|
|
$result = $collector->getExtractedJournals();
|
|
|
|
Log::debug(sprintf('Found %d journals with currency %s.', count($result), $currency->code));
|
|
|
|
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
|
|
|
|
$journals = $result + $journals;
|
|
|
|
}
|
|
|
|
$array = [];
|
2019-08-31 02:35:35 -05:00
|
|
|
|
|
|
|
foreach ($journals as $journal) {
|
2021-03-11 23:20:01 -06:00
|
|
|
$currencyId = (int)$journal['currency_id'];
|
2019-08-31 02:35:35 -05:00
|
|
|
$array[$currencyId] = $array[$currencyId] ?? [
|
|
|
|
'sum' => '0',
|
|
|
|
'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'],
|
|
|
|
];
|
|
|
|
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
|
2020-10-01 05:48:27 -05:00
|
|
|
|
|
|
|
// also do foreign amount:
|
2021-03-11 23:20:01 -06:00
|
|
|
$foreignId = (int)$journal['foreign_currency_id'];
|
|
|
|
if (0 !== $foreignId) {
|
2020-10-01 05:48:27 -05:00
|
|
|
$array[$foreignId] = $array[$foreignId] ?? [
|
|
|
|
'sum' => '0',
|
|
|
|
'currency_id' => $foreignId,
|
|
|
|
'currency_name' => $journal['foreign_currency_name'],
|
|
|
|
'currency_symbol' => $journal['foreign_currency_symbol'],
|
|
|
|
'currency_code' => $journal['foreign_currency_code'],
|
|
|
|
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
|
|
|
|
];
|
|
|
|
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->negative($journal['foreign_amount']));
|
|
|
|
}
|
2019-08-31 02:35:35 -05:00
|
|
|
}
|
2021-03-11 23:20:01 -06:00
|
|
|
|
2021-08-20 13:49:42 -05:00
|
|
|
return $array;
|
2021-03-11 23:20:01 -06:00
|
|
|
}
|
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function getBudgets(): Collection
|
|
|
|
{
|
|
|
|
/** @var BudgetRepositoryInterface $repos */
|
|
|
|
$repos = app(BudgetRepositoryInterface::class);
|
|
|
|
|
|
|
|
return $repos->getActiveBudgets();
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:42:55 -05:00
|
|
|
/**
|
|
|
|
* For now, simply refer to whichever repository holds this function.
|
2021-08-20 13:49:42 -05:00
|
|
|
* See reference nr. 14
|
2019-08-29 14:42:55 -05:00
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon|null $start
|
|
|
|
* @param Carbon|null $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection
|
|
|
|
{
|
2019-08-30 01:09:39 -05:00
|
|
|
/** @var BudgetLimitRepositoryInterface $blRepository */
|
|
|
|
$blRepository = app(BudgetLimitRepositoryInterface::class);
|
2019-08-29 14:42:55 -05:00
|
|
|
|
2019-08-30 01:09:39 -05:00
|
|
|
return $blRepository->getBudgetLimits($budget, $start, $end);
|
2019-08-29 14:42:55 -05:00
|
|
|
}
|
2020-05-30 00:33:06 -05:00
|
|
|
}
|