New command to make sure opening balance currency information is correct.

This commit is contained in:
James Cole 2019-08-18 13:01:38 +02:00
parent 98ae0efb16
commit c218a12af7
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 188 additions and 8 deletions

View File

@ -70,7 +70,7 @@ php artisan firefly-iii:cc-liabilities
php artisan firefly-iii:back-to-journals
php artisan firefly-iii:rename-account-meta
# there are 13 verify commands
# there are 14 verify commands
php artisan firefly-iii:fix-piggies
php artisan firefly-iii:create-link-types
php artisan firefly-iii:create-access-tokens
@ -80,10 +80,11 @@ php artisan firefly-iii:fix-transfer-budgets
php artisan firefly-iii:fix-uneven-amount
php artisan firefly-iii:delete-zero-amount
php artisan firefly-iii:delete-orphaned-transactions
php artisan firefly-iii:delete-empty-journals
php artisan firefly-iii:delete-empty-journals
php artisan firefly-iii:delete-empty-groups
php artisan firefly-iii:fix-account-types
php artisan firefly-iii:rename-meta-fields
php artisan firefly-iii:fix-ob-currencies
# report commands
php artisan firefly-iii:report-empty-objects

View File

@ -70,6 +70,7 @@ class CorrectDatabase extends Command
'firefly-iii:delete-empty-groups',
'firefly-iii:fix-account-types',
'firefly-iii:rename-meta-fields',
'firefly-iii:fix-ob-currencies'
];
foreach ($commands as $command) {
$this->line(sprintf('Now executing %s', $command));

View File

@ -0,0 +1,158 @@
<?php
/**
* CorrectOpeningBalanceCurrencies.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Console\Command;
use Log;
/**
* Class CorrectOpeningBalanceCurrencies
*/
class CorrectOpeningBalanceCurrencies extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Will make sure that opening balance transaction currencies match the account they\'re for.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:fix-ob-currencies';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
// get all OB journals:
$set = TransactionJournal
::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->whereNull('transaction_journals.deleted_at')
->where('transaction_types.type', TransactionType::OPENING_BALANCE)->get(['transaction_journals.*']);
$this->line(sprintf('Going to verify %d opening balance transactions.', $set->count()));
$count = 0;
/** @var TransactionJournal $journal */
foreach ($set as $journal) {
$count += $this->correctJournal($journal);
}
if ($count > 0) {
$this->line(sprintf('Corrected %d opening balance transactions.', $count));
}
if (0 === $count) {
$this->info('There was nothing to fix in the opening balance transactions.');
}
return 0;
}
/**
* @param TransactionJournal $journal
*
* @return int
*/
private function correctJournal(TransactionJournal $journal): int
{
Log::debug(sprintf('Going to correct journal #%d', $journal->id));
// get the asset account for this opening balance:
$account = $this->getAccount($journal);
if (null === $account) {
$this->warn(sprintf('Transaction journal #%d has no valid account. Cant fix this line.', $journal->id));
return 0;
}
Log::debug(sprintf('Found %s #%d "%s".', $account->accountType->type, $account->id, $account->name));
$currency = $this->getCurrency($account);
Log::debug(sprintf('Found currency #%d (%s)', $currency->id, $currency->code));
// update journal and all transactions:
$this->setCurrency($journal, $currency);
return 1;
}
/**
* @param TransactionJournal $journal
*
* @return Account|null
*/
private function getAccount(TransactionJournal $journal): ?Account
{
$excluded = [];
$transactions = $journal->transactions()->with(['account', 'account.accountType'])->get();
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$account = $transaction->account;
if (null !== $account) {
if (AccountType::INITIAL_BALANCE !== $account->accountType->type) {
return $account;
}
}
}
return null;
}
/**
* @param Account $account
*
* @return TransactionCurrency
*/
private function getCurrency(Account $account): TransactionCurrency
{
/** @var AccountRepositoryInterface $repos */
$repos = app(AccountRepositoryInterface::class);
$repos->setUser($account->user);
return $repos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
}
/**
* @param TransactionJournal $journal
* @param TransactionCurrency $currency
*/
private function setCurrency(TransactionJournal $journal, TransactionCurrency $currency): void
{
$journal->transaction_currency_id = $currency->id;
$journal->save();
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
}
}

View File

@ -53,6 +53,7 @@ class UpgradeDatabase extends Command
*/
public function handle(): int
{
$this->callInitialCommands();
@ -71,7 +72,7 @@ class UpgradeDatabase extends Command
'firefly-iii:back-to-journals',
'firefly-iii:rename-account-meta',
// there are 13 verify commands.
// there are 14 verify commands.
'firefly-iii:fix-piggies',
'firefly-iii:create-link-types',
'firefly-iii:create-access-tokens',
@ -85,6 +86,7 @@ class UpgradeDatabase extends Command
'firefly-iii:delete-empty-groups',
'firefly-iii:fix-account-types',
'firefly-iii:rename-meta-fields',
'firefly-iii:fix-ob-currencies',
// two report commands
'firefly-iii:report-empty-objects',
@ -103,8 +105,8 @@ class UpgradeDatabase extends Command
$result = Artisan::output();
echo $result;
}
// set new DB version.
app('fireflyconfig')->set('db_version', (int)config('firefly.db_version'));
// index will set FF3 version.
app('fireflyconfig')->set('ff3_version', (string)config('firefly.version'));
@ -113,8 +115,21 @@ class UpgradeDatabase extends Command
private function callInitialCommands(): void
{
Artisan::call('migrate', ['--seed' => true]);
$this->line('Now seeding the database...');
Artisan::call('migrate', ['--seed' => true, '--force' => true]);
$result = Artisan::output();
echo $result;
$this->line('Now decrypting the database (if necessary)...');
Artisan::call('firefly-iii:decrypt-all');
$result = Artisan::output();
echo $result;
$this->line('Now installing OAuth2 keys...');
Artisan::call('passport:install');
$result = Artisan::output();
echo $result;
$this->line('Done!');
}
}

View File

@ -84,7 +84,7 @@ class InstallController extends Controller
'firefly-iii:back-to-journals' => [],
'firefly-iii:rename-account-meta' => [],
// there are 13 verify commands.
// there are 14 verify commands.
'firefly-iii:fix-piggies' => [],
'firefly-iii:create-link-types' => [],
'firefly-iii:create-access-tokens' => [],
@ -98,6 +98,7 @@ class InstallController extends Controller
'firefly-iii:delete-empty-groups' => [],
'firefly-iii:fix-account-types' => [],
'firefly-iii:rename-meta-fields' => [],
'firefly-iii:fix-ob-currencies' => [],
];
}
@ -111,6 +112,9 @@ class InstallController extends Controller
// index will set FF3 version.
app('fireflyconfig')->set('ff3_version', (string)config('firefly.version'));
// set new DB version.
app('fireflyconfig')->set('db_version', (int)config('firefly.db_version'));
return view('install.index');
}

View File

@ -243,6 +243,7 @@ trait AccountServiceTrait
'destination_id' => $destId,
'destination_name' => $destName,
'user' => $account->user_id,
'currency_id' => $data['currency_id'],
'order' => 0,
'amount' => $amount,
'foreign_amount' => null,

2
public/v1/js/app.js vendored

File diff suppressed because one or more lines are too long

View File

@ -19,7 +19,7 @@
-->
<template>
<div class="form-group" v-bind:class="{ 'has-error': hasError()}" v-if="this.enabledCurrencies.length > 1">
<div class="form-group" v-bind:class="{ 'has-error': hasError()}" v-if="this.enabledCurrencies.length > 0">
<div class="col-sm-4">
<select class="form-control" ref="currency_select" name="foreign_currency[]"
v-if="this.enabledCurrencies.length > 0" @input="handleInput">