| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  | use FireflyIII\Models\Rule; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleGroup; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  | use FireflyIII\Rules\Processor; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** @var  Collection */ | 
					
						
							|  |  |  |     protected $journals; | 
					
						
							| 
									
										
										
										
											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-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function getErrors(): array | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->errors; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function getImported(): int | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->imported; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function getJournals(): Collection | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->journals; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-06 20:56:20 +02:00
										 |  |  |      * Used by CsvController | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function getRows(): int | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->rows; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function getSpecifix(): array | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return is_array($this->specifix) ? $this->specifix : []; | 
					
						
							| 
									
										
										
										
											2015-07-09 18:38:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2016-04-03 10:34:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                      | 
					
						
							| 
									
										
										
										
											2015-07-05 14:37:36 +02:00
										 |  |  |                     $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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // once all journals have been imported (or not)
 | 
					
						
							|  |  |  |         // fire the rules.
 | 
					
						
							|  |  |  |         $this->fireRules(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @param Data $data | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     public function setData(Data $data) | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->data = $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |      * FIXME can return text | 
					
						
							|  |  |  |      *  | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @return TransactionJournal|string | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     protected function createTransactionJournal(): TransactionJournal | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         $date = $this->importData['date']; | 
					
						
							|  |  |  |         if (is_null($this->importData['date'])) { | 
					
						
							|  |  |  |             $date = $this->importData['date-rent']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $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'],] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         if ($journal->getErrors()->count() == 0) { | 
					
						
							|  |  |  |             // first transaction
 | 
					
						
							|  |  |  |             $accountId   = $this->importData['asset-account-object']->id; // create first transaction:
 | 
					
						
							|  |  |  |             $amount      = $this->importData['amount']; | 
					
						
							|  |  |  |             $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); | 
					
						
							|  |  |  |             $errors      = $transaction->getErrors(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // second transaction
 | 
					
						
							|  |  |  |             $accountId   = $this->importData['opposing-account-object']->id; // create second transaction:
 | 
					
						
							| 
									
										
										
										
											2016-02-11 08:11:12 +01:00
										 |  |  |             $amount      = bcmul($this->importData['amount'], '-1'); | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |             $transaction = Transaction::create(['transaction_journal_id' => $journal->id, 'account_id' => $accountId, 'amount' => $amount]); | 
					
						
							|  |  |  |             $errors      = $transaction->getErrors()->merge($errors); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($errors->count() == 0) { | 
					
						
							|  |  |  |             $journal->completed = 1; | 
					
						
							|  |  |  |             $journal->save(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $text = join(',', $errors->all()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $text; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->saveBudget($journal); | 
					
						
							|  |  |  |         $this->saveCategory($journal); | 
					
						
							|  |  |  |         $this->saveTags($journal); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // some debug info:
 | 
					
						
							|  |  |  |         $journalId = $journal->id; | 
					
						
							| 
									
										
										
										
											2016-03-02 13:37:28 +01:00
										 |  |  |         $type      = $journal->transaction_type_type ?? $journal->transactionType->type; | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         /** @var Account $asset */ | 
					
						
							|  |  |  |         $asset = $this->importData['asset-account-object']; | 
					
						
							|  |  |  |         /** @var Account $opposing */ | 
					
						
							|  |  |  |         $opposing = $this->importData['opposing-account-object']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::info('Created journal #' . $journalId . ' of type ' . $type . '!'); | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  |         Log::info('Asset account #' . $asset->id . ' lost/gained: ' . $this->importData['amount']); | 
					
						
							| 
									
										
										
										
											2016-02-11 08:11:12 +01:00
										 |  |  |         Log::info($opposing->accountType->type . ' #' . $opposing->id . ' lost/gained: ' . bcmul($this->importData['amount'], '-1')); | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $journal; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return TransactionType | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTransactionType() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $transactionType = TransactionType::where('type', TransactionType::DEPOSIT)->first(); | 
					
						
							|  |  |  |         if ($this->importData['amount'] < 0) { | 
					
						
							|  |  |  |             $transactionType = TransactionType::where('type', TransactionType::WITHDRAWAL)->first(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (in_array($this->importData['opposing-account-object']->accountType->type, ['Asset account', 'Default account'])) { | 
					
						
							|  |  |  |             $transactionType = TransactionType::where('type', TransactionType::TRANSFER)->first(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $transactionType; | 
					
						
							| 
									
										
										
										
											2015-07-05 18:18:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |      * @param array $row | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-07-05 06:59:05 +02:00
										 |  |  |      * @return string|bool | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     protected function importRow(array $row) | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2016-02-05 08:03:26 +01:00
										 |  |  |             $role  = $this->roles[$index] ?? '_ignore'; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |             $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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 21:24:27 +01:00
										 |  |  |             // here would be the place where preprocessors would fire.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |             $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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * @param int $index | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     protected function parseRow(int $index) | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |         return (($this->data->hasHeaders() && $index >= 1) || !$this->data->hasHeaders()); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2016-01-20 21:24:27 +01:00
										 |  |  |             if ($specifix->getProcessorType() == SpecifixInterface::POST_PROCESSOR) { | 
					
						
							|  |  |  |                 $specifix->setData($this->importData); | 
					
						
							|  |  |  |                 $specifix->setRow($this->importRow); | 
					
						
							|  |  |  |                 Log::debug('Now post-process specifix named ' . $className . ':'); | 
					
						
							|  |  |  |                 $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); | 
					
						
							| 
									
										
										
										
											2016-02-11 08:11:12 +01:00
										 |  |  |             $array         = $this->importData ?? []; | 
					
						
							|  |  |  |             $postProcessor->setData($array); | 
					
						
							| 
									
										
										
										
											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:56:20 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @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
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool|string | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |     protected function validateData() | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-11 08:11:12 +01:00
										 |  |  |         $date     = $this->importData['date'] ?? null; | 
					
						
							|  |  |  |         $rentDate = $this->importData['date-rent'] ?? null; | 
					
						
							|  |  |  |         if (is_null($date) && is_null($rentDate)) { | 
					
						
							| 
									
										
										
										
											2016-01-19 13:59:54 +01:00
										 |  |  |             return 'No date value for this row.'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (is_null($this->importData['opposing-account-object'])) { | 
					
						
							|  |  |  |             return 'Opposing account is null'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!($this->importData['asset-account-object'] instanceof Account)) { | 
					
						
							|  |  |  |             return 'No asset account to import into.'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-07-05 06:18:02 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-06 20:21:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Collection         $groups | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function fireRule(Collection $groups, TransactionJournal $journal) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var RuleGroup $group */ | 
					
						
							|  |  |  |         foreach ($groups as $group) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /** @var Rule $rule */ | 
					
						
							|  |  |  |             foreach ($group->rules as $rule) { | 
					
						
							| 
									
										
										
										
											2016-02-17 21:14:32 +01:00
										 |  |  |                 $processor = Processor::make($rule); | 
					
						
							|  |  |  |                 $processor->handleTransactionJournal($journal); | 
					
						
							| 
									
										
										
										
											2016-01-30 07:36:22 +01:00
										 |  |  |                 if ($rule->stop_processing) { | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function fireRules() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // get all users rules.
 | 
					
						
							|  |  |  |         /** @var User $user */ | 
					
						
							|  |  |  |         $user   = Auth::user(); | 
					
						
							|  |  |  |         $groups = $user | 
					
						
							|  |  |  |             ->ruleGroups() | 
					
						
							|  |  |  |             ->where('rule_groups.active', 1) | 
					
						
							|  |  |  |             ->orderBy('order', 'ASC') | 
					
						
							|  |  |  |             ->with( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'rules' => function (HasMany $q) { | 
					
						
							|  |  |  |                         $q->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id') | 
					
						
							|  |  |  |                           ->where('rule_triggers.trigger_type', 'user_action') | 
					
						
							|  |  |  |                           ->where('rule_triggers.trigger_value', 'store-journal') | 
					
						
							|  |  |  |                           ->where('rules.active', 1) | 
					
						
							|  |  |  |                           ->orderBy('rules.order', 'ASC'); | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             ->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($this->journals as $journal) { | 
					
						
							|  |  |  |             $this->fireRule($groups, $journal); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getFiller() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $filler = []; | 
					
						
							|  |  |  |         foreach (Config::get('csv.roles') as $role) { | 
					
						
							|  |  |  |             if (isset($role['field'])) { | 
					
						
							|  |  |  |                 $fieldName          = $role['field']; | 
					
						
							|  |  |  |                 $filler[$fieldName] = null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // some extra's:
 | 
					
						
							|  |  |  |         $filler['bill-id']                 = null; | 
					
						
							|  |  |  |         $filler['opposing-account-object'] = null; | 
					
						
							|  |  |  |         $filler['asset-account-object']    = null; | 
					
						
							|  |  |  |         $filler['amount-modifier']         = '1'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $filler; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 21:26:40 +02:00
										 |  |  | } |