diff --git a/app/Helpers/Csv/Importer.php b/app/Helpers/Csv/Importer.php index 83c9f1553b..ca2e2e2d1f 100644 --- a/app/Helpers/Csv/Importer.php +++ b/app/Helpers/Csv/Importer.php @@ -7,6 +7,7 @@ use Auth; use Config; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Csv\Converter\ConverterInterface; +use FireflyIII\Helpers\Csv\PostProcessing\PostProcessorInterface; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; @@ -171,33 +172,15 @@ class Importer */ protected function postProcess(array $data) { - // fix two simple fields: - bcscale(2); - $data['description'] = trim($data['description']); - $data['amount'] = bcmul($data['amount'], $data['amount-modifier']); - - if (strlen($data['description']) == 0) { - $data['description'] = trans('firefly.csv_empty_description'); + $set = Config::get('csv.post_processors'); + foreach ($set as $className) { + /** @var PostProcessorInterface $postProcessor */ + $postProcessor = App::make('FireflyIII\Helpers\Csv\PostProcessing\\' . $className); + $postProcessor->setData($data); + $data = $postProcessor->process(); } - // fix currency - if (is_null($data['currency'])) { - $currencyPreference = Preferences::get('currencyPreference', 'EUR'); - $data['currency'] = TransactionCurrency::whereCode($currencyPreference->data)->first(); - } - - // get bill id. - if (!is_null($data['bill'])) { - $data['bill-id'] = $data['bill']->id; - } - - // opposing account can be difficult. - - // get opposing account, which is quite complex: - $opposingAccount = new OpposingAccount($data); - $data['opposing-account-object'] = $opposingAccount->parse(); - - // do bank specific fixes: + // do bank specific fixes: TODO // $specifix = new Specifix(); // $specifix->setData($data); diff --git a/app/Helpers/Csv/PostProcessing/Amount.php b/app/Helpers/Csv/PostProcessing/Amount.php new file mode 100644 index 0000000000..c4e6f6e890 --- /dev/null +++ b/app/Helpers/Csv/PostProcessing/Amount.php @@ -0,0 +1,35 @@ +data['amount'] = bcmul($this->data['amount'], $this->data['amount-modifier']); + + return $this->data; + } + + /** + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/PostProcessing/Bill.php b/app/Helpers/Csv/PostProcessing/Bill.php new file mode 100644 index 0000000000..e21d8f6720 --- /dev/null +++ b/app/Helpers/Csv/PostProcessing/Bill.php @@ -0,0 +1,37 @@ +data['bill'])) { + $this->data['bill-id'] = $this->data['bill']->id; + } + + return $this->data; + } + + /** + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/PostProcessing/Currency.php b/app/Helpers/Csv/PostProcessing/Currency.php new file mode 100644 index 0000000000..66c77ef189 --- /dev/null +++ b/app/Helpers/Csv/PostProcessing/Currency.php @@ -0,0 +1,40 @@ +data['currency'])) { + $currencyPreference = Preferences::get('currencyPreference', 'EUR'); + $this->data['currency'] = TransactionCurrency::whereCode($currencyPreference->data)->first(); + } + + return $this->data; + } + + /** + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/PostProcessing/Description.php b/app/Helpers/Csv/PostProcessing/Description.php new file mode 100644 index 0000000000..2f2b019918 --- /dev/null +++ b/app/Helpers/Csv/PostProcessing/Description.php @@ -0,0 +1,38 @@ +data['description'] = trim($this->data['description']); + if (strlen($this->data['description']) == 0) { + $this->data['description'] = trans('firefly.csv_empty_description'); + } + + + return $this->data; + } + + /** + * @param array $data + */ + public function setData(array $data) + { + + $this->data = $data; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/OpposingAccount.php b/app/Helpers/Csv/PostProcessing/OpposingAccount.php similarity index 78% rename from app/Helpers/Csv/OpposingAccount.php rename to app/Helpers/Csv/PostProcessing/OpposingAccount.php index 021423aeca..1ffaafc721 100644 --- a/app/Helpers/Csv/OpposingAccount.php +++ b/app/Helpers/Csv/PostProcessing/OpposingAccount.php @@ -1,65 +1,74 @@ data = $data; - } - - /** - * @return \FireflyIII\Models\Account|null - */ - public function parse() + public function process() { // first priority. try to find the account based on ID, // if any. if ($this->data['opposing-account-id'] instanceof Account) { + $this->data['opposing-account-object'] = $this->data['opposing-account-id']; - return $this->data['opposing-account-id']; + return $this->data; } // second: try to find the account based on IBAN, if any. if ($this->data['opposing-account-iban'] instanceof Account) { - return $this->data['opposing-account-iban']; + $this->data['opposing-account-object'] = $this->data['opposing-account-iban']; + + return $this->data; } if (is_string($this->data['opposing-account-iban'])) { - return $this->parseIbanString(); + $this->data['opposing-account-object'] = $this->parseIbanString(); + + return $this->data; } // third: try to find account based on name, if any. if ($this->data['opposing-account-name'] instanceof Account) { - return $this->data['opposing-account-name']; + $this->data['opposing-account-object'] = $this->data['opposing-account-name']; + + return $this->data; } if (is_string($this->data['opposing-account-name'])) { - return $this->parseNameString(); + $this->data['opposing-account-object'] = $this->parseNameString(); + + return $this->data; } return null; - // if nothing, create expense/revenue, never asset. TODO + + } + + /** + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; } /** diff --git a/app/Helpers/Csv/PostProcessing/PostProcessorInterface.php b/app/Helpers/Csv/PostProcessing/PostProcessorInterface.php new file mode 100644 index 0000000000..dc2b04e64c --- /dev/null +++ b/app/Helpers/Csv/PostProcessing/PostProcessorInterface.php @@ -0,0 +1,28 @@ + [ + 'specifix' => [ + 'rabo_description' + ], + 'post_processors' => [ + 'OpposingAccount', + 'Description', + 'Amount', + 'Currency', + 'Bill' + + ], + 'roles' => [ '_ignore' => [ 'name' => '(ignore this column)', 'mappable' => false,