| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers\Csv; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  | use Auth; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | use Config; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Csv\Converter\ConverterInterface; | 
					
						
							| 
									
										
										
										
											2015-07-05 19:31:58 +02:00
										 |  |  | use FireflyIII\Helpers\Csv\PostProcessing\PostProcessorInterface; | 
					
						
							| 
									
										
										
										
											2015-07-05 19:57:44 +02:00
										 |  |  | use FireflyIII\Helpers\Csv\Specifix\SpecifixInterface; | 
					
						
							| 
									
										
										
										
											2015-07-09 15:36:56 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  | use Illuminate\Support\MessageBag; | 
					
						
							|  |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class Importer | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Helpers\Csv | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Importer | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @var Data */ | 
					
						
							|  |  |  |     protected $data; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |     /** @var array */ | 
					
						
							|  |  |  |     protected $errors; | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     /** @var  array */ | 
					
						
							|  |  |  |     protected $importData; | 
					
						
							|  |  |  |     /** @var  array */ | 
					
						
							|  |  |  |     protected $importRow; | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     /** @var int */ | 
					
						
							| 
									
										
										
										
											2015-07-06 10:39:44 +02:00
										 |  |  |     protected $imported = 0; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     /** @var array */ | 
					
						
							|  |  |  |     protected $map; | 
					
						
							|  |  |  |     /** @var  array */ | 
					
						
							|  |  |  |     protected $mapped; | 
					
						
							|  |  |  |     /** @var  array */ | 
					
						
							|  |  |  |     protected $roles; | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     /** @var  int */ | 
					
						
							|  |  |  |     protected $rows = 0; | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  |     /** @var array */ | 
					
						
							| 
									
										
										
										
											2015-07-09 15:27:40 +02:00
										 |  |  |     protected $specifix = []; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $journals; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getErrors() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->errors; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getImported() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->imported; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRows() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->rows; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getJournals() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->journals; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function run() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-06 16:33:54 +02:00
										 |  |  |         set_time_limit(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |         $this->journals = new Collection; | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  |         $this->map      = $this->data->getMap(); | 
					
						
							|  |  |  |         $this->roles    = $this->data->getRoles(); | 
					
						
							|  |  |  |         $this->mapped   = $this->data->getMapped(); | 
					
						
							|  |  |  |         $this->specifix = $this->data->getSpecifix(); | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         foreach ($this->data->getReader() as $index => $row) { | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |             if ($this->parseRow($index)) { | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |                 Log::debug('--- Importing row ' . $index); | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |                 $this->rows++; | 
					
						
							|  |  |  |                 $result = $this->importRow($row); | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |                 if (!($result instanceof TransactionJournal)) { | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |                     Log::error('Caught error at row #' . $index . ': ' . $result); | 
					
						
							|  |  |  |                     $this->errors[$index] = $result; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $this->imported++; | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |                     $this->journals->push($result); | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |                 Log::debug('---'); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param int $index | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function parseRow($index) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-30 21:32:58 +02:00
										 |  |  |         return (($this->data->hasHeaders() && $index >= 1) || !$this->data->hasHeaders()); | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $row | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |      * @return string|bool | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function importRow($row) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $data = $this->getFiller(); // These fields are necessary to create a new transaction journal. Some are optional
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |         foreach ($row as $index => $value) { | 
					
						
							|  |  |  |             $role  = isset($this->roles[$index]) ? $this->roles[$index] : '_ignore'; | 
					
						
							|  |  |  |             $class = Config::get('csv.roles.' . $role . '.converter'); | 
					
						
							|  |  |  |             $field = Config::get('csv.roles.' . $role . '.field'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |             Log::debug('Column #' . $index . ' (role: ' . $role . ') : converter ' . $class . ' stores its data into field ' . $field . ':'); | 
					
						
							| 
									
										
										
										
											2015-07-09 16:07:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |             /** @var ConverterInterface $converter */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |             $converter = app('FireflyIII\Helpers\Csv\Converter\\' . $class); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |             $converter->setData($data); // the complete array so far.
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:26:34 +02:00
										 |  |  |             $converter->setField($field); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |             $converter->setIndex($index); | 
					
						
							| 
									
										
										
										
											2015-07-05 08:45:05 +02:00
										 |  |  |             $converter->setMapped($this->mapped); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |             $converter->setValue($value); | 
					
						
							|  |  |  |             $converter->setRole($role); | 
					
						
							|  |  |  |             $data[$field] = $converter->convert(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         // move to class vars.
 | 
					
						
							|  |  |  |         $this->importData = $data; | 
					
						
							|  |  |  |         $this->importRow  = $row; | 
					
						
							|  |  |  |         unset($data, $row); | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |         // post processing and validating.
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $this->postProcess(); | 
					
						
							|  |  |  |         $result = $this->validateData(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!($result === true)) { | 
					
						
							|  |  |  |             return $result; // return error.
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $journal = $this->createTransactionJournal(); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 15:39:41 +02:00
										 |  |  |         return $journal; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getFiller() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         $filler = []; | 
					
						
							|  |  |  |         foreach (Config::get('csv.roles') as $role) { | 
					
						
							| 
									
										
										
										
											2015-07-05 16:39:25 +02:00
										 |  |  |             if (isset($role['field'])) { | 
					
						
							|  |  |  |                 $fieldName          = $role['field']; | 
					
						
							|  |  |  |                 $filler[$fieldName] = null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         // some extra's:
 | 
					
						
							|  |  |  |         $filler['bill-id']                 = null; | 
					
						
							|  |  |  |         $filler['opposing-account-object'] = null; | 
					
						
							| 
									
										
										
										
											2015-07-09 16:37:42 +02:00
										 |  |  |         $filler['asset-account-object']    = null; | 
					
						
							| 
									
										
										
										
											2015-07-05 16:39:25 +02:00
										 |  |  |         $filler['amount-modifier']         = '1'; | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $filler; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-05 07:18:48 +02:00
										 |  |  |      * Row denotes the original data. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * @return void | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     protected function postProcess() | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-05 19:57:44 +02:00
										 |  |  |         // do bank specific fixes (must be enabled but now all of them.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  |         foreach ($this->getSpecifix() as $className) { | 
					
						
							| 
									
										
										
										
											2015-07-05 19:57:44 +02:00
										 |  |  |             /** @var SpecifixInterface $specifix */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |             $specifix = app('FireflyIII\Helpers\Csv\Specifix\\' . $className); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $specifix->setData($this->importData); | 
					
						
							|  |  |  |             $specifix->setRow($this->importRow); | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |             Log::debug('Now post-process specifix named ' . $className . ':'); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $this->importData = $specifix->fix(); | 
					
						
							| 
									
										
										
										
											2015-07-05 19:57:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 19:31:58 +02:00
										 |  |  |         $set = Config::get('csv.post_processors'); | 
					
						
							|  |  |  |         foreach ($set as $className) { | 
					
						
							|  |  |  |             /** @var PostProcessorInterface $postProcessor */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |             $postProcessor = app('FireflyIII\Helpers\Csv\PostProcessing\\' . $className); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $postProcessor->setData($this->importData); | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |             Log::debug('Now post-process processor named ' . $className . ':'); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $this->importData = $postProcessor->process(); | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getSpecifix() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-09 15:27:40 +02:00
										 |  |  |         return is_array($this->specifix) ? $this->specifix : []; | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     protected function validateData() | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if (is_null($this->importData['date']) && is_null($this->importData['date-rent'])) { | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |             return 'No date value for this row.'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if (is_null($this->importData['opposing-account-object'])) { | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |             return 'Opposing account is null'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |         if (!($this->importData['asset-account-object'] instanceof Account)) { | 
					
						
							|  |  |  |             return 'No asset account to import into.'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-06 16:33:54 +02:00
										 |  |  |      * @return TransactionJournal|string | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     protected function createTransactionJournal() | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         bcscale(2); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $date = $this->importData['date']; | 
					
						
							|  |  |  |         if (is_null($this->importData['date'])) { | 
					
						
							|  |  |  |             $date = $this->importData['date-rent']; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-09 15:36:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $transactionType = $this->getTransactionType(); // defaults to deposit
 | 
					
						
							|  |  |  |         $errors          = new MessageBag; | 
					
						
							|  |  |  |         $journal         = TransactionJournal::create( | 
					
						
							|  |  |  |             ['user_id'     => Auth::user()->id, 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $this->importData['currency']->id, | 
					
						
							|  |  |  |              'description' => $this->importData['description'], 'completed' => 0, 'date' => $date, 'bill_id' => $this->importData['bill-id'],] | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |         if ($journal->getErrors()->count() == 0) { | 
					
						
							| 
									
										
										
										
											2015-07-09 16:37:42 +02:00
										 |  |  |             // first transaction
 | 
					
						
							|  |  |  |             $accountId   = $this->importData['asset-account-object']->id; // create first transaction:
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $amount      = $this->importData['amount']; | 
					
						
							|  |  |  |             $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); | 
					
						
							|  |  |  |             $errors      = $transaction->getErrors(); | 
					
						
							| 
									
										
										
										
											2015-07-09 16:37:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // second transaction
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |             $accountId   = $this->importData['opposing-account-object']->id; // create second transaction:
 | 
					
						
							|  |  |  |             $amount      = bcmul($this->importData['amount'], -1); | 
					
						
							|  |  |  |             $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); | 
					
						
							| 
									
										
										
										
											2015-07-05 07:18:48 +02:00
										 |  |  |             $errors      = $transaction->getErrors()->merge($errors); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-05 07:18:48 +02:00
										 |  |  |         if ($errors->count() == 0) { | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |             $journal->completed = 1; | 
					
						
							|  |  |  |             $journal->save(); | 
					
						
							| 
									
										
										
										
											2015-07-06 10:39:44 +02:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $text = join(',', $errors->all()); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:42:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 10:39:44 +02:00
										 |  |  |             return $text; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         $this->saveBudget($journal); | 
					
						
							|  |  |  |         $this->saveCategory($journal); | 
					
						
							|  |  |  |         $this->saveTags($journal); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |         // some debug info:
 | 
					
						
							| 
									
										
										
										
											2015-07-09 21:48:05 +02:00
										 |  |  |         $journalId = $journal->id; | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |         $type      = $journal->getTransactionType(); | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  |         /** @var Account $asset */ | 
					
						
							|  |  |  |         $asset = $this->importData['asset-account-object']; | 
					
						
							|  |  |  |         /** @var Account $opposing */ | 
					
						
							|  |  |  |         $opposing = $this->importData['opposing-account-object']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 21:48:05 +02:00
										 |  |  |         Log::info('Created journal #' . $journalId . ' of type ' . $type . '!'); | 
					
						
							| 
									
										
										
										
											2015-07-10 20:25:17 +02:00
										 |  |  |         Log::info('Asset account ****** (#' . $asset->id . ') lost/gained: ' . $this->importData['amount']); | 
					
						
							|  |  |  |         Log::info($opposing->accountType->type . ' ****** (#' . $opposing->id . ') lost/gained: ' . bcmul($this->importData['amount'], -1)); | 
					
						
							| 
									
										
										
										
											2015-07-09 18:33:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         return $journal; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return TransactionType | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTransactionType() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |         $transactionType = TransactionType::where('type', TransactionType::DEPOSIT)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if ($this->importData['amount'] < 0) { | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |             $transactionType = TransactionType::where('type', TransactionType::WITHDRAWAL)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if (in_array($this->importData['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) { | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |             $transactionType = TransactionType::where('type', TransactionType::TRANSFER)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $transactionType; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function saveBudget(TransactionJournal $journal) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         // add budget:
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if (!is_null($this->importData['budget'])) { | 
					
						
							|  |  |  |             $journal->budgets()->save($this->importData['budget']); | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function saveCategory(TransactionJournal $journal) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         // add category:
 | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |         if (!is_null($this->importData['category'])) { | 
					
						
							|  |  |  |             $journal->categories()->save($this->importData['category']); | 
					
						
							| 
									
										
										
										
											2015-07-05 15:16:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function saveTags(TransactionJournal $journal) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!is_null($this->importData['tags'])) { | 
					
						
							|  |  |  |             foreach ($this->importData['tags'] as $tag) { | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |                 $journal->tags()->save($tag); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Data $data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setData($data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->data = $data; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 21:26:40 +02:00
										 |  |  | } |