Expanded Amount and expanded TransactionJournal to include source and destination information.

This commit is contained in:
James Cole 2016-03-02 12:25:00 +01:00
parent 869360f26c
commit f2c1dd41d0
2 changed files with 59 additions and 22 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\JoinClause;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Watson\Validating\ValidatingTrait;
@ -56,7 +57,19 @@ class TransactionJournal extends Model
const QUERYFIELDS
= [
'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 */
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:
$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');
}

View File

@ -70,31 +70,29 @@ class Amount
*/
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('formatJournal');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
$locale = setlocale(LC_MONETARY, 0);
$float = floatval($journal->destination_amount);
if ($journal->isWithdrawal()) {
$float = floatval($journal->source_amount);
}
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$result = $formatter->formatCurrency($float, $journal->transaction_currency_code);
if ($journal->isTransfer() && $coloured) {
$txt = '<span class="text-info">' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '</span>';
$cache->store($txt);
return $txt;
if ($coloured === true && $float == 0) {
return '<span style="color:#999">' . $result . '</span>'; // always grey.
}
if ($journal->isTransfer() && !$coloured) {
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false);
$cache->store($txt);
return $txt;
if (!$coloured) {
return $result;
}
if (!$journal->isTransfer()) {
if ($float > 0) {
return '<span class="text-success">' . $result . '</span>';
}
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured);
$cache->store($txt);
return $txt;
return '<span class="text-danger">' . $result . '</span>';
} else {
return '<span class="text-info">' . $result . '</span>';
}
}
/**