mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Expanded Amount and expanded TransactionJournal to include source and destination information.
This commit is contained in:
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Watson\Validating\ValidatingTrait;
|
use Watson\Validating\ValidatingTrait;
|
||||||
|
|
||||||
@@ -56,7 +57,19 @@ class TransactionJournal extends Model
|
|||||||
const QUERYFIELDS
|
const QUERYFIELDS
|
||||||
= [
|
= [
|
||||||
'transaction_journals.*',
|
'transaction_journals.*',
|
||||||
'transaction_types.type as transaction_type_type', // the other field is called "transaction_type_id" so this is pretty consistent.
|
'transaction_types.type AS transaction_type_type', // the other field is called "transaction_type_id" so this is pretty consistent.
|
||||||
|
'transaction_currencies.code AS transaction_currency_code',
|
||||||
|
// all for destination:
|
||||||
|
'destination.amount AS destination_amount',
|
||||||
|
'destination_account.id AS destination_account_id',
|
||||||
|
'destination_account.name AS destination_account_name',
|
||||||
|
'destination_acct_type.type AS destination_account_type',
|
||||||
|
// all for source:
|
||||||
|
'source.amount AS source_amount',
|
||||||
|
'source_account.id AS source_account_id',
|
||||||
|
'source_account.name AS source_account_name',
|
||||||
|
'source_acct_type.type AS source_account_type',
|
||||||
|
|
||||||
];
|
];
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $dates = ['created_at', 'updated_at', 'date', 'deleted_at', 'interest_date', 'book_date'];
|
protected $dates = ['created_at', 'updated_at', 'date', 'deleted_at', 'interest_date', 'book_date'];
|
||||||
@@ -279,7 +292,33 @@ class TransactionJournal extends Model
|
|||||||
// left join transaction type:
|
// left join transaction type:
|
||||||
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
||||||
|
|
||||||
// try to get amount
|
// left join transaction currency:
|
||||||
|
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
|
||||||
|
|
||||||
|
// left join destination (for amount and account info).
|
||||||
|
$query->leftJoin(
|
||||||
|
'transactions as destination', function (JoinClause $join) {
|
||||||
|
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->where('destination.amount', '>', 0);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
// join destination account
|
||||||
|
$query->leftJoin('accounts as destination_account', 'destination_account.id', '=', 'destination.account_id');
|
||||||
|
// join destination account type
|
||||||
|
$query->leftJoin('account_types as destination_acct_type', 'destination_account.account_type_id', '=', 'destination_acct_type.id');
|
||||||
|
|
||||||
|
// left join source (for amount and account info).
|
||||||
|
$query->leftJoin(
|
||||||
|
'transactions as source', function (JoinClause $join) {
|
||||||
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->where('source.amount', '<', 0);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
// join destination account
|
||||||
|
$query->leftJoin('accounts as source_account', 'source_account.id', '=', 'source.account_id');
|
||||||
|
// join destination account type
|
||||||
|
$query->leftJoin('account_types as source_acct_type', 'source_account.account_type_id', '=', 'source_acct_type.id');
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -70,31 +70,29 @@ class Amount
|
|||||||
*/
|
*/
|
||||||
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
|
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
|
||||||
{
|
{
|
||||||
$cache = new CacheProperties;
|
$locale = setlocale(LC_MONETARY, 0);
|
||||||
$cache->addProperty($journal->id);
|
$float = floatval($journal->destination_amount);
|
||||||
$cache->addProperty('formatJournal');
|
if ($journal->isWithdrawal()) {
|
||||||
|
$float = floatval($journal->source_amount);
|
||||||
if ($cache->has()) {
|
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
|
||||||
}
|
}
|
||||||
|
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
||||||
|
$result = $formatter->formatCurrency($float, $journal->transaction_currency_code);
|
||||||
|
|
||||||
if ($journal->isTransfer() && $coloured) {
|
if ($coloured === true && $float == 0) {
|
||||||
$txt = '<span class="text-info">' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '</span>';
|
return '<span style="color:#999">' . $result . '</span>'; // always grey.
|
||||||
$cache->store($txt);
|
|
||||||
|
|
||||||
return $txt;
|
|
||||||
}
|
}
|
||||||
if ($journal->isTransfer() && !$coloured) {
|
if (!$coloured) {
|
||||||
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false);
|
return $result;
|
||||||
$cache->store($txt);
|
|
||||||
|
|
||||||
return $txt;
|
|
||||||
}
|
}
|
||||||
|
if (!$journal->isTransfer()) {
|
||||||
|
if ($float > 0) {
|
||||||
|
return '<span class="text-success">' . $result . '</span>';
|
||||||
|
}
|
||||||
|
|
||||||
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured);
|
return '<span class="text-danger">' . $result . '</span>';
|
||||||
$cache->store($txt);
|
} else {
|
||||||
|
return '<span class="text-info">' . $result . '</span>';
|
||||||
return $txt;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user