2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BillController.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
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/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02: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-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;
|
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;
|
2018-07-08 12:28:42 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-11-05 11:44:41 +01:00
|
|
|
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
|
|
|
|
{
|
2018-07-21 08:06:24 +02:00
|
|
|
/** @var GeneratorInterface Chart generation methods. */
|
2015-06-27 11:44:18 +02:00
|
|
|
protected $generator;
|
|
|
|
|
2015-05-16 09:41:14 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* BillController constructor.
|
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
|
|
|
/**
|
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
|
|
|
*
|
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
|
|
|
{
|
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()) {
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
2016-12-11 17:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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-11 17:30:55 +01:00
|
|
|
];
|
|
|
|
|
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
|
|
|
/**
|
2018-07-22 08:58:58 +02:00
|
|
|
* Shows overview for a single bill.
|
2018-07-21 08:06:24 +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
|
|
|
}
|
|
|
|
|
2018-07-22 08:58:58 +02:00
|
|
|
$results = $collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->getJournals();
|
2018-08-06 19:14:30 +02:00
|
|
|
/** @var Collection $results */
|
|
|
|
$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
|
|
|
}
|
|
|
|
);
|
2016-12-11 17:46:30 +01:00
|
|
|
$chartData = [
|
2018-07-15 09:38:49 +02:00
|
|
|
['type' => 'bar', 'label' => (string)trans('firefly.min-amount'), 'entries' => []],
|
|
|
|
['type' => 'bar', 'label' => (string)trans('firefly.max-amount'), 'entries' => []],
|
|
|
|
['type' => 'line', 'label' => (string)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
|
2018-07-22 08:58:58 +02:00
|
|
|
|
|
|
|
// append amount because there are more than one per moment:
|
|
|
|
if (!isset($chartData[2]['entries'][$date])) {
|
|
|
|
$chartData[2]['entries'][$date] = '0';
|
|
|
|
}
|
|
|
|
$amount = bcmul($entry->transaction_amount, '-1');
|
|
|
|
$chartData[2]['entries'][$date] = bcadd($chartData[2]['entries'][$date], $amount); // 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);
|
2015-06-02 17:44:50 +02:00
|
|
|
|
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
|
|
|
}
|