Tag view update.

This commit is contained in:
James Cole
2017-09-24 21:18:43 +02:00
parent 37250cbde3
commit 260ef1a07e
6 changed files with 177 additions and 146 deletions

View File

@@ -194,17 +194,13 @@ class TagController extends Controller
$end = null;
$periods = new Collection;
$apiKey = env('GOOGLE_MAPS_API_KEY', '');
$sum = '0';
$path = route('tags.show', [$tag->id]);
// prep for "all" view.
if ($moment === 'all') {
$subTitle = trans('firefly.all_journals_for_tag', ['tag' => $tag->tag]);
$start = $repository->firstUseDate($tag);
$end = new Carbon;
$sum = $repository->sumOfTag($tag, null, null);
$result = $repository->resultOfTag($tag, null, null);
$path = route('tags.show', [$tag->id, 'all']);
}
@@ -218,20 +214,16 @@ class TagController extends Controller
'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getPeriodOverview($tag);
$sum = $repository->sumOfTag($tag, $start, $end);
$result = $repository->resultOfTag($tag, $start, $end);
$path = route('tags.show', [$tag->id, $moment]);
}
// prep for current period
if (strlen($moment) === 0) {
/** @var Carbon $start */
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
/** @var Carbon $end */
$end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
$periods = $this->getPeriodOverview($tag);
$sum = $repository->sumOfTag($tag, $start, $end);
$result = $repository->resultOfTag($tag, $start, $end);
$subTitle = trans(
'firefly.journals_in_period_for_tag',
['tag' => $tag->tag, 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
@@ -245,8 +237,9 @@ class TagController extends Controller
$journals = $collector->getPaginatedJournals();
$journals->setPath($path);
$sums = $repository->sumsOfTag($tag, $start, $end);
return view('tags.show', compact('apiKey', 'tag', 'result', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment'));
return view('tags.show', compact('apiKey', 'tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'start', 'end', 'moment'));
}
/**

View File

@@ -186,34 +186,6 @@ class TagRepository implements TagRepositoryInterface
return new Carbon;
}
/**
* Same as sum of tag but substracts income instead of adding it as well.
*
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return string
*/
public function resultOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
if (!is_null($start) && !is_null($end)) {
$collector->setRange($start, $end);
}
$collector->setAllAssetAccounts()->setTag($tag);
$journals = $collector->getJournals();
$sum = '0';
foreach ($journals as $journal) {
$sum = bcadd($sum, strval($journal->transaction_amount));
}
return strval($sum);
}
/**
* @param User $user
*/
@@ -290,6 +262,43 @@ class TagRepository implements TagRepositoryInterface
return strval($sum);
}
/**
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return string
*/
public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
if (!is_null($start) && !is_null($end)) {
$collector->setRange($start, $end);
}
$collector->setAllAssetAccounts()->setTag($tag);
$journals = $collector->getJournals();
$sums = [
TransactionType::WITHDRAWAL => '0',
TransactionType::DEPOSIT => '0',
TransactionType::TRANSFER => '0',
];
foreach ($journals as $journal) {
$amount = app('steam')->positive(strval($journal->transaction_amount));
$type = $journal->transaction_type_type;
if ($type === TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
$sums[$type] = bcadd($sums[$type], $amount);
}
return $sums;
}
/**
* Generates a tag cloud.
*

View File

@@ -103,15 +103,6 @@ interface TagRepositoryInterface
*/
public function lastUseDate(Tag $tag): Carbon;
/**
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return string
*/
public function resultOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string;
/**
* @param User $user
*/
@@ -126,6 +117,17 @@ interface TagRepositoryInterface
*/
public function spentInPeriod(Tag $tag, Carbon $start, Carbon $end): string;
/**
* Calculates various amounts in tag.
*
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return array
*/
public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array;
/**
* This method stores a tag.
*