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

128 lines
4.3 KiB
PHP
Raw Normal View History

2015-05-16 09:41:14 +02:00
<?php
/**
* BillController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-16 09:41:14 +02:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2016-12-11 17:46:30 +01:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2015-05-16 09:41:14 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
2015-05-16 09:41:14 +02:00
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;
use Illuminate\Support\Collection;
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
{
2016-12-11 18:34:18 +01:00
/** @var GeneratorInterface */
2015-06-27 11:44:18 +02:00
protected $generator;
2015-05-16 09:41:14 +02:00
/**
2017-11-15 12:25:49 +01:00
* checked.
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
}
2018-07-08 12:08:53 +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
*
2015-07-09 11:13:38 +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
{
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('chart.bill.frontpage');
if ($cache->has()) {
2018-03-10 20:30:09 +01:00
return response()->json($cache->get()); // @codeCoverageIgnore
}
$paid = $repository->getBillsPaidInRange($start, $end); // will be a negative amount.
$unpaid = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount.
$chartData = [
2018-04-02 15:10:40 +02:00
(string)trans('firefly.unpaid') => $unpaid,
(string)trans('firefly.paid') => $paid,
];
2016-12-14 18:59:12 +01:00
$data = $this->generator->pieChart($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
}
2015-05-16 09:41:14 +02:00
2018-07-08 12:08:53 +02:00
2015-06-27 11:44:18 +02:00
/**
2016-12-11 17:46:30 +01:00
* @param JournalCollectorInterface $collector
* @param Bill $bill
2015-06-27 11:44:18 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2015-06-27 11:44:18 +02:00
*/
2018-07-08 12:28:42 +02:00
public function single(JournalCollectorInterface $collector, Bill $bill): JsonResponse
2015-06-27 11:44:18 +02: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()) {
2018-03-10 20:30:09 +01:00
return response()->json($cache->get()); // @codeCoverageIgnore
2015-05-16 09:41:14 +02:00
}
2016-12-30 13:47:23 +01:00
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
$results = $results->sortBy(
function (Transaction $transaction) {
return $transaction->date->format('U');
2015-07-01 13:18:50 +02:00
}
);
2016-12-11 17:46:30 +01:00
$chartData = [
2017-11-15 12:25:49 +01:00
['type' => 'bar', 'label' => trans('firefly.min-amount'), 'entries' => []],
['type' => 'bar', 'label' => trans('firefly.max-amount'), 'entries' => []],
['type' => 'line', 'label' => trans('firefly.journal-amount'), 'entries' => []],
2016-12-11 17:46:30 +01:00
];
/** @var Transaction $entry */
foreach ($results as $entry) {
2018-04-02 15:10:40 +02:00
$date = $entry->date->formatLocalized((string)trans('config.month_and_day'));
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
$chartData[2]['entries'][$date] = bcmul($entry->transaction_amount, '-1'); // 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
}