From 30af48a859db392575bf923b042a11c0d295d0e8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 18 Jan 2023 07:05:04 +0100 Subject: [PATCH] Fix script for https://github.com/firefly-iii/firefly-iii/issues/6879 --- autosave.php | 18 +++++++++--------- readme.md | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/autosave.php b/autosave.php index 0db055c..1345150 100644 --- a/autosave.php +++ b/autosave.php @@ -29,10 +29,8 @@ bcscale(12); * Keep in mind that running this script is entirely AT YOUR OWN RISK with ZERO GUARANTEES. */ -define('FIREFLY_III_URL', 'http://firefly.sd.home'); -define('FIREFLY_III_TOKEN', - 'ey...' -); +const FIREFLY_III_URL = 'http://firefly.sd.home'; +const FIREFLY_III_TOKEN = 'ey...'; /* * HERE BE MONSTERS @@ -83,7 +81,7 @@ foreach ($groups as $group) { continue; } - // get the main transaction (we know its one) + // get the main transaction (we know it's one) $transaction = $group['attributes']['transactions'][0]; // maybe already has a link to existing auto-save? @@ -93,8 +91,8 @@ foreach ($groups as $group) { foreach ($links as $link) { $opposingTransactionId = getOpposingTransaction($transaction['transaction_journal_id'], $link); - // if the opposing transaction is a transfer, and it's a autosave link (recognized by the tag) - // we dont need to create another one. + // if the opposing transaction is a transfer, and it's an autosave link (recognized by the tag) + // we don't need to create another one. $opposingTransaction = getTransaction($opposingTransactionId); if (isAutoSaveTransaction($opposingTransaction)) { @@ -110,6 +108,8 @@ foreach ($groups as $group) { /** * @param array $group * @param array $arguments + * + * @throws JsonException */ function createAutoSaveTransaction(array $group, array $arguments): void { @@ -243,7 +243,7 @@ function getOpposingTransaction(int $transactionId, array $link): int function getLinks(array $transaction): array { $journalId = $transaction['transaction_journal_id']; - $links = getCurlRequest(sprintf('/api/v1/transactions/%d/transaction_links', $journalId)); + $links = getCurlRequest(sprintf('/api/v1/transaction-journals/%d/links', $journalId)); if (count($links['data']) > 0) { return $links['data']; @@ -427,7 +427,7 @@ function getArguments(array $arguments): array /** @var string $argument */ foreach ($arguments as $argument) { foreach ($fields as $field) { - if (0 === strpos($argument, sprintf('--%s=', $field))) { + if (str_starts_with($argument, sprintf('--%s=', $field))) { $result[$field] = (int) str_replace(sprintf('--%s=', $field), '', $argument); if ('amount' === $field) { $result[$field] = (float) str_replace(sprintf('--%s=', $field), '', $argument); diff --git a/readme.md b/readme.md index 7d69090..ccd1cf2 100644 --- a/readme.md +++ b/readme.md @@ -29,7 +29,7 @@ This little script can automatically create these transactions for you, so you d ## Installation -Just grab `autosave.php` and save it wherever. You will need php 7.4 with the BCMath, curl and JSON extensions. This is pretty common and it shouldn't be a problem. +Just grab `autosave.php` and save it wherever. You will need PHP 8.0 with the BCMath, curl and JSON extensions. This is pretty common, and it shouldn't be a problem. Open the file and edit the Firefly III URL and add a Personal Access Token.