firefly-iii/app/Http/Controllers/JsonController.php

322 lines
10 KiB
PHP
Raw Normal View History

2015-02-24 15:53:38 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-03-06 01:20:27 -06:00
use Amount;
use Carbon\Carbon;
2015-04-09 14:27:35 -05:00
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Models\Account;
2015-03-06 01:20:27 -06:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-03-06 01:20:27 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2015-04-11 12:59:41 -05:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-04-28 03:36:13 -05:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
2015-07-12 05:45:41 -05:00
use Preferences;
2015-02-24 15:53:38 -06:00
use Response;
2015-03-06 01:20:27 -06:00
use Session;
2015-03-29 00:51:56 -05:00
2015-02-24 15:53:38 -06:00
/**
* Class JsonController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-06 01:20:27 -06:00
class JsonController extends Controller
{
2015-07-12 10:36:38 -05:00
/**
* @return \Illuminate\Http\JsonResponse
*/
2015-07-12 05:45:41 -05:00
public function endTour()
{
Preferences::set('tour', false);
2015-07-12 10:36:38 -05:00
return Response::json('true');
2015-07-12 05:45:41 -05:00
}
2015-07-11 03:01:13 -05:00
/**
*
*/
public function tour()
{
2015-07-12 05:45:41 -05:00
$pref = Preferences::get('tour', true);
if (!$pref) {
abort(404);
}
$headers = ['main-content', 'sidebar-toggle', 'account-menu', 'budget-menu', 'report-menu', 'transaction-menu', 'option-menu', 'main-content-end'];
2015-07-11 03:01:13 -05:00
$steps = [];
foreach ($headers as $header) {
$steps[] = [
2015-07-12 10:59:13 -05:00
'element' => '#' . $header,
2015-07-11 03:01:13 -05:00
'title' => trans('help.' . $header . '-title'),
'content' => trans('help.' . $header . '-text'),
];
}
2015-07-12 10:36:38 -05:00
$steps[0]['orphan'] = true;// orphan and backdrop for first element.
$steps[0]['backdrop'] = true;
$steps[1]['placement'] = 'left';// sidebar position left:
$steps[7]['orphan'] = true; // final in the center again.
$steps[7]['backdrop'] = true;
2015-07-12 10:59:13 -05:00
$template = view('json.tour')->render();
2015-12-26 02:39:35 -06:00
2015-07-11 03:01:13 -05:00
return Response::json(['steps' => $steps, 'template' => $template]);
}
2015-03-06 01:20:27 -06:00
/**
2015-05-03 05:58:55 -05:00
* @param BillRepositoryInterface $repository
*
* @param AccountRepositoryInterface $accountRepository
2015-03-06 01:20:27 -06:00
*
* @return \Symfony\Component\HttpFoundation\Response
2015-03-06 01:20:27 -06:00
*/
public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
2015-03-06 01:20:27 -06:00
{
2015-12-27 00:59:00 -06:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-07-06 11:04:13 -05:00
bcscale(2);
2015-06-02 11:05:42 -05:00
// works for json too!
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-bills-paid');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 11:05:42 -05:00
}
2015-12-26 02:39:35 -06:00
// get amount from bills
$amount = $repository->billsPaidInRange($start, $end)->sum('paid');
2015-09-30 12:41:21 -05:00
2015-12-26 02:39:35 -06:00
// add credit card bill.
2015-12-27 00:59:00 -06:00
$creditCards = $accountRepository->getCreditCards($end); // Find credit card accounts and possibly unpaid credit card bills.
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
2015-12-27 00:59:00 -06:00
if ($creditCard->balance == 0) {
// find a transfer TO the credit card which should account for
// anything paid. If not, the CC is not yet used.
2015-07-06 11:04:13 -05:00
$amount = bcadd($amount, $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount'));
}
}
2015-06-02 11:05:42 -05:00
$data = ['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 14:25:11 -05:00
$cache->store($data);
2015-06-02 11:05:42 -05:00
return Response::json($data);
}
/**
* @param BillRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$amount = 0;
2015-04-11 12:59:41 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-07-06 11:04:13 -05:00
bcscale(2);
2015-06-02 11:05:42 -05:00
// works for json too!
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-bills-unpaid');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 11:05:42 -05:00
}
$bills = $repository->getActiveBills();
$unpaid = new Collection; // bills
/** @var Bill $bill */
foreach ($bills as $bill) {
2015-04-11 12:59:41 -05:00
$ranges = $repository->getRanges($bill, $start, $end);
2015-04-10 00:19:45 -05:00
foreach ($ranges as $range) {
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
if ($journals->count() == 0) {
$unpaid->push([$bill, $range['start']]);
}
}
}
2015-04-11 05:27:56 -05:00
unset($bill, $bills, $range, $ranges);
2015-12-27 00:59:00 -06:00
$creditCards = $accountRepository->getCreditCards($end);
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
2015-12-27 00:59:00 -06:00
$date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));
if ($creditCard->balance < 0) {
// unpaid! create a fake bill that matches the amount.
$description = $creditCard->name;
2015-12-27 00:59:00 -06:00
$fakeAmount = $creditCard->balance * -1;
$fakeBill = $repository->createFakeBill($description, $date, $fakeAmount);
$unpaid->push([$fakeBill, $date]);
}
2015-03-06 01:20:27 -06:00
}
/** @var Bill $entry */
foreach ($unpaid as $entry) {
2015-07-26 12:42:28 -05:00
$current = bcdiv(bcadd($entry[0]->amount_max, $entry[0]->amount_min), 2);
2015-07-07 12:09:45 -05:00
$amount = bcadd($amount, $current);
}
2015-06-02 11:05:42 -05:00
$data = ['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 14:25:11 -05:00
$cache->store($data);
2015-06-02 11:05:42 -05:00
return Response::json($data);
}
/**
* @param ReportQueryInterface $reportQuery
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-13 02:35:58 -06:00
public function boxIn(ReportQueryInterface $reportQuery, AccountRepositoryInterface $accountRepository)
{
2015-06-02 11:05:42 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
// works for json too!
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-in');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 11:05:42 -05:00
}
2015-12-13 02:35:58 -06:00
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
2015-12-26 02:21:45 -06:00
$amount = $reportQuery->incomeInPeriod($start, $end, $accounts)->sum('to_amount');
2015-06-02 11:05:42 -05:00
$data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 14:25:11 -05:00
$cache->store($data);
2015-06-02 11:05:42 -05:00
return Response::json($data);
}
/**
* @param ReportQueryInterface $reportQuery
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-12-13 02:35:58 -06:00
public function boxOut(ReportQueryInterface $reportQuery, AccountRepositoryInterface $accountRepository)
{
2015-06-02 11:05:42 -05:00
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
2015-12-13 02:35:58 -06:00
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
2015-06-02 11:05:42 -05:00
// works for json too!
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-out');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 11:05:42 -05:00
}
2015-12-26 02:21:45 -06:00
$amount = $reportQuery->expenseInPeriod($start, $end, $accounts)->sum('to_amount');
2015-04-10 00:19:45 -05:00
2015-06-02 11:05:42 -05:00
$data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 14:25:11 -05:00
$cache->store($data);
2015-06-02 11:05:42 -05:00
return Response::json($data);
2015-03-06 01:20:27 -06:00
}
2015-02-24 15:53:38 -06:00
/**
* Returns a list of categories.
*
2015-05-03 05:58:55 -05:00
* @param CategoryRepositoryInterface $repository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function categories(CategoryRepositoryInterface $repository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $repository->getCategories();
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
2015-05-03 05:58:55 -05:00
* @param AccountRepositoryInterface $accountRepository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function expenseAccounts(AccountRepositoryInterface $accountRepository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $accountRepository->getAccounts(['Expense account', 'Beneficiary account']);
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
return Response::json($return);
}
/**
2015-05-03 05:58:55 -05:00
* @param AccountRepositoryInterface $accountRepository
*
2015-02-24 15:53:38 -06:00
* @return \Illuminate\Http\JsonResponse
*/
2015-04-11 12:59:41 -05:00
public function revenueAccounts(AccountRepositoryInterface $accountRepository)
2015-02-24 15:53:38 -06:00
{
2015-04-11 12:59:41 -05:00
$list = $accountRepository->getAccounts(['Revenue account']);
2015-03-06 01:20:27 -06:00
$return = [];
2015-02-24 15:53:38 -06:00
foreach ($list as $entry) {
$return[] = $entry->name;
}
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
* @param TagRepositoryInterface $tagRepository
*
* @return \Illuminate\Http\JsonResponse
*/
public function tags(TagRepositoryInterface $tagRepository)
{
$list = $tagRepository->get();
$return = [];
foreach ($list as $entry) {
$return[] = $entry->tag;
}
return Response::json($return);
}
2015-04-07 11:25:21 -05:00
/**
2015-05-03 05:54:39 -05:00
* @param JournalRepositoryInterface $repository
* @param $what
2015-04-07 11:25:21 -05:00
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2015-05-03 05:54:39 -05:00
public function transactionJournals(JournalRepositoryInterface $repository, $what)
2015-03-27 07:16:14 -05:00
{
$descriptions = [];
2015-04-11 12:59:41 -05:00
$dbType = $repository->getTransactionType($what);
$journals = $repository->getJournalsOfType($dbType);
2015-03-29 00:51:56 -05:00
foreach ($journals as $j) {
$descriptions[] = $j->description;
}
2015-03-27 07:16:14 -05:00
$descriptions = array_unique($descriptions);
2015-03-27 07:25:46 -05:00
sort($descriptions);
2015-03-29 00:51:56 -05:00
2015-03-27 07:16:14 -05:00
return Response::json($descriptions);
2015-03-10 11:26:31 -05:00
}
2015-02-24 15:53:38 -06:00
}