mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-02 03:51:18 +00:00
Finish multi-currency budget and account report.
This commit is contained in:
@@ -198,7 +198,7 @@ class BudgetReportController extends Controller
|
||||
$spentKey = sprintf('%d-spent', $currency['currency_id']);
|
||||
$chartData[$spentKey] = $chartData[$spentKey] ?? [
|
||||
'label' => sprintf(
|
||||
'%s (%s)', (string)trans('firefly.spent_in_specific_budget', ['budget' => $budget['name']]), $currency['currency_name']
|
||||
'%s (%s)', (string)trans('firefly.spent_in_specific_budget', ['budget' => $budget->name]), $currency['currency_name']
|
||||
),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
|
||||
@@ -24,10 +24,9 @@ namespace FireflyIII\Http\Controllers\Chart;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||
use FireflyIII\Helpers\Chart\MetaPieChartInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
|
||||
use FireflyIII\Support\Http\Controllers\AugumentData;
|
||||
use FireflyIII\Support\Http\Controllers\TransactionCalculation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -44,9 +43,12 @@ class CategoryReportController extends Controller
|
||||
|
||||
/** @var GeneratorInterface Chart generation methods. */
|
||||
private $generator;
|
||||
/** @var OperationsRepositoryInterface */
|
||||
private $opsRepository;
|
||||
|
||||
/**
|
||||
* CategoryReportController constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
@@ -54,240 +56,405 @@ class CategoryReportController extends Controller
|
||||
parent::__construct();
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$this->generator = app(GeneratorInterface::class);
|
||||
$this->generator = app(GeneratorInterface::class);
|
||||
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// /**
|
||||
// * Chart for expenses grouped by expense account.
|
||||
// *
|
||||
// * TODO this chart is not multi-currency aware.
|
||||
// *
|
||||
// * @param Collection $accounts
|
||||
// * @param Collection $categories
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// * @param string $others
|
||||
// *
|
||||
// * @return JsonResponse
|
||||
// */
|
||||
// public function accountExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
// {
|
||||
// /** @var MetaPieChartInterface $helper */
|
||||
// $helper = app(MetaPieChartInterface::class);
|
||||
// $helper->setAccounts($accounts)->setCategories($categories)->setStart($start)->setEnd($end)->setCollectOtherObjects(1 === (int)$others);
|
||||
//
|
||||
// $chartData = $helper->generate('expense', 'account');
|
||||
// $data = $this->generator->pieChart($chartData);
|
||||
//
|
||||
// return response()->json($data);
|
||||
// }
|
||||
|
||||
//
|
||||
// /**
|
||||
// * Chart for income grouped by revenue account.
|
||||
// *
|
||||
// * TODO this chart is not multi-currency aware.
|
||||
// *
|
||||
// * @param Collection $accounts
|
||||
// * @param Collection $categories
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// * @param string $others
|
||||
// *
|
||||
// * @return JsonResponse
|
||||
// */
|
||||
// public function accountIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
// {
|
||||
// /** @var MetaPieChartInterface $helper */
|
||||
// $helper = app(MetaPieChartInterface::class);
|
||||
// $helper->setAccounts($accounts);
|
||||
// $helper->setCategories($categories);
|
||||
// $helper->setStart($start);
|
||||
// $helper->setEnd($end);
|
||||
// $helper->setCollectOtherObjects(1 === (int)$others);
|
||||
// $chartData = $helper->generate('income', 'account');
|
||||
// $data = $this->generator->pieChart($chartData);
|
||||
//
|
||||
// return response()->json($data);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Chart for expenses grouped by expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string $others
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function accountExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
{
|
||||
/** @var MetaPieChartInterface $helper */
|
||||
$helper = app(MetaPieChartInterface::class);
|
||||
$helper->setAccounts($accounts)->setCategories($categories)->setStart($start)->setEnd($end)->setCollectOtherObjects(1 === (int)$others);
|
||||
|
||||
$chartData = $helper->generate('expense', 'account');
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chart for income grouped by revenue account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string $others
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function accountIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
{
|
||||
/** @var MetaPieChartInterface $helper */
|
||||
$helper = app(MetaPieChartInterface::class);
|
||||
$helper->setAccounts($accounts);
|
||||
$helper->setCategories($categories);
|
||||
$helper->setStart($start);
|
||||
$helper->setEnd($end);
|
||||
$helper->setCollectOtherObjects(1 === (int)$others);
|
||||
$chartData = $helper->generate('income', 'account');
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chart for expenses grouped by expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string $others
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
{
|
||||
/** @var MetaPieChartInterface $helper */
|
||||
$helper = app(MetaPieChartInterface::class);
|
||||
$helper->setAccounts($accounts);
|
||||
$helper->setCategories($categories);
|
||||
$helper->setStart($start);
|
||||
$helper->setEnd($end);
|
||||
$helper->setCollectOtherObjects(1 === (int)$others);
|
||||
$chartData = $helper->generate('expense', 'category');
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Piechart for income grouped by account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string $others
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
*/
|
||||
public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
|
||||
{
|
||||
/** @var MetaPieChartInterface $helper */
|
||||
$helper = app(MetaPieChartInterface::class);
|
||||
$helper->setAccounts($accounts);
|
||||
$helper->setCategories($categories);
|
||||
$helper->setStart($start);
|
||||
$helper->setEnd($end);
|
||||
$helper->setCollectOtherObjects(1 === (int)$others);
|
||||
$chartData = $helper->generate('income', 'category');
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Main report category chart.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
*/
|
||||
public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
public function budgetExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('chart.category.report.main');
|
||||
$cache->addProperty($accounts);
|
||||
$cache->addProperty($categories);
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$result = [];
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
|
||||
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
||||
$function = app('navigation')->preferredEndOfPeriod($start, $end);
|
||||
$chartData = [];
|
||||
$currentStart = clone $start;
|
||||
|
||||
// prep chart data:
|
||||
foreach ($categories as $category) {
|
||||
$chartData[$category->id . '-in'] = [
|
||||
'label' => $category->name . ' (' . strtolower((string)trans('firefly.income')) . ')',
|
||||
'type' => 'bar',
|
||||
'yAxisID' => 'y-axis-0',
|
||||
'entries' => [],
|
||||
];
|
||||
$chartData[$category->id . '-out'] = [
|
||||
'label' => $category->name . ' (' . strtolower((string)trans('firefly.expenses')) . ')',
|
||||
'type' => 'bar',
|
||||
'yAxisID' => 'y-axis-0',
|
||||
'entries' => [],
|
||||
];
|
||||
// total in, total out:
|
||||
$chartData[$category->id . '-total-in'] = [
|
||||
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_income')) . ')',
|
||||
'type' => 'line',
|
||||
'fill' => false,
|
||||
'yAxisID' => 'y-axis-1',
|
||||
'entries' => [],
|
||||
];
|
||||
$chartData[$category->id . '-total-out'] = [
|
||||
'label' => $category->name . ' (' . strtolower((string)trans('firefly.sum_of_expenses')) . ')',
|
||||
'type' => 'line',
|
||||
'fill' => false,
|
||||
'yAxisID' => 'y-axis-1',
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
$sumOfIncome = [];
|
||||
$sumOfExpense = [];
|
||||
|
||||
while ($currentStart < $end) {
|
||||
$currentEnd = clone $currentStart;
|
||||
$currentEnd = $currentEnd->$function();
|
||||
$expenses = $this->groupByCategory($this->getExpensesInCategories($accounts, $categories, $currentStart, $currentEnd));
|
||||
$income = $this->groupByCategory($this->getIncomeForCategories($accounts, $categories, $currentStart, $currentEnd));
|
||||
$label = $currentStart->formatLocalized($format);
|
||||
|
||||
/** @var Category $category */
|
||||
foreach ($categories as $category) {
|
||||
$labelIn = $category->id . '-in';
|
||||
$labelOut = $category->id . '-out';
|
||||
$labelSumIn = $category->id . '-total-in';
|
||||
$labelSumOut = $category->id . '-total-out';
|
||||
$currentIncome = $income[$category->id] ?? '0';
|
||||
$currentExpense = $expenses[$category->id] ?? '0';
|
||||
|
||||
// add to sum:
|
||||
$sumOfIncome[$category->id] = $sumOfIncome[$category->id] ?? '0';
|
||||
$sumOfExpense[$category->id] = $sumOfExpense[$category->id] ?? '0';
|
||||
$sumOfIncome[$category->id] = bcadd($sumOfIncome[$category->id], $currentIncome);
|
||||
$sumOfExpense[$category->id] = bcadd($sumOfExpense[$category->id], $currentExpense);
|
||||
|
||||
// add to chart:
|
||||
$chartData[$labelIn]['entries'][$label] = $currentIncome;
|
||||
$chartData[$labelOut]['entries'][$label] = $currentExpense;
|
||||
$chartData[$labelSumIn]['entries'][$label] = $sumOfIncome[$category->id];
|
||||
$chartData[$labelSumOut]['entries'][$label] = $sumOfExpense[$category->id];
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$objectName = $journal['budget_name'] ?? trans('firefly.no_budget');
|
||||
$title = sprintf('%s (%s)', $objectName, $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
/** @var Carbon $currentStart */
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
$title = sprintf('%s (%s)', $category['name'], $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param string $others
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
*/
|
||||
public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($earned as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
$title = sprintf('%s (%s)', $category['name'], $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function destinationExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$objectName = $journal['destination_account_name'] ?? trans('firefly.empty');
|
||||
$title = sprintf('%s (%s)', $objectName, $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function destinationIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$objectName = $journal['destination_account_name'] ?? trans('firefly.empty');
|
||||
$title = sprintf('%s (%s)', $objectName, $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Category $category
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
*/
|
||||
public function mainChart(Collection $accounts, Category $category, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$chartData = [];
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, new Collection([$category]));
|
||||
$earned = $this->opsRepository->listIncome($start, $end, $accounts, new Collection([$category]));
|
||||
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
// add things to chart Data for each currency:
|
||||
$spentKey = sprintf('%d-spent', $currency['currency_id']);
|
||||
$chartData[$spentKey] = $chartData[$spentKey] ?? [
|
||||
'label' => sprintf(
|
||||
'%s (%s)', (string)trans('firefly.spent_in_specific_category', ['category' => $category->name]), $currency['currency_name']
|
||||
),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'entries' => $this->makeEntries($start, $end),
|
||||
];
|
||||
|
||||
foreach ($currency['categories'] as $currentCategory) {
|
||||
foreach ($currentCategory['transaction_journals'] as $journal) {
|
||||
$key = $journal['date']->formatLocalized($format);
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$chartData[$spentKey]['entries'][$key] = $chartData[$spentKey]['entries'][$key] ?? '0';
|
||||
$chartData[$spentKey]['entries'][$key] = bcadd($chartData[$spentKey]['entries'][$key], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loop income.
|
||||
foreach ($earned as $currency) {
|
||||
// add things to chart Data for each currency:
|
||||
$spentKey = sprintf('%d-earned', $currency['currency_id']);
|
||||
$chartData[$spentKey] = $chartData[$spentKey] ?? [
|
||||
'label' => sprintf(
|
||||
'%s (%s)', (string)trans('firefly.earned_in_specific_category', ['category' => $category->name]), $currency['currency_name']
|
||||
),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
'currency_id' => $currency['currency_id'],
|
||||
'entries' => $this->makeEntries($start, $end),
|
||||
];
|
||||
|
||||
foreach ($currency['categories'] as $currentCategory) {
|
||||
foreach ($currentCategory['transaction_journals'] as $journal) {
|
||||
$key = $journal['date']->formatLocalized($format);
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$chartData[$spentKey]['entries'][$key] = $chartData[$spentKey]['entries'][$key] ?? '0';
|
||||
$chartData[$spentKey]['entries'][$key] = bcadd($chartData[$spentKey]['entries'][$key], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function sourceExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($spent as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$objectName = $journal['source_account_name'] ?? trans('firefly.empty');
|
||||
$title = sprintf('%s (%s)', $objectName, $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function sourceIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
$result = [];
|
||||
$earned = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
|
||||
|
||||
// loop expenses.
|
||||
foreach ($earned as $currency) {
|
||||
/** @var array $category */
|
||||
foreach ($currency['categories'] as $category) {
|
||||
foreach ($category['transaction_journals'] as $journal) {
|
||||
$objectName = $journal['source_account_name'] ?? trans('firefly.empty');
|
||||
$title = sprintf('%s (%s)', $objectName, $currency['currency_name']);
|
||||
$result[$title] = $result[$title] ?? [
|
||||
'amount' => '0',
|
||||
'currency_symbol' => $currency['currency_symbol'],
|
||||
];
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$result[$title]['amount'] = bcadd($result[$title]['amount'], $amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiCurrencyPieChart($result);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO duplicate function
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function makeEntries(Carbon $start, Carbon $end): array
|
||||
{
|
||||
$return = [];
|
||||
$format = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
|
||||
$preferredRange = app('navigation')->preferredRangeFormat($start, $end);
|
||||
$currentStart = clone $start;
|
||||
while ($currentStart <= $end) {
|
||||
$currentEnd = app('navigation')->endOfPeriod($currentStart, $preferredRange);
|
||||
$key = $currentStart->formatLocalized($format);
|
||||
$return[$key] = '0';
|
||||
$currentStart = clone $currentEnd;
|
||||
$currentStart->addDay();
|
||||
$currentStart->startOfDay();
|
||||
$currentStart->addDay()->startOfDay();
|
||||
}
|
||||
// remove all empty entries to prevent cluttering:
|
||||
$newSet = [];
|
||||
foreach ($chartData as $key => $entry) {
|
||||
if (0 === !array_sum($entry['entries'])) {
|
||||
$newSet[$key] = $chartData[$key]; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
if (0 === count($newSet)) {
|
||||
$newSet = $chartData;
|
||||
}
|
||||
$data = $this->generator->multiSet($newSet);
|
||||
$cache->store($data);
|
||||
|
||||
return response()->json($data);
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user