2018-08-11 14:06:49 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TransactionCalculation.php
|
2019-10-02 06:37:26 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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.
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-08-11 14:06:49 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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/>.
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Http\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-05-30 06:23:25 +02:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2018-08-11 14:06:49 +02:00
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait TransactionCalculation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
trait TransactionCalculation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get all expenses for a set of accounts.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $opposing
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getExpensesForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-31 13:35:33 +02:00
|
|
|
$total = $accounts->merge($opposing);
|
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2019-05-31 13:35:33 +02:00
|
|
|
$collector->setAccounts($total)
|
2019-05-30 06:23:25 +02:00
|
|
|
->setRange($start, $end)
|
2019-05-31 13:35:33 +02:00
|
|
|
->withAccountInformation()
|
|
|
|
->setTypes([TransactionType::WITHDRAWAL]);
|
2019-05-30 06:23:25 +02:00
|
|
|
|
2019-06-29 08:14:28 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all expenses by tags.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $tags
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getExpensesForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
|
|
|
->setTags($tags)->withAccountInformation();
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that collects expenses for the given budgets.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $budgets
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getExpensesInBudgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2018-08-11 14:06:49 +02:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
2019-05-30 06:23:25 +02:00
|
|
|
->setBudgets($budgets)->withAccountInformation();
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all expenses in a period for categories.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $categories
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getExpensesInCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector
|
|
|
|
->setAccounts($accounts)
|
|
|
|
->setRange($start, $end)
|
|
|
|
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
|
|
|
->setCategories($categories)
|
|
|
|
->withAccountInformation();
|
|
|
|
|
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all income for a period and a bunch of categories.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $categories
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getIncomeForCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2018-08-11 14:06:49 +02:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
2019-05-30 06:23:25 +02:00
|
|
|
->setCategories($categories)->withAccountInformation();
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the income for a set of accounts.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $opposing
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
2019-05-30 06:23:25 +02:00
|
|
|
* @return array
|
2018-08-11 14:06:49 +02:00
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getIncomeForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-31 13:35:33 +02:00
|
|
|
$total =$accounts->merge($opposing);
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2019-05-31 13:35:33 +02:00
|
|
|
$collector->setAccounts($total)->setRange($start, $end)->withAccountInformation()->setTypes([TransactionType::DEPOSIT]);
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all income by tag.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Collection $tags
|
2019-05-30 06:23:25 +02:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2018-08-11 14:06:49 +02:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-05-30 06:23:25 +02:00
|
|
|
protected function getIncomeForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): array
|
2018-08-11 14:06:49 +02:00
|
|
|
{
|
2019-05-30 06:23:25 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2018-08-11 14:06:49 +02:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
2019-05-30 06:23:25 +02:00
|
|
|
->setTags($tags)->withAccountInformation();
|
2018-08-11 14:06:49 +02:00
|
|
|
|
2019-05-30 06:23:25 +02:00
|
|
|
return $collector->getExtractedJournals();
|
2018-08-11 14:06:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:48:23 +01:00
|
|
|
}
|