mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add some code to fix liabilities.
This commit is contained in:
parent
39925f8139
commit
e3b93af297
175
app/Console/Commands/Upgrade/UpgradeLiabilities.php
Normal file
175
app/Console/Commands/Upgrade/UpgradeLiabilities.php
Normal file
@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Console\Commands\Upgrade;
|
||||
|
||||
use FireflyIII\Factory\AccountMetaFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class UpgradeLiabilities
|
||||
*/
|
||||
class UpgradeLiabilities extends Command
|
||||
{
|
||||
public const CONFIG_NAME = '560_upgrade_liabilities';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Upgrade liabilities to new 5.6.0 structure.';
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'firefly-iii:upgrade-liabilities {--F|force : Force the execution of this command.}';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$start = microtime(true);
|
||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||
$this->warn('This command has already been executed.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
$this->upgradeLiabilities();
|
||||
|
||||
//$this->markAsExecuted();
|
||||
|
||||
$end = round(microtime(true) - $start, 2);
|
||||
$this->info(sprintf('in %s seconds.', $end));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isExecuted(): bool
|
||||
{
|
||||
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
||||
if (null !== $configVar) {
|
||||
return (bool)$configVar->data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param TransactionJournal $openingBalance
|
||||
*/
|
||||
private function correctOpeningBalance(Account $account, TransactionJournal $openingBalance): void
|
||||
{
|
||||
$source = $this->getSourceTransaction($openingBalance);
|
||||
$destination = $this->getDestinationTransaction($openingBalance);
|
||||
if (null === $source || null === $destination) {
|
||||
return;
|
||||
}
|
||||
// source MUST be the liability.
|
||||
if ((int)$destination->account_id === (int)$account->id) {
|
||||
Log::debug(sprintf('Must switch around, because account #%d is the destination.', $destination->account_id));
|
||||
// so if not, switch things around:
|
||||
$sourceAccountId = (int)$source->account_id;
|
||||
$source->account_id = $destination->account_id;
|
||||
$destination->account_id = $sourceAccountId;
|
||||
$source->save();
|
||||
$destination->save();
|
||||
Log::debug(sprintf('Source transaction #%d now has account #%d', $source->id, $source->account_id));
|
||||
Log::debug(sprintf('Dest transaction #%d now has account #%d', $destination->id, $destination->account_id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
*/
|
||||
private function getSourceTransaction(TransactionJournal $journal): ?Transaction
|
||||
{
|
||||
return $journal->transactions()->where('amount', '<', 0)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
*/
|
||||
private function getDestinationTransaction(TransactionJournal $journal): ?Transaction
|
||||
{
|
||||
return $journal->transactions()->where('amount', '>', 0)->first();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function markAsExecuted(): void
|
||||
{
|
||||
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function upgradeLiabilities(): void
|
||||
{
|
||||
Log::debug('Upgrading liabilities.');
|
||||
$users = User::get();
|
||||
/** @var User $user */
|
||||
foreach ($users as $user) {
|
||||
$this->upgradeForUser($user);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
private function upgradeForUser(User $user): void
|
||||
{
|
||||
Log::debug(sprintf('Upgrading liabilities for user #%d', $user->id));
|
||||
$accounts = $user->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('account_types.type', config('firefly.valid_liabilities'))
|
||||
->get(['accounts.*']);
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
$this->upgradeLiability($account);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
private function upgradeLiability(Account $account): void
|
||||
{
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
Log::debug(sprintf('Upgrade liability #%d', $account->id));
|
||||
|
||||
// get opening balance, and correct if necessary.
|
||||
$openingBalance = $repository->getOpeningBalance($account);
|
||||
if (null !== $openingBalance) {
|
||||
// correct if necessary
|
||||
$this->correctOpeningBalance($account, $openingBalance);
|
||||
}
|
||||
|
||||
// add liability direction property
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
$factory->crud($account, 'liability_direction', 'debit');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user