Skip to content

Firefly III: a personal finances manager

Code for #1607

James Cole5b771f7defd9
diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php
index ff76eeedbc..52ef3c34e0 100644
--- a/app/Handlers/Events/UserEventHandler.php
+++ b/app/Handlers/Events/UserEventHandler.php
@@ -61,6 +61,7 @@ class UserEventHandler
// first user ever?
if (1 === $repository->count()) {
+ Log::debug('User count is one, attach role.');
$repository->attachRole($event->user, 'owner');
}
diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php
index 1a61259dab..35fe26d618 100644
--- a/app/Repositories/User/UserRepository.php
+++ b/app/Repositories/User/UserRepository.php
@@ -54,6 +54,7 @@ class UserRepository implements UserRepositoryInterface
{
$roleObject = Role::where('name', $role)->first();
if (null === $roleObject) {
+ Log::error(sprintf('Could not find role "%s" in attachRole()', $role));
return false;
}
@@ -61,7 +62,7 @@ class UserRepository implements UserRepositoryInterface
$user->roles()->attach($roleObject);
} catch (QueryException $e) {
// don't care
- Log::info(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
+ Log::error(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
}
return true;
@@ -77,6 +78,7 @@ class UserRepository implements UserRepositoryInterface
* @see updateEmail
*
* @return bool
+ * @throws \Exception
*/
public function changeEmail(User $user, string $newEmail): bool
{
diff --git a/app/Support/Import/Routine/Bunq/StageImportDataHandler.php b/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
index 8581c9ddde..457bcb8014 100644
--- a/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
+++ b/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
@@ -116,7 +116,7 @@ class StageImportDataHandler
* @return array
* @throws FireflyException
*/
- private function convertPayment(BunqPayment $payment, LocalAccount $source): array
+ private function convertPayment(BunqPayment $payment, int $bunqAccountId, LocalAccount $source): array
{
Log::debug(sprintf('Now at payment with ID #%d', $payment->getId()));
$type = TransactionType::WITHDRAWAL;
@@ -300,15 +300,158 @@ class StageImportDataHandler
*/
private function getTransactionsFromBunq(int $bunqAccountId, LocalAccount $localAccount): array
{
- $return = [];
- // make request:
- /** @var Payment $paymentRequest */
- $paymentRequest = app(Payment::class);
- $result = $paymentRequest->listing($bunqAccountId, ['count' => 100]);
- // loop result:
- /** @var BunqPayment $payment */
- foreach ($result->getValue() as $payment) {
- $return[] = $this->convertPayment($payment, $localAccount);
+ Log::debug('Now in getTransactionsFromBunq(%d).');
+
+ // what was the last transaction we grabbed from bunq?
+ $return = [];
+ $preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
+ $transactionPref = \Preferences::getForUser($this->importJob->user, $preferenceName, 0);
+ $transactionId = (int)$transactionPref->data;
+
+ Log::debug(sprintf('ID of latest transaction is #%d', $transactionId));
+
+ if (0 === $transactionId) {
+ Log::debug('Its zero so we go back in time.');
+ // we go back into the past, way until the system says there is no more.
+ $return = $this->goBackInTime($bunqAccountId, $localAccount);
+ }
+ if (0 !== $transactionId) {
+ $return = $this->goForwardInTime($bunqAccountId, $localAccount);
+ // work my way forward.
+ }
+ sleep(1);
+
+ return $return;
+ }
+
+ /**
+ * @param int $bunqAccountId
+ * @param LocalAccount $localAccount
+ *
+ * @return array
+ * @throws FireflyException
+ */
+ private function goBackInTime(int $bunqAccountId, LocalAccount $localAccount): array
+ {
+ Log::debug('Now in goBackInTime().');
+ $hasMoreTransactions = true;
+ $olderId = null;
+ $count = 0;
+ $return = [];
+ $veryFirstTransaction = null;
+
+ // loop die loop!
+ sleep(1);
+ while ($hasMoreTransactions && $count < 50) {
+ Log::debug(sprintf('Now in loop #%d', $count));
+ /** @var Payment $paymentRequest */
+ $paymentRequest = app(Payment::class);
+ $response = $paymentRequest->listing($bunqAccountId, ['count' => 20, 'older_id' => $olderId]);
+ $pagination = $response->getPagination();
+ /*
+ * If pagination is not null, we can go back even further.
+ */
+ if (null !== $pagination) {
+ $olderId = $pagination->getOlderId();
+ Log::debug(sprintf('Pagination object is not null, olderID is "%s"', $olderId));
+ }
+ Log::debug('Now looping results...');
+ /** @var BunqPayment $payment */
+ foreach ($response->getValue() as $payment) {
+ $return[] = $this->convertPayment($payment, $bunqAccountId, $localAccount);
+
+ // store the very first transaction ID for this particular account.
+ if (null === $veryFirstTransaction) {
+ $veryFirstTransaction = $payment->getId();
+ }
+
+ }
+ Log::debug(sprintf('Count of result is now %d', \count($return)));
+ $count++;
+ if (null === $olderId) {
+ Log::debug('Older ID is NULL, so stop looping cause we are done!');
+ $hasMoreTransactions = false;
+ }
+ if (null === $pagination) {
+ Log::debug('No pagination object, stop looping.');
+ $hasMoreTransactions = false;
+ }
+ sleep(1);
+ }
+ Log::debug(sprintf('Done with looping. Final loop count is %d, first transaction is %d', $count, $veryFirstTransaction));
+ if (null !== $veryFirstTransaction) {
+ Log::debug('Very first transaction is not null, so set the preference!');
+ $preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
+ $pref = \Preferences::setForUser($this->importJob->user, $preferenceName, $veryFirstTransaction);
+ Log::debug(sprintf('Preference set to: %s', $pref->data));
+ }
+
+ return $return;
+ }
+
+ /**
+ * @param int $bunqAccountId
+ * @param LocalAccount $localAccount
+ *
+ * @return array
+ * @throws FireflyException
+ */
+ private function goForwardInTime(int $bunqAccountId, LocalAccount $localAccount): array
+ {
+ Log::debug('Now in goForwardInTime().');
+ $hasMoreTransactions = true;
+ $count = 0;
+ $return = [];
+ $latestTransaction = null;
+
+ // newer ID comes from pref:
+ $preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
+ $transactionPref = \Preferences::getForUser($this->importJob->user, $preferenceName, 0);
+ $newerId = (int)$transactionPref->data;
+
+ // loop die loop!
+ sleep(1);
+ while ($hasMoreTransactions && $count < 50) {
+ Log::debug(sprintf('Now in loop #%d', $count));
+ /** @var Payment $paymentRequest */
+ $paymentRequest = app(Payment::class);
+ $params = ['count' => 20, 'newer_id' => $newerId];
+ $response = $paymentRequest->listing($bunqAccountId, $params);
+ $pagination = $response->getPagination();
+ Log::debug('Submit payment request with params', $params);
+ /*
+ * If pagination is not null, we can go forward further.
+ */
+ if (null !== $pagination) {
+ $newerId = $pagination->getNewerId();
+ Log::debug(sprintf('Pagination object is not null, newerID is "%s"', $newerId));
+ }
+ Log::debug('Now looping results...');
+ /** @var BunqPayment $payment */
+ foreach ($response->getValue() as $payment) {
+ $return[] = $this->convertPayment($payment, $bunqAccountId, $localAccount);
+
+ // store the very last transaction ID for this particular account.
+ $latestTransaction = $payment->getId() > $latestTransaction ? $payment->getId() : $latestTransaction;
+ }
+ Log::debug(sprintf('Count of result is now %d', \count($return)));
+ $count++;
+ if (null === $newerId) {
+ Log::debug('Newer ID is NULL, so stop looping cause we are done!');
+ $hasMoreTransactions = false;
+ }
+ if (null === $pagination) {
+ Log::debug('No pagination object, stop looping.');
+ $hasMoreTransactions = false;
+ }
+ sleep(1);
+ }
+ Log::debug(sprintf('Done with looping. Final loop count is %d, latest transaction is %d', $count, $latestTransaction));
+ if (null !== $latestTransaction) {
+ Log::debug('Latest transaction is not null, so set the preference!');
+ $preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
+ $pref = \Preferences::setForUser($this->importJob->user, $preferenceName, $latestTransaction);
+ Log::debug(sprintf('Preference set to: %s', $pref->data));
}
return $return;
diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php
index c1a097efc6..f86340ded9 100644
--- a/app/Support/Preferences.php
+++ b/app/Support/Preferences.php
@@ -62,7 +62,7 @@ class Preferences
try {
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
} catch (Exception $e) {
- Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
+ Log::debug(sprintf('Could not delete preference: %s', $e->getMessage()));
// don't care.
}
diff --git a/app/Support/Search/Modifier.php b/app/Support/Search/Modifier.php
index 5b6972c46b..9e29d24925 100644
--- a/app/Support/Search/Modifier.php
+++ b/app/Support/Search/Modifier.php
@@ -101,7 +101,7 @@ class Modifier
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
- Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
+ Log::debug(sprintf('Not interesting in Modifier:dateAfter(): %s', $e->getMessage()));
return false;
}
@@ -120,7 +120,7 @@ class Modifier
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
- Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
+ Log::debug(sprintf('Not interesting in modifier:dateBefore(): %s', $e->getMessage()));
return false;
}
@@ -139,7 +139,7 @@ class Modifier
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
- Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
+ Log::debug(sprintf('Not interesting in Modifier:sameDate(): %s', $e->getMessage()));
return false;
}
diff --git a/app/Support/Steam.php b/app/Support/Steam.php
index 1f7999722f..1e1d135a73 100644
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@ -439,7 +439,6 @@ class Steam
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
// do not care.
- Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
}
return $value;