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

367 lines
11 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* JsonController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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 08:57:45 +02:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-24 22:53:38 +01:00
2015-03-06 08:20:27 +01:00
use Amount;
use Carbon\Carbon;
2016-02-17 15:52:46 +01:00
use FireflyIII\Exceptions\FireflyException;
2016-12-28 11:34:00 +01:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2016-05-20 11:02:07 +02:00
use FireflyIII\Models\AccountType;
2016-10-10 07:49:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-10-09 07:58:27 +02:00
use FireflyIII\Repositories\Account\AccountTaskerInterface;
2015-03-06 08:20:27 +01:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2017-01-22 09:15:53 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-04-28 10:36:13 +02:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2015-06-03 18:22:47 +02:00
use FireflyIII\Support\CacheProperties;
2016-12-28 13:02:56 +01:00
use Illuminate\Http\Request;
2015-07-12 12:45:41 +02:00
use Preferences;
2015-02-24 22:53:38 +01:00
use Response;
2015-03-29 07:51:56 +02:00
2015-02-24 22:53:38 +01:00
/**
* Class JsonController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-06 08:20:27 +01:00
class JsonController extends Controller
{
/**
* JsonController constructor.
*/
2016-01-08 18:29:47 +01:00
public function __construct()
{
2016-01-08 20:40:48 +01:00
parent::__construct();
2016-01-08 18:29:47 +01:00
}
2015-03-06 08:20:27 +01:00
2016-01-14 16:41:15 +01:00
/**
2016-12-28 13:02:56 +01:00
* @param Request $request
*
2016-01-14 16:41:15 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2016-12-28 13:02:56 +01:00
public function action(Request $request)
2016-01-14 16:41:15 +01:00
{
2016-12-28 13:02:56 +01:00
$count = intval($request->get('count')) > 0 ? intval($request->get('count')) : 1;
2016-04-26 21:40:15 +02:00
$keys = array_keys(config('firefly.rule-actions'));
2016-01-14 16:41:15 +01:00
$actions = [];
foreach ($keys as $key) {
$actions[$key] = trans('firefly.rule_action_' . $key . '_choice');
}
$view = view('rules.partials.action', compact('actions', 'count'))->render();
2016-01-14 16:41:15 +01:00
return Response::json(['html' => $view]);
}
/**
* Returns a JSON list of all accounts.
*
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*
*/
public function allAccounts(AccountRepositoryInterface $repository)
{
$return = array_unique(
$repository->getAccountsByType(
[AccountType::REVENUE, AccountType::EXPENSE, AccountType::BENEFICIARY, AccountType::DEFAULT, AccountType::ASSET]
)->pluck('name')->toArray()
);
sort($return);
return Response::json($return);
}
2017-01-22 09:15:53 +01:00
/**
* @param JournalCollectorInterface $collector
*
* @return \Illuminate\Http\JsonResponse
*/
public function allTransactionJournals(JournalCollectorInterface $collector)
{
$collector->setLimit(100)->setPage(1);
$return = array_unique($collector->getJournals()->pluck('description')->toArray());
sort($return);
return Response::json($return);
}
2015-03-06 08:20:27 +01:00
/**
2015-12-29 22:48:55 +01:00
* @param BillRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
* @return \Symfony\Component\HttpFoundation\Response
2015-03-06 08:20:27 +01:00
*/
2015-12-28 08:00:42 +01:00
public function boxBillsPaid(BillRepositoryInterface $repository)
2015-03-06 08:20:27 +01:00
{
2016-02-04 07:27:03 +01:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
2015-06-02 18:05:42 +02:00
/*
* Since both this method and the chart use the exact same data, we can suffice
* with calling the one method in the bill repository that will get this amount.
*/
2016-05-13 15:53:39 +02:00
$amount = $repository->getBillsPaidInRange($start, $end); // will be a negative amount.
2016-02-05 15:41:40 +01:00
$amount = bcmul($amount, '-1');
2015-06-02 18:05:42 +02:00
$data = ['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 21:25:11 +02:00
2015-06-02 18:05:42 +02:00
return Response::json($data);
}
/**
* @param BillRepositoryInterface $repository
*
2015-12-28 08:00:42 +01:00
* @return \Illuminate\Http\JsonResponse
*/
public function boxBillsUnpaid(BillRepositoryInterface $repository)
{
2016-05-13 15:53:39 +02:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
$amount = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount.
$data = ['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-02 18:05:42 +02:00
return Response::json($data);
}
/**
2016-10-10 07:49:39 +02:00
* @param AccountTaskerInterface $accountTasker
* @param AccountRepositoryInterface $repository
*
2016-05-20 11:02:07 +02:00
* @return \Illuminate\Http\JsonResponse
2016-10-10 07:49:39 +02:00
*
*/
2016-10-10 07:49:39 +02:00
public function boxIn(AccountTaskerInterface $accountTasker, AccountRepositoryInterface $repository)
{
2016-02-04 07:27:03 +01:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
2015-06-02 18:05:42 +02:00
// works for json too!
2015-06-03 21:25:11 +02:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-in');
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 18:05:42 +02:00
}
2016-10-10 07:49:39 +02:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
$assets = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2016-10-09 07:58:27 +02:00
$amount = $accountTasker->amountInInPeriod($accounts, $assets, $start, $end);
2016-05-14 13:51:33 +02:00
$data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 21:25:11 +02:00
$cache->store($data);
2015-06-02 18:05:42 +02:00
return Response::json($data);
}
/**
2016-10-10 07:49:39 +02:00
* @param AccountTaskerInterface $accountTasker
* @param AccountRepositoryInterface $repository
*
* @return \Symfony\Component\HttpFoundation\Response
*/
2016-10-10 07:49:39 +02:00
public function boxOut(AccountTaskerInterface $accountTasker, AccountRepositoryInterface $repository)
{
2016-02-04 07:27:03 +01:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
2015-06-02 18:05:42 +02:00
// works for json too!
2015-06-03 21:25:11 +02:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-out');
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return Response::json($cache->get()); // @codeCoverageIgnore
2015-06-02 18:05:42 +02:00
}
2016-10-10 07:49:39 +02:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
$assets = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2016-10-09 07:58:27 +02:00
$amount = $accountTasker->amountOutInPeriod($accounts, $assets, $start, $end);
2015-04-10 07:19:45 +02:00
2015-06-02 18:05:42 +02:00
$data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
2015-06-03 21:25:11 +02:00
$cache->store($data);
2015-06-02 18:05:42 +02:00
return Response::json($data);
2015-03-06 08:20:27 +01:00
}
2015-02-24 22:53:38 +01:00
2017-01-22 09:15:53 +01:00
/**
* @param BudgetRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*/
public function budgets(BudgetRepositoryInterface $repository)
{
$return = array_unique($repository->getBudgets()->pluck('name')->toArray());
sort($return);
return Response::json($return);
}
2015-02-24 22:53:38 +01:00
/**
* Returns a list of categories.
*
2017-01-22 09:15:53 +01:00
* @param CategoryRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
2015-02-24 22:53:38 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2017-01-22 09:15:53 +01:00
public function categories(CategoryRepositoryInterface $repository)
2015-02-24 22:53:38 +01:00
{
$return = array_unique($repository->getCategories()->pluck('name')->toArray());
sort($return);
2015-02-24 22:53:38 +01:00
return Response::json($return);
}
2016-01-24 15:58:16 +01:00
/**
* @return \Illuminate\Http\JsonResponse
*/
public function endTour()
{
Preferences::set('tour', false);
return Response::json('true');
}
2015-02-24 22:53:38 +01:00
/**
* Returns a JSON list of all beneficiaries.
*
2016-10-10 07:49:39 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
2015-02-24 22:53:38 +01:00
* @return \Illuminate\Http\JsonResponse
2016-10-10 07:49:39 +02:00
*
2015-02-24 22:53:38 +01:00
*/
2016-10-10 07:49:39 +02:00
public function expenseAccounts(AccountRepositoryInterface $repository)
2015-02-24 22:53:38 +01:00
{
$return = array_unique($repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY])->pluck('name')->toArray());
sort($return);
2015-02-24 22:53:38 +01:00
return Response::json($return);
}
/**
2016-10-10 07:49:39 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
2015-02-24 22:53:38 +01:00
* @return \Illuminate\Http\JsonResponse
2016-10-10 07:49:39 +02:00
*
2015-02-24 22:53:38 +01:00
*/
2016-10-10 07:49:39 +02:00
public function revenueAccounts(AccountRepositoryInterface $repository)
2015-02-24 22:53:38 +01:00
{
$return = array_unique($repository->getAccountsByType([AccountType::REVENUE])->pluck('name')->toArray());
sort($return);
2015-02-24 22:53:38 +01:00
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
* @param TagRepositoryInterface $tagRepository
*
* @return \Illuminate\Http\JsonResponse
*/
public function tags(TagRepositoryInterface $tagRepository)
{
$return = array_unique($tagRepository->get()->pluck('tag')->toArray());
sort($return);
return Response::json($return);
}
2016-01-24 15:58:16 +01:00
/**
*
*/
public function tour()
{
$pref = Preferences::get('tour', true);
if (!$pref) {
2016-02-17 15:52:46 +01:00
throw new FireflyException('Cannot find preference for tour. Exit.');
2016-01-24 15:58:16 +01:00
}
$headers = ['main-content', 'sidebar-toggle', 'account-menu', 'budget-menu', 'report-menu', 'transaction-menu', 'option-menu', 'main-content-end'];
$steps = [];
foreach ($headers as $header) {
$steps[] = [
'element' => '#' . $header,
'title' => trans('help.' . $header . '-title'),
'content' => trans('help.' . $header . '-text'),
];
}
$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;
$template = view('json.tour')->render();
return Response::json(['steps' => $steps, 'template' => $template]);
}
2015-04-07 18:25:21 +02:00
/**
2016-12-28 13:02:56 +01:00
* @param JournalCollectorInterface $collector
* @param string $what
2015-04-07 18:25:21 +02:00
*
* @return \Illuminate\Http\JsonResponse
2015-04-07 18:25:21 +02:00
*/
2016-12-28 13:02:56 +01:00
public function transactionJournals(JournalCollectorInterface $collector, string $what)
2015-03-27 13:16:14 +01:00
{
2017-01-22 09:15:53 +01:00
$type = config('firefly.transactionTypesByWhat.' . $what);
$types = [$type];
$collector->setTypes($types)->setLimit(100)->setPage(1);
$return = array_unique($collector->getJournals()->pluck('description')->toArray());
sort($return);
2015-03-27 13:16:14 +01:00
return Response::json($return);
2015-03-29 07:51:56 +02:00
2015-03-10 17:26:31 +01:00
}
/**
2017-02-25 05:57:01 +01:00
* @param JournalRepositoryInterface $repository
*
2017-02-25 05:57:01 +01:00
* @return \Illuminate\Http\JsonResponse
*/
public function transactionTypes(JournalRepositoryInterface $repository)
{
$return = array_unique($repository->getTransactionTypes()->pluck('type')->toArray());
sort($return);
2015-03-10 17:26:31 +01:00
return Response::json($return);
2015-03-10 17:26:31 +01:00
}
2016-01-24 15:58:16 +01:00
/**
2016-12-28 13:02:56 +01:00
* @param Request $request
*
2016-01-24 15:58:16 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2016-12-28 13:02:56 +01:00
public function trigger(Request $request)
2016-01-24 15:58:16 +01:00
{
2016-12-28 13:02:56 +01:00
$count = intval($request->get('count')) > 0 ? intval($request->get('count')) : 1;
2016-04-26 21:40:15 +02:00
$keys = array_keys(config('firefly.rule-triggers'));
2016-01-24 15:58:16 +01:00
$triggers = [];
foreach ($keys as $key) {
if ($key != 'user_action') {
$triggers[$key] = trans('firefly.rule_trigger_' . $key . '_choice');
}
}
$view = view('rules.partials.trigger', compact('triggers', 'count'))->render();
2016-01-24 15:58:16 +01:00
return Response::json(['html' => $view]);
}
2015-02-24 22:53:38 +01:00
}