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

117 lines
3.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;
use FireflyIII\Helpers\Collector\JournalCollector;
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);
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
/**
* Shows the overview for a bill. The min/max amount and matched journals.
*
* @param Bill $bill
2015-06-27 11:44:18 +02:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function single(Bill $bill)
2015-06-27 11:44:18 +02:00
{
$cache = new CacheProperties;
$cache->addProperty('single');
$cache->addProperty('bill');
$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
}
2015-06-27 11:44:18 +02:00
// get first transaction or today for start:
$collector = new JournalCollector(auth()->user());
$collector->setAllAssetAccounts()->setBills(new Collection([$bill]));
$results = $collector->getJournals();
2015-05-16 09:41:14 +02:00
2015-07-01 13:18:50 +02:00
// resort:
$results = $results->sortBy(
function (Transaction $transaction) {
return $transaction->date->format('U');
2015-07-01 13:18:50 +02:00
}
);
2015-06-27 11:44:18 +02:00
$data = $this->generator->single($bill, $results);
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
}