Skip to content

Firefly III: a personal finances manager

Massively complex but working never the less.

James Cole12ee5da87250
diff --git a/app/Helpers/Csv/Converter/OpposingAccountIban.php b/app/Helpers/Csv/Converter/OpposingAccountIban.php
new file mode 100644
index 0000000000..cbd4b70a73
--- /dev/null
+++ b/app/Helpers/Csv/Converter/OpposingAccountIban.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace FireflyIII\Helpers\Csv\Converter;
+
+use Auth;
+use FireflyIII\Models\Account;
+
+/**
+ * Class OpposingAccountIban
+ *
+ * @package FireflyIII\Helpers\Csv\Converter
+ */
+class OpposingAccountIban extends BasicConverter implements ConverterInterface
+{
+
+ /**
+ * If mapped, return account. Otherwise, only return the name itself.
+ *
+ * @return Account|string
+ */
+ public function convert()
+ {
+ if (isset($this->mapped[$this->index][$this->value])) {
+ $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
+
+ return $account;
+ } else {
+ $set = Auth::user()->accounts()->get();
+ /** @var Account $account */
+ foreach ($set as $account) {
+ if ($account->iban == $this->value) {
+ return $account;
+ }
+ }
+
+ return $this->value;
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Helpers/Csv/Converter/OpposingAccountId.php b/app/Helpers/Csv/Converter/OpposingAccountId.php
new file mode 100644
index 0000000000..8d14ab5e75
--- /dev/null
+++ b/app/Helpers/Csv/Converter/OpposingAccountId.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace FireflyIII\Helpers\Csv\Converter;
+
+use Auth;
+use FireflyIII\Models\Account;
+
+/**
+ * Class OpposingName
+ *
+ * @package FireflyIII\Helpers\Csv\Converter
+ */
+class OpposingAccountId extends BasicConverter implements ConverterInterface
+{
+
+
+ /**
+ * @return Account
+ */
+ public function convert()
+ {
+ if (isset($this->mapped[$this->index][$this->value])) {
+ $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
+
+ } else {
+ $account = Auth::user()->accounts()->find($this->value);
+ }
+
+ return $account;
+
+ }
+}
\ No newline at end of file
diff --git a/app/Helpers/Csv/Converter/OpposingAccountName.php b/app/Helpers/Csv/Converter/OpposingAccountName.php
new file mode 100644
index 0000000000..835ae0ad7c
--- /dev/null
+++ b/app/Helpers/Csv/Converter/OpposingAccountName.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace FireflyIII\Helpers\Csv\Converter;
+
+use Auth;
+use FireflyIII\Models\Account;
+
+/**
+ * Class OpposingName
+ *
+ * @package FireflyIII\Helpers\Csv\Converter
+ */
+class OpposingAccountName extends BasicConverter implements ConverterInterface
+{
+
+ /**
+ * If mapped, return account. Otherwise, only return the name itself.
+ *
+ * @return Account|string
+ */
+ public function convert()
+ {
+ if (isset($this->mapped[$this->index][$this->value])) {
+ $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
+
+ return $account;
+ } else {
+ return $this->value;
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/Helpers/Csv/Converter/OpposingName.php b/app/Helpers/Csv/Converter/OpposingName.php
deleted file mode 100644
index 6a0662db29..0000000000
--- a/app/Helpers/Csv/Converter/OpposingName.php
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-namespace FireflyIII\Helpers\Csv\Converter;
-
-/**
- * Class OpposingName
- *
- * @package FireflyIII\Helpers\Csv\Converter
- */
-class OpposingName extends BasicConverter implements ConverterInterface
-{
-
- /**
- * 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.
- *
- * @return mixed
- */
- public function convert()
- {
- return $this->value;
- }
-}
\ No newline at end of file
diff --git a/app/Helpers/Csv/Importer.php b/app/Helpers/Csv/Importer.php
index b179a9acd0..df16ab374d 100644
--- a/app/Helpers/Csv/Importer.php
+++ b/app/Helpers/Csv/Importer.php
@@ -150,12 +150,15 @@ class Importer
{
$filler = [];
foreach (Config::get('csv.roles') as $role) {
- $fieldName = $role['field'];
- $filler[$fieldName] = null;
+ if (isset($role['field'])) {
+ $fieldName = $role['field'];
+ $filler[$fieldName] = null;
+ }
}
// some extra's:
$filler['bill-id'] = null;
$filler['opposing-account-object'] = null;
+ $filler['amount-modifier'] = '1';
return $filler;
@@ -175,18 +178,10 @@ class Importer
$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();
- } else {
- // create revenue account:
- $accountType = AccountType::where('type', 'Revenue account')->first();
- }
-
if (strlen($data['description']) == 0) {
$data['description'] = trans('firefly.csv_empty_description');
}
@@ -205,38 +200,103 @@ class Importer
$specifix->setData($data);
$specifix->setRow($row);
//$specifix->fix($data, $row); // TODO
-
// get data back:
//$data = $specifix->getData(); // TODO
- $data['opposing-account-object'] = Account::firstOrCreateEncrypted(
- [
- 'user_id' => Auth::user()->id,
- 'name' => ucwords($data['opposing-account']),
- 'account_type_id' => $accountType->id,
- 'active' => 1,
- ]
- );
-
return $data;
}
/**
* @param array $data
*/
- public function processOpposingAccount(array $data)
+ protected function processOpposingAccount(array $data)
{
// first priority. try to find the account based on ID,
// if any.
+ if ($data['opposing-account-id'] instanceof Account) {
+ return $data['opposing-account-id'];
+ }
// second: try to find the account based on IBAN, if any.
+ if ($data['opposing-account-iban'] instanceof Account) {
+ return $data['opposing-account-iban'];
+ }
+
+ $accountType = $this->getAccountType($data['amount']);
+
+ if (is_string($data['opposing-account-iban'])) {
+ $accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
+ foreach ($accounts as $entry) {
+ if ($entry->iban == $data['opposing-account-iban']) {
+
+ //return $entry;
+ }
+ }
+ // create if not exists:
+ $account = Account::firstOrCreateEncrypted(
+ [
+ 'user_id' => Auth::user()->id,
+ 'account_type_id' => $accountType->id,
+ 'name' => $data['opposing-account-iban'],
+ 'iban' => $data['opposing-account-iban'],
+ 'active' => true,
+ ]
+ );
+
+ return $account;
+
+ }
// third: try to find account based on name, if any.
+ if ($data['opposing-account-name'] instanceof Account) {
+ return $data['opposing-account-name'];
+ }
- // if nothing, create expense/revenue, never asset.
+ if (is_string($data['opposing-account-name'])) {
+ $accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
+ foreach ($accounts as $entry) {
+ if ($entry->name == $data['opposing-account-name']) {
+ return $entry;
+ }
+ }
+ // create if not exists:
+ $account = Account::firstOrCreateEncrypted(
+ [
+ 'user_id' => Auth::user()->id,
+ 'account_type_id' => $accountType->id,
+ 'name' => $data['opposing-account-name'],
+ 'iban' => '',
+ 'active' => true,
+ ]
+ );
+
+ return $account;
+ }
+ return null;
+
+ // if nothing, create expense/revenue, never asset. TODO
}
+ /**
+ * @param $amount
+ *
+ * @return AccountType
+ */
+ protected function getAccountType($amount)
+ {
+ // opposing account type:
+ if ($amount < 0) {
+ // create expense account:
+
+ return AccountType::where('type', 'Expense account')->first();
+ } else {
+ // create revenue account:
+
+ return AccountType::where('type', 'Revenue account')->first();
+ }
+ }
+
/**
* @param $data
*
@@ -247,9 +307,6 @@ class Importer
if (is_null($data['date']) && is_null($data['date-rent'])) {
return 'No date value for this row.';
}
- if (strlen($data['description']) == 0) {
- return 'No valid description';
- }
if (is_null($data['opposing-account-object'])) {
return 'Opposing account is null';
}
diff --git a/config/csv.php b/config/csv.php
index 6164cf280c..1fcd459bc6 100644
--- a/config/csv.php
+++ b/config/csv.php
@@ -122,19 +122,22 @@ return [
'mapper' => 'AssetAccount'
],
'opposing-id' => [
- 'name' => 'Opposing account account ID (matching Firefly)',
- 'mappable' => true,
- 'field' => 'opposing-account-id',
+ 'name' => 'Opposing account account ID (matching Firefly)',
+ 'mappable' => true,
+ 'field' => 'opposing-account-id',
+ 'converter' => 'OpposingAccountId'
],
'opposing-name' => [
'name' => 'Opposing account name',
'mappable' => true,
'field' => 'opposing-account-name',
+ 'converter' => 'OpposingAccountName'
],
'opposing-iban' => [
'name' => 'Opposing account IBAN',
'mappable' => true,
'field' => 'opposing-account-iban',
+ 'converter' => 'OpposingAccountIban'
],
'amount' => [
'name' => 'Amount',