2017-11-04 07:10:21 +01:00
|
|
|
<?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/>.
|
2017-11-04 07:10:21 +01:00
|
|
|
*/
|
|
|
|
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;
|
2017-11-04 07:10:21 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal as JournalModel;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-03-10 22:34:02 +01:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2017-11-04 07:10:21 +01:00
|
|
|
use Twig_Extension;
|
|
|
|
|
2017-12-17 14:30:53 +01:00
|
|
|
/**
|
|
|
|
* Class TransactionJournal
|
|
|
|
*/
|
2017-11-04 07:10:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-19 19:39: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) {
|
2018-03-19 19:39:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-28 06:23:13 +02:00
|
|
|
if (\strlen((string)$result) === 0) {
|
2018-03-19 19:39:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-04 07:10:21 +01:00
|
|
|
/**
|
|
|
|
* @param JournalModel $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-11-15 10:52:29 +01:00
|
|
|
public function totalAmount(JournalModel $journal): string
|
2017-11-04 07:10:21 +01:00
|
|
|
{
|
|
|
|
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
|
|
|
|
$totals = [];
|
|
|
|
$type = $journal->transactionType->type;
|
2017-11-25 08:54:52 +01:00
|
|
|
/** @var TransactionModel $transaction */
|
2017-11-04 07:10:21 +01:00
|
|
|
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) {
|
2017-11-04 07:10:21 +01:00
|
|
|
$foreignId = $transaction->foreign_currency_id;
|
|
|
|
$foreign = $transaction->foreignCurrency;
|
|
|
|
if (!isset($totals[$foreignId])) {
|
|
|
|
$totals[$foreignId] = [
|
|
|
|
'amount' => '0',
|
|
|
|
'currency' => $foreign,
|
|
|
|
];
|
|
|
|
}
|
2018-02-21 08:51:30 +01:00
|
|
|
$totals[$foreignId]['amount'] = bcadd(
|
|
|
|
$transaction->foreign_amount,
|
2018-03-10 22:34:02 +01:00
|
|
|
$totals[$foreignId]['amount']
|
|
|
|
);
|
2017-11-04 07:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$array = [];
|
|
|
|
foreach ($totals as $total) {
|
2017-11-15 12:25:49 +01:00
|
|
|
if (TransactionType::WITHDRAWAL === $type) {
|
2017-11-04 07:10:21 +01:00
|
|
|
$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-04 07:10:21 +01:00
|
|
|
}
|
2017-11-08 09:05:10 +01:00
|
|
|
}
|