| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * BillName.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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Bill\BillRepositoryInterface; | 
					
						
							|  |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BillName | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Import\Converter | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BillName extends BasicConverter implements ConverterInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return Bill | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function convert($value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |         $value = trim($value); | 
					
						
							|  |  |  |         Log::debug('Going to convert using BillName', ['value' => $value]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (strlen($value) === 0) { | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |             $this->setCertainty(0); | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |             return new Bill; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var BillRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(BillRepositoryInterface::class, [$this->user]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->mapping[$value])) { | 
					
						
							|  |  |  |             Log::debug('Found bill in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); | 
					
						
							|  |  |  |             $bill = $repository->find(intval($this->mapping[$value])); | 
					
						
							|  |  |  |             if (!is_null($bill->id)) { | 
					
						
							|  |  |  |                 Log::debug('Found bill by ID', ['id' => $bill->id]); | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |                 $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |                 return $bill; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // not mapped? Still try to find it first:
 | 
					
						
							|  |  |  |         $bill = $repository->findByName($value); | 
					
						
							|  |  |  |         if (!is_null($bill->id)) { | 
					
						
							|  |  |  |             Log::debug('Found bill by name ', ['id' => $bill->id]); | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |             $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |             return $bill; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create new bill. Use a lot of made up values.
 | 
					
						
							|  |  |  |         $bill = $repository->store( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'name'        => $value, | 
					
						
							|  |  |  |                 'match'       => $value, | 
					
						
							|  |  |  |                 'amount_min'  => 1, | 
					
						
							| 
									
										
										
										
											2016-08-26 08:21:31 +02:00
										 |  |  |                 'user'        => $this->user->id, | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  |                 'amount_max'  => 10, | 
					
						
							|  |  |  |                 'date'        => date('Ymd'), | 
					
						
							|  |  |  |                 'repeat_freq' => 'monthly', | 
					
						
							|  |  |  |                 'skip'        => 0, | 
					
						
							|  |  |  |                 'automatch'   => 0, | 
					
						
							|  |  |  |                 'active'      => 1, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:11 +02:00
										 |  |  |         if (is_null($bill->id)) { | 
					
						
							|  |  |  |             $this->setCertainty(0); | 
					
						
							|  |  |  |             Log::info('Could not store new bill by name', $bill->getErrors()->toArray()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return new Bill; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 21:40:58 +02:00
										 |  |  |         $this->setCertainty(100); | 
					
						
							| 
									
										
										
										
											2016-07-23 21:37:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $bill; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 08:25:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-12 15:10:03 +02:00
										 |  |  | } |