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

203 lines
6.6 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\Transaction;
use FireflyIII\Models\TransactionType;
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
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-29 14:21:10 -05:00
$collector->setAccounts($attributes['accounts'])->setRange($attributes['startDate'], $attributes['endDate']);
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
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2017-03-29 14:21:10 -05:00
$collector->setAccounts($attributes['accounts'])->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setRange($attributes['startDate'], $attributes['endDate'])->withAccountInformation()
2017-03-29 14:21:10 -05:00
->setCategory($category);
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
{
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
$collector->setAccounts(new Collection([$account]))->setRange($attributes['startDate'], $attributes['endDate'])
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]);
$journals = $collector->getExtractedJournals();
2017-03-29 14:21:10 -05:00
$report = $attributes['accounts']->pluck('id')->toArray(); // accounts used in this report
$filtered = [];
// TODO not sure if filter is necessary.
/** @var array $journal */
foreach ($journals as $journal) {
if (in_array($journal['source_account_id'], $report, true)) {
$filtered[] = $journal;
2017-03-29 14:21:10 -05:00
}
}
return $filtered;
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);
2017-03-29 14:21:10 -05:00
$collector->setAccounts(new Collection([$account]))->setRange($attributes['startDate'], $attributes['endDate'])
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->withAccountInformation();
$journals = $collector->getExtractedJournals();
$report = $attributes['accounts']->pluck('id')->toArray(); // accounts used in this report
2017-03-29 14:21:10 -05:00
// filter the set so the destinations outside of $attributes['accounts'] are not included.
// TODO not sure if filter is necessary.
$filtered = [];
/** @var array $journal */
foreach ($journals as $journal) {
if (in_array($journal['destination_account_id'], $report, true)) {
$filtered[] = $journal;
2017-03-29 14:21:10 -05:00
}
}
return $filtered;
2017-03-29 14:21:10 -05:00
}
2017-07-07 01:09:42 -05:00
}