From 601f9f86bb993178cd3c4c76a55c9f954febe9f8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 5 Jul 2015 15:16:44 +0200 Subject: [PATCH] Some more importers. --- app/Helpers/Csv/Converter/BudgetId.php | 29 +++++++++++ app/Helpers/Csv/Converter/BudgetName.php | 35 +++++++++++++ app/Helpers/Csv/Converter/CategoryId.php | 29 +++++++++++ app/Helpers/Csv/Converter/CategoryName.php | 34 ++++++++++++ app/Helpers/Csv/Converter/CurrencyId.php | 28 ++++++++++ app/Helpers/Csv/Converter/CurrencyName.php | 27 ++++++++++ app/Helpers/Csv/Converter/CurrencySymbol.php | 28 ++++++++++ app/Helpers/Csv/Converter/OpposingName.php | 2 +- app/Helpers/Csv/Importer.php | 55 +++++++++++++++----- config/csv.php | 51 +++++++++++------- 10 files changed, 285 insertions(+), 33 deletions(-) create mode 100644 app/Helpers/Csv/Converter/BudgetId.php create mode 100644 app/Helpers/Csv/Converter/BudgetName.php create mode 100644 app/Helpers/Csv/Converter/CategoryId.php create mode 100644 app/Helpers/Csv/Converter/CategoryName.php create mode 100644 app/Helpers/Csv/Converter/CurrencyId.php create mode 100644 app/Helpers/Csv/Converter/CurrencyName.php create mode 100644 app/Helpers/Csv/Converter/CurrencySymbol.php diff --git a/app/Helpers/Csv/Converter/BudgetId.php b/app/Helpers/Csv/Converter/BudgetId.php new file mode 100644 index 0000000000..daaec82688 --- /dev/null +++ b/app/Helpers/Csv/Converter/BudgetId.php @@ -0,0 +1,29 @@ +mapped[$this->index][$this->value])) { + $budget = Auth::user()->budgets()->find($this->mapped[$this->index][$this->value]); + } else { + $budget = Auth::user()->budgets()->find($this->value); + } + + return $budget; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/BudgetName.php b/app/Helpers/Csv/Converter/BudgetName.php new file mode 100644 index 0000000000..26ff92a1ec --- /dev/null +++ b/app/Helpers/Csv/Converter/BudgetName.php @@ -0,0 +1,35 @@ +mapped[$this->index][$this->value])) { + $budget = Auth::user()->budgets()->find($this->mapped[$this->index][$this->value]); + } else { + $budget = Budget::firstOrCreateEncrypted( + [ + 'name' => $this->value, + 'user_id' => Auth::user()->id, + 'active' => true, + ] + ); + } + + return $budget; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/CategoryId.php b/app/Helpers/Csv/Converter/CategoryId.php new file mode 100644 index 0000000000..9b6b089d37 --- /dev/null +++ b/app/Helpers/Csv/Converter/CategoryId.php @@ -0,0 +1,29 @@ +mapped[$this->index][$this->value])) { + $category = Auth::user()->categories()->find($this->mapped[$this->index][$this->value]); + } else { + $category = Auth::user()->categories()->find($this->value); + } + + return $category; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/CategoryName.php b/app/Helpers/Csv/Converter/CategoryName.php new file mode 100644 index 0000000000..5a35ecbcb9 --- /dev/null +++ b/app/Helpers/Csv/Converter/CategoryName.php @@ -0,0 +1,34 @@ +mapped[$this->index][$this->value])) { + $category = Auth::user()->categories()->find($this->mapped[$this->index][$this->value]); + } else { + $category = Category::firstOrCreateEncrypted( + [ + 'name' => $this->value, + 'user_id' => Auth::user()->id + ] + ); + } + + return $category; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/CurrencyId.php b/app/Helpers/Csv/Converter/CurrencyId.php new file mode 100644 index 0000000000..1544acae83 --- /dev/null +++ b/app/Helpers/Csv/Converter/CurrencyId.php @@ -0,0 +1,28 @@ +mapped[$this->index][$this->value])) { + $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]); + } else { + $currency = TransactionCurrency::find($this->value); + } + + return $currency; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/CurrencyName.php b/app/Helpers/Csv/Converter/CurrencyName.php new file mode 100644 index 0000000000..8788de2397 --- /dev/null +++ b/app/Helpers/Csv/Converter/CurrencyName.php @@ -0,0 +1,27 @@ +mapped[$this->index][$this->value])) { + $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]); + } else { + $currency = TransactionCurrency::whereName($this->value)->first(); + } + + return $currency; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/CurrencySymbol.php b/app/Helpers/Csv/Converter/CurrencySymbol.php new file mode 100644 index 0000000000..957674751f --- /dev/null +++ b/app/Helpers/Csv/Converter/CurrencySymbol.php @@ -0,0 +1,28 @@ +mapped[$this->index][$this->value])) { + $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]); + } else { + $currency = TransactionCurrency::whereSymbol($this->value)->first(); + } + + return $currency; + } +} \ No newline at end of file diff --git a/app/Helpers/Csv/Converter/OpposingName.php b/app/Helpers/Csv/Converter/OpposingName.php index 25ab7b00be..6a0662db29 100644 --- a/app/Helpers/Csv/Converter/OpposingName.php +++ b/app/Helpers/Csv/Converter/OpposingName.php @@ -11,7 +11,7 @@ class OpposingName extends BasicConverter implements ConverterInterface { /** - * This method cannot search yet for the correct account (Expense account or Revenue account) because simply put, + * This method cannot search yet for the correct account (Expense account, Revenue account or Asset account) because simply put, * Firefly doesn't know yet if this account needs to be an Expense account or a Revenue account. This depends * on the amount which is in the current row and that's a big unknown. * diff --git a/app/Helpers/Csv/Importer.php b/app/Helpers/Csv/Importer.php index 65cdceb4e9..b179a9acd0 100644 --- a/app/Helpers/Csv/Importer.php +++ b/app/Helpers/Csv/Importer.php @@ -148,20 +148,16 @@ class Importer */ protected function getFiller() { - return [ - 'description' => '', - 'asset-account' => null, - 'opposing-account' => '', - 'opposing-account-object' => null, - 'date' => null, - 'currency' => null, - 'amount' => null, - 'amount-modifier' => 1, - 'ignored' => null, - 'date-rent' => null, - 'bill' => null, - 'bill-id' => null, - ]; + $filler = []; + foreach (Config::get('csv.roles') as $role) { + $fieldName = $role['field']; + $filler[$fieldName] = null; + } + // some extra's: + $filler['bill-id'] = null; + $filler['opposing-account-object'] = null; + + return $filler; } @@ -178,6 +174,11 @@ class Importer bcscale(2); $data['description'] = trim($data['description']); $data['amount'] = bcmul($data['amount'], $data['amount-modifier']); + + // get opposing account, which is quite complex: + $data['opposing-account-object'] = $this->processOpposingAccount($data); + + // opposing account type: if ($data['amount'] < 0) { // create expense account: $accountType = AccountType::where('type', 'Expense account')->first(); @@ -220,6 +221,22 @@ class Importer return $data; } + /** + * @param array $data + */ + public function processOpposingAccount(array $data) + { + // first priority. try to find the account based on ID, + // if any. + + // second: try to find the account based on IBAN, if any. + + // third: try to find account based on name, if any. + + // if nothing, create expense/revenue, never asset. + + } + /** * @param $data * @@ -295,6 +312,16 @@ class Importer $journal->save(); } + // add budget: + if (!is_null($data['budget'])) { + $journal->budgets()->save($data['budget']); + } + + // add category: + if (!is_null($data['category'])) { + $journal->categories()->save($data['category']); + } + return $journal; diff --git a/config/csv.php b/config/csv.php index f5ebdad7fa..6164cf280c 100644 --- a/config/csv.php +++ b/config/csv.php @@ -20,12 +20,16 @@ return [ 'field' => 'bill', ], 'currency-id' => [ - 'name' => 'Currency ID (matching Firefly)', - 'mappable' => true, + 'name' => 'Currency ID (matching Firefly)', + 'mappable' => true, + 'converter' => 'CurrencyId', + 'field' => 'currency', ], 'currency-name' => [ - 'name' => 'Currency name (matching Firefly)', - 'mappable' => true, + 'name' => 'Currency name (matching Firefly)', + 'mappable' => true, + 'converter' => 'CurrencyName', + 'field' => 'currency', ], 'currency-code' => [ 'name' => 'Currency code (ISO 4217)', @@ -35,8 +39,10 @@ return [ 'mapper' => 'TransactionCurrency' ], 'currency-symbol' => [ - 'name' => 'Currency symbol (matching Firefly)', - 'mappable' => true, + 'name' => 'Currency symbol (matching Firefly)', + 'mappable' => true, + 'converter' => 'CurrencySymbol', + 'field' => 'currency', ], 'description' => [ 'name' => 'Description', @@ -57,12 +63,16 @@ return [ 'field' => 'date-rent', ], 'budget-id' => [ - 'name' => 'Budget ID (matching Firefly)', - 'mappable' => true, + 'name' => 'Budget ID (matching Firefly)', + 'mappable' => true, + 'converter' => 'BudgetId', + 'field' => 'budget' ], 'budget-name' => [ - 'name' => 'Budget name', - 'mappable' => true, + 'name' => 'Budget name', + 'mappable' => true, + 'converter' => 'BudgetName', + 'field' => 'budget' ], 'rabo-debet-credit' => [ 'name' => 'Rabobank specific debet/credit indicator', @@ -71,12 +81,16 @@ return [ 'field' => 'amount-modifier', ], 'category-id' => [ - 'name' => 'Category ID (matching Firefly)', - 'mappable' => true, + 'name' => 'Category ID (matching Firefly)', + 'mappable' => true, + 'converter' => 'CategoryId', + 'field' => 'category' ], 'category-name' => [ - 'name' => 'Category name', - 'mappable' => true, + 'name' => 'Category name', + 'mappable' => true, + 'converter' => 'CategoryName', + 'field' => 'category' ], 'tags-comma' => [ 'name' => 'Tags (comma separated)', @@ -110,16 +124,17 @@ return [ 'opposing-id' => [ 'name' => 'Opposing account account ID (matching Firefly)', 'mappable' => true, + 'field' => 'opposing-account-id', ], 'opposing-name' => [ - 'name' => 'Opposing account name', - 'mappable' => true, - 'converter' => 'OpposingName', - 'field' => 'opposing-account' + 'name' => 'Opposing account name', + 'mappable' => true, + 'field' => 'opposing-account-name', ], 'opposing-iban' => [ 'name' => 'Opposing account IBAN', 'mappable' => true, + 'field' => 'opposing-account-iban', ], 'amount' => [ 'name' => 'Amount',