firefly-iii/app/Helpers/Report/PopupReport.php

257 lines
8.6 KiB
PHP
Raw Normal View History

2017-03-29 14:21:10 -05:00
<?php
/**
* PopupReport.php
2020-01-28 01:46:01 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-03-29 14:21:10 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 01:40:00 -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/>.
2017-03-29 14:21:10 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Helpers\Report;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2017-03-29 14:21:10 -05:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionType;
2021-01-07 13:31:48 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-08-17 05:08:09 -05:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-03-29 14:21:10 -05:00
use Illuminate\Support\Collection;
2019-02-13 10:38:41 -06:00
2017-03-29 14:21:10 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class PopupReport.
2018-08-30 13:58:07 -05:00
*
* @codeCoverageIgnore
2017-03-29 14:21:10 -05:00
*/
class PopupReport implements PopupReportInterface
{
/**
2019-05-29 14:56:39 -05:00
* Collect the transactions for one account and one budget.
*
2019-08-28 05:28:23 -05:00
* @param Budget $budget
2017-03-29 14:21:10 -05:00
* @param Account $account
2019-08-28 05:28:23 -05:00
* @param array $attributes
2017-03-29 14:21:10 -05:00
*
* @return array
2017-03-29 14:21:10 -05:00
*/
public function balanceForBudget(Budget $budget, Account $account, array $attributes): array
2017-03-29 14:21:10 -05:00
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-28 05:28:23 -05:00
$collector->setAccounts(new Collection([$account]))
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
->setRange($attributes['startDate'], $attributes['endDate'])->setBudget($budget);
2017-03-29 14:21:10 -05:00
return $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
}
2017-03-30 11:42:02 -05:00
/**
* Collect the transactions for one account and no budget.
*
2017-03-30 11:42:02 -05:00
* @param Account $account
2019-08-28 05:28:23 -05:00
* @param array $attributes
2017-03-30 11:42:02 -05:00
*
* @return array
2017-03-30 11:42:02 -05:00
*/
public function balanceForNoBudget(Account $account, array $attributes): array
2017-03-30 11:42:02 -05:00
{
// filter by currency, if set.
$currencyId = $attributes['currencyId'] ?? null;
$currency = null;
if (null !== $currencyId) {
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
2021-03-21 03:15:40 -05:00
$currency = $repos->find((int)$currencyId);
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-30 11:42:02 -05:00
$collector
->setAccounts(new Collection([$account]))
->setTypes([TransactionType::WITHDRAWAL])
2019-08-28 05:28:23 -05:00
->withAccountInformation()
->withCategoryInformation()
2017-03-30 11:42:02 -05:00
->setRange($attributes['startDate'], $attributes['endDate'])
->withoutBudget();
if (null !== $currency) {
$collector->setCurrency($currency);
}
return $collector->getExtractedJournals();
2017-03-30 11:42:02 -05:00
}
2017-03-29 14:21:10 -05:00
/**
* Collect the transactions for a budget.
*
2017-03-29 14:21:10 -05:00
* @param Budget $budget
2019-08-28 05:28:23 -05:00
* @param array $attributes
2017-03-29 14:21:10 -05:00
*
* @return array
2017-03-29 14:21:10 -05:00
*/
public function byBudget(Budget $budget, array $attributes): array
2017-03-29 14:21:10 -05:00
{
2019-08-17 05:08:09 -05:00
// filter by currency, if set.
$currencyId = $attributes['currencyId'] ?? null;
$currency = null;
if (null !== $currencyId) {
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
2021-03-21 03:15:40 -05:00
$currency = $repos->find((int)$currencyId);
2019-08-17 05:08:09 -05:00
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-28 05:28:23 -05:00
$collector->setAccounts($attributes['accounts'])
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
->setRange($attributes['startDate'], $attributes['endDate']);
2017-03-29 14:21:10 -05:00
2019-08-17 05:08:09 -05:00
if (null !== $currency) {
$collector->setCurrency($currency);
}
2017-11-15 05:25:49 -06:00
if (null === $budget->id) {
2017-03-29 14:21:10 -05:00
$collector->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
}
2017-11-15 05:25:49 -06:00
if (null !== $budget->id) {
2017-03-29 14:21:10 -05:00
$collector->setBudget($budget);
}
return $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
}
/**
* Collect journals by a category.
*
* @param Category|null $category
* @param array $attributes
2017-03-29 14:21:10 -05:00
*
* @return array
2017-03-29 14:21:10 -05:00
*/
public function byCategory(?Category $category, array $attributes): array
2017-03-29 14:21:10 -05:00
{
2019-08-17 05:08:09 -05:00
// filter by currency, if set.
$currencyId = $attributes['currencyId'] ?? null;
$currency = null;
if (null !== $currencyId) {
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
2021-03-21 03:15:40 -05:00
$currency = $repos->find((int)$currencyId);
2019-08-17 05:08:09 -05:00
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-17 05:08:09 -05:00
$collector->setAccounts($attributes['accounts'])
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::DEPOSIT])
2019-08-28 05:28:23 -05:00
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
->setRange($attributes['startDate'], $attributes['endDate'])->withAccountInformation();
if (null !== $category) {
$collector->setCategory($category);
}
if (null === $category) {
$collector->withoutCategory();
}
2019-08-17 05:08:09 -05:00
if (null !== $currency) {
$collector->setCurrency($currency);
}
2017-03-29 14:21:10 -05:00
return $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
}
/**
* Group transactions by expense.
*
2017-03-29 14:21:10 -05:00
* @param Account $account
2019-08-28 05:28:23 -05:00
* @param array $attributes
2017-03-29 14:21:10 -05:00
*
* @return array
2017-03-29 14:21:10 -05:00
*/
public function byExpenses(Account $account, array $attributes): array
2017-03-29 14:21:10 -05:00
{
2019-08-17 05:08:09 -05:00
// filter by currency, if set.
$currencyId = $attributes['currencyId'] ?? null;
$currency = null;
if (null !== $currencyId) {
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
2021-03-21 03:15:40 -05:00
$currency = $repos->find((int)$currencyId);
2019-08-17 05:08:09 -05:00
}
2018-07-07 14:17:46 -05:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$repository->setUser($account->user);
2021-01-07 13:31:48 -06:00
$accountRepository = app(AccountRepositoryInterface::class);
$accountRepository->setUser($account->user);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-29 14:21:10 -05:00
2021-01-07 13:31:48 -06:00
// set report accounts + the request accounts:
$set = $attributes['accounts'] ?? new Collection;
$set->push($account);
$collector->setBothAccounts($set)
2019-08-17 05:08:09 -05:00
->setRange($attributes['startDate'], $attributes['endDate'])
2019-08-28 05:28:23 -05:00
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
2017-03-29 14:21:10 -05:00
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]);
2019-08-17 05:08:09 -05:00
if (null !== $currency) {
$collector->setCurrency($currency);
}
2019-08-17 05:08:09 -05:00
return $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
}
/**
* Collect transactions by income.
*
2017-03-29 14:21:10 -05:00
* @param Account $account
2019-08-28 05:28:23 -05:00
* @param array $attributes
2017-03-29 14:21:10 -05:00
*
* @return array
2017-03-29 14:21:10 -05:00
*/
public function byIncome(Account $account, array $attributes): array
2017-03-29 14:21:10 -05:00
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$repository->setUser($account->user);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2019-08-14 12:51:46 -05:00
$collector
->setSourceAccounts(new Collection([$account]))
->setDestinationAccounts($attributes['accounts'])
->setRange($attributes['startDate'], $attributes['endDate'])
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
2019-08-28 05:28:23 -05:00
->withAccountInformation()
->withBudgetInformation()
->withCategoryInformation()
->withAccountInformation();
2019-08-28 05:28:23 -05:00
2019-08-14 12:51:46 -05:00
return $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
}
2017-07-07 01:09:42 -05:00
}