2018-08-10 23:39:29 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PeriodOverview.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Http\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2018-08-11 07:33:47 -05:00
|
|
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
2018-08-10 23:39:29 -05:00
|
|
|
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\Category;
|
|
|
|
use FireflyIII\Models\Tag;
|
|
|
|
use FireflyIII\Models\Transaction;
|
2018-09-10 13:24:19 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2018-08-10 23:39:29 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
|
|
|
use FireflyIII\Support\CacheProperties;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait PeriodOverview.
|
|
|
|
*
|
|
|
|
* - Group expenses, income, etc. under this period.
|
|
|
|
* - Returns collection of arrays. Possible fields are:
|
2018-09-10 13:24:19 -05:00
|
|
|
* - start (string),
|
|
|
|
* end (string),
|
|
|
|
* title (string),
|
|
|
|
* spent (string),
|
|
|
|
* earned (string),
|
|
|
|
* transferred (string)
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
trait PeriodOverview
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
|
|
|
|
* and for each period, the amount of money spent and earned. This is a complex operation which is cached for
|
|
|
|
* performance reasons.
|
|
|
|
*
|
2018-10-07 02:45:50 -05:00
|
|
|
* The method has been refactored recently for better performance.
|
|
|
|
*
|
|
|
|
* @param Account $account The account involved
|
|
|
|
* @param Carbon $date The start date.
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-07 02:45:50 -05:00
|
|
|
protected function getAccountPeriodOverview(Account $account, Carbon $date): Collection
|
2018-08-10 23:39:29 -05:00
|
|
|
{
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2018-09-10 13:24:19 -05:00
|
|
|
$end = $repository->oldestJournalDate($account) ?? Carbon::now()->subMonth()->startOfMonth();
|
|
|
|
$start = clone $date;
|
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
if ($end < $start) {
|
|
|
|
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
// properties for cache
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('account-show-period-entries');
|
|
|
|
$cache->addProperty($account->id);
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
/** @var array $dates */
|
|
|
|
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
|
|
|
$entries = new Collection;
|
|
|
|
// loop dates
|
|
|
|
foreach ($dates as $currentDate) {
|
2018-08-11 07:33:47 -05:00
|
|
|
/** @var TransactionCollectorInterface $collector */
|
|
|
|
$collector = app(TransactionCollectorInterface::class);
|
2018-08-10 23:39:29 -05:00
|
|
|
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::DEPOSIT])
|
|
|
|
->withOpposingAccount();
|
2018-10-07 02:45:50 -05:00
|
|
|
$earnedSet = $collector->getTransactions();
|
|
|
|
$earned = $this->groupByCurrency($earnedSet);
|
2018-08-10 23:39:29 -05:00
|
|
|
|
2018-08-11 07:33:47 -05:00
|
|
|
/** @var TransactionCollectorInterface $collector */
|
|
|
|
$collector = app(TransactionCollectorInterface::class);
|
2018-08-10 23:39:29 -05:00
|
|
|
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->withOpposingAccount();
|
2018-10-07 02:45:50 -05:00
|
|
|
$spentSet = $collector->getTransactions();
|
|
|
|
$spent = $this->groupByCurrency($spentSet);
|
2018-08-10 23:39:29 -05:00
|
|
|
|
2018-09-10 13:24:19 -05:00
|
|
|
$title = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
|
2018-08-10 23:39:29 -05:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
$entries->push(
|
|
|
|
[
|
2018-09-10 13:24:19 -05:00
|
|
|
'transactions' => 0,
|
|
|
|
'title' => $title,
|
|
|
|
'spent' => $spent,
|
|
|
|
'earned' => $earned,
|
|
|
|
'transferred' => '0',
|
|
|
|
'route' => route('accounts.show', [$account->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
2018-08-10 23:39:29 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache->store($entries);
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 02:45:50 -05:00
|
|
|
* Overview for single category. Has been refactored recently.
|
|
|
|
*
|
|
|
|
* @param Category $category
|
|
|
|
* @param Carbon $date
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-07 02:45:50 -05:00
|
|
|
protected function getCategoryPeriodOverview(Category $category, Carbon $date): Collection
|
2018-08-10 23:39:29 -05:00
|
|
|
{
|
2018-10-07 02:45:50 -05:00
|
|
|
/** @var JournalRepositoryInterface $journalRepository */
|
|
|
|
$journalRepository = app(JournalRepositoryInterface::class);
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
|
|
|
$first = $journalRepository->firstNull();
|
|
|
|
$end = null === $first ? new Carbon : $first->date;
|
|
|
|
$start = clone $date;
|
|
|
|
|
|
|
|
if ($end < $start) {
|
|
|
|
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
// properties for entries with their amounts.
|
|
|
|
$cache = new CacheProperties();
|
2018-08-10 23:39:29 -05:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2018-10-07 02:45:50 -05:00
|
|
|
$cache->addProperty($range);
|
|
|
|
$cache->addProperty('category-show-period-entries');
|
|
|
|
$cache->addProperty($category->id);
|
2018-08-10 23:39:29 -05:00
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
2018-09-10 13:24:19 -05:00
|
|
|
/** @var array $dates */
|
2018-10-07 02:45:50 -05:00
|
|
|
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
|
|
|
$entries = new Collection;
|
|
|
|
/** @var CategoryRepositoryInterface $categoryRepository */
|
|
|
|
$categoryRepository = app(CategoryRepositoryInterface::class);
|
|
|
|
|
2018-09-10 13:24:19 -05:00
|
|
|
foreach ($dates as $currentDate) {
|
2018-10-07 02:45:50 -05:00
|
|
|
$spent = $categoryRepository->spentInPeriodCollection(new Collection([$category]), new Collection, $currentDate['start'], $currentDate['end']);
|
|
|
|
$earned = $categoryRepository->earnedInPeriodCollection(new Collection([$category]), new Collection, $currentDate['start'], $currentDate['end']);
|
|
|
|
$spent = $this->groupByCurrency($spent);
|
|
|
|
$earned = $this->groupByCurrency($earned);
|
|
|
|
|
|
|
|
// amount transferred
|
2018-08-11 07:33:47 -05:00
|
|
|
/** @var TransactionCollectorInterface $collector */
|
|
|
|
$collector = app(TransactionCollectorInterface::class);
|
2018-10-07 02:45:50 -05:00
|
|
|
$collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->setCategory($category)
|
|
|
|
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
|
|
|
|
$collector->removeFilter(InternalTransferFilter::class);
|
|
|
|
$transferred = $this->groupByCurrency($collector->getTransactions());
|
|
|
|
|
2018-09-10 13:24:19 -05:00
|
|
|
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
2018-08-10 23:39:29 -05:00
|
|
|
$entries->push(
|
2018-09-10 13:24:19 -05:00
|
|
|
[
|
2018-10-07 02:45:50 -05:00
|
|
|
'transactions' => 0,
|
2018-09-10 13:24:19 -05:00
|
|
|
'title' => $title,
|
|
|
|
'spent' => $spent,
|
2018-10-07 02:45:50 -05:00
|
|
|
'earned' => $earned,
|
|
|
|
'transferred' => $transferred,
|
|
|
|
'route' => route('categories.show', [$category->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
2018-08-10 23:39:29 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$cache->store($entries);
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 02:45:50 -05:00
|
|
|
* Same as above, but for lists that involve transactions without a budget.
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
2018-10-07 02:45:50 -05:00
|
|
|
* This method has been refactored recently.
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
2018-10-07 02:45:50 -05:00
|
|
|
* @param Carbon $date
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-07 02:45:50 -05:00
|
|
|
protected function getNoBudgetPeriodOverview(Carbon $date): Collection
|
2018-08-10 23:39:29 -05:00
|
|
|
{
|
2018-10-07 02:45:50 -05:00
|
|
|
/** @var JournalRepositoryInterface $repository */
|
|
|
|
$repository = app(JournalRepositoryInterface::class);
|
|
|
|
$first = $repository->firstNull();
|
|
|
|
$end = null === $first ? new Carbon : $first->date;
|
|
|
|
$start = clone $date;
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2018-09-10 13:24:19 -05:00
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
if ($end < $start) {
|
|
|
|
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
|
|
|
}
|
2018-08-10 23:39:29 -05:00
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
$cache = new CacheProperties;
|
2018-08-10 23:39:29 -05:00
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2018-10-07 02:45:50 -05:00
|
|
|
$cache->addProperty('no-budget-period-entries');
|
2018-08-10 23:39:29 -05:00
|
|
|
|
|
|
|
if ($cache->has()) {
|
2018-10-07 02:45:50 -05:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
2018-10-07 02:45:50 -05:00
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
/** @var array $dates */
|
|
|
|
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
|
|
|
$entries = new Collection;
|
|
|
|
foreach ($dates as $currentDate) {
|
2018-08-11 07:33:47 -05:00
|
|
|
/** @var TransactionCollectorInterface $collector */
|
|
|
|
$collector = app(TransactionCollectorInterface::class);
|
2018-10-07 02:45:50 -05:00
|
|
|
$collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->withoutBudget()->withOpposingAccount()->setTypes(
|
|
|
|
[TransactionType::WITHDRAWAL]
|
|
|
|
);
|
|
|
|
$set = $collector->getTransactions();
|
|
|
|
$count = $set->count();
|
|
|
|
$spent = $this->groupByCurrency($set);
|
|
|
|
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
2018-08-10 23:39:29 -05:00
|
|
|
$entries->push(
|
|
|
|
[
|
2018-10-07 02:45:50 -05:00
|
|
|
'transactions' => $count,
|
|
|
|
'title' => $title,
|
|
|
|
'spent' => $spent,
|
|
|
|
'earned' => '0',
|
|
|
|
'transferred' => '0',
|
|
|
|
'route' => route('budgets.no-budget', [$currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
2018-08-10 23:39:29 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$cache->store($entries);
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 02:45:50 -05:00
|
|
|
* This shows a period overview for a tag. It goes back in time and lists all relevant transactions and sums.
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
* @param Tag $tag
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-10-07 02:45:50 -05:00
|
|
|
protected function getTagPeriodOverview(Tag $tag, Carbon $date): Collection // period overview for tags.
|
2018-08-10 23:39:29 -05:00
|
|
|
{
|
|
|
|
/** @var TagRepositoryInterface $repository */
|
|
|
|
$repository = app(TagRepositoryInterface::class);
|
2018-10-07 02:45:50 -05:00
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2018-08-10 23:39:29 -05:00
|
|
|
/** @var Carbon $end */
|
2018-10-07 02:45:50 -05:00
|
|
|
$start = clone $date;
|
|
|
|
$end = $repository->firstUseDate($tag) ?? new Carbon;
|
2018-08-10 23:39:29 -05:00
|
|
|
|
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
if ($end < $start) {
|
|
|
|
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
// properties for entries with their amounts.
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
2018-10-07 02:45:50 -05:00
|
|
|
$cache->addProperty('tag-period-entries');
|
2018-08-10 23:39:29 -05:00
|
|
|
$cache->addProperty($tag->id);
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
/** @var array $dates */
|
|
|
|
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
|
|
|
$entries = new Collection;
|
2018-08-10 23:39:29 -05:00
|
|
|
// while end larger or equal to start
|
2018-10-07 02:45:50 -05:00
|
|
|
foreach ($dates as $currentDate) {
|
|
|
|
|
|
|
|
$spentSet = $repository->expenseInPeriod($tag, $currentDate['start'], $currentDate['end']);
|
|
|
|
$spent = $this->groupByCurrency($spentSet);
|
|
|
|
$earnedSet = $repository->incomeInPeriod($tag, $currentDate['start'], $currentDate['end']);
|
|
|
|
$earned = $this->groupByCurrency($earnedSet);
|
|
|
|
$transferredSet = $repository->transferredInPeriod($tag, $currentDate['start'], $currentDate['end']);
|
|
|
|
$transferred = $this->groupByCurrency($transferredSet);
|
|
|
|
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
|
|
|
|
|
|
|
$entries->push(
|
|
|
|
[
|
|
|
|
'transactions' => $spentSet->count() + $earnedSet->count() + $transferredSet->count(),
|
|
|
|
'title' => $title,
|
|
|
|
'spent' => $spent,
|
|
|
|
'earned' => $earned,
|
|
|
|
'transferred' => $transferred,
|
|
|
|
'route' => route('tags.show', [$tag->id, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
2018-10-07 02:45:50 -05:00
|
|
|
$cache->store($entries);
|
2018-08-10 23:39:29 -05:00
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
return $entries;
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 02:45:50 -05:00
|
|
|
* This list shows the overview of a type of transaction, for the period blocks on the list of transactions.
|
2018-08-10 23:39:29 -05:00
|
|
|
*
|
|
|
|
* @param string $what
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
protected function getTransactionPeriodOverview(string $what, Carbon $date): Collection // period overview for transactions.
|
|
|
|
{
|
|
|
|
/** @var JournalRepositoryInterface $repository */
|
|
|
|
$repository = app(JournalRepositoryInterface::class);
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2018-10-07 02:45:50 -05:00
|
|
|
$endJournal = $repository->firstNull();
|
|
|
|
$end = null === $endJournal ? new Carbon : $endJournal->date;
|
|
|
|
$start = clone $date;
|
2018-08-10 23:39:29 -05:00
|
|
|
$types = config('firefly.transactionTypesByWhat.' . $what);
|
2018-10-07 02:45:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
if ($end < $start) {
|
|
|
|
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
|
|
|
|
2018-10-07 02:45:50 -05:00
|
|
|
// properties for entries with their amounts.
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty('transactions-period-entries');
|
|
|
|
$cache->addProperty($what);
|
|
|
|
|
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
/** @var array $dates */
|
2018-10-07 02:45:50 -05:00
|
|
|
$dates = app('navigation')->blockPeriods($start, $end, $range);
|
|
|
|
$entries = new Collection;
|
2018-08-10 23:39:29 -05:00
|
|
|
|
|
|
|
foreach ($dates as $currentDate) {
|
2018-10-07 02:45:50 -05:00
|
|
|
|
|
|
|
// get all expenses, income or transfers:
|
2018-08-11 07:33:47 -05:00
|
|
|
/** @var TransactionCollectorInterface $collector */
|
|
|
|
$collector = app(TransactionCollectorInterface::class);
|
2018-08-10 23:39:29 -05:00
|
|
|
$collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->withOpposingAccount()->setTypes($types);
|
|
|
|
$collector->removeFilter(InternalTransferFilter::class);
|
2018-08-11 12:21:58 -05:00
|
|
|
$transactions = $collector->getTransactions();
|
2018-10-07 02:45:50 -05:00
|
|
|
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
|
|
|
|
$grouped = $this->groupByCurrency($transactions);
|
|
|
|
$spent = [];
|
|
|
|
$earned = [];
|
|
|
|
$transferred = [];
|
|
|
|
if ('expenses' === $what || 'withdrawal' === $what) {
|
|
|
|
$spent = $grouped;
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
2018-10-07 02:45:50 -05:00
|
|
|
if ('revenue' === $what || 'deposit' === $what) {
|
|
|
|
$earned = $grouped;
|
|
|
|
}
|
|
|
|
if ('transfer' === $what || 'transfers' === $what) {
|
|
|
|
$transferred = $grouped;
|
|
|
|
}
|
|
|
|
$entries->push(
|
|
|
|
[
|
|
|
|
'transactions' => $transactions->count(),
|
|
|
|
'title' => $title,
|
|
|
|
'spent' => $spent,
|
|
|
|
'earned' => $earned,
|
|
|
|
'transferred' => $transferred,
|
|
|
|
'route' => route('transactions.index', [$what, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
|
|
|
|
]
|
|
|
|
);
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collect the sum per currency.
|
|
|
|
*
|
|
|
|
* @param Collection $collection
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function sumPerCurrency(Collection $collection): array // helper for transactions (math, calculations)
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($collection as $transaction) {
|
|
|
|
$currencyId = (int)$transaction->transaction_currency_id;
|
|
|
|
|
|
|
|
// save currency information:
|
|
|
|
if (!isset($return[$currencyId])) {
|
|
|
|
$currencySymbol = $transaction->transaction_currency_symbol;
|
|
|
|
$decimalPlaces = $transaction->transaction_currency_dp;
|
|
|
|
$currencyCode = $transaction->transaction_currency_code;
|
|
|
|
$return[$currencyId] = [
|
|
|
|
'currency' => [
|
|
|
|
'id' => $currencyId,
|
|
|
|
'code' => $currencyCode,
|
|
|
|
'symbol' => $currencySymbol,
|
|
|
|
'dp' => $decimalPlaces,
|
|
|
|
],
|
|
|
|
'sum' => '0',
|
|
|
|
'count' => 0,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
// save amount:
|
|
|
|
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $transaction->transaction_amount);
|
|
|
|
++$return[$currencyId]['count'];
|
|
|
|
}
|
|
|
|
asort($return);
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:24:19 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $transactions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function groupByCurrency(Collection $transactions): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$currencyId = (int)$transaction->transaction_currency_id;
|
|
|
|
if (!isset($return[$currencyId])) {
|
|
|
|
$currency = new TransactionCurrency;
|
|
|
|
$currency->symbol = $transaction->transaction_currency_symbol;
|
|
|
|
$currency->decimal_places = $transaction->transaction_currency_dp;
|
|
|
|
$currency->name = $transaction->transaction_currency_name;
|
|
|
|
$return[$currencyId] = [
|
|
|
|
'amount' => '0',
|
|
|
|
'currency' => $currency,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$return[$currencyId]['amount'] = bcadd($return[$currencyId]['amount'], $transaction->transaction_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:39:29 -05:00
|
|
|
}
|