Various cleanup in transaction and journal display code.

This commit is contained in:
James Cole committed 2017-11-04 07:10:21 +01:00
1 parent b4dc70244a
commit bb46d034cd
25 files changed
+885 -543

No files matched your search

-136
View File
@@ -267,140 +267,4 @@ class Amount
];
}
/**
* @param TransactionJournal $journal
* @param bool $coloured
*
* @return string
*/
public function journalAmount(TransactionJournal $journal, bool $coloured = true): string
{
$amounts = [];
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
/** @var TransactionModel $transaction */
foreach ($transactions as $transaction) {
// model some fields to fit "transactionAmount()":
$transaction->transaction_amount = $transaction->amount;
$transaction->transaction_foreign_amount = $transaction->foreign_amount;
$transaction->transaction_type_type = $journal->transactionType->type;
$transaction->transaction_currency_symbol = $transaction->transactionCurrency->symbol;
$transaction->transaction_currency_dp = $transaction->transactionCurrency->decimal_places;
if (!is_null($transaction->foreign_currency_id)) {
$transaction->foreign_currency_symbol = $transaction->foreignCurrency->symbol;
$transaction->foreign_currency_dp = $transaction->foreignCurrency->decimal_places;
}
$amounts[] = $this->transactionAmount($transaction, $coloured);
}
return join(' / ', $amounts);
}
/**
* @param TransactionJournal $journal
* @param bool $coloured
*
* @return string
*/
public function journalTotalAmount(TransactionJournal $journal, bool $coloured = true): string
{
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
$totals = [];
$type = $journal->transactionType->type;
/** @var TransactionModel $transaction */
foreach ($transactions as $transaction) {
// model some fields to fit "transactionAmount()":
$currencyId = $transaction->transaction_currency_id;
if (!isset($totals[$currencyId])) {
$totals[$currencyId] = [
'amount' => '0',
'symbol' => $transaction->transactionCurrency->symbol,
'dp' => $transaction->transactionCurrency->decimal_places,
];
}
$totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']);
if (!is_null($transaction->foreign_currency_id)) {
$foreignId = $transaction->foreign_currency_id;
if (!isset($totals[$foreignId])) {
$totals[$foreignId] = [
'amount' => '0',
'symbol' => $transaction->foreignCurrency->symbol,
'dp' => $transaction->foreignCurrency->decimal_places,
];
}
$totals[$foreignId]['amount'] = bcadd($transaction->foreign_amount, $totals[$foreignId]['amount']);
}
}
$array = [];
foreach ($totals as $total) {
$currency = new TransactionCurrency;
$currency->symbol = $total['symbol'];
$currency->decimal_places = $total['dp'];
if ($type === TransactionType::WITHDRAWAL) {
$total['amount'] = bcmul($total['amount'], '-1');
}
$array[] = $this->formatAnything($currency, $total['amount']);
}
return join(' / ', $array);
}
/**
* This formats a transaction, IF that transaction has been "collected" using the JournalCollector.
*
* @param TransactionModel $transaction
* @param bool $coloured
*
* @return string
*/
public function transactionAmount(TransactionModel $transaction, bool $coloured = true): string
{
$amount = bcmul(app('steam')->positive(strval($transaction->transaction_amount)), '-1');
$format = '%s';
if ($transaction->transaction_type_type === TransactionType::DEPOSIT) {
$amount = bcmul($amount, '-1');
}
if ($transaction->transaction_type_type === TransactionType::TRANSFER) {
$amount = app('steam')->positive($amount);
$coloured = false;
$format = '<span class="text-info">%s</span>';
}
if ($transaction->transaction_type_type === TransactionType::OPENING_BALANCE) {
$amount = strval($transaction->transaction_amount);
}
$currency = new TransactionCurrency;
$currency->symbol = $transaction->transaction_currency_symbol;
$currency->decimal_places = $transaction->transaction_currency_dp;
$str = sprintf($format, $this->formatAnything($currency, $amount, $coloured));
if (!is_null($transaction->transaction_foreign_amount)) {
$amount = bcmul(app('steam')->positive(strval($transaction->transaction_foreign_amount)), '-1');
if ($transaction->transaction_type_type === TransactionType::DEPOSIT) {
$amount = bcmul($amount, '-1');
}
if ($transaction->transaction_type_type === TransactionType::TRANSFER) {
$amount = app('steam')->positive($amount);
$coloured = false;
$format = '<span class="text-info">%s</span>';
}
$currency = new TransactionCurrency;
$currency->symbol = $transaction->foreign_currency_symbol;
$currency->decimal_places = $transaction->foreign_currency_dp;
$str .= ' (' . sprintf($format, $this->formatAnything($currency, $amount, $coloured)) . ')';
}
return $str;
}
}