firefly-iii/app/Support/Twig/TransactionGroupTwig.php

339 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* TransactionGroupTwig.php
2020-02-16 06:56:52 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
2019-04-16 09:20:46 -05:00
use Carbon\Carbon;
use DB;
2019-11-19 10:39:55 -06:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Log;
2019-12-28 02:44:56 -06:00
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Class TransactionGroupTwig
*/
2019-12-28 02:44:56 -06:00
class TransactionGroupTwig extends AbstractExtension
{
2019-04-18 13:05:40 -05:00
/** @noinspection PhpMissingParentCallCommonInspection */
/**
* @return array
2019-04-18 13:05:40 -05:00
*
*/
public function getFunctions(): array
{
return [
$this->journalArrayAmount(),
$this->journalObjectAmount(),
$this->groupAmount(),
2019-04-16 09:20:46 -05:00
$this->journalHasMeta(),
$this->journalGetMetaDate(),
2019-04-18 13:05:40 -05:00
$this->journalGetMetaField(),
];
}
/**
2019-12-28 02:44:56 -06:00
* @return TwigFunction
*/
2019-12-28 02:44:56 -06:00
public function groupAmount(): TwigFunction
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
'groupAmount',
2019-04-18 13:05:40 -05:00
static function (array $array): string {
$sums = $array['sums'];
$return = [];
$first = reset($array['transactions']);
$type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$colored = true;
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
2019-04-18 13:05:40 -05:00
/** @var array $sum */
foreach ($sums as $sum) {
$amount = $sum['amount'];
// do multiplication thing.
if ($type !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
$return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored);
}
return implode(', ', $return);
},
['is_safe' => ['html']]
);
}
2019-04-16 09:20:46 -05:00
/**
2019-12-28 02:44:56 -06:00
* @return TwigFunction
2019-04-16 09:20:46 -05:00
*/
2019-12-28 02:44:56 -06:00
public function journalGetMetaDate(): TwigFunction
2019-04-16 09:20:46 -05:00
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
2019-04-16 09:20:46 -05:00
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
2019-06-22 22:53:01 -05:00
Log::warning('Twig TransactionGroup::journalGetMetaDate should NOT be called in the TEST environment!');
}
2019-04-16 09:20:46 -05:00
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return new Carbon;
}
return new Carbon(json_decode($entry->data, false));
}
);
}
/**
2019-12-28 02:44:56 -06:00
* @return TwigFunction
2019-04-16 09:20:46 -05:00
*/
2019-12-28 02:44:56 -06:00
public function journalGetMetaField(): TwigFunction
2019-04-16 09:20:46 -05:00
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
2019-04-16 09:20:46 -05:00
'journalGetMetaField',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
2019-06-22 22:53:01 -05:00
Log::warning('Twig TransactionGroup::journalGetMetaField should NOT be called in the TEST environment!');
}
2019-04-16 09:20:46 -05:00
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return '';
}
return json_decode($entry->data, true);
}
);
}
/**
2019-12-28 02:44:56 -06:00
* @return TwigFunction
2019-04-16 09:20:46 -05:00
*/
2019-12-28 02:44:56 -06:00
public function journalHasMeta(): TwigFunction
2019-04-16 09:20:46 -05:00
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
2019-04-16 09:20:46 -05:00
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count();
return 1 === $count;
}
);
}
/**
* Shows the amount for a single journal array.
*
2019-12-28 02:44:56 -06:00
* @return TwigFunction
*/
2019-12-28 02:44:56 -06:00
public function journalArrayAmount(): TwigFunction
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
'journalArrayAmount',
function (array $array): string {
// if is not a withdrawal, amount positive.
$result = $this->normalJournalArrayAmount($array);
// now append foreign amount, if any.
if (null !== $array['foreign_amount']) {
$foreign = $this->foreignJournalArrayAmount($array);
$result = sprintf('%s (%s)', $result, $foreign);
}
return $result;
},
['is_safe' => ['html']]
);
}
/**
* Shows the amount for a single journal object.
*
2019-12-28 02:44:56 -06:00
* @return TwigFunction
*/
2019-12-28 02:44:56 -06:00
public function journalObjectAmount(): TwigFunction
{
2019-12-28 02:44:56 -06:00
return new TwigFunction(
'journalObjectAmount',
function (TransactionJournal $journal): string {
// if is not a withdrawal, amount positive.
$result = $this->normalJournalObjectAmount($journal);
// now append foreign amount, if any.
if ($this->journalObjectHasForeign($journal)) {
$foreign = $this->foreignJournalObjectAmount($journal);
$result = sprintf('%s (%s)', $result, $foreign);
}
return $result;
},
['is_safe' => ['html']]
);
}
/**
* Generate foreign amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function foreignJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['foreign_amount'] ?? '0';
$colored = true;
if ($type !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
return $result;
}
/**
* Generate foreign amount for journal from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function foreignJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
$amount = $first->foreign_amount ?? '0';
$colored = true;
if ($type !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
return $result;
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
2019-11-19 10:39:55 -06:00
// withdrawals are negative
if ($type !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
2019-11-19 10:39:55 -06:00
$destinationType = $array['destination_account_type'] ?? 'invalid';
if ($type === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE === $destinationType) {
$amount = bcmul($amount, '-1');
}
2019-12-28 02:44:56 -06:00
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
2019-11-19 10:39:55 -06:00
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
return $result;
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function normalJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $journal->transactionCurrency;
$amount = $first->amount ?? '0';
$colored = true;
if ($type !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
return $result;
}
/**
* @param TransactionJournal $journal
* @return bool
*/
private function journalObjectHasForeign(TransactionJournal $journal): bool
{
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
return null !== $first->foreign_amount;
}
2019-08-17 05:09:03 -05:00
}