Akaunting/app/Widgets/ExpensesByCategory.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Widgets;
2019-12-29 00:01:19 +00:00
use App\Abstracts\Widget;
2019-11-16 07:21:14 +00:00
use App\Models\Setting\Category;
2019-12-29 00:01:19 +00:00
class ExpensesByCategory extends Widget
2019-11-16 07:21:14 +00:00
{
2020-01-15 21:42:20 +00:00
public $default_name = 'widgets.expenses_by_category';
2020-01-10 14:49:31 +00:00
2022-06-01 07:15:55 +00:00
public $description = 'widgets.description.expenses_by_category';
public $report_class = 'App\Reports\ExpenseSummary';
2020-01-10 14:49:31 +00:00
2019-12-29 00:01:19 +00:00
public function show()
2026-02-07 18:55:11 +00:00
{
$this->setData();
return $this->view('widgets.donut_chart', $this->data);
}
public function setData(): void
2019-11-16 07:21:14 +00:00
{
2023-06-13 12:06:23 +00:00
Category::with('expense_transactions')->expense()->withSubCategory()->getWithoutChildren()->each(function ($category) {
2019-11-16 07:21:14 +00:00
$amount = 0;
2020-06-08 15:48:59 +00:00
$this->applyFilters($category->expense_transactions)->each(function ($transaction) use (&$amount) {
$amount += $transaction->getAmountConvertedToDefault();
});
2019-11-16 07:21:14 +00:00
2022-08-13 14:33:19 +00:00
$this->addMoneyToDonutChart($category->colorHexCode, $amount, $category->name);
2019-12-29 00:01:19 +00:00
});
2019-11-16 07:21:14 +00:00
2022-06-01 07:15:55 +00:00
$chart = $this->getDonutChart(trans_choice('general.expenses', 2), '100%', 300, 6);
2019-11-16 07:21:14 +00:00
2022-06-10 14:39:14 +00:00
$chart->options['legend']['width'] = 160;
2022-06-09 11:52:26 +00:00
$chart->options['legend']['position'] = 'right';
2022-07-21 08:46:18 +00:00
2026-02-07 18:55:11 +00:00
$this->data = [
2019-12-28 19:30:10 +00:00
'chart' => $chart,
2026-02-07 18:55:11 +00:00
];
2019-11-16 07:21:14 +00:00
}
}