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

223 lines
7.2 KiB
PHP
Raw Normal View History

2017-03-29 14:21:10 -05:00
<?php
/**
* PopupReport.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://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;
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;
use Log;
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
{
/**
* 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)));
}
}
2017-03-29 14:21:10 -05:00
/**
2019-05-29 14:56:39 -05:00
* Collect the transactions for one account and one budget.
*
* @param Budget $budget
2017-03-29 14:21:10 -05:00
* @param Account $account
* @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);
2017-03-29 14:21:10 -05:00
$collector->setAccounts(new Collection([$account]))->setRange($attributes['startDate'], $attributes['endDate'])->setBudget($budget);
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
* @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
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-30 11:42:02 -05:00
$collector
->setAccounts(new Collection([$account]))
->setTypes([TransactionType::WITHDRAWAL])
->setRange($attributes['startDate'], $attributes['endDate'])
->withoutBudget();
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
* @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);
$currency = $repos->find((int)$currencyId);
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-29 14:21:10 -05:00
$collector->setAccounts($attributes['accounts'])->setRange($attributes['startDate'], $attributes['endDate']);
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.
*
2017-03-29 14:21:10 -05:00
* @param Category $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);
$currency = $repos->find((int)$currencyId);
}
/** @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])
->setRange($attributes['startDate'], $attributes['endDate'])->withAccountInformation()
2017-03-29 14:21:10 -05:00
->setCategory($category);
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
* @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);
$currency = $repos->find((int)$currencyId);
}
2018-07-07 14:17:46 -05:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$repository->setUser($account->user);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-29 14:21:10 -05:00
2019-08-17 05:08:09 -05:00
$collector->setAccounts(new Collection([$account]))
->setRange($attributes['startDate'], $attributes['endDate'])
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
* @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])
->withAccountInformation();
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
}