| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountSearch.php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  |  * Copyright (c) 2020 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | namespace FireflyIII\Support\Search; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2023-06-04 06:30:22 +02:00
										 |  |  | use Illuminate\Contracts\Auth\Authenticatable; | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountSearch | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountSearch implements GenericSearchInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |     public const string SEARCH_ALL    = 'all'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |     public const string SEARCH_IBAN   = 'iban'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |     public const string SEARCH_ID     = 'id'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |     public const string SEARCH_NAME   = 'name'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |     /** @var string */ | 
					
						
							| 
									
										
										
										
											2023-12-02 12:56:48 +01:00
										 |  |  |     public const string SEARCH_NUMBER = 'number'; | 
					
						
							| 
									
										
										
										
											2020-10-24 18:40:17 +02:00
										 |  |  |     private string $field; | 
					
						
							|  |  |  |     private string $query; | 
					
						
							|  |  |  |     private array  $types; | 
					
						
							|  |  |  |     private User   $user; | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->types = []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function search(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |         $searchQuery   = $this->user->accounts() | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             ->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id') | 
					
						
							|  |  |  |             ->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id') | 
					
						
							|  |  |  |             ->whereIn('account_types.type', $this->types) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |         $like          = sprintf('%%%s%%', $this->query); | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |         $originalQuery = $this->query; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |         switch ($this->field) { | 
					
						
							| 
									
										
										
										
											2020-10-03 17:53:23 +02:00
										 |  |  |             default: | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |             case self::SEARCH_ALL: | 
					
						
							| 
									
										
										
										
											2020-10-24 18:40:17 +02:00
										 |  |  |                 $searchQuery->where( | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |                     static function (Builder $q) use ($like): void { | 
					
						
							| 
									
										
										
										
											2024-10-10 06:30:05 +02:00
										 |  |  |                         $q->whereLike('accounts.id', $like); | 
					
						
							|  |  |  |                         $q->orWhereLike('accounts.name', $like); | 
					
						
							|  |  |  |                         $q->orWhereLike('accounts.iban', $like); | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |                 // meta data:
 | 
					
						
							| 
									
										
										
										
											2020-10-24 18:40:17 +02:00
										 |  |  |                 $searchQuery->orWhere( | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |                     static function (Builder $q) use ($originalQuery): void { | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                         $json = json_encode($originalQuery, JSON_THROW_ON_ERROR); | 
					
						
							| 
									
										
										
										
											2020-02-22 11:05:16 +01:00
										 |  |  |                         $q->where('account_meta.name', '=', 'account_number'); | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |                         $q->whereLike('account_meta.data', $json); | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |             case self::SEARCH_ID: | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $searchQuery->where('accounts.id', '=', (int) $originalQuery); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |             case self::SEARCH_NAME: | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |                 $searchQuery->whereLike('accounts.name', $like); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |             case self::SEARCH_IBAN: | 
					
						
							| 
									
										
										
										
											2024-10-10 06:30:05 +02:00
										 |  |  |                 $searchQuery->whereLike('accounts.iban', $like); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |             case self::SEARCH_NUMBER: | 
					
						
							|  |  |  |                 // meta data:
 | 
					
						
							| 
									
										
										
										
											2020-10-24 18:40:17 +02:00
										 |  |  |                 $searchQuery->Where( | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |                     static function (Builder $q) use ($originalQuery): void { | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                         $json = json_encode($originalQuery, JSON_THROW_ON_ERROR); | 
					
						
							|  |  |  |                         $q->where('account_meta.name', 'account_number'); | 
					
						
							|  |  |  |                         $q->where('account_meta.data', $json); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-24 18:40:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $searchQuery->distinct()->get(['accounts.*']); | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setField(string $field): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->field = $field; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setQuery(string $query): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->query = $query; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setTypes(array $types): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->types = $types; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |     public function setUser(null|Authenticatable|User $user): void | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-30 19:49:40 +01:00
										 |  |  |         if ($user instanceof User) { | 
					
						
							| 
									
										
										
										
											2023-06-04 06:30:22 +02:00
										 |  |  |             $this->user = $user; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-02 18:00:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-30 07:33:06 +02:00
										 |  |  | } |