2016-10-14 23:19:21 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* UpgradeDatabase.php
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-08 00:00:51 -05:00
|
|
|
declare(strict_types=1);
|
2016-10-14 23:19:21 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Console\Commands;
|
|
|
|
|
|
|
|
|
|
|
|
use DB;
|
2017-04-14 00:11:30 -05:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountMeta;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2016-12-29 13:19:20 -06:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2017-04-08 00:00:51 -05:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2016-10-14 23:19:21 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2017-04-14 00:11:30 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2016-10-14 23:19:21 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2017-04-08 00:00:51 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2016-10-14 23:19:21 -05:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Log;
|
2017-04-14 00:11:30 -05:00
|
|
|
use Preferences;
|
2016-11-07 13:25:09 -06:00
|
|
|
use Schema;
|
2016-10-14 23:19:21 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class UpgradeDatabase
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Console\Commands
|
|
|
|
*/
|
|
|
|
class UpgradeDatabase extends Command
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Will run various commands to update database records.';
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'firefly:upgrade-database';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$this->setTransactionIdentifier();
|
2016-12-29 13:19:20 -06:00
|
|
|
$this->migrateRepetitions();
|
2017-04-08 00:00:51 -05:00
|
|
|
$this->repairPiggyBanks();
|
2017-04-14 00:11:30 -05:00
|
|
|
$this->updateAccountCurrencies();
|
|
|
|
$this->info('Firefly III database is up to date.');
|
2016-12-29 13:19:20 -06:00
|
|
|
}
|
|
|
|
|
2017-04-14 00:11:30 -05:00
|
|
|
/**
|
|
|
|
* Migrate budget repetitions to new format.
|
|
|
|
*/
|
2016-12-29 13:19:20 -06:00
|
|
|
private function migrateRepetitions()
|
|
|
|
{
|
|
|
|
if (!Schema::hasTable('budget_limits')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get all budget limits with end_date NULL
|
|
|
|
$set = BudgetLimit::whereNull('end_date')->get();
|
2017-04-08 00:00:51 -05:00
|
|
|
if ($set->count() > 0) {
|
|
|
|
$this->line(sprintf('Found %d budget limit(s) to update', $set->count()));
|
|
|
|
}
|
2016-12-29 13:19:20 -06:00
|
|
|
/** @var BudgetLimit $budgetLimit */
|
|
|
|
foreach ($set as $budgetLimit) {
|
|
|
|
// get limit repetition (should be just one):
|
|
|
|
/** @var LimitRepetition $repetition */
|
|
|
|
$repetition = $budgetLimit->limitrepetitions()->first();
|
|
|
|
if (!is_null($repetition)) {
|
|
|
|
$budgetLimit->end_date = $repetition->enddate;
|
|
|
|
$budgetLimit->save();
|
|
|
|
$this->line(sprintf('Updated budget limit #%d', $budgetLimit->id));
|
|
|
|
$repetition->delete();
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 23:19:21 -05:00
|
|
|
}
|
|
|
|
|
2017-04-08 00:00:51 -05:00
|
|
|
/**
|
|
|
|
* Make sure there are only transfers linked to piggy bank events.
|
|
|
|
*/
|
|
|
|
private function repairPiggyBanks()
|
|
|
|
{
|
|
|
|
// if table does not exist, return false
|
|
|
|
if (!Schema::hasTable('piggy_bank_events')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$set = PiggyBankEvent::with(['PiggyBank', 'TransactionJournal', 'TransactionJournal.TransactionType'])->get();
|
|
|
|
/** @var PiggyBankEvent $event */
|
|
|
|
foreach ($set as $event) {
|
|
|
|
|
2017-04-08 11:00:45 -05:00
|
|
|
if (is_null($event->transaction_journal_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $event->transactionJournal()->first();
|
|
|
|
if (is_null($journal)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-08 00:00:51 -05:00
|
|
|
|
2017-04-08 11:00:45 -05:00
|
|
|
$type = $journal->transactionType->type;
|
|
|
|
if ($type !== TransactionType::TRANSFER) {
|
|
|
|
$event->transaction_journal_id = null;
|
|
|
|
$event->save();
|
|
|
|
$this->line(
|
|
|
|
sprintf(
|
|
|
|
'Piggy bank #%d ("%s") was referenced by an invalid event. This has been fixed.', $event->piggy_bank_id,
|
|
|
|
$event->piggyBank->name
|
|
|
|
)
|
|
|
|
);
|
2017-04-08 00:00:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:19:21 -05:00
|
|
|
/**
|
|
|
|
* This is strangely complex, because the HAVING modifier is a no-no. And subqueries in Laravel are weird.
|
|
|
|
*/
|
|
|
|
private function setTransactionIdentifier()
|
|
|
|
{
|
2016-11-07 13:25:09 -06:00
|
|
|
// if table does not exist, return false
|
|
|
|
if (!Schema::hasTable('transaction_journals')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-28 13:38:03 -06:00
|
|
|
$subQuery = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2016-12-14 11:59:12 -06:00
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->whereNull('transactions.deleted_at')
|
|
|
|
->groupBy(['transaction_journals.id'])
|
|
|
|
->select(['transaction_journals.id', DB::raw('COUNT(transactions.id) AS t_count')]);
|
2016-10-14 23:19:21 -05:00
|
|
|
|
|
|
|
$result = DB::table(DB::raw('(' . $subQuery->toSql() . ') AS derived'))
|
|
|
|
->mergeBindings($subQuery->getQuery())
|
|
|
|
->where('t_count', '>', 2)
|
|
|
|
->select(['id', 't_count']);
|
|
|
|
$journalIds = array_unique($result->pluck('id')->toArray());
|
|
|
|
|
|
|
|
foreach ($journalIds as $journalId) {
|
2016-12-29 13:19:20 -06:00
|
|
|
$this->updateJournal(intval($journalId));
|
2016-12-27 12:59:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 00:11:30 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function updateAccountCurrencies()
|
|
|
|
{
|
|
|
|
$accounts = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
|
|
|
->whereIn('account_types.type', [AccountType::DEFAULT, AccountType::ASSET])->get(['accounts.*']);
|
|
|
|
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
// get users preference, fall back to system pref.
|
|
|
|
$defaultCurrencyCode = Preferences::getForUser($account->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data;
|
|
|
|
$defaultCurrency = TransactionCurrency::where('code', $defaultCurrencyCode)->first();
|
|
|
|
$accountCurrency = intval($account->getMeta('currency_id'));
|
|
|
|
$openingBalance = $account->getOpeningBalance();
|
|
|
|
$openingBalanceCurrency = intval($openingBalance->transaction_currency_id);
|
|
|
|
|
|
|
|
// both 0? set to default currency:
|
|
|
|
if ($accountCurrency === 0 && $openingBalanceCurrency === 0) {
|
|
|
|
AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $defaultCurrency->id]);
|
|
|
|
$this->line(sprintf('Account #%d ("%s") now has a currency setting (%s).', $account->id, $account->name, $defaultCurrencyCode));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// opening balance 0, account not zero? just continue:
|
|
|
|
if ($accountCurrency > 0 && $openingBalanceCurrency === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// account is set to 0, opening balance is not?
|
|
|
|
if ($accountCurrency === 0 && $openingBalanceCurrency > 0) {
|
|
|
|
AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $openingBalanceCurrency]);
|
|
|
|
$this->line(sprintf('Account #%d ("%s") now has a currency setting (%s).', $account->id, $account->name, $defaultCurrencyCode));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both are equal, just continue:
|
|
|
|
if ($accountCurrency === $openingBalanceCurrency) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// do not match:
|
|
|
|
if ($accountCurrency !== $openingBalanceCurrency) {
|
|
|
|
// update opening balance:
|
|
|
|
$openingBalance->transaction_currency_id = $accountCurrency;
|
|
|
|
$openingBalance->save();
|
|
|
|
$this->line(sprintf('Account #%d ("%s") now has a correct currency for opening balance.', $account->id, $account->name));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-27 12:59:56 -06:00
|
|
|
/**
|
|
|
|
* grab all positive transactiosn from this journal that are not deleted. for each one, grab the negative opposing one
|
|
|
|
* which has 0 as an identifier and give it the same identifier.
|
|
|
|
*
|
|
|
|
* @param int $journalId
|
|
|
|
*/
|
|
|
|
private function updateJournal(int $journalId)
|
|
|
|
{
|
|
|
|
$identifier = 0;
|
|
|
|
$processed = [];
|
|
|
|
$transactions = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->get();
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
// find opposing:
|
|
|
|
$amount = bcmul(strval($transaction->amount), '-1');
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var Transaction $opposing */
|
|
|
|
$opposing = Transaction::where('transaction_journal_id', $journalId)
|
|
|
|
->where('amount', $amount)->where('identifier', '=', 0)
|
|
|
|
->whereNotIn('id', $processed)
|
|
|
|
->first();
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
$this->error('Firefly III could not find the "identifier" field in the "transactions" table.');
|
|
|
|
$this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version')));
|
|
|
|
$this->error('Please run "php artisan migrate" to add this field to the table.');
|
|
|
|
$this->info('Then, run "php artisan firefly:upgrade-database" to try again.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!is_null($opposing)) {
|
|
|
|
// give both a new identifier:
|
|
|
|
$transaction->identifier = $identifier;
|
|
|
|
$transaction->save();
|
|
|
|
$opposing->identifier = $identifier;
|
|
|
|
$opposing->save();
|
|
|
|
$processed[] = $transaction->id;
|
|
|
|
$processed[] = $opposing->id;
|
2016-10-14 23:19:21 -05:00
|
|
|
}
|
2016-12-27 12:59:56 -06:00
|
|
|
$identifier++;
|
2016-10-14 23:19:21 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 05:42:44 -05:00
|
|
|
}
|