Big update to properly support multi currencies.

This commit is contained in:
James Cole
2017-06-04 13:39:16 +02:00
parent 771ebde295
commit 82e74a2afd
34 changed files with 470 additions and 498 deletions

View File

@@ -17,6 +17,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Preferences as Prefs;
@@ -101,17 +102,6 @@ class Amount
return $format;
}
/**
* @param string $amount
* @param bool $coloured
*
* @return string
*/
public function format(string $amount, bool $coloured = true): string
{
return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
}
/**
* This method will properly format the given number, in color or "black and white",
* as a currency, given two things: the currency required and the current locale.
@@ -159,49 +149,6 @@ class Amount
return $result;
}
/**
* Used in many places (unfortunately).
*
* @param string $currencyCode
* @param string $amount
* @param bool $coloured
*
* @return string
*/
public function formatByCode(string $currencyCode, string $amount, bool $coloured = true): string
{
$currency = TransactionCurrency::where('code', $currencyCode)->first();
return $this->formatAnything($currency, $amount, $coloured);
}
/**
*
* @param \FireflyIII\Models\TransactionJournal $journal
* @param bool $coloured
*
* @return string
*/
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
{
$currency = $journal->transactionCurrency;
return $this->formatAnything($currency, $journal->amount(), $coloured);
}
/**
* @param Transaction $transaction
* @param bool $coloured
*
* @return string
*/
public function formatTransaction(Transaction $transaction, bool $coloured = true)
{
$currency = $transaction->transactionJournal->transactionCurrency;
return $this->formatAnything($currency, strval($transaction->amount), $coloured);
}
/**
* @return Collection
*/

View File

@@ -37,15 +37,8 @@ class JournalList implements BinderInterface
$ids = explode(',', $value);
/** @var \Illuminate\Support\Collection $object */
$object = TransactionJournal::whereIn('transaction_journals.id', $ids)
->expanded()
->where('transaction_journals.user_id', auth()->user()->id)
->get(
[
'transaction_journals.*',
'transaction_types.type AS transaction_type_type',
'transaction_currencies.code AS transaction_currency_code',
]
);
->get(['transaction_journals.*',]);
if ($object->count() > 0) {
return $object;

View File

@@ -213,6 +213,14 @@ trait TransactionJournalTrait
return 0;
}
/**
* @return Transaction
*/
public function positiveTransaction(): Transaction
{
return $this->transactions()->where('amount', '>', 0)->first();
}
/**
* @return Collection
*/

View File

@@ -1,63 +0,0 @@
<?php
/**
* Account.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount as AmountFacade;
use Twig_Extension;
use Twig_SimpleFunction;
/**
* Class Account
*
* @package FireflyIII\Support\Twig
*/
class Account extends Twig_Extension
{
/**
* {@inheritDoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
];
}
/**
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return Twig_SimpleFunction
*/
protected function formatAmountByAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatAmountByAccount', function (AccountModel $account, string $amount, bool $coloured = true): string {
$currencyId = intval($account->getMeta('currency_id'));
if ($currencyId === 0) {
// Format using default currency:
return AmountFacade::format($amount, $coloured);
}
$currency = TransactionCurrency::find($currencyId);
return AmountFacade::formatAnything($currency, $amount, $coloured);
}, ['is_safe' => ['html']]
);
}
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* AmountFormat.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Contains all amount formatting routines.
*
* @package FireflyIII\Support\Twig
*/
class AmountFormat extends Twig_Extension
{
/**
* {@inheritDoc}
*/
public function getFilters(): array
{
return [
$this->formatAmount(),
$this->formatAmountPlain(),
];
}
/**
* {@inheritDoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
];
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName(): string
{
return 'FireflyIII\Support\Twig\AmountFormat';
}
/**
*
* @return Twig_SimpleFilter
*/
protected function formatAmount(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'formatAmount', function (string $string): string {
return app('amount')->format($string);
}, ['is_safe' => ['html']]
);
}
/**
* Will format the amount by the currency related to the given account.
*
* @return Twig_SimpleFunction
*/
protected function formatAmountByAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatAmountByAccount', function (AccountModel $account, string $amount, bool $coloured = true): string {
$currencyId = intval($account->getMeta('currency_id'));
if ($currencyId === 0) {
// Format using default currency:
return app('amount')->format($amount, $coloured);
}
$currency = TransactionCurrency::find($currencyId);
return app('amount')->formatAnything($currency, $amount, $coloured);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmountPlain(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'formatAmountPlain', function (string $string): string {
return app('amount')->format($string, false);
}, ['is_safe' => ['html']]
);
}
}

View File

@@ -38,9 +38,6 @@ class General extends Twig_Extension
public function getFilters(): array
{
return [
$this->formatAmount(),
$this->formatAmountPlain(),
$this->formatJournal(),
$this->balance(),
$this->formatFilesize(),
$this->mimeIcon(),
@@ -173,33 +170,6 @@ class General extends Twig_Extension
);
}
/**
*
* @return Twig_SimpleFilter
*/
protected function formatAmount(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'formatAmount', function (string $string): string {
return app('amount')->format($string);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmountPlain(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'formatAmountPlain', function (string $string): string {
return app('amount')->format($string, false);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFilter
*/
@@ -223,17 +193,6 @@ class General extends Twig_Extension
);
}
/**
* @return Twig_SimpleFilter
*/
protected function formatJournal(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'formatJournal', function (TransactionJournal $journal): string {
return app('amount')->formatJournal($journal);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFunction

View File

@@ -16,7 +16,6 @@ namespace FireflyIII\Support\Twig;
use Amount;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use Steam;
use Twig_Extension;
@@ -30,49 +29,6 @@ use Twig_SimpleFunction;
*/
class Transaction extends Twig_Extension
{
/**
* @return Twig_SimpleFunction
*/
public function formatAnything(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatAnything', function (TransactionCurrency $currency, string $amount): string {
return Amount::formatAnything($currency, $amount, true);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFunction
*/
public function formatAnythingPlain(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatAnythingPlain', function (TransactionCurrency $currency, string $amount): string {
return Amount::formatAnything($currency, $amount, false);
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFunction
*/
public function formatByCode(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatByCode', function (string $currencyCode, string $amount): string {
return Amount::formatByCode($currencyCode, $amount, true);
}, ['is_safe' => ['html']]
);
}
/**
* @return array
*/
@@ -91,17 +47,13 @@ class Transaction extends Twig_Extension
public function getFunctions(): array
{
$functions = [
$this->formatAnything(),
$this->formatAnythingPlain(),
$this->transactionSourceAccount(),
$this->transactionDestinationAccount(),
$this->optionalJournalAmount(),
$this->transactionBudgets(),
$this->transactionIdBudgets(),
$this->transactionCategories(),
$this->transactionIdCategories(),
$this->splitJournalIndicator(),
$this->formatByCode(),
];
return $functions;
@@ -117,33 +69,6 @@ class Transaction extends Twig_Extension
return 'transaction';
}
/**
* @return Twig_SimpleFunction
*/
public function optionalJournalAmount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'optionalJournalAmount', function (int $journalId, string $transactionAmount, string $code, string $type): string {
// get amount of journal:
$amount = strval(TransactionModel::where('transaction_journal_id', $journalId)->whereNull('deleted_at')->where('amount', '<', 0)->sum('amount'));
// display deposit and transfer positive
if ($type === TransactionType::DEPOSIT || $type === TransactionType::TRANSFER) {
$amount = bcmul($amount, '-1');
}
// not equal to transaction amount?
if (bccomp($amount, $transactionAmount) !== 0 && bccomp($amount, bcmul($transactionAmount, '-1')) !== 0) {
//$currency =
return sprintf(' (%s)', Amount::formatByCode($code, $amount, true));
}
return '';
}, ['is_safe' => ['html']]
);
}
/**
* @return Twig_SimpleFunction
*/