2019-03-24 09:23:36 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TransactionGroupTwig.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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.
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-03-24 09:23:36 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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/>.
|
2019-03-24 09:23:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-16 07:24:04 +02:00
|
|
|
namespace FireflyIII\Support\Twig;
|
2019-03-24 09:23:36 +01:00
|
|
|
|
2019-04-16 16:20:46 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use DB;
|
2020-07-27 10:53:36 +02:00
|
|
|
use FireflyIII\Models\Account;
|
2019-11-19 17:39:55 +01:00
|
|
|
use FireflyIII\Models\AccountType;
|
2019-06-23 11:13:36 +02:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2019-03-24 09:23:36 +01:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-06-22 10:25:57 +02:00
|
|
|
use Log;
|
2019-12-28 09:44:56 +01:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFunction;
|
2019-03-24 09:23:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionGroupTwig
|
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
class TransactionGroupTwig extends AbstractExtension
|
2019-03-24 09:23:36 +01:00
|
|
|
{
|
2019-04-18 20:05:40 +02:00
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */
|
2019-03-24 09:23:36 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
2019-04-18 20:05:40 +02:00
|
|
|
*
|
2019-03-24 09:23:36 +01:00
|
|
|
*/
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
return [
|
2019-06-23 11:13:36 +02:00
|
|
|
$this->journalArrayAmount(),
|
|
|
|
$this->journalObjectAmount(),
|
2019-03-24 09:23:36 +01:00
|
|
|
$this->groupAmount(),
|
2019-04-16 16:20:46 +02:00
|
|
|
$this->journalHasMeta(),
|
|
|
|
$this->journalGetMetaDate(),
|
2019-04-18 20:05:40 +02:00
|
|
|
$this->journalGetMetaField(),
|
2019-03-24 09:23:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-28 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-03-24 09:23:36 +01:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function groupAmount(): TwigFunction
|
2019-03-24 09:23:36 +01:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-03-24 09:23:36 +01:00
|
|
|
'groupAmount',
|
2020-07-27 10:53:36 +02:00
|
|
|
function (array $array, Account $account): string {
|
2019-04-18 20:05:40 +02:00
|
|
|
$sums = $array['sums'];
|
|
|
|
$return = [];
|
|
|
|
$first = reset($array['transactions']);
|
|
|
|
$type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
|
|
|
$colored = true;
|
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$colored = false;
|
2019-03-24 09:23:36 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:05:40 +02:00
|
|
|
|
|
|
|
/** @var array $sum */
|
|
|
|
foreach ($sums as $sum) {
|
|
|
|
$amount = $sum['amount'];
|
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
$sourceType = $first['source_account_type'] ?? 'invalid';
|
2020-07-27 10:53:36 +02:00
|
|
|
$sourceAccountId = $first['source_account_id'];
|
2020-08-01 14:08:03 +02:00
|
|
|
$amount = $this->signAmountFromAccountPOV($amount, $type, $sourceType, $sourceAccountId, $account->id);
|
2019-04-18 20:05:40 +02:00
|
|
|
|
|
|
|
$return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored);
|
|
|
|
}
|
2020-07-27 10:53:36 +02:00
|
|
|
$result = implode(', ', $return);
|
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$result = sprintf('<span class="text-info">%s</span>', $result);
|
|
|
|
}
|
|
|
|
return $result;
|
2019-03-24 09:23:36 +01:00
|
|
|
},
|
|
|
|
['is_safe' => ['html']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:20:46 +02:00
|
|
|
/**
|
2019-12-28 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function journalGetMetaDate(): TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-04-16 16:20:46 +02:00
|
|
|
'journalGetMetaDate',
|
|
|
|
static function (int $journalId, string $metaField) {
|
2019-06-22 10:25:57 +02:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-23 05:53:01 +02:00
|
|
|
Log::warning('Twig TransactionGroup::journalGetMetaDate should NOT be called in the TEST environment!');
|
2019-06-22 10:25:57 +02:00
|
|
|
}
|
2019-04-16 16:20:46 +02: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 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function journalGetMetaField(): TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-04-16 16:20:46 +02:00
|
|
|
'journalGetMetaField',
|
|
|
|
static function (int $journalId, string $metaField) {
|
2019-06-22 10:25:57 +02:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-23 05:53:01 +02:00
|
|
|
Log::warning('Twig TransactionGroup::journalGetMetaField should NOT be called in the TEST environment!');
|
2019-06-22 10:25:57 +02:00
|
|
|
}
|
2019-04-16 16:20:46 +02: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 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function journalHasMeta(): TwigFunction
|
2019-04-16 16:20:46 +02:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-04-16 16:20:46 +02: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;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-24 09:23:36 +01:00
|
|
|
/**
|
2019-06-23 11:13:36 +02:00
|
|
|
* Shows the amount for a single journal array.
|
|
|
|
*
|
2019-12-28 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-03-24 09:23:36 +01:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function journalArrayAmount(): TwigFunction
|
2019-03-24 09:23:36 +01:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-06-23 11:13:36 +02:00
|
|
|
'journalArrayAmount',
|
2020-07-27 10:53:36 +02:00
|
|
|
function (array $journal, Account $account): string {
|
2019-03-24 09:23:36 +01:00
|
|
|
// if is not a withdrawal, amount positive.
|
2020-07-27 10:53:36 +02:00
|
|
|
$result = $this->normalJournalArrayAmount($journal, $account);
|
2019-03-24 09:23:36 +01:00
|
|
|
// now append foreign amount, if any.
|
2020-07-27 10:53:36 +02:00
|
|
|
if (null !== $journal['foreign_amount']) {
|
|
|
|
$foreign = $this->foreignJournalArrayAmount($journal, $account);
|
2019-06-23 11:13:36 +02:00
|
|
|
$result = sprintf('%s (%s)', $result, $foreign);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
['is_safe' => ['html']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the amount for a single journal object.
|
|
|
|
*
|
2019-12-28 09:44:56 +01:00
|
|
|
* @return TwigFunction
|
2019-06-23 11:13:36 +02:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
public function journalObjectAmount(): TwigFunction
|
2019-06-23 11:13:36 +02:00
|
|
|
{
|
2019-12-28 09:44:56 +01:00
|
|
|
return new TwigFunction(
|
2019-06-23 11:13:36 +02:00
|
|
|
'journalObjectAmount',
|
|
|
|
function (TransactionJournal $journal): string {
|
|
|
|
$result = $this->normalJournalObjectAmount($journal);
|
|
|
|
// now append foreign amount, if any.
|
|
|
|
if ($this->journalObjectHasForeign($journal)) {
|
|
|
|
$foreign = $this->foreignJournalObjectAmount($journal);
|
2019-03-24 09:23:36 +01:00
|
|
|
$result = sprintf('%s (%s)', $result, $foreign);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
['is_safe' => ['html']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate foreign amount for transaction from a transaction group.
|
|
|
|
*
|
2020-07-27 10:53:36 +02:00
|
|
|
* @param array $journal
|
|
|
|
* @param Account $account
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-27 10:53:36 +02:00
|
|
|
private function foreignJournalArrayAmount(array $journal, Account $account): string
|
2019-03-24 09:23:36 +01:00
|
|
|
{
|
2020-07-27 10:53:36 +02:00
|
|
|
$type = $journal['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
|
|
|
$amount = $journal['foreign_amount'] ?? '0';
|
2019-03-24 09:23:36 +01:00
|
|
|
$colored = true;
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
$sourceType = $journal['source_account_type'] ?? 'invalid';
|
2020-07-27 10:53:36 +02:00
|
|
|
$sourceAccountId = $journal['source_account_id'];
|
2020-08-01 14:08:03 +02:00
|
|
|
$amount = $this->signAmountFromAccountPOV($amount, $type, $sourceType, $sourceAccountId, $account->id);
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2019-03-24 09:23:36 +01:00
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$colored = false;
|
|
|
|
}
|
2020-07-27 10:53:36 +02:00
|
|
|
$result = app('amount')->formatFlat($journal['foreign_currency_symbol'], (int)$journal['foreign_currency_decimal_places'], $amount, $colored);
|
2019-03-24 09:23:36 +01:00
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$result = sprintf('<span class="text-info">%s</span>', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2020-08-01 14:08:03 +02:00
|
|
|
$sourceType = $first->account()->first()->accountType()->first()->type;
|
|
|
|
|
|
|
|
$amount = $this->signAmount($amount, $type, $sourceType);
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-24 09:23:36 +01:00
|
|
|
/**
|
|
|
|
* Generate normal amount for transaction from a transaction group.
|
|
|
|
*
|
|
|
|
* @param array $array
|
2020-07-27 10:53:36 +02:00
|
|
|
* @param Account $account
|
2019-03-24 09:23:36 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-27 10:53:36 +02:00
|
|
|
private function normalJournalArrayAmount(array $journal, Account $account): string
|
2019-03-24 09:23:36 +01:00
|
|
|
{
|
2020-07-27 10:53:36 +02:00
|
|
|
$type = $journal['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
|
|
|
$amount = $journal['amount'] ?? '0';
|
2019-03-24 09:23:36 +01:00
|
|
|
$colored = true;
|
2020-08-01 14:08:03 +02:00
|
|
|
$sourceType = $journal['source_account_type'] ?? 'invalid';
|
2020-07-27 10:53:36 +02:00
|
|
|
$sourceAccountId = $journal['source_account_id'];
|
2020-08-01 14:08:03 +02:00
|
|
|
$amount = $this->signAmount($amount, $type, $sourceType, $sourceAccountId, $account->id);
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2019-03-24 09:23:36 +01:00
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$colored = false;
|
|
|
|
}
|
2019-11-19 17:39:55 +01:00
|
|
|
|
2020-07-27 10:53:36 +02:00
|
|
|
$result = app('amount')->formatFlat($journal['currency_symbol'], (int)$journal['currency_decimal_places'], $amount, $colored);
|
2019-03-24 09:23:36 +01:00
|
|
|
if ($type === TransactionType::TRANSFER) {
|
|
|
|
$result = sprintf('<span class="text-info">%s</span>', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-06-23 11:13:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2020-08-01 14:08:03 +02:00
|
|
|
$sourceType = $first->account()->first()->accountType()->first()->type;
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
$amount = $this->signAmount($amount, $type, $sourceType);
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-07-27 10:53:36 +02:00
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
private function signAmount( string $amount, string $transactionType, string $sourceType ): string {
|
2020-07-27 10:53:36 +02:00
|
|
|
|
|
|
|
// withdrawals stay negative
|
2020-08-01 14:08:03 +02:00
|
|
|
if ($transactionType !== TransactionType::WITHDRAWAL) {
|
2020-07-27 10:53:36 +02:00
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
// opening balance and it comes from initial balance? its expense.
|
|
|
|
if ($transactionType === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE !== $sourceType) {
|
2020-07-27 10:53:36 +02:00
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
// reconciliation and it comes from reconciliation?
|
|
|
|
if ($transactionType === TransactionType::RECONCILIATION && AccountType::RECONCILIATION !== $sourceType) {
|
2020-07-27 10:53:36 +02:00
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
|
|
|
|
2020-08-01 14:08:03 +02:00
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function signAmountFromAccountPOV(string $amount, string $transactionType, string $sourceType, int $sourceAccountId, $displayedAccountId): string {
|
|
|
|
$amount = $this->signAmount( $amount, $transactionType, $sourceType );
|
|
|
|
|
|
|
|
// transfers stay negative from source point of view
|
|
|
|
if ($transactionType === TransactionType::TRANSFER
|
|
|
|
&& $sourceAccountId === $displayedAccountId) {
|
2020-07-27 11:06:00 +02:00
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
2020-08-01 14:08:03 +02:00
|
|
|
|
2020-07-27 10:53:36 +02:00
|
|
|
return $amount;
|
|
|
|
}
|
2019-08-17 12:09:03 +02:00
|
|
|
}
|