2015-05-16 09:41:14 +02:00
|
|
|
<?php
|
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-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface;
|
2015-05-16 09:41:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Bill;
|
2015-07-01 13:18:50 +02:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
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;
|
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-02-04 07:27:03 +01:00
|
|
|
$start = session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = session('end', Carbon::now()->endOfMonth());
|
2015-12-27 17:29:41 +01:00
|
|
|
$paid = $repository->getBillsPaidInRange($start, $end); // will be a negative amount.
|
|
|
|
$unpaid = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount.
|
|
|
|
$creditCardDue = $repository->getCreditCardBill($start, $end);
|
2015-05-16 09:41:14 +02:00
|
|
|
|
2015-12-27 17:29:41 +01:00
|
|
|
if ($creditCardDue < 0) {
|
|
|
|
// expenses are negative (bill not yet paid),
|
|
|
|
$creditCardDue = bcmul($creditCardDue, '-1');
|
|
|
|
$unpaid = bcadd($unpaid, $creditCardDue);
|
2016-04-29 08:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if $creditCardDue more than zero, the bill has been paid: (transfer = positive).
|
|
|
|
// amount must be negative to be added to $paid:
|
|
|
|
if ($creditCardDue >= 0) {
|
2015-12-27 17:29:41 +01:00
|
|
|
$paid = bcadd($paid, $creditCardDue);
|
|
|
|
}
|
2015-05-16 09:41:14 +02:00
|
|
|
|
2015-06-27 11:44:18 +02:00
|
|
|
// build chart:
|
|
|
|
$data = $this->generator->frontpage($paid, $unpaid);
|
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 BillRepositoryInterface $repository
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function single(BillRepositoryInterface $repository, Bill $bill)
|
|
|
|
{
|
|
|
|
$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-04-23 20:00:48 +02:00
|
|
|
$results = $repository->getJournals($bill, 1, 200);
|
2015-05-16 09:41:14 +02:00
|
|
|
|
2015-07-01 13:18:50 +02:00
|
|
|
// resort:
|
|
|
|
$results = $results->sortBy(
|
|
|
|
function (TransactionJournal $journal) {
|
|
|
|
return $journal->date->format('U');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|