Possible fix for #1325

This commit is contained in:
James Cole 2018-04-07 18:00:09 +02:00
parent 7af10aca9e
commit d3701837e3
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 57 additions and 35 deletions

View File

@ -171,6 +171,7 @@ class TagController extends Controller
$now = new Carbon; $now = new Carbon;
$clouds = []; $clouds = [];
$clouds['no-date'] = $repository->tagCloud(null); $clouds['no-date'] = $repository->tagCloud(null);
while ($now > $start) { while ($now > $start) {
$year = $now->year; $year = $now->year;
$clouds[$year] = $repository->tagCloud($year); $clouds[$year] = $repository->tagCloud($year);

View File

@ -30,6 +30,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@ -323,44 +324,42 @@ class TagRepository implements TagRepositoryInterface
$min = null; $min = null;
$max = '0'; $max = '0';
$return = []; $return = [];
// get all tags // get all tags in the year (if present):
$allTags = $this->user->tags(); $tagQuery = $this->user->tags()
// get tags with a certain amount (in this range): ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')
$query = $this->user->tags() ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id') ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') ->where(
->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') function (Builder $query) {
->where('transactions.amount', '>', 0) $query->where('transactions.amount', '>', 0);
->groupBy(['tags.id', 'tags.tag']); $query->orWhereNull('transactions.amount');
}
)
->groupBy(['tags.id', 'tags.tag']);
// add date range (or not): // add date range (or not):
if (null === $year) { if (null === $year) {
$query->whereNull('tags.date'); Log::debug('Get tags without a date.');
$allTags->whereNull('tags.date'); $tagQuery->whereNull('tags.date');
} }
if (null !== $year) { if (null !== $year) {
$start = $year . '-01-01'; Log::debug(sprintf('Get tags with year %s.', $year));
$end = $year . '-12-31'; $start = $year . '-01-01 00:00:00';
$query->where('tags.date', '>=', $start)->where('tags.date', '<=', $end); $end = $year . '-12-31 23:59:59';
$allTags->where('tags.date', '>=', $start)->where('tags.date', '<=', $end); $tagQuery->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;
} }
$tags = $allTags->orderBy('tags.id', 'desc')->get(['tags.id', 'tags.tag']); $result = $tagQuery->get(['tags.id', 'tags.tag', DB::raw('SUM(transactions.amount) as amount_sum')]);
$temporary = [];
/** @var Tag $tag */ /** @var Tag $tag */
foreach ($tags as $tag) { foreach ($result as $tag) {
$amount = $tagsWithAmounts[$tag->id] ?? '0'; $tagsWithAmounts[$tag->id] = (string)$tag->amount_sum;
$amount = strlen($tagsWithAmounts[$tag->id]) ? $tagsWithAmounts[$tag->id] : '0';
if (null === $min) { if (null === $min) {
$min = $amount; $min = $amount;
} }
$max = 1 === bccomp($amount, $max) ? $amount : $max; $max = 1 === bccomp($amount, $max) ? $amount : $max;
$min = bccomp($amount, $min) === -1 ? $amount : $min; $min = -1 === bccomp($amount, $min) ? $amount : $min;
$temporary[] = [ $temporary[] = [
'amount' => $amount, 'amount' => $amount,
@ -369,16 +368,38 @@ class TagRepository implements TagRepositoryInterface
'tag' => $tag->tag, '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));
/** @var array $entry */ // each euro difference is this step in the scale:
foreach ($temporary as $entry) { $step = (float)$range !== 0.0 ? 8 / (float)$range : 0;
$scale = $this->cloudScale([12, 20], (float)$entry['amount'], (float)$min, (float)$max); Log::debug(sprintf('The step is: %f', $step));
$tagId = $entry['tag']['id']; $return = [];
$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; return $return;

View File

@ -28,8 +28,8 @@
<div class="box-body"> <div class="box-body">
<p class="tagcloud"> <p class="tagcloud">
{% for tagInfo in entries %} {% for tagInfo in entries %}
<a style="font-size:{{ tagInfo.scale }}px;" class="label label-success" <a style="font-size:{{ tagInfo.size }}px;" class="label label-success"
href="{{ route('tags.show',tagInfo.tag.id) }}"><i class="fa fa-fw fa-tag"></i> {{ tagInfo.tag.tag }}</a> href="{{ route('tags.show',tagInfo.id) }}"><i class="fa fa-fw fa-tag"></i> {{ tagInfo.tag }}</a>
{% endfor %} {% endfor %}
</p> </p>
</div> </div>