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

100 lines
3.0 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
2015-07-01 06:18:50 -05:00
use FireflyIII\Models\TransactionJournal;
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;
2015-05-16 02:41:14 -05:00
use Response;
use Session;
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
/** @var \FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface */
2015-06-27 04:44:18 -05:00
protected $generator;
2015-05-16 02:41:14 -05:00
/**
2015-06-28 03:03:34 -05:00
* @codeCoverageIgnore
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();
// create chart generator:
$this->generator = app('FireflyIII\Generator\Chart\Bill\BillChartGeneratorInterface');
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::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$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 02:41:14 -05:00
if ($creditCardDue < 0) {
// expenses are negative (bill not yet paid),
$creditCardDue = bcmul($creditCardDue, '-1');
$unpaid = bcadd($unpaid, $creditCardDue);
} else {
// if more than zero, the bill has been paid: (transfer = positive).
// amount must be negative to be added to $paid:
$paid = bcadd($paid, $creditCardDue);
}
2015-05-16 02:41:14 -05:00
2015-06-27 04:44:18 -05:00
// build chart:
$data = $this->generator->frontpage($paid, $unpaid);
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
/**
* 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()) {
return Response::json($cache->get()); // @codeCoverageIgnore
2015-05-16 02:41:14 -05:00
}
2015-06-27 04:44:18 -05:00
// get first transaction or today for start:
$results = $repository->getJournals($bill);
2015-05-16 02:41:14 -05:00
2015-07-01 06:18:50 -05:00
// resort:
$results = $results->sortBy(
function (TransactionJournal $journal) {
return $journal->date->format('U');
}
);
2015-06-27 04:44:18 -05:00
$data = $this->generator->single($bill, $results);
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
}