Update tag routine for #993

This commit is contained in:
James Cole 2017-11-13 16:26:44 +01:00
parent 6c8f631582
commit d413615943
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 83 additions and 44 deletions

View File

@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Tag;
use Carbon\Carbon; use Carbon\Carbon;
use DB;
use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Models\Tag; use FireflyIII\Models\Tag;
@ -328,39 +329,69 @@ class TagRepository implements TagRepositoryInterface
*/ */
public function tagCloud(?int $year): array public function tagCloud(?int $year): array
{ {
/*
* Some vars
*/
$min = null; $min = null;
$max = 0; $max = '0';
$query = $this->user->tags();
$return = []; $return = [];
Log::debug('Going to build tag-cloud'); /*
* 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');
// add date range (or not):
if (is_null($year)) {
$query->whereNull('tags.date');
}
if (!is_null($year)) {
$start = $year . '-01-01';
$end = $year . '-12-31';
$query->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] = strval($tag->amount_sum);
}
/*
* get all tags in period:
*/
$query = $this->user->tags();
if (!is_null($year)) { if (!is_null($year)) {
Log::debug(sprintf('Year is not null: %d', $year));
$start = $year . '-01-01'; $start = $year . '-01-01';
$end = $year . '-12-31'; $end = $year . '-12-31';
$query->where('date', '>=', $start)->where('date', '<=', $end); $query->where('date', '>=', $start)->where('date', '<=', $end);
} }
if (is_null($year)) { if (is_null($year)) {
$query->whereNull('date'); $query->whereNull('date');
Log::debug('Year is NULL');
} }
$tags = $query->orderBy('id', 'desc')->get(); $tags = $query->orderBy('id', 'desc')->get();
$temporary = []; $temporary = [];
Log::debug(sprintf('Found %d tags', $tags->count()));
/** @var Tag $tag */ /** @var Tag $tag */
foreach ($tags as $tag) { foreach ($tags as $tag) {
$amount = floatval($this->sumOfTag($tag, null, null)); $amount = $tagsWithAmounts[$tag->id] ?? '0';
$min = $amount < $min || is_null($min) ? $amount : $min; if (is_null($min)) {
$max = $amount > $max ? $amount : $max; $min = $amount;
}
$max = bccomp($amount, $max) === 1 ? $amount : $max;
$min = bccomp($amount, $min) === -1 ? $amount : $min;
$temporary[] = [ $temporary[] = [
'amount' => $amount, 'amount' => $amount,
'tag' => $tag, 'tag' => $tag,
]; ];
Log::debug(sprintf('Now working on tag %s with total amount %s', $tag->tag, $amount));
Log::debug(sprintf('Minimum is now %f, maximum is %f', $min, $max));
} }
/** @var array $entry */ /** @var array $entry */
foreach ($temporary as $entry) { foreach ($temporary as $entry) {
$scale = $this->cloudScale([12, 20], $entry['amount'], $min, $max); $scale = $this->cloudScale([12, 20], floatval($entry['amount']), floatval($min), floatval($max));
$tagId = $entry['tag']->id; $tagId = $entry['tag']->id;
$return[$tagId] = [ $return[$tagId] = [
'scale' => $scale, 'scale' => $scale,
@ -368,8 +399,6 @@ class TagRepository implements TagRepositoryInterface
]; ];
} }
Log::debug('DONE with tagcloud');
return $return; return $return;
} }
@ -402,13 +431,10 @@ class TagRepository implements TagRepositoryInterface
*/ */
private function cloudScale(array $range, float $amount, float $min, float $max): int private function cloudScale(array $range, float $amount, float $min, float $max): int
{ {
Log::debug(sprintf('Now in cloudScale with %s as amount and %f min, %f max', $amount, $min, $max));
$amountDiff = $max - $min; $amountDiff = $max - $min;
Log::debug(sprintf('AmountDiff is %f', $amountDiff));
// no difference? Every tag same range: // no difference? Every tag same range:
if ($amountDiff === 0.0) { if ($amountDiff === 0.0) {
Log::debug(sprintf('AmountDiff is zero, return %d', $range[0]));
return $range[0]; return $range[0];
} }

View File

@ -67,12 +67,17 @@
</tr> </tr>
</tbody> </tbody>
<tfoot> <tfoot>
<tr class="update_date_button" style="display:none;"> <tr>
<td colspan="4"> <td colspan="3">
<a href="#" class="btn btn-default update_view"> <div class="update_balance_instruction">
{{ 'update_view'|_ }} {{ 'update_balance_dates_instruction'|_ }}
</a> </div>
<span class="text-muted">(unsaved progress will be lost!)</span> <div class="select_transactions_instruction" style="display:none;">
{{ 'select_transactions_instruction'|_ }}
</div>
</td>
<td>
<a href="#" class="btn btn-default start_reconcile">{{ 'start_reconcile'|_ }}</a>
</td> </td>
</tr> </tr>
</tfoot> </tfoot>
@ -96,12 +101,11 @@
<tbody> <tbody>
<tr> <tr>
<td> <td>
<span id="difference"></span> <p class="lead" id="difference"></p>
</td> </td>
<td> <td>
<div class="btn-group"> <div class="btn-group">
<a href="#" class="btn btn-default">Reconcile</a> <button class="btn btn-default store_reconcile" disabled><i class="fa fa-fw fa-check"></i> {{ 'store_reconcile'|_ }}</button>
<a href="#" class="btn btn-default">Reconcile with transaction</a>
</div> </div>
</td> </td>
</tr> </tr>
@ -157,6 +161,25 @@
</tr> </tr>
{% set startSet = true %} {% set startSet = true %}
{% endif %} {% endif %}
{# end marker #}
{% if transaction.date <= end and endSet == false %}
<tr>
<td colspan="5">
&nbsp;
</td>
<td colspan="3">
<span class="label label-default">
{{ trans('firefly.end_of_reconcile_period', {period: end.formatLocalized(monthAndDayFormat) }) }}
</span>
</td>
<td colspan="2">
&nbsp;
</td>
</tr>
{% set endSet = true %}
{% endif %}
<tr data-date="{{ transaction.date.format('Y-m-d') }}" data-id="{{ transaction.journal_id }}" <tr data-date="{{ transaction.date.format('Y-m-d') }}" data-id="{{ transaction.journal_id }}"
data-transaction-id="{{ transaction.id }}"> data-transaction-id="{{ transaction.id }}">
<td class="hidden-xs"> <td class="hidden-xs">
@ -185,7 +208,13 @@
</td> </td>
<td style="text-align: right;"><span style="margin-right:5px;">{{ transaction|transactionAmount }}</span></td> <td style="text-align: right;"><span style="margin-right:5px;">{{ transaction|transactionAmount }}</span></td>
<td> <td>
<input type="checkbox" name="reconciled[]" value="{{ transaction.amount }}" class="reconcile_checkbox"> {% if transaction.reconciled %}
{{ transaction|transactionReconciled }}
{% else %}
<input type="checkbox" name="reconciled[]"
data-younger="{% if transaction.date > end %}true{% else %}false{% endif %}"
value="{{ transaction.transaction_amount }}" data-id="{{ transaction.id }}" disabled class="reconcile_checkbox">
{% endif %}
</td> </td>
<td class="hidden-sm hidden-xs"> <td class="hidden-sm hidden-xs">
{{ transaction.date.formatLocalized(monthAndDayFormat) }} {{ transaction.date.formatLocalized(monthAndDayFormat) }}
@ -205,23 +234,7 @@
{{ transaction|transactionCategories }} {{ transaction|transactionCategories }}
</td> </td>
</tr> </tr>
{# end marker #}
{% if transaction.date <= end and endSet == false %}
<tr>
<td colspan="5">
&nbsp;
</td>
<td colspan="3">
<span class="label label-default">
{{ trans('firefly.end_of_reconcile_period', {period: end.formatLocalized(monthAndDayFormat) }) }}
</span>
</td>
<td colspan="2">
&nbsp;
</td>
</tr>
{% set endSet = true %}
{% endif %}
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>