Files
firefly-iii/app/Support/Twig/Extension/TransactionJournal.php

142 lines
4.4 KiB
PHP
Raw Normal View History

<?php
/**
* TransactionJournal.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* 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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
2018-03-10 22:34:02 +01:00
use Carbon\Carbon;
2017-11-25 08:54:52 +01:00
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionJournal as JournalModel;
use FireflyIII\Models\TransactionType;
2018-03-10 22:34:02 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Twig_Extension;
2017-12-17 14:30:53 +01:00
/**
* Class TransactionJournal
*/
class TransactionJournal extends Twig_Extension
{
2018-03-10 22:34:02 +01:00
/**
* @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);
2018-03-11 08:23:47 +01:00
$result = $repository->getMetaField($journal, $field);
2018-04-02 14:50:17 +02:00
if (null === $result) {
2018-03-11 08:23:47 +01:00
return '';
}
2018-03-10 22:34:02 +01:00
2018-03-11 08:23:47 +01:00
return $result;
2018-03-10 22:34:02 +01:00
}
/**
* Return if journal HAS field.
*
* @param JournalModel $journal
* @param string $field
*
* @return bool
*/
public function hasMetaField(JournalModel $journal, string $field): bool
{
// HIER BEN JE
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$result = $repository->getMetaField($journal, $field);
2018-04-02 14:50:17 +02:00
if (null === $result) {
return false;
}
2018-04-28 06:23:13 +02:00
if (\strlen((string)$result) === 0) {
return false;
}
return true;
}
/**
* @param JournalModel $journal
*
* @return string
*/
2017-11-15 10:52:29 +01:00
public function totalAmount(JournalModel $journal): string
{
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
$totals = [];
$type = $journal->transactionType->type;
2017-11-25 08:54:52 +01:00
/** @var TransactionModel $transaction */
foreach ($transactions as $transaction) {
$currencyId = $transaction->transaction_currency_id;
$currency = $transaction->transactionCurrency;
if (!isset($totals[$currencyId])) {
$totals[$currencyId] = [
'amount' => '0',
'currency' => $currency,
];
}
$totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']);
2017-11-15 12:25:49 +01:00
if (null !== $transaction->foreign_currency_id) {
$foreignId = $transaction->foreign_currency_id;
$foreign = $transaction->foreignCurrency;
if (!isset($totals[$foreignId])) {
$totals[$foreignId] = [
'amount' => '0',
'currency' => $foreign,
];
}
$totals[$foreignId]['amount'] = bcadd(
$transaction->foreign_amount,
2018-03-10 22:34:02 +01:00
$totals[$foreignId]['amount']
);
}
}
$array = [];
foreach ($totals as $total) {
2017-11-15 12:25:49 +01:00
if (TransactionType::WITHDRAWAL === $type) {
$total['amount'] = bcmul($total['amount'], '-1');
}
$array[] = app('amount')->formatAnything($total['currency'], $total['amount']);
}
2018-04-28 06:23:13 +02:00
return implode(' / ', $array);
}
2017-11-08 09:05:10 +01:00
}