Skip to content

Firefly III: a personal finances manager

More code to catch exceptions in #1238

James Colea55d18709cb5
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php
index dd49eb5025..7694cd82f9 100644
--- a/app/Models/TransactionJournal.php
+++ b/app/Models/TransactionJournal.php
@@ -39,6 +39,7 @@ use Watson\Validating\ValidatingTrait;
/**
* Class TransactionJournal.
+ *
* @property User $user
*/
class TransactionJournal extends Model
@@ -181,6 +182,7 @@ class TransactionJournal extends Model
*
* @param string $name
*
+ * @deprecated
* @return string
*/
public function getMeta(string $name)
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 3eca0906eb..112b25a5f0 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -367,6 +367,35 @@ class JournalRepository implements JournalRepositoryInterface
return $amount;
}
+ /**
+ * Return Carbon value of a meta field (or NULL).
+ *
+ * @param TransactionJournal $journal
+ * @param string $field
+ *
+ * @return null|Carbon
+ */
+ public function getMetaDate(TransactionJournal $journal, string $field): ?Carbon
+ {
+ $cache = new CacheProperties;
+ $cache->addProperty('journal-meta-updated');
+ $cache->addProperty($journal->id);
+ $cache->addProperty($field);
+
+ if ($cache->has()) {
+ return $cache->get(); // @codeCoverageIgnore
+ }
+
+ $entry = $journal->transactionJournalMeta()->where('name', $field)->first();
+ if (is_null($entry)) {
+ return null;
+ }
+ $value = new Carbon($entry->data);
+ $cache->store($value);
+
+ return $value;
+ }
+
/**
* Return value of a meta field (or NULL) as a string.
*
@@ -377,12 +406,8 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function getMetaField(TransactionJournal $journal, string $field): ?string
{
- $class = new \stdClass;
- $class->value = 'hi there';
-
- $value = null;
$cache = new CacheProperties;
- $cache->addProperty('journal-meta');
+ $cache->addProperty('journal-meta-updated');
$cache->addProperty($journal->id);
$cache->addProperty($field);
@@ -390,19 +415,25 @@ class JournalRepository implements JournalRepositoryInterface
return $cache->get(); // @codeCoverageIgnore
}
- Log::debug(sprintf('Looking for journal #%d meta field "%s".', $journal->id, $field));
$entry = $journal->transactionJournalMeta()->where('name', $field)->first();
if (is_null($entry)) {
return null;
}
+
$value = $entry->data;
- $cache->store($value);
+ // return when array:
if (is_array($value)) {
- return join(',', $value);
+ $return = join(',', $value);
+ $cache->store($return);
+
+ return $return;
}
+
+ // return when something else:
try {
$return = strval($value);
+ $cache->store($return);
} catch (Exception $e) {
Log::error($e->getMessage());
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 3d71181414..445a14f2c2 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Journal;
+use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\Note;
use FireflyIII\Models\Transaction;
@@ -164,6 +165,16 @@ interface JournalRepositoryInterface
*/
public function getJournalTotal(TransactionJournal $journal): string;
+ /**
+ * Return Carbon value of a meta field (or NULL).
+ *
+ * @param TransactionJournal $journal
+ * @param string $field
+ *
+ * @return null|Carbon
+ */
+ public function getMetaDate(TransactionJournal $journal, string $field): ?Carbon;
+
/**
* Return value of a meta field (or NULL).
*
diff --git a/app/Support/Twig/Extension/TransactionJournal.php b/app/Support/Twig/Extension/TransactionJournal.php
index 925b1613f0..cb206f4246 100644
--- a/app/Support/Twig/Extension/TransactionJournal.php
+++ b/app/Support/Twig/Extension/TransactionJournal.php
@@ -22,9 +22,11 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
+use Carbon\Carbon;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionJournal as JournalModel;
use FireflyIII\Models\TransactionType;
+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Twig_Extension;
/**
@@ -32,6 +34,34 @@ use Twig_Extension;
*/
class TransactionJournal extends Twig_Extension
{
+ /**
+ * @param JournalModel $journal
+ * @param string $field
+ *
+ * @return null|Carbon
+ */
+ public function getMetaDate(JournalModel $journal, string $field): ?Carbon
+ {
+ /** @var JournalRepositoryInterface $repository */
+ $repository = app(JournalRepositoryInterface::class);
+
+ return $repository->getMetaDate($journal, $field);
+ }
+
+ /**
+ * @param JournalModel $journal
+ * @param string $field
+ *
+ * @return string
+ */
+ public function getMetaField(JournalModel $journal, string $field): string
+ {
+ /** @var JournalRepositoryInterface $repository */
+ $repository = app(JournalRepositoryInterface::class);
+
+ return $repository->getMetaField($journal, $field);
+ }
+
/**
* @param JournalModel $journal
*
@@ -66,7 +96,8 @@ class TransactionJournal extends Twig_Extension
}
$totals[$foreignId]['amount'] = bcadd(
$transaction->foreign_amount,
- $totals[$foreignId]['amount']);
+ $totals[$foreignId]['amount']
+ );
}
}
$array = [];
diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php
index c098b2d96b..3e18a565b3 100644
--- a/app/Support/Twig/Journal.php
+++ b/app/Support/Twig/Journal.php
@@ -94,6 +94,8 @@ class Journal extends Twig_Extension
$this->getDestinationAccount(),
$this->journalBudgets(),
$this->journalCategories(),
+ new Twig_SimpleFunction('getMetaField', [TransactionJournalExtension::class, 'getMetaField']),
+ new Twig_SimpleFunction('getMetaDate', [TransactionJournalExtension::class, 'getMetaDate']),
];
return $functions;
diff --git a/resources/views/reports/partials/journals-audit.twig b/resources/views/reports/partials/journals-audit.twig
index 35d7e2f698..ec594fe40f 100644
--- a/resources/views/reports/partials/journals-audit.twig
+++ b/resources/views/reports/partials/journals-audit.twig
@@ -66,18 +66,18 @@
<td class="hide-date">{{ transaction.date.formatLocalized(monthAndDayFormat) }}</td>
<td class="hide-book_date">
{% if transaction.transactionJournal.hasMeta('book_date') %}
- {{ transaction.transactionJournal.getMeta('book_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'book_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
<td class="hide-process_date">
{% if transaction.transactionJournal.hasMeta('process_date') %}
- {{ transaction.transactionJournal.getMeta('process_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'process_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
<td class="hide-interest_date">
{% if transaction.transactionJournal.hasMeta('interest_date') %}
- {{ transaction.transactionJournal.getMeta('interest_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'interest_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
@@ -85,19 +85,19 @@
{# new optional fields (3x) #}
<td class="hide-due_date">
{% if transaction.transactionJournal.hasMeta('due_date') %}
- {{ transaction.transactionJournal.getMeta('due_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'due_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
<td class="hide-payment_date">
{% if transaction.transactionJournal.hasMeta('payment_date') %}
- {{ transaction.transactionJournal.getMeta('payment_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'payment_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
<td class="hide-invoice_date">
{% if transaction.transactionJournal.hasMeta('invoice_date') %}
- {{ transaction.transactionJournal.getMeta('invoice_date').formatLocalized(monthAndDayFormat) }}
+ {{ getMetaDate(transaction.transactionJournal, 'invoice_date').formatLocalized(monthAndDayFormat) }}
{% endif %}
</td>
@@ -129,7 +129,7 @@
<td class="hide-internal_reference">
{% if transaction.transactionJournal.hasMeta('internal_reference') %}
- {{ transaction.transactionJournal.getMeta('internal_reference') }}
+ {{ getMetaField(transaction.transactionJournal,'internal_reference') }}
{% endif %}
</td>
diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig
index 65473d8bb7..d0cd6c494e 100644
--- a/resources/views/transactions/show.twig
+++ b/resources/views/transactions/show.twig
@@ -191,7 +191,7 @@
{% if journal.hasMeta('interest_date') %}
<tr>
<td>{{ trans('list.interest_date') }}</td>
- <td>{{ journal.getMeta('interest_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'interest_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
@@ -199,13 +199,13 @@
{% if journal.hasMeta('book_date') %}
<tr>
<td>{{ trans('list.book_date') }}</td>
- <td>{{ journal.getMeta('book_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'book_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.hasMeta('process_date') %}
<tr>
<td>{{ trans('list.process_date') }}</td>
- <td>{{ journal.getMeta('process_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'process_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
@@ -213,27 +213,28 @@
{% if journal.hasMeta('due_date') %}
<tr>
<td>{{ trans('list.due_date') }}</td>
- <td>{{ journal.getMeta('due_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'due_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.hasMeta('payment_date') %}
<tr>
<td>{{ trans('list.payment_date') }}</td>
- <td>{{ journal.getMeta('payment_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'payment_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.hasMeta('invoice_date') %}
<tr>
<td>{{ trans('list.invoice_date') }}</td>
- <td>{{ journal.getMeta('invoice_date').formatLocalized(monthAndDayFormat) }}</td>
+ <td>{{ getMetaDate(journal,'invoice_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
- {% if journal.hasMeta('internal_reference') and journal.getMeta('internal_reference') != "" %}
+ {% set intRef = getMetaField(journal, 'internal_reference') %}
+ {% if intRef != "" %}
<tr>
<td>{{ trans('list.internal_reference') }}</td>
- <td>{{ journal.getMeta('internal_reference') }}</td>
+ <td>{{ intRef }}</td>
</tr>
{% endif %}
{% if journal.notes.count == 1 %}