More code to catch exceptions in #1238

This commit is contained in:
James Cole
2018-03-10 22:34:02 +01:00
parent dd2f3c861b
commit a55d18709c
7 changed files with 103 additions and 25 deletions

View File

@@ -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)

View File

@@ -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);
$value = $entry->data;
// 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());

View File

@@ -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).
*

View File

@@ -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 = [];

View File

@@ -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;

View File

@@ -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>

View File

@@ -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 %}