Files
firefly-iii/app/Http/Controllers/Chart/BillController.php

192 lines
6.5 KiB
PHP
Raw Normal View History

2015-05-16 09:41:14 +02:00
<?php
2022-12-29 19:41:57 +01:00
/**
* BillController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-16 09:41:14 +02:00
namespace FireflyIII\Http\Controllers\Chart;
2022-12-29 19:41:57 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2015-05-16 09:41:14 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2015-06-03 18:22:47 +02:00
use FireflyIII\Support\CacheProperties;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2015-06-03 21:25:11 +02:00
2015-05-16 09:41:14 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class BillController.
2015-05-16 09:41:14 +02:00
*/
class BillController extends Controller
{
protected GeneratorInterface $generator;
2015-06-27 11:44:18 +02:00
2015-05-16 09:41:14 +02:00
/**
2018-07-21 08:06:24 +02:00
* BillController constructor.
*
2015-05-16 09:41:14 +02:00
*/
2015-06-27 11:44:18 +02:00
public function __construct()
2015-05-16 09:41:14 +02:00
{
2015-06-27 11:44:18 +02:00
parent::__construct();
2016-12-11 18:34:18 +01:00
$this->generator = app(GeneratorInterface::class);
2015-05-16 09:41:14 +02:00
}
2021-03-28 11:46:23 +02:00
2015-05-16 09:41:14 +02:00
/**
* Shows all bills and whether or not they've been paid this month (pie chart).
2015-05-16 09:41:14 +02:00
*
2023-06-21 12:34:58 +02:00
* @param BillRepositoryInterface $repository
2015-05-16 09:41:14 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2015-05-16 09:41:14 +02:00
*/
2018-07-08 12:28:42 +02:00
public function frontpage(BillRepositoryInterface $repository): JsonResponse
2015-05-16 09:41:14 +02:00
{
2023-02-11 07:36:45 +01:00
$start = session('start', today(config('app.timezone'))->startOfMonth());
$end = session('end', today(config('app.timezone'))->endOfMonth());
2022-10-30 14:24:19 +01:00
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('chart.bill.frontpage');
if ($cache->has()) {
return response()->json($cache->get());
}
$chartData = [];
$paid = $repository->sumPaidInRange($start, $end);
$unpaid = $repository->sumUnpaidInRange($start, $end);
/**
* @var array $info
*/
foreach ($paid as $info) {
$amount = $info['sum'];
$label = (string)trans('firefly.paid_in_currency', ['currency' => $info['name']]);
$chartData[$label] = [
2022-12-29 19:41:57 +01:00
'amount' => $amount,
'currency_symbol' => $info['symbol'],
'currency_code' => $info['code'],
2022-12-29 19:41:57 +01:00
];
2018-08-28 05:21:23 +02:00
}
/**
* @var array $info
*/
foreach ($unpaid as $info) {
$amount = $info['sum'];
$label = (string)trans('firefly.unpaid_in_currency', ['currency' => $info['name']]);
$chartData[$label] = [
2022-12-29 19:41:57 +01:00
'amount' => $amount,
'currency_symbol' => $info['symbol'],
'currency_code' => $info['code'],
2022-12-29 19:41:57 +01:00
];
2018-08-28 05:21:23 +02:00
}
$data = $this->generator->multiCurrencyPieChart($chartData);
2016-12-11 17:46:30 +01:00
$cache->store($data);
2015-05-16 09:41:14 +02:00
2018-03-10 20:30:09 +01:00
return response()->json($data);
2015-06-27 11:44:18 +02:00
}
2021-03-28 11:46:23 +02:00
2015-06-27 11:44:18 +02:00
/**
2018-07-22 08:58:58 +02:00
* Shows overview for a single bill.
2018-07-21 08:06:24 +02:00
*
2023-06-21 12:34:58 +02:00
* @param Bill $bill
2015-06-27 11:44:18 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2022-12-29 19:41:57 +01:00
* @throws FireflyException
2015-06-27 11:44:18 +02:00
*/
public function single(Bill $bill): JsonResponse
2015-06-27 11:44:18 +02:00
{
2022-10-30 14:24:19 +01:00
$cache = new CacheProperties();
2016-12-11 17:46:30 +01:00
$cache->addProperty('chart.bill.single');
2015-06-27 11:44:18 +02:00
$cache->addProperty($bill->id);
if ($cache->has()) {
return response()->json($cache->get());
2015-05-16 09:41:14 +02:00
}
$locale = app('steam')->getLocale();
2015-05-16 09:41:14 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$journals = $collector->setBill($bill)->getExtractedJournals();
// sort the other way around:
usort(
$journals,
static function (array $left, array $right) {
2021-03-28 11:46:23 +02:00
if ($left['date']->gt($right['date'])) {
2021-01-20 18:42:07 +01:00
return 1;
}
2021-03-28 11:46:23 +02:00
if ($left['date']->lt($right['date'])) {
2021-01-20 18:42:07 +01:00
return -1;
}
2021-03-28 11:46:23 +02:00
2021-01-20 18:42:07 +01:00
return 0;
}
);
2022-10-23 14:53:13 +02:00
$chartData = [
2022-12-29 19:41:57 +01:00
[
'type' => 'line',
'label' => (string)trans('firefly.min-amount'),
'currency_symbol' => $bill->transactionCurrency->symbol,
'currency_code' => $bill->transactionCurrency->code,
'entries' => [],
],
[
'type' => 'line',
'label' => (string)trans('firefly.max-amount'),
'currency_symbol' => $bill->transactionCurrency->symbol,
'currency_code' => $bill->transactionCurrency->code,
'entries' => [],
],
[
'type' => 'bar',
'label' => (string)trans('firefly.journal-amount'),
'currency_symbol' => $bill->transactionCurrency->symbol,
'currency_code' => $bill->transactionCurrency->code,
'entries' => [],
],
2016-12-11 17:46:30 +01:00
];
2022-12-29 19:41:57 +01:00
$currencyId = (int)$bill->transaction_currency_id;
foreach ($journals as $journal) {
2022-12-29 19:41:57 +01:00
$date = $journal['date']->isoFormat((string)trans('config.month_and_day_js', [], $locale));
2016-12-28 19:00:39 +01:00
$chartData[0]['entries'][$date] = $bill->amount_min; // minimum amount of bill
$chartData[1]['entries'][$date] = $bill->amount_max; // maximum amount of bill
2018-07-22 08:58:58 +02:00
// append amount because there are more than one per moment:
2021-04-07 07:28:43 +02:00
if (!array_key_exists($date, $chartData[2]['entries'])) {
2018-07-22 08:58:58 +02:00
$chartData[2]['entries'][$date] = '0';
}
2022-10-23 14:53:13 +02:00
$amount = bcmul($journal['amount'], '-1');
if ($currencyId === $journal['foreign_currency_id']) {
$amount = bcmul($journal['foreign_amount'], '-1');
}
2018-07-22 08:58:58 +02:00
$chartData[2]['entries'][$date] = bcadd($chartData[2]['entries'][$date], $amount); // amount of journal
2016-12-11 17:46:30 +01:00
}
2016-12-14 18:59:12 +01:00
$data = $this->generator->multiSet($chartData);
2015-06-03 21:25:11 +02:00
$cache->store($data);
2018-03-10 20:30:09 +01:00
return response()->json($data);
2015-05-16 09:41:14 +02:00
}
2015-05-20 19:56:14 +02:00
}