Skip to content

Firefly III: a personal finances manager

Possible fix for #1325

James Coled3701837e3d8
diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php
index 0957c6db62..53652a00cc 100644
--- a/app/Http/Controllers/TagController.php
+++ b/app/Http/Controllers/TagController.php
@@ -171,6 +171,7 @@ class TagController extends Controller
$now = new Carbon;
$clouds = [];
$clouds['no-date'] = $repository->tagCloud(null);
+
while ($now > $start) {
$year = $now->year;
$clouds[$year] = $repository->tagCloud($year);
diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php
index 49b6da7e3f..0bca55405a 100644
--- a/app/Repositories/Tag/TagRepository.php
+++ b/app/Repositories/Tag/TagRepository.php
@@ -30,6 +30,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Log;
@@ -323,44 +324,42 @@ class TagRepository implements TagRepositoryInterface
$min = null;
$max = '0';
$return = [];
- // get all tags
- $allTags = $this->user->tags();
- // get tags with a certain amount (in this range):
- $query = $this->user->tags()
- ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')
- ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
- ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
- ->where('transactions.amount', '>', 0)
- ->groupBy(['tags.id', 'tags.tag']);
+ // get all tags in the year (if present):
+ $tagQuery = $this->user->tags()
+ ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')
+ ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->where(
+ function (Builder $query) {
+ $query->where('transactions.amount', '>', 0);
+ $query->orWhereNull('transactions.amount');
+ }
+ )
+ ->groupBy(['tags.id', 'tags.tag']);
// add date range (or not):
if (null === $year) {
- $query->whereNull('tags.date');
- $allTags->whereNull('tags.date');
+ Log::debug('Get tags without a date.');
+ $tagQuery->whereNull('tags.date');
}
if (null !== $year) {
- $start = $year . '-01-01';
- $end = $year . '-12-31';
- $query->where('tags.date', '>=', $start)->where('tags.date', '<=', $end);
- $allTags->where('tags.date', '>=', $start)->where('tags.date', '<=', $end);
- }
- $set = $query->get(['tags.id', DB::raw('SUM(transactions.amount) as amount_sum')]);
- $tagsWithAmounts = [];
- /** @var Tag $tag */
- foreach ($set as $tag) {
- $tagsWithAmounts[$tag->id] = (string)$tag->amount_sum;
+ Log::debug(sprintf('Get tags with year %s.', $year));
+ $start = $year . '-01-01 00:00:00';
+ $end = $year . '-12-31 23:59:59';
+ $tagQuery->where('tags.date', '>=', $start)->where('tags.date', '<=', $end);
}
- $tags = $allTags->orderBy('tags.id', 'desc')->get(['tags.id', 'tags.tag']);
- $temporary = [];
+ $result = $tagQuery->get(['tags.id', 'tags.tag', DB::raw('SUM(transactions.amount) as amount_sum')]);
+
/** @var Tag $tag */
- foreach ($tags as $tag) {
- $amount = $tagsWithAmounts[$tag->id] ?? '0';
+ foreach ($result as $tag) {
+ $tagsWithAmounts[$tag->id] = (string)$tag->amount_sum;
+ $amount = strlen($tagsWithAmounts[$tag->id]) ? $tagsWithAmounts[$tag->id] : '0';
if (null === $min) {
$min = $amount;
}
$max = 1 === bccomp($amount, $max) ? $amount : $max;
- $min = bccomp($amount, $min) === -1 ? $amount : $min;
+ $min = -1 === bccomp($amount, $min) ? $amount : $min;
$temporary[] = [
'amount' => $amount,
@@ -369,16 +368,38 @@ class TagRepository implements TagRepositoryInterface
'tag' => $tag->tag,
],
];
+ Log::debug(sprintf('After tag "%s", max is %s and min is %s.', $tag->tag, $max, $min));
}
+ $min = $min ?? '0';
+ Log::debug(sprintf('FINAL max is %s, FINAL min is %s', $max, $min));
+ // the difference between max and min:
+ $range = bcsub($max, $min);
+ Log::debug(sprintf('The range is: %s', $range));
+
+ // each euro difference is this step in the scale:
+ $step = (float)$range !== 0.0 ? 8 / (float)$range : 0;
+ Log::debug(sprintf('The step is: %f', $step));
+ $return = [];
- /** @var array $entry */
- foreach ($temporary as $entry) {
- $scale = $this->cloudScale([12, 20], (float)$entry['amount'], (float)$min, (float)$max);
- $tagId = $entry['tag']['id'];
- $return[$tagId] = [
- 'scale' => $scale,
- 'tag' => $entry['tag'],
+
+ foreach ($result as $tag) {
+ if ($step === 0) {
+ // easy: size is 12:
+ $size = 12;
+ }
+ if ($step !== 0) {
+ $amount = bcsub((string)$tag->amount_sum, $min);
+ Log::debug(sprintf('Work with amount %s for tag %s', $amount, $tag->tag));
+ $size = ((int)(float)$amount * $step) + 12;
+ }
+
+ $return[$tag->id] = [
+ 'size' => $size,
+ 'tag' => $tag->tag,
+ 'id' => $tag->id,
];
+
+ Log::debug(sprintf('Size is %d', $size));
}
return $return;
diff --git a/resources/views/tags/index.twig b/resources/views/tags/index.twig
index f25a58614b..1b6c8bf6fb 100644
--- a/resources/views/tags/index.twig
+++ b/resources/views/tags/index.twig
@@ -28,8 +28,8 @@
<div class="box-body">
<p class="tagcloud">
{% for tagInfo in entries %}
- <a style="font-size:{{ tagInfo.scale }}px;" class="label label-success"
- href="{{ route('tags.show',tagInfo.tag.id) }}"><i class="fa fa-fw fa-tag"></i> {{ tagInfo.tag.tag }}</a>
+ <a style="font-size:{{ tagInfo.size }}px;" class="label label-success"
+ href="{{ route('tags.show',tagInfo.id) }}"><i class="fa fa-fw fa-tag"></i> {{ tagInfo.tag }}</a>
{% endfor %}
</p>
</div>