Final code for tag report.

This commit is contained in:
James Cole
2017-02-24 21:09:20 +01:00
parent fc2cee7a54
commit 40c38af766
4 changed files with 80 additions and 4 deletions

View File

@@ -14,11 +14,13 @@ namespace FireflyIII\Helpers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Steam;
@@ -46,6 +48,7 @@ class MetaPieChart implements MetaPieChartInterface
'account' => ['opposing_account_id'],
'budget' => ['transaction_journal_budget_id', 'transaction_budget_id'],
'category' => ['transaction_journal_category_id', 'transaction_category_id'],
'tag' => [],
];
/** @var array */
protected $repositories
@@ -53,6 +56,7 @@ class MetaPieChart implements MetaPieChartInterface
'account' => AccountRepositoryInterface::class,
'budget' => BudgetRepositoryInterface::class,
'category' => CategoryRepositoryInterface::class,
'tag' => TagRepositoryInterface::class,
];
/** @var Carbon */
protected $start;
@@ -249,8 +253,13 @@ class MetaPieChart implements MetaPieChartInterface
*
* @return array
*/
protected function groupByFields(Collection $set, array $fields)
protected function groupByFields(Collection $set, array $fields): array
{
if (count($fields) === 0 && $this->tags->count() > 0) {
// do a special group on tags:
return $this->groupByTag($set);
}
$grouped = [];
/** @var Transaction $transaction */
foreach ($set as $transaction) {
@@ -280,7 +289,7 @@ class MetaPieChart implements MetaPieChartInterface
foreach ($array as $objectId => $amount) {
if (!isset($names[$objectId])) {
$object = $repository->find(intval($objectId));
$names[$objectId] = $object->name;
$names[$objectId] = $object->name ?? $object->tag;
}
$amount = Steam::positive($amount);
$this->total = bcadd($this->total, $amount);
@@ -290,4 +299,22 @@ class MetaPieChart implements MetaPieChartInterface
return $chartData;
}
private function groupByTag(Collection $set): array
{
$grouped = [];
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$journal = $transaction->transactionJournal;
$tags = $journal->tags;
/** @var Tag $tag */
foreach ($tags as $tag) {
$tagId = $tag->id;
$grouped[$tagId] = $grouped[$tagId] ?? '0';
$grouped[$tagId] = bcadd($transaction->transaction_amount, $grouped[$tagId]);
}
}
return $grouped;
}
}

View File

@@ -241,6 +241,55 @@ class TagReportController extends Controller
return Response::json($data);
}
/**
* @param Collection $accounts
* @param Collection $tags
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
* @return \Illuminate\Http\JsonResponse
*/
public function tagExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others)
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
$helper->setAccounts($accounts);
$helper->setTags($tags);
$helper->setStart($start);
$helper->setEnd($end);
$helper->setCollectOtherObjects(intval($others) === 1);
$chartData = $helper->generate('expense', 'tag');
$data = $this->generator->pieChart($chartData);
return Response::json($data);
}
/**
* @param Collection $accounts
* @param Collection $tags
* @param Carbon $start
* @param Carbon $end
* @param string $others
*
* @return \Illuminate\Http\JsonResponse
*/
public function tagIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others)
{
/** @var MetaPieChartInterface $helper */
$helper = app(MetaPieChartInterface::class);
$helper->setAccounts($accounts);
$helper->setTags($tags);
$helper->setStart($start);
$helper->setEnd($end);
$helper->setCollectOtherObjects(intval($others) === 1);
$chartData = $helper->generate('income', 'tag');
$data = $this->generator->pieChart($chartData);
return Response::json($data);
}
/**
* @param Collection $accounts
* @param Collection $tags

View File

@@ -747,6 +747,8 @@ return [
'include_expense_not_in_category' => 'Included expenses not in the selected category(ies)',
'include_income_not_in_category' => 'Included income not in the selected category(ies)',
'include_income_not_in_account' => 'Included income not in the selected account(s)',
'include_income_not_in_tags' => 'Included income not in the selected tag(s)',
'include_expense_not_in_tags' => 'Included expenses not in the selected tag(s)',
'everything_else' => 'Everything else',
'income_and_expenses' => 'Income and expenses',
'spent_average' => 'Spent (average)',

View File

@@ -81,7 +81,6 @@
</div>
<div class="box-body">
<canvas id="budgets-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
Uitgaven per budget voor de gevonden transacties
</div>
</div>
</div>
@@ -160,7 +159,6 @@
</div>
<div class="box-body">
<canvas id="categories-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
Uitgaven per category voor de gevonden transacties
</div>
</div>
</div>