2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BillController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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-05-20 12:41:23 +02:00
|
|
|
*/
|
|
|
|
|
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;
|
2016-12-11 17:30:55 +01:00
|
|
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
|
2016-11-05 11:44:41 +01:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollector;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Bill;
|
2016-11-05 11:44:41 +01:00
|
|
|
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;
|
2016-11-05 11:44:41 +01:00
|
|
|
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
|
|
|
|
2016-01-28 21:50:20 +01: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
|
|
|
/**
|
2016-05-05 21:25:20 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-27 17:29:41 +01: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
|
|
|
{
|
2016-12-11 17:30:55 +01: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.
|
|
|
|
*
|
2016-11-05 11:44:41 +01:00
|
|
|
* @param Bill $bill
|
2015-06-27 11:44:18 +02:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-11-05 11:44:41 +01:00
|
|
|
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:
|
2016-11-05 11:44:41 +01:00
|
|
|
$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(
|
2016-11-05 11:44:41 +01:00
|
|
|
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);
|
2015-06-02 17:44:50 +02:00
|
|
|
|
|
|
|
return Response::json($data);
|
2015-05-16 09:41:14 +02:00
|
|
|
}
|
2015-05-20 19:56:14 +02:00
|
|
|
}
|