firefly-iii/app/Http/Controllers/Json/AutoCompleteController.php

171 lines
5.1 KiB
PHP
Raw Normal View History

2017-08-15 10:34:34 -05:00
<?php
/**
* AutoCompleteController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* 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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-08-15 10:34:34 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-08-15 10:34:34 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
2017-08-15 10:34:34 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2017-08-15 10:34:34 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AutoCompleteController.
2017-08-15 10:34:34 -05:00
*/
class AutoCompleteController extends Controller
{
/**
* 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);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-08-15 10:34:34 -05:00
}
2017-08-30 00:00:17 -05:00
/**
* @param JournalCollectorInterface $collector
*
* @return \Illuminate\Http\JsonResponse
*/
public function allTransactionJournals(JournalCollectorInterface $collector)
{
$collector->setLimit(250)->setPage(1);
$return = array_unique($collector->getJournals()->pluck('description')->toArray());
sort($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-08-30 00:00:17 -05:00
}
2017-08-15 10:34:34 -05:00
/**
* Returns a JSON list of all beneficiaries.
*
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*/
public function expenseAccounts(AccountRepositoryInterface $repository)
{
$set = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$filtered = $set->filter(
function (Account $account) {
2017-12-23 10:42:07 -06:00
if ($account->active === true) {
2017-08-15 10:34:34 -05:00
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
sort($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-08-15 10:34:34 -05:00
}
/**
* @param JournalCollectorInterface $collector
2017-09-08 23:41:45 -05:00
* @param TransactionJournal $except
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function journalsWithId(JournalCollectorInterface $collector, TransactionJournal $except)
{
$cache = new CacheProperties;
$cache->addProperty('recent-journals-id');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$collector->setLimit(400)->setPage(1);
$set = $collector->getJournals()->pluck('description', 'journal_id')->toArray();
$return = [];
foreach ($set as $id => $description) {
$id = intval($id);
if ($id !== $except->id) {
$return[] = [
'id' => $id,
'name' => $id . ': ' . $description,
];
}
}
$cache->store($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
}
2017-08-15 10:34:34 -05:00
/**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*/
public function revenueAccounts(AccountRepositoryInterface $repository)
{
$set = $repository->getAccountsByType([AccountType::REVENUE]);
$filtered = $set->filter(
function (Account $account) {
2017-12-23 10:42:07 -06:00
if ($account->active === true) {
2017-08-15 10:34:34 -05:00
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
sort($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-08-15 10:34:34 -05:00
}
2017-08-30 00:00:17 -05:00
/**
* @param JournalCollectorInterface $collector
* @param string $what
*
* @return \Illuminate\Http\JsonResponse
*/
public function transactionJournals(JournalCollectorInterface $collector, string $what)
{
$type = config('firefly.transactionTypesByWhat.' . $what);
$types = [$type];
$collector->setTypes($types)->setLimit(250)->setPage(1);
$return = array_unique($collector->getJournals()->pluck('description')->toArray());
sort($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-08-30 00:00:17 -05:00
}
2017-08-30 23:47:18 -05:00
}