| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * BudgetId.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 08:25:39 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Import\Converter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							|  |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BudgetId | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Import\Converter | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BudgetId extends BasicConverter implements ConverterInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |      * @return Budget | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function convert($value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |         $value = intval(trim($value)); | 
					
						
							|  |  |  |         Log::debug('Going to convert using BudgetId', ['value' => $value]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($value === 0) { | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |             $this->setCertainty(0); | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |             return new Budget; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var BudgetRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(BudgetRepositoryInterface::class, [$this->user]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->mapping[$value])) { | 
					
						
							|  |  |  |             Log::debug('Found budget in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); | 
					
						
							|  |  |  |             $budget = $repository->find(intval($this->mapping[$value])); | 
					
						
							|  |  |  |             if (!is_null($budget->id)) { | 
					
						
							|  |  |  |                 Log::debug('Found budget by ID', ['id' => $budget->id]); | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |                 $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $budget; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // not mapped? Still try to find it first:
 | 
					
						
							|  |  |  |         $budget = $repository->find($value); | 
					
						
							|  |  |  |         if (!is_null($budget->id)) { | 
					
						
							|  |  |  |             Log::debug('Found budget by ID ', ['id' => $budget->id]); | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |             $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |             return $budget; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // should not really happen. If the ID does not match FF, what is FF supposed to do?
 | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |         $this->setCertainty(0); | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Log::info(sprintf('Could not find budget with ID %d. Will return NULL', $value)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-24 18:47:55 +02:00
										 |  |  |         return new Budget; | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-12 15:10:03 +02:00
										 |  |  | } |