| 
									
										
										
										
											2016-07-16 07:58:25 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * AssetAccountName.php | 
					
						
							|  |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2016-07-16 07:58:25 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Import\Converter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2016-10-10 07:12:39 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2016-07-16 07:58:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AssetAccountName | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Import\Converter | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AssetAccountName extends BasicConverter implements ConverterInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2016-07-16 07:58:25 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function convert($value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |         $value = trim($value); | 
					
						
							|  |  |  |         Log::debug('Going to convert using AssetAccountName', ['value' => $value]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (strlen($value) === 0) { | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |             $this->setCertainty(0); | 
					
						
							| 
									
										
										
										
											2016-08-11 10:21:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |             return new Account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 07:12:39 +02:00
										 |  |  |         /** @var AccountRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(AccountRepositoryInterface::class, [$this->user]); | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->mapping[$value])) { | 
					
						
							|  |  |  |             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-29 21:40:58 +02:00
										 |  |  |                 $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-08-11 10:21:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |                 return $account; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // not mapped? Still try to find it first:
 | 
					
						
							| 
									
										
										
										
											2016-10-10 07:20:49 +02:00
										 |  |  |         $account = $repository->findByName($value, [AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |         if (!is_null($account->id)) { | 
					
						
							| 
									
										
										
										
											2016-08-11 10:21:32 +02:00
										 |  |  |             Log::debug('Found asset account by name', ['value' => $value, 'id' => $account->id]); | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 08:03:03 +02:00
										 |  |  |         $account = $repository->store( | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |             ['name'   => $value, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, | 
					
						
							|  |  |  |              'active' => true] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (is_null($account->id)) { | 
					
						
							|  |  |  |             $this->setCertainty(0); | 
					
						
							|  |  |  |             Log::info('Could not store new asset account by name', $account->getErrors()->toArray()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return new Account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |         $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 10:21:32 +02:00
										 |  |  |         Log::debug('Created new asset account ', ['name' => $account->name, 'id' => $account->id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |         return $account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 07:58:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-12 15:10:03 +02:00
										 |  |  | } |