Skip to content

Firefly III: a personal finances manager

Expand Spectre import code.

James Colec89486b6d934
diff --git a/app/Services/Spectre/Object/Transaction.php b/app/Services/Spectre/Object/Transaction.php
index 23c8633a91..603c1777c7 100644
--- a/app/Services/Spectre/Object/Transaction.php
+++ b/app/Services/Spectre/Object/Transaction.php
@@ -23,19 +23,126 @@ declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Object;
+use Carbon\Carbon;
+
/**
* Class Transaction
*/
class Transaction extends SpectreObject
{
+ /** @var int */
+ private $accountId;
+ /** @var string */
+ private $amount;
+ /** @var string */
+ private $category;
+ /** @var Carbon */
+ private $createdAt;
+ /** @var string */
+ private $currencyCode;
+ /** @var string */
+ private $description;
+ /** @var bool */
+ private $duplicated;
+ /** @var TransactionExtra */
+ private $extra;
+ /** @var int */
+ private $id;
+ /** @var Carbon */
+ private $madeOn;
+ /** @var string */
+ private $mode;
+ /** @var string */
+ private $status;
+ /** @var Carbon */
+ private $updatedAt;
+
/**
* Transaction constructor.
*
* @param array $data
*/
- public function __construct(array $data) {
- var_dump($data);
- exit;
+ public function __construct(array $data)
+ {
+ $this->id = $data['id'];
+ $this->mode = $data['mode'];
+ $this->status = $data['status'];
+ $this->madeOn = new Carbon($data['made_on']);
+ $this->amount = $data['amount'];
+ $this->currencyCode = $data['currency_code'];
+ $this->description = $data['description'];
+ $this->category = $data['category'];
+ $this->duplicated = $data['duplicated'];
+ $this->extra = new TransactionExtra($data['extra'] ?? []);
+ $this->accountId = $data['account_id'];
+ $this->createdAt = new Carbon($data['created_at']);
+ $this->updatedAt = new Carbon($data['updated_at']);
+ }
+
+ /**
+ * @return string
+ */
+ public function getAmount(): string
+ {
+ return strval($this->amount);
+ }
+
+ /**
+ * @return string
+ */
+ public function getCategory(): string
+ {
+ return $this->category;
+ }
+
+ /**
+ * @return string
+ */
+ public function getCurrencyCode(): string
+ {
+ return $this->currencyCode;
}
+ /**
+ * @return string
+ */
+ public function getDescription(): string
+ {
+ return $this->description;
+ }
+
+ /**
+ * @return string
+ */
+ public function getHash(): string
+ {
+ $array = [
+ 'id' => $this->id,
+ 'mode' => $this->mode,
+ 'status' => $this->status,
+ 'made_on' => $this->madeOn->toIso8601String(),
+ 'amount' => $this->amount,
+ 'currency_code' => $this->currencyCode,
+ 'description' => $this->description,
+ 'category' => $this->category,
+ 'duplicated' => $this->duplicated,
+ 'extra' => $this->extra->toArray(),
+ 'account_id' => $this->accountId,
+ 'created_at' => $this->createdAt->toIso8601String(),
+ 'updated_at' => $this->updatedAt->toIso8601String(),
+ ];
+ $hashed = hash('sha256', json_encode($array));
+
+ return $hashed;
+ }
+
+ /**
+ * @return Carbon
+ */
+ public function getMadeOn(): Carbon
+ {
+ return $this->madeOn;
+ }
+
+
}
\ No newline at end of file
diff --git a/app/Services/Spectre/Object/TransactionExtra.php b/app/Services/Spectre/Object/TransactionExtra.php
new file mode 100644
index 0000000000..3874b5f8a8
--- /dev/null
+++ b/app/Services/Spectre/Object/TransactionExtra.php
@@ -0,0 +1,168 @@
+<?php
+/**
+ * TransactionExtra.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Services\Spectre\Object;
+
+use Carbon\Carbon;
+
+/**
+ * Class TransactionExtra
+ */
+class TransactionExtra extends SpectreObject
+{
+ /** @var string */
+ private $accountBalanceSnapshot;
+ /** @var string */
+ private $accountNumber;
+ /** @var string */
+ private $additional;
+ /** @var string */
+ private $assetAmount;
+ /** @var string */
+ private $assetCode;
+ /** @var string */
+ private $categorizationConfidence;
+ /** @var string */
+ private $checkNumber;
+ /** @var string */
+ private $customerCategoryCode;
+ /** @var string */
+ private $customerCategoryName;
+ /** @var string */
+ private $id;
+ /** @var string */
+ private $information;
+ /** @var string */
+ private $mcc;
+ /** @var string */
+ private $originalAmount;
+ /** @var string */
+ private $originalCategory;
+ /** @var string */
+ private $originalCurrencyCode;
+ /** @var string */
+ private $originalSubCategory;
+ /** @var string */
+ private $payee;
+ /** @var bool */
+ private $possibleDuplicate;
+ /** @var Carbon */
+ private $postingDate;
+ /** @var Carbon */
+ private $postingTime;
+ /** @var string */
+ private $recordNumber;
+ /** @var array */
+ private $tags;
+ /** @var Carbon */
+ private $time;
+ /** @var string */
+ private $type;
+ /** @var string */
+ private $unitPrice;
+ /** @var string */
+ private $units;
+
+ /**
+ * TransactionExtra constructor.
+ *
+ * @param array $data
+ */
+ public function __construct(array $data)
+ {
+ $this->id = $data['id'] ?? null;
+ $this->recordNumber = $data['record_number'] ?? null;
+ $this->information = $data['information'] ?? null;
+ $this->time = isset($data['time']) ? new Carbon($data['time']) : null;
+ $this->postingDate = isset($data['posting_date']) ? new Carbon($data['posting_date']) : null;
+ $this->postingTime = isset($data['posting_time']) ? new Carbon($data['posting_time']) : null;
+ $this->accountNumber = $data['account_number'] ?? null;
+ $this->originalAmount = $data['original_amount'] ?? null;
+ $this->originalCurrencyCode = $data['original_currency_code'] ?? null;
+ $this->assetCode = $data['asset_code'] ?? null;
+ $this->assetAmount = $data['asset_amount'] ?? null;
+ $this->originalCategory = $data['original_category'] ?? null;
+ $this->originalSubCategory = $data['original_subcategory'] ?? null;
+ $this->customerCategoryCode = $data['customer_category_code'] ?? null;
+ $this->customerCategoryName = $data['customer_category_name'] ?? null;
+ $this->possibleDuplicate = $data['possible_duplicate'] ?? null;
+ $this->tags = $data['tags'] ?? null;
+ $this->mcc = $data['mcc'] ?? null;
+ $this->payee = $data['payee'] ?? null;
+ $this->type = $data['type'] ?? null;
+ $this->checkNumber = $data['check_number'] ?? null;
+ $this->units = $data['units'] ?? null;
+ $this->additional = $data['additional'] ?? null;
+ $this->unitPrice = $data['unit_price'] ?? null;
+ $this->accountBalanceSnapshot = $data['account_balance_snapshot'] ?? null;
+ $this->categorizationConfidence = $data['categorization_confidence'] ?? null;
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getId(): ?string
+ {
+ return $this->id;
+ }
+
+ /**
+ * @return array
+ */
+ public function toArray(): array
+ {
+
+ $array = [
+ 'id' => $this->id,
+ 'record_number' => $this->recordNumber,
+ 'information' => $this->information,
+ 'time' => is_null($this->time) ? null : $this->time->toIso8601String(),
+ 'posting_date' => is_null($this->postingDate) ? null : $this->postingDate->toIso8601String(),
+ 'posting_time' => is_null($this->postingTime) ? null : $this->postingTime->toIso8601String(),
+ 'account_number' => $this->accountNumber,
+ 'original_amount' => $this->originalAmount,
+ 'original_currency_code' => $this->originalCurrencyCode,
+ 'asset_code' => $this->assetCode,
+ 'asset_amount' => $this->assetAmount,
+ 'original_category' => $this->originalCategory,
+ 'original_subcategory' => $this->originalSubCategory,
+ 'customer_category_code' => $this->customerCategoryCode,
+ 'customer_category_name' => $this->customerCategoryName,
+ 'possible_duplicate' => $this->possibleDuplicate,
+ 'tags' => $this->tags,
+ 'mcc' => $this->mcc,
+ 'payee' => $this->payee,
+ 'type' => $this->type,
+ 'check_number' => $this->checkNumber,
+ 'units' => $this->units,
+ 'additional' => $this->additional,
+ 'unit_price' => $this->unitPrice,
+ 'account_balance_snapshot' => $this->accountBalanceSnapshot,
+ 'categorization_confidence' => $this->categorizationConfidence,
+ ];
+
+ return $array;
+ }
+
+
+}
\ No newline at end of file
diff --git a/app/Services/Spectre/Request/ListTransactionsRequest.php b/app/Services/Spectre/Request/ListTransactionsRequest.php
index 57f45f199a..bb03a2d2f1 100644
--- a/app/Services/Spectre/Request/ListTransactionsRequest.php
+++ b/app/Services/Spectre/Request/ListTransactionsRequest.php
@@ -23,9 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Request;
+use FireflyIII\Exceptions\FireflyException;
+use FireflyIII\Services\Spectre\Exception\SpectreException;
use FireflyIII\Services\Spectre\Object\Account;
use FireflyIII\Services\Spectre\Object\Transaction;
use Log;
+
/**
* Class ListTransactionsRequest
*/
@@ -37,7 +40,8 @@ class ListTransactionsRequest extends SpectreRequest
private $transactions = [];
/**
- *
+ * @throws FireflyException
+ * @throws SpectreException
*/
public function call(): void
{
@@ -45,7 +49,7 @@ class ListTransactionsRequest extends SpectreRequest
$nextId = 0;
while ($hasNextPage) {
Log::debug(sprintf('Now calling ListTransactionsRequest for next_id %d', $nextId));
- $parameters = ['from_id' => $nextId,'account_id' => $this->account->getId()];
+ $parameters = ['from_id' => $nextId, 'account_id' => $this->account->getId()];
$uri = '/api/v3/transactions?' . http_build_query($parameters);
$response = $this->sendSignedSpectreGet($uri, []);
@@ -59,7 +63,7 @@ class ListTransactionsRequest extends SpectreRequest
$nextId = $response['meta']['next_id'];
Log::debug(sprintf('Next ID is now %d.', $nextId));
} else {
- Log::debug('No next page.');
+ Log::debug('No next page, done with ListTransactionsRequest.');
}
// store customers:
diff --git a/resources/views/import/spectre/accounts.twig b/resources/views/import/spectre/accounts.twig
index 4d2c11aef1..3868cff0c7 100644
--- a/resources/views/import/spectre/accounts.twig
+++ b/resources/views/import/spectre/accounts.twig
@@ -57,7 +57,7 @@
<div class="checkbox">
<label>
<input type="checkbox" value="1" name="do_import[{{ account.id }}]" checked>
- {{ 'spectre_do_import'|_ }}
+ {{ trans('import.spectre_do_import') }}
</label>
</div>