Update some code style

This commit is contained in:
James Cole
2020-07-31 15:12:26 +02:00
parent c3d8d70b34
commit e46958c23e
11 changed files with 51 additions and 117 deletions

View File

@@ -43,13 +43,11 @@ class OtherCurrenciesCorrections extends Command
public const CONFIG_NAME = '480_other_currencies';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update all journal currency information.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:other-currencies {--F|force : Force the execution of this command.}';
@@ -69,7 +67,6 @@ class OtherCurrenciesCorrections extends Command
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
@@ -96,13 +93,12 @@ class OtherCurrenciesCorrections extends Command
/**
* @param Account $account
*
* @return TransactionCurrency|null
*/
private function getCurrency(Account $account): ?TransactionCurrency
{
$accountId = $account->id;
if (isset($this->accountCurrencies[$accountId]) && 0 === $this->accountCurrencies[$accountId]) {
if (array_key_exists($accountId, $this->accountCurrencies) && 0 === $this->accountCurrencies[$accountId]) {
return null; // @codeCoverageIgnore
}
if (isset($this->accountCurrencies[$accountId]) && $this->accountCurrencies[$accountId] instanceof TransactionCurrency) {
@@ -124,9 +120,7 @@ class OtherCurrenciesCorrections extends Command
/**
* Gets the transaction that determines the transaction that "leads" and will determine
* the currency to be used by all transactions, and the journal itself.
*
* @param TransactionJournal $journal
*
* @return Transaction|null
*/
private function getLeadTransaction(TransactionJournal $journal): ?Transaction
@@ -142,19 +136,11 @@ class OtherCurrenciesCorrections extends Command
break;
case TransactionType::OPENING_BALANCE:
// whichever isn't an initial balance account:
$lead = $journal->transactions()
->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
->where('account_types.type', '!=', AccountType::INITIAL_BALANCE)
->first(['transactions.*']);
$lead = $journal->transactions()->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')->where('account_types.type', '!=', AccountType::INITIAL_BALANCE)->first(['transactions.*']);
break;
case TransactionType::RECONCILIATION:
// whichever isn't the reconciliation account:
$lead = $journal->transactions()
->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
->where('account_types.type', '!=', AccountType::RECONCILIATION)
->first(['transactions.*']);
$lead = $journal->transactions()->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')->where('account_types.type', '!=', AccountType::RECONCILIATION)->first(['transactions.*']);
break;
}
@@ -168,7 +154,7 @@ class OtherCurrenciesCorrections extends Command
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool) $configVar->data;
return (bool)$configVar->data;
}
return false; // @codeCoverageIgnore
@@ -186,7 +172,6 @@ class OtherCurrenciesCorrections extends Command
* Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is
* executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should
* be called from the handle method instead of using the constructor to initialize the command.
*
* @codeCoverageIgnore
*/
private function stupidLaravel(): void
@@ -224,36 +209,27 @@ class OtherCurrenciesCorrections extends Command
$currency = $this->getCurrency($account);
if (null === $currency) {
// @codeCoverageIgnoreStart
$this->error(
sprintf(
'Account #%d ("%s") has no currency preference, so transaction journal #%d can\'t be corrected',
$account->id,
$account->name,
$journal->id
)
);
$this->error(sprintf('Account #%d ("%s") has no currency preference, so transaction journal #%d can\'t be corrected', $account->id, $account->name, $journal->id));
$this->count++;
return;
// @codeCoverageIgnoreEnd
}
// fix each transaction:
$journal->transactions->each(
static function (Transaction $transaction) use ($currency) {
if (null === $transaction->transaction_currency_id) {
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
// when mismatch in transaction:
if (!((int) $transaction->transaction_currency_id === (int) $currency->id)) {
$transaction->foreign_currency_id = (int) $transaction->transaction_currency_id;
$transaction->foreign_amount = $transaction->amount;
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
$journal->transactions->each(static function (Transaction $transaction) use ($currency) {
if (null === $transaction->transaction_currency_id) {
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
);
// when mismatch in transaction:
if (!((int)$transaction->transaction_currency_id === (int)$currency->id)) {
$transaction->foreign_currency_id = (int)$transaction->transaction_currency_id;
$transaction->foreign_amount = $transaction->amount;
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
});
// also update the journal, of course:
$journal->transaction_currency_id = $currency->id;
$this->count++;
@@ -263,21 +239,12 @@ class OtherCurrenciesCorrections extends Command
/**
* This routine verifies that withdrawals, deposits and opening balances have the correct currency settings for
* the accounts they are linked to.
*
* Both source and destination must match the respective currency preference of the related asset account.
* So FF3 must verify all transactions.
*/
private function updateOtherJournalsCurrencies(): void
{
$set
= $this->cliRepos->getAllJournals(
[
TransactionType::WITHDRAWAL,
TransactionType::DEPOSIT,
TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,
]
);
$set = $this->cliRepos->getAllJournals([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,]);
/** @var TransactionJournal $journal */
foreach ($set as $journal) {