2017-01-03 10:02:17 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MetaPieChart.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2017-01-03 10:02:17 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Helpers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2017-04-29 01:22:56 -05:00
|
|
|
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
|
|
|
|
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
|
|
|
|
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
|
2017-04-28 13:08:25 -05:00
|
|
|
use FireflyIII\Helpers\Filter\TransferFilter;
|
2017-02-24 14:09:20 -06:00
|
|
|
use FireflyIII\Models\Tag;
|
2017-01-03 10:02:17 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2017-02-24 14:09:20 -06:00
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
2017-01-03 10:02:17 -06:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-02-11 08:52:55 -06:00
|
|
|
use Steam;
|
2017-01-03 10:02:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MetaPieChart
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Chart
|
|
|
|
*/
|
|
|
|
class MetaPieChart implements MetaPieChartInterface
|
|
|
|
{
|
|
|
|
/** @var Collection */
|
|
|
|
protected $accounts;
|
|
|
|
/** @var Collection */
|
|
|
|
protected $budgets;
|
|
|
|
/** @var Collection */
|
|
|
|
protected $categories;
|
|
|
|
/** @var bool */
|
|
|
|
protected $collectOtherObjects = false;
|
|
|
|
/** @var Carbon */
|
|
|
|
protected $end;
|
|
|
|
/** @var array */
|
|
|
|
protected $grouping
|
|
|
|
= [
|
|
|
|
'account' => ['opposing_account_id'],
|
|
|
|
'budget' => ['transaction_journal_budget_id', 'transaction_budget_id'],
|
|
|
|
'category' => ['transaction_journal_category_id', 'transaction_category_id'],
|
2017-02-24 14:09:20 -06:00
|
|
|
'tag' => [],
|
2017-01-03 10:02:17 -06:00
|
|
|
];
|
|
|
|
/** @var array */
|
|
|
|
protected $repositories
|
|
|
|
= [
|
|
|
|
'account' => AccountRepositoryInterface::class,
|
|
|
|
'budget' => BudgetRepositoryInterface::class,
|
|
|
|
'category' => CategoryRepositoryInterface::class,
|
2017-02-24 14:09:20 -06:00
|
|
|
'tag' => TagRepositoryInterface::class,
|
2017-01-03 10:02:17 -06:00
|
|
|
];
|
|
|
|
/** @var Carbon */
|
|
|
|
protected $start;
|
2017-02-24 13:27:26 -06:00
|
|
|
/** @var Collection */
|
|
|
|
protected $tags;
|
2017-01-03 10:02:17 -06:00
|
|
|
/** @var string */
|
|
|
|
protected $total = '0';
|
|
|
|
/** @var User */
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->accounts = new Collection;
|
|
|
|
$this->budgets = new Collection;
|
|
|
|
$this->categories = new Collection;
|
2017-02-24 14:01:33 -06:00
|
|
|
$this->tags = new Collection;
|
2017-01-03 10:02:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $direction
|
|
|
|
* @param string $group
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function generate(string $direction, string $group): array
|
|
|
|
{
|
|
|
|
$transactions = $this->getTransactions($direction);
|
|
|
|
$grouped = $this->groupByFields($transactions, $this->grouping[$group]);
|
|
|
|
$chartData = $this->organizeByType($group, $grouped);
|
|
|
|
|
|
|
|
// also collect all other transactions
|
|
|
|
if ($this->collectOtherObjects && $direction === 'expense') {
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 09:16:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-04-28 11:04:57 -05:00
|
|
|
$collector->setUser($this->user);
|
2017-01-03 10:02:17 -06:00
|
|
|
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]);
|
|
|
|
$journals = $collector->getJournals();
|
|
|
|
$sum = strval($journals->sum('transaction_amount'));
|
|
|
|
$sum = bcmul($sum, '-1');
|
|
|
|
$sum = bcsub($sum, $this->total);
|
|
|
|
$chartData[strval(trans('firefly.everything_else'))] = $sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->collectOtherObjects && $direction === 'income') {
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 09:16:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-04-28 11:04:57 -05:00
|
|
|
$collector->setUser($this->user);
|
2017-01-03 10:02:17 -06:00
|
|
|
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]);
|
|
|
|
$journals = $collector->getJournals();
|
|
|
|
$sum = strval($journals->sum('transaction_amount'));
|
|
|
|
$sum = bcsub($sum, $this->total);
|
|
|
|
$chartData[strval(trans('firefly.everything_else'))] = $sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $chartData;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setAccounts(Collection $accounts): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->accounts = $accounts;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param Collection $budgets
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setBudgets(Collection $budgets): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->budgets = $budgets;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param Collection $categories
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setCategories(Collection $categories): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->categories = $categories;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param bool $collectOtherObjects
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setCollectOtherObjects(bool $collectOtherObjects): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->collectOtherObjects = $collectOtherObjects;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setEnd(Carbon $end): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->end = $end;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param Carbon $start
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setStart(Carbon $start): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->start = $start;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-02-24 13:27:26 -06:00
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-02-24 13:27:26 -06:00
|
|
|
* @param Collection $tags
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setTags(Collection $tags): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->tags = $tags;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:02:17 -06:00
|
|
|
/**
|
2017-04-28 11:04:57 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2017-01-03 10:02:17 -06:00
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return MetaPieChartInterface
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): MetaPieChartInterface
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:04:57 -05:00
|
|
|
/**
|
|
|
|
* @param string $direction
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
protected function getTransactions(string $direction): Collection
|
2017-01-03 10:02:17 -06:00
|
|
|
{
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 09:16:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-04-29 01:22:56 -05:00
|
|
|
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
|
|
|
|
$collector->addFilter(NegativeAmountFilter::class);
|
|
|
|
if ($direction === 'expense') {
|
|
|
|
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
|
|
|
|
$collector->addFilter(PositiveAmountFilter::class);
|
|
|
|
$collector->removeFilter(NegativeAmountFilter::class);
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:04:57 -05:00
|
|
|
$collector->setUser($this->user);
|
2017-01-03 10:02:17 -06:00
|
|
|
$collector->setAccounts($this->accounts);
|
|
|
|
$collector->setRange($this->start, $this->end);
|
|
|
|
$collector->setTypes($types);
|
|
|
|
$collector->withOpposingAccount();
|
2017-04-29 01:22:56 -05:00
|
|
|
$collector->addFilter(OpposingAccountFilter::class);
|
2017-01-03 10:02:17 -06:00
|
|
|
|
|
|
|
if ($direction === 'income') {
|
2017-04-28 13:08:25 -05:00
|
|
|
$collector->removeFilter(TransferFilter::class);
|
2017-01-03 10:02:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->budgets->count() > 0) {
|
|
|
|
$collector->setBudgets($this->budgets);
|
|
|
|
}
|
|
|
|
if ($this->categories->count() > 0) {
|
|
|
|
$collector->setCategories($this->categories);
|
|
|
|
}
|
2017-02-24 13:27:26 -06:00
|
|
|
if ($this->tags->count() > 0) {
|
|
|
|
$collector->setTags($this->tags);
|
|
|
|
$collector->withCategoryInformation();
|
|
|
|
$collector->withBudgetInformation();
|
|
|
|
}
|
2017-01-03 10:02:17 -06:00
|
|
|
|
2017-04-29 01:22:56 -05:00
|
|
|
return $collector->getJournals();
|
2017-01-03 10:02:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $set
|
|
|
|
* @param array $fields
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-24 14:09:20 -06:00
|
|
|
protected function groupByFields(Collection $set, array $fields): array
|
2017-01-03 10:02:17 -06:00
|
|
|
{
|
2017-02-24 14:09:20 -06:00
|
|
|
if (count($fields) === 0 && $this->tags->count() > 0) {
|
|
|
|
// do a special group on tags:
|
|
|
|
return $this->groupByTag($set);
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:02:17 -06:00
|
|
|
$grouped = [];
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($set as $transaction) {
|
|
|
|
$values = [];
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$values[] = intval($transaction->$field);
|
|
|
|
}
|
|
|
|
$value = max($values);
|
|
|
|
$grouped[$value] = $grouped[$value] ?? '0';
|
|
|
|
$grouped[$value] = bcadd($transaction->transaction_amount, $grouped[$value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $grouped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function organizeByType(string $type, array $array): array
|
|
|
|
{
|
|
|
|
$chartData = [];
|
|
|
|
$names = [];
|
2017-02-05 09:16:15 -06:00
|
|
|
$repository = app($this->repositories[$type]);
|
2017-04-28 11:04:57 -05:00
|
|
|
$repository->setUser($this->user);
|
2017-01-03 10:02:17 -06:00
|
|
|
foreach ($array as $objectId => $amount) {
|
|
|
|
if (!isset($names[$objectId])) {
|
|
|
|
$object = $repository->find(intval($objectId));
|
2017-02-24 14:09:20 -06:00
|
|
|
$names[$objectId] = $object->name ?? $object->tag;
|
2017-01-03 10:02:17 -06:00
|
|
|
}
|
2017-02-11 08:52:55 -06:00
|
|
|
$amount = Steam::positive($amount);
|
2017-01-03 10:02:17 -06:00
|
|
|
$this->total = bcadd($this->total, $amount);
|
|
|
|
$chartData[$names[$objectId]] = $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $chartData;
|
|
|
|
|
|
|
|
}
|
2017-02-24 14:09:20 -06:00
|
|
|
|
2017-04-28 11:04:57 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $set
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-24 14:09:20 -06:00
|
|
|
private function groupByTag(Collection $set): array
|
|
|
|
{
|
|
|
|
$grouped = [];
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($set as $transaction) {
|
|
|
|
$journal = $transaction->transactionJournal;
|
|
|
|
$tags = $journal->tags;
|
|
|
|
/** @var Tag $tag */
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$tagId = $tag->id;
|
|
|
|
$grouped[$tagId] = $grouped[$tagId] ?? '0';
|
|
|
|
$grouped[$tagId] = bcadd($transaction->transaction_amount, $grouped[$tagId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $grouped;
|
|
|
|
}
|
2017-02-05 12:51:58 -06:00
|
|
|
}
|