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

142 lines
4.4 KiB
PHP
Raw Normal View History

2015-05-16 09:41:14 +02:00
<?php
/**
* BillController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 12:08:25 +01:00
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-05-02 20:49:19 +02:00
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
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;
use Illuminate\Support\Collection;
2015-05-16 09:41:14 +02:00
use Response;
2015-06-03 21:25:11 +02:00
2015-05-16 09:41:14 +02:00
/**
* Class BillController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class BillController extends Controller
{
2015-06-27 11:44:18 +02:00
/** @var \FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface */
2015-06-27 11:44:18 +02:00
protected $generator;
2015-05-16 09:41:14 +02: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();
// create chart generator:
2016-05-02 20:49:19 +02:00
$this->generator = app(BillChartGeneratorInterface::class);
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
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-07-09 09:41:54 +02:00
public function frontpage(BillRepositoryInterface $repository)
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()) {
return Response::json($cache->get());
}
$paid = $repository->getBillsPaidInRange($start, $end); // will be a negative amount.
$unpaid = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount.
$chartData = [
strval(trans('firefly.unpaid')) => $unpaid,
strval(trans('firefly.paid')) => $paid,
];
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->pieChart($chartData);
2016-12-11 17:46:30 +01:00
$cache->store($data);
2015-05-16 09:41:14 +02:00
2015-06-27 11:44:18 +02:00
return Response::json($data);
}
2015-05-16 09:41:14 +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
*
2016-12-11 17:46:30 +01:00
* @return \Illuminate\Http\JsonResponse
2015-06-27 11:44:18 +02:00
*/
2016-12-11 17:46:30 +01:00
public function single(JournalCollectorInterface $collector, Bill $bill)
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()) {
2016-04-06 09:27:45 +02:00
return Response::json($cache->get());
2015-05-16 09:41:14 +02:00
}
2016-12-11 17:46:30 +01:00
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
2015-07-01 13:18:50 +02:00
$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 = [
[
'type' => 'bar',
'label' => trans('firefly.min-amount'),
'entries' => [],
],
[
'type' => 'bar',
'label' => trans('firefly.max-amount'),
'entries' => [],
],
[
'type' => 'line',
'label' => trans('firefly.journal-amount'),
'entries' => [],
],
];
/** @var Transaction $entry */
foreach ($results as $entry) {
$date = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
// minimum amount of bill:
$chartData[0]['entries'][$date] = $bill->amount_min;
// maximum amount of bill:
2016-12-11 17:47:47 +01:00
$chartData[1]['entries'][$date] = $bill->amount_max;
2016-12-11 17:46:30 +01:00
// amount of journal:
$chartData[2]['entries'][$date] = bcmul($entry->transaction_amount, '-1');
}
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
$data = $generator->multiSet($chartData);
2015-06-03 21:25:11 +02:00
$cache->store($data);
return Response::json($data);
2015-05-16 09:41:14 +02:00
}
2015-05-20 19:56:14 +02:00
}