mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Fix #764
This commit is contained in:
		
							
								
								
									
										101
									
								
								app/Http/Controllers/Json/AutoCompleteController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								app/Http/Controllers/Json/AutoCompleteController.php
									
									
									
									
									
										Normal 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); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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. | ||||
|      * | ||||
|   | ||||
| @@ -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; | ||||
|     } | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user