Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Improving bunq import.
James Cole1319cb2e4e58
diff --git a/app/Http/Controllers/Import/BankController.php b/app/Http/Controllers/Import/BankController.phpindex d8a0279063..1e853620b5 100644--- a/app/Http/Controllers/Import/BankController.php+++ b/app/Http/Controllers/Import/BankController.php@@ -21,6 +21,7 @@ use Session;class BankController extends Controller{+/*** This method must ask the user all parameters necessary to start importing data. This may not be enough* to finish the import itself (ie. mapping) but it should be enough to begin: accounts to import from,@@ -42,6 +43,49 @@ class BankController extends Controller$object->setUser(auth()->user());$remoteAccounts = $object->getAccounts();+ return view('import.bank.form', compact('remoteAccounts', 'bank'));++ }++ /**+ * With the information given in the submitted form Firefly III will call upon the bank's classes to return transaction+ * information as requested. The user will be able to map unknown data and continue. Or maybe, it's put into some kind of+ * fake CSV file and forwarded to the import routine.+ *+ * @param Request $request+ * @param string $bank+ *+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector+ */+ public function postForm(Request $request, string $bank)+ {++ $class = config(sprintf('firefly.import_pre.%s', $bank));+ /** @var PrerequisitesInterface $object */+ $object = app($class);+ $object->setUser(auth()->user());++ if ($object->hasPrerequisites()) {+ return redirect(route('import.bank.prerequisites', [$bank]));+ }+ $remoteAccounts = $request->get('do_import');+ if (!is_array($remoteAccounts) || count($remoteAccounts) === 0) {+ Session::flash('error', 'Must select accounts');++ return redirect(route('import.bank.form', [$bank]));+ }+ $remoteAccounts = array_keys($remoteAccounts);++ $class = config(sprintf('firefly.import_pre.%s', $bank));++ var_dump($remoteAccounts);exit;++ // get import file++ // get import config+++}/**@@ -66,6 +110,7 @@ class BankController extends Controller$object->setUser(auth()->user());if (!$object->hasPrerequisites()) {Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));+return redirect(route('import.bank.form', [$bank]));}Log::debug('Going to store entered preprerequisites.');@@ -102,7 +147,8 @@ class BankController extends Controllerreturn view($view, $parameters);}- return redirect(route('import.bank.form', [$bank]));++ return redirect(route('import.bank.form', [$bank]));}}diff --git a/app/Services/Bunq/Object/Alias.php b/app/Services/Bunq/Object/Alias.phpindex 7d1703eabc..7b0ed84ea3 100644--- a/app/Services/Bunq/Object/Alias.php+++ b/app/Services/Bunq/Object/Alias.php@@ -65,5 +65,4 @@ class Alias extends BunqObject}-}\ No newline at end of filediff --git a/app/Services/Bunq/Object/Amount.php b/app/Services/Bunq/Object/Amount.phpindex 857e85f304..cd7782a0ad 100644--- a/app/Services/Bunq/Object/Amount.php+++ b/app/Services/Bunq/Object/Amount.php@@ -54,5 +54,4 @@ class Amount extends BunqObject}-}\ No newline at end of filediff --git a/app/Services/Bunq/Object/MonetaryAccountBank.php b/app/Services/Bunq/Object/MonetaryAccountBank.phpindex 0295983581..5082c858e0 100644--- a/app/Services/Bunq/Object/MonetaryAccountBank.php+++ b/app/Services/Bunq/Object/MonetaryAccountBank.php@@ -62,56 +62,6 @@ class MonetaryAccountBank extends BunqObject/** @var int */private $userId = 0;- /**- * @return string- */- public function getDescription(): string- {- return $this->description;- }-- /**- * @return int- */- public function getId(): int- {- return $this->id;- }-- /**- * @return array- */- public function getAliases(): array- {- return $this->aliases;- }-- /**- * @return string- */- public function getCurrency(): string- {- return $this->currency;- }-- /**- * @return Amount- */- public function getBalance(): Amount- {- return $this->balance;- }-- /**- * @return MonetaryAccountSetting- */- public function getSetting(): MonetaryAccountSetting- {- return $this->setting;- }---/*** MonetaryAccountBank constructor.*@@ -150,4 +100,52 @@ class MonetaryAccountBank extends BunqObjectreturn;}+ /**+ * @return array+ */+ public function getAliases(): array+ {+ return $this->aliases;+ }++ /**+ * @return Amount+ */+ public function getBalance(): Amount+ {+ return $this->balance;+ }++ /**+ * @return string+ */+ public function getCurrency(): string+ {+ return $this->currency;+ }++ /**+ * @return string+ */+ public function getDescription(): string+ {+ return $this->description;+ }++ /**+ * @return int+ */+ public function getId(): int+ {+ return $this->id;+ }++ /**+ * @return MonetaryAccountSetting+ */+ public function getSetting(): MonetaryAccountSetting+ {+ return $this->setting;+ }+}\ No newline at end of filediff --git a/app/Services/Bunq/Object/MonetaryAccountProfile.php b/app/Services/Bunq/Object/MonetaryAccountProfile.phpindex e4995ea19c..a19d3403ee 100644--- a/app/Services/Bunq/Object/MonetaryAccountProfile.php+++ b/app/Services/Bunq/Object/MonetaryAccountProfile.php@@ -37,6 +37,7 @@ class MonetaryAccountProfile extends BunqObject$this->profileFill = null;$this->profileActionRequired = $data['profile_action_required'];$this->profileAmountRequired = new Amount($data['profile_amount_required']);+return;}diff --git a/app/Services/Bunq/Object/MonetaryAccountSetting.php b/app/Services/Bunq/Object/MonetaryAccountSetting.phpindex e1277c3dbc..38fe8db75d 100644--- a/app/Services/Bunq/Object/MonetaryAccountSetting.php+++ b/app/Services/Bunq/Object/MonetaryAccountSetting.php@@ -36,6 +36,7 @@ class MonetaryAccountSetting extends BunqObject$this->color = $data['color'];$this->defaultAvatarStatus = $data['default_avatar_status'];$this->restrictionChat = $data['restriction_chat'];+return;}diff --git a/app/Services/Bunq/Object/NotificationFilter.php b/app/Services/Bunq/Object/NotificationFilter.phpindex f12843c159..06bf96b01e 100644--- a/app/Services/Bunq/Object/NotificationFilter.php+++ b/app/Services/Bunq/Object/NotificationFilter.php@@ -24,7 +24,8 @@ class NotificationFilter extends BunqObject** @param array $data*/- public function __construct(array $data) {+ public function __construct(array $data)+ {var_dump($data);exit;}diff --git a/app/Services/Bunq/Object/ServerPublicKey.php b/app/Services/Bunq/Object/ServerPublicKey.phpindex 78426d34d1..56aa17654b 100644--- a/app/Services/Bunq/Object/ServerPublicKey.php+++ b/app/Services/Bunq/Object/ServerPublicKey.php@@ -27,24 +27,25 @@ class ServerPublicKey extends BunqObject** @param array $response*/- public function __construct(array $response) {+ public function __construct(array $response)+ {$this->publicKey = $response['server_public_key'];}/**- * @param string $publicKey+ * @return string*/- public function setPublicKey(string $publicKey)+ public function getPublicKey(): string{- $this->publicKey = $publicKey;+ return $this->publicKey;}/**- * @return string+ * @param string $publicKey*/- public function getPublicKey(): string+ public function setPublicKey(string $publicKey){- return $this->publicKey;+ $this->publicKey = $publicKey;}diff --git a/app/Services/Bunq/Object/UserCompany.php b/app/Services/Bunq/Object/UserCompany.phpindex 57c250773a..a0efcaf876 100644--- a/app/Services/Bunq/Object/UserCompany.php+++ b/app/Services/Bunq/Object/UserCompany.php@@ -103,5 +103,4 @@ class UserCompany extends BunqObject}-}\ No newline at end of filediff --git a/app/Services/Bunq/Object/UserLight.php b/app/Services/Bunq/Object/UserLight.phpindex fe47df54a7..baed9d9620 100644--- a/app/Services/Bunq/Object/UserLight.php+++ b/app/Services/Bunq/Object/UserLight.php@@ -51,7 +51,7 @@ class UserLight extends BunqObject*/public function __construct(array $data){- if(count($data) === 0) {+ if (count($data) === 0) {return;}$this->id = intval($data['id']);diff --git a/app/Services/Bunq/Request/BunqRequest.php b/app/Services/Bunq/Request/BunqRequest.phpindex 629378377f..0046d2f35b 100644--- a/app/Services/Bunq/Request/BunqRequest.php+++ b/app/Services/Bunq/Request/BunqRequest.php@@ -45,6 +45,7 @@ abstract class BunqRequest*/public function __construct(){+ $this->server = config('firefly.bunq.server');}/**@@ -60,14 +61,6 @@ abstract class BunqRequestreturn $this->server;}- /**- * @param string $server- */- public function setServer(string $server)- {- $this->server = $server;- }-/*** @param string $privateKey*/diff --git a/app/Support/Import/Information/BunqInformation.php b/app/Support/Import/Information/BunqInformation.phpindex 9acf363f2e..6d0c955280 100644--- a/app/Support/Import/Information/BunqInformation.php+++ b/app/Support/Import/Information/BunqInformation.php@@ -13,6 +13,7 @@ declare(strict_types=1);namespace FireflyIII\Support\Import\Information;use FireflyIII\Exceptions\FireflyException;+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;use FireflyIII\Services\Bunq\Object\Alias;use FireflyIII\Services\Bunq\Object\MonetaryAccountBank;use FireflyIII\Services\Bunq\Request\DeleteDeviceSessionRequest;@@ -20,6 +21,7 @@ use FireflyIII\Services\Bunq\Request\DeviceSessionRequest;use FireflyIII\Services\Bunq\Request\ListMonetaryAccountRequest;use FireflyIII\Services\Bunq\Request\ListUserRequest;use FireflyIII\Services\Bunq\Token\SessionToken;+use FireflyIII\Support\CacheProperties;use FireflyIII\User;use Illuminate\Support\Collection;use Log;@@ -56,22 +58,28 @@ class BunqInformation implements InformationInterface*/public function getAccounts(): array{+ // cache for an hour:+ $cache = new CacheProperties;+ $cache->addProperty('bunq.get-accounts');+ $cache->addProperty(date('dmy h'));+ if ($cache->has()) {+ return $cache->get(); // @codeCoverageIgnore+ }Log::debug('Now in getAccounts()');$sessionToken = $this->startSession();$id = $this->getUserInformation($sessionToken);-// get list of Bunq accounts:$accounts = $this->getMonetaryAccounts($sessionToken, $id);$return = [];/** @var MonetaryAccountBank $account */foreach ($accounts as $account) {+$current = ['id' => $account->getId(),'name' => $account->getDescription(),'currency' => $account->getCurrency(),'balance' => $account->getBalance()->getValue(),'color' => $account->getSetting()->getColor(),-];/** @var Alias $alias */foreach ($account->getAliases() as $alias) {@@ -81,6 +89,7 @@ class BunqInformation implements InformationInterface}$return[] = $current;}+ $cache->store($return);$this->closeSession($sessionToken);@@ -105,11 +114,9 @@ class BunqInformation implements InformationInterfaceLog::debug('Going to close session');$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;- $server = config('firefly.bunq.server');$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;$request = new DeleteDeviceSessionRequest();$request->setSecret($apiKey);- $request->setServer($server);$request->setPrivateKey($privateKey);$request->setServerPublicKey($serverPublicKey);$request->setSessionToken($sessionToken);@@ -128,14 +135,12 @@ class BunqInformation implements InformationInterface{$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;- $server = config('firefly.bunq.server');$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;$request = new ListMonetaryAccountRequest;$request->setSessionToken($sessionToken);$request->setSecret($apiKey);$request->setServerPublicKey($serverPublicKey);- $request->setServer($server);$request->setPrivateKey($privateKey);$request->setUserId($userId);$request->call();@@ -154,13 +159,11 @@ class BunqInformation implements InformationInterface{$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;- $server = config('firefly.bunq.server');$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;$request = new ListUserRequest;$request->setSessionToken($sessionToken);$request->setSecret($apiKey);$request->setServerPublicKey($serverPublicKey);- $request->setServer($server);$request->setPrivateKey($privateKey);$request->call();// return the first that isn't null?@@ -183,13 +186,11 @@ class BunqInformation implements InformationInterfaceLog::debug('Now in startSession.');$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;- $server = config('firefly.bunq.server');$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;$installationToken = Preferences::getForUser($this->user, 'bunq_installation_token')->data;$request = new DeviceSessionRequest();$request->setSecret($apiKey);$request->setServerPublicKey($serverPublicKey);- $request->setServer($server);$request->setPrivateKey($privateKey);$request->setInstallationToken($installationToken);$request->call();diff --git a/app/Support/Import/Prerequisites/BunqPrerequisites.php b/app/Support/Import/Prerequisites/BunqPrerequisites.phpindex 022d1e44b9..a83d4ec4c8 100644--- a/app/Support/Import/Prerequisites/BunqPrerequisites.php+++ b/app/Support/Import/Prerequisites/BunqPrerequisites.php@@ -157,7 +157,6 @@ class BunqPrerequisites implements PrerequisitesInterface$request->setInstallationToken($installationToken);$request->setServerPublicKey($serverPublicKey);$request->setPrivateKey($this->getPrivateKey());- $request->setServer(config('firefly.bunq.server'));$request->call();$devices = $request->getDevices();/** @var DeviceServer $device */@@ -186,7 +185,6 @@ class BunqPrerequisites implements PrerequisitesInterface// verify bunq api code:$publicKey = $this->getPublicKey();$request = new InstallationTokenRequest;- $request->setServer(strval(config('firefly.bunq.server')));$request->setPublicKey($publicKey);$request->call();Log::debug('Sent request');@@ -301,7 +299,6 @@ class BunqPrerequisites implements PrerequisitesInterface$serverPublicKey = $this->getServerPublicKey();$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', '');$request = new DeviceServerRequest;- $request->setServer(strval(config('firefly.bunq.server')));$request->setPrivateKey($this->getPrivateKey());$request->setDescription('Firefly III v' . config('firefly.version') . ' for ' . $this->user->email);$request->setSecret($apiKey->data);diff --git a/config/firefly.php b/config/firefly.phpindex 51c26ecb91..2ecb8a3782 100644--- a/config/firefly.php+++ b/config/firefly.php@@ -45,6 +45,9 @@ return ['import_info' => ['bunq' => 'FireflyIII\Support\Import\Information\BunqInformation',],+ 'import_transactions' => [+ 'bunq' => 'FireflyIII\Support\Import\Transactions\BunqTransactions',+ ],'bunq' => ['server' => 'https://sandbox.public.api.bunq.com',],diff --git a/resources/views/import/bank/form.twig b/resources/views/import/bank/form.twignew file mode 100644index 0000000000..17bd9ac029--- /dev/null+++ b/resources/views/import/bank/form.twig@@ -0,0 +1,69 @@+{% extends "./layout/default" %}++{% block breadcrumbs %}+ {{ Breadcrumbs.renderIfExists }}+{% endblock %}+{% block content %}+ <div class="row">+ <form class="form-horizontal" action="{{ route('import.bank.form.post',[bank]) }}" method="post">+ <input type="hidden" name="_token" value="{{ csrf_token() }}"/>+ <div class="col-lg-12 col-md-12 col-sm-12">+ <div class="box box-default">+ <div class="box-header with-border">+ <h3 class="box-title">{{ trans('bank.bank_form_title') }}</h3>+ </div>+ <div class="box-body">+ <div class="row">+ <div class="col-lg-8">+ <p>+ {{ trans('bank.bank_form_text') }}+ </p>+ </div>+ </div>+ <div class="row">+ <div class="col-lg-8">+ <table class="table">+ <thead>+ <tr>+ <th colspan="2">Account</th>+ <th>Current balance</th>+ </tr>+ </thead>+ <tbody>++ {% for remoteAccount in remoteAccounts %}+ <tr>+ <td>+ <input type="checkbox" name="do_import[{{ remoteAccount.id }}]" checked id="do_import_{{ remoteAccount.id }}" />+ </td>+ <td>+ <strong {% if remoteAccount.color !='' %} style="color:{{ remoteAccount.color }}"{% endif %}>+ {{ remoteAccount.name }}+ </strong>+ <br />{{ remoteAccount.number }}+ </td>+ <td>+ {{ remoteAccount.currency }}+ {{ remoteAccount.balance }}+ </td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ </div>+ </div>+ </div>+ <div class="box-footer">+ <button type="submit" class="btn pull-right btn-success">+ {{ ('submit')|_ }}+ </button>+ </div>+ </div>+ </div>+ </form>+ </div>+{% endblock %}+{% block scripts %}+{% endblock %}+{% block styles %}+{% endblock %}diff --git a/resources/views/import/bunq/prerequisites.twig b/resources/views/import/bunq/prerequisites.twigindex aafe01c93d..a70da151d1 100644--- a/resources/views/import/bunq/prerequisites.twig+++ b/resources/views/import/bunq/prerequisites.twig@@ -5,7 +5,7 @@{% endblock %}{% block content %}<div class="row">- <form class="form-horizontal" id="report-form" action="{{ route('import.bank.prerequisites.post',['bunq']) }}" method="post">+ <form class="form-horizontal" action="{{ route('import.bank.prerequisites.post',['bunq']) }}" method="post"><input type="hidden" name="_token" value="{{ csrf_token() }}"/><div class="col-lg-12 col-md-12 col-sm-12"><div class="box box-default">