This commit is contained in:
James Cole 2017-08-15 17:34:34 +02:00
parent 34894fb76b
commit 7b3ef0e3ab
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 125 additions and 61 deletions

View File

@ -0,0 +1,101 @@
<?php
/**
* AutoCompleteController.php
* Copyright (c) 2017 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.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Response;
/**
* Class AutoCompleteController
*
* @package FireflyIII\Http\Controllers\Json
*/
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);
return Response::json($return);
}
/**
* 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) {
if ($account->active) {
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
sort($return);
return Response::json($return);
}
/**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*
*/
public function revenueAccounts(AccountRepositoryInterface $repository)
{
$set = $repository->getAccountsByType([AccountType::REVENUE]);
$filtered = $set->filter(
function (Account $account) {
if ($account->active) {
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
sort($return);
return Response::json($return);
}
}

View File

@ -62,27 +62,6 @@ class JsonController extends Controller
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);
}
/**
* @param JournalCollectorInterface $collector
*
@ -235,36 +214,6 @@ class JsonController extends Controller
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*
*/
public function expenseAccounts(AccountRepositoryInterface $repository)
{
$return = array_unique($repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY])->pluck('name')->toArray());
sort($return);
return Response::json($return);
}
/**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*
*/
public function revenueAccounts(AccountRepositoryInterface $repository)
{
$return = array_unique($repository->getAccountsByType([AccountType::REVENUE])->pluck('name')->toArray());
sort($return);
return Response::json($return);
}
/**
* Returns a JSON list of all beneficiaries.
*

View File

@ -205,10 +205,12 @@ trait FindAccountsTrait
*/
public function getCashAccount(): Account
{
$type = AccountType::where('type', AccountType::CASH)->first();
$account = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $type->id, 'name' => 'Cash account', 'active' => 1]
$type = AccountType::where('type', AccountType::CASH)->first();
$account = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $type->id, 'name' => 'Cash account']
);
$account->active = true;
$account->save();
return $account;
}

View File

@ -117,8 +117,11 @@ trait SupportJournalsTrait
if (strlen($data['source_account_name']) > 0) {
$sourceType = AccountType::where('type', 'Revenue account')->first();
$sourceAccount = Account::firstOrCreateEncrypted(
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => $data['source_account_name'], 'active' => 1]
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => $data['source_account_name']]
);
// always make account active
$sourceAccount->active = true;
$sourceAccount->save();
Log::debug(sprintf('source account name is "%s", account is %d', $data['source_account_name'], $sourceAccount->id));
@ -132,8 +135,11 @@ trait SupportJournalsTrait
$sourceType = AccountType::where('type', AccountType::CASH)->first();
$sourceAccount = Account::firstOrCreateEncrypted(
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => 'Cash account', 'active' => 1]
['user_id' => $user->id, 'account_type_id' => $sourceType->id, 'name' => 'Cash account']
);
// always make account active
$sourceAccount->active = true;
$sourceAccount->save();
return [
'source' => $sourceAccount,
@ -161,10 +167,13 @@ trait SupportJournalsTrait
'user_id' => $user->id,
'account_type_id' => $destinationType->id,
'name' => $data['destination_account_name'],
'active' => 1,
]
);
// always make account active
$destinationAccount->active = true;
$destinationAccount->save();
Log::debug(sprintf('destination account name is "%s", account is %d', $data['destination_account_name'], $destinationAccount->id));
return [
@ -175,8 +184,11 @@ trait SupportJournalsTrait
Log::debug('destination_account_name is empty, so default to cash account!');
$destinationType = AccountType::where('type', AccountType::CASH)->first();
$destinationAccount = Account::firstOrCreateEncrypted(
['user_id' => $user->id, 'account_type_id' => $destinationType->id, 'name' => 'Cash account', 'active' => 1]
['user_id' => $user->id, 'account_type_id' => $destinationType->id, 'name' => 'Cash account']
);
// always make account active
$destinationAccount->active = true;
$destinationAccount->save();
return [
'source' => $sourceAccount,

View File

@ -430,9 +430,9 @@ Route::group(
*/
Route::group(
['middleware' => 'user-full-auth', 'prefix' => 'json', 'as' => 'json.'], function () {
Route::get('expense-accounts', ['uses' => 'JsonController@expenseAccounts', 'as' => 'expense-accounts']);
Route::get('all-accounts', ['uses' => 'JsonController@allAccounts', 'as' => 'all-accounts']);
Route::get('revenue-accounts', ['uses' => 'JsonController@revenueAccounts', 'as' => 'revenue-accounts']);
Route::get('expense-accounts', ['uses' => 'Json\AutoCompleteController@expenseAccounts', 'as' => 'expense-accounts']);
Route::get('all-accounts', ['uses' => 'Json\AutoCompleteController@allAccounts', 'as' => 'all-accounts']);
Route::get('revenue-accounts', ['uses' => 'Json\AutoCompleteController@revenueAccounts', 'as' => 'revenue-accounts']);
Route::get('categories', ['uses' => 'JsonController@categories', 'as' => 'categories']);
Route::get('budgets', ['uses' => 'JsonController@budgets', 'as' => 'budgets']);
Route::get('tags', ['uses' => 'JsonController@tags', 'as' => 'tags']);