| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AssetAccountIban.php | 
					
						
							|  |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This software may be modified and distributed under the terms | 
					
						
							|  |  |  |  * of the MIT license.  See the LICENSE file for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Import\Converter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Crud\Account\AccountCrudInterface; | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  | use Log; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AssetAccountIban | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Import\Converter | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AssetAccountIban extends BasicConverter implements ConverterInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |     public function convert($value): Account | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Going to convert value ' . $value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountCrudInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(AccountCrudInterface::class, [$this->user]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->mapping[$value])) { | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |             Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); | 
					
						
							|  |  |  |             $account = $repository->find(intval($this->mapping[$value])); | 
					
						
							|  |  |  |             if (!is_null($account->id)) { | 
					
						
							|  |  |  |                 Log::debug('Found account by ID', ['id' => $account->id]); | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |                 return $account; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |         // not mapped? Still try to find it first:
 | 
					
						
							|  |  |  |         $account = $repository->findByIban($value); | 
					
						
							|  |  |  |         if (!is_null($account->id)) { | 
					
						
							|  |  |  |             Log::debug('Found account by IBAN', ['id' => $account->id]); | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 22:37:47 +02:00
										 |  |  |             return $account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $repository->store( | 
					
						
							|  |  |  |             ['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'active' => true] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							| 
									
										
										
										
											2016-07-15 22:26:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |