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

120 lines
3.8 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05: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 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2016-12-11 10:46:30 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
2015-05-16 02:41:14 -05:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
2015-05-16 02:41:14 -05:00
use Response;
2015-06-03 14:25:11 -05:00
2015-05-16 02:41:14 -05:00
/**
* Class BillController
*
* @package FireflyIII\Http\Controllers\Chart
*/
class BillController extends Controller
{
2015-06-27 04:44:18 -05:00
2016-12-11 11:34:18 -06:00
/** @var GeneratorInterface */
2015-06-27 04:44:18 -05:00
protected $generator;
2015-05-16 02:41:14 -05:00
/**
* checked
2015-05-16 02:41:14 -05:00
*/
2015-06-27 04:44:18 -05:00
public function __construct()
2015-05-16 02:41:14 -05:00
{
2015-06-27 04:44:18 -05:00
parent::__construct();
2016-12-11 11:34:18 -06:00
$this->generator = app(GeneratorInterface::class);
2015-05-16 02:41:14 -05:00
}
/**
* Shows all bills and whether or not they've been paid this month (pie chart).
2015-05-16 02:41:14 -05:00
*
2015-07-09 04:13:38 -05:00
* @param BillRepositoryInterface $repository
2015-05-16 02:41:14 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-07-09 02:41:54 -05:00
public function frontpage(BillRepositoryInterface $repository)
2015-05-16 02:41:14 -05: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,
];
2016-12-14 11:59:12 -06:00
$data = $this->generator->pieChart($chartData);
2016-12-11 10:46:30 -06:00
$cache->store($data);
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
return Response::json($data);
}
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
/**
2016-12-11 10:46:30 -06:00
* @param JournalCollectorInterface $collector
* @param Bill $bill
2015-06-27 04:44:18 -05:00
*
2016-12-11 10:46:30 -06:00
* @return \Illuminate\Http\JsonResponse
2015-06-27 04:44:18 -05:00
*/
2016-12-11 10:46:30 -06:00
public function single(JournalCollectorInterface $collector, Bill $bill)
2015-06-27 04:44:18 -05:00
{
$cache = new CacheProperties;
2016-12-11 10:46:30 -06:00
$cache->addProperty('chart.bill.single');
2015-06-27 04:44:18 -05:00
$cache->addProperty($bill->id);
if ($cache->has()) {
2016-04-06 02:27:45 -05:00
return Response::json($cache->get());
2015-05-16 02:41:14 -05:00
}
2016-12-30 06:47:23 -06:00
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
$results = $results->sortBy(
function (Transaction $transaction) {
return $transaction->date->format('U');
2015-07-01 06:18:50 -05:00
}
);
2016-12-11 10:46:30 -06:00
$chartData = [
2016-12-28 12:00:39 -06: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 10:46:30 -06:00
];
/** @var Transaction $entry */
foreach ($results as $entry) {
2016-12-28 12:00:39 -06:00
$date = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
$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 10:46:30 -06:00
}
2016-12-14 11:59:12 -06:00
$data = $this->generator->multiSet($chartData);
2015-06-03 14:25:11 -05:00
$cache->store($data);
return Response::json($data);
2015-05-16 02:41:14 -05:00
}
2015-05-20 12:56:14 -05:00
}