mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fixed a bug in the import routine that would mislabel the import account and botch any import process.
This commit is contained in:
parent
ba4bddf756
commit
a7d75ea94a
@ -35,6 +35,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->_user->accounts()->find($id);
|
return $this->_user->accounts()->find($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Account $from
|
* @param \Account $from
|
||||||
* @param \Account $to
|
* @param \Account $to
|
||||||
@ -44,7 +45,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return \TransactionType|null
|
* @return \TransactionType|null
|
||||||
*/
|
*/
|
||||||
public function transactionTypeByAccounts(\Account $from, \Account $to, $amount = 0) {
|
public function transactionTypeByAccounts(\Account $from, \Account $to, $amount = 0)
|
||||||
|
{
|
||||||
// account types for both:
|
// account types for both:
|
||||||
$toAT = $to->accountType->type;
|
$toAT = $to->accountType->type;
|
||||||
$fromAT = $from->accountType->type;
|
$fromAT = $from->accountType->type;
|
||||||
@ -83,20 +85,36 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This method finds the expense account mentioned by name. This method is a sneaky little hobbits,
|
||||||
|
* because when you feed it "Import account" it will always return an import account of that type.
|
||||||
|
*
|
||||||
* @param $name
|
* @param $name
|
||||||
*
|
*
|
||||||
* @return |Account|null
|
* @return null|\Account
|
||||||
*/
|
*/
|
||||||
public function findExpenseAccountByName($name)
|
public function findExpenseAccountByName($name)
|
||||||
{
|
{
|
||||||
|
// catch Import account:
|
||||||
|
if ($name == 'Import account') {
|
||||||
|
$importType = $this->findAccountType('Import account');
|
||||||
|
$import = $this->firstOrCreate(
|
||||||
|
[
|
||||||
|
'name' => 'Import account',
|
||||||
|
'user_id' => $this->_user->id,
|
||||||
|
'account_type_id' => $importType->id,
|
||||||
|
'active' => 1
|
||||||
|
]
|
||||||
|
);
|
||||||
|
return $import;
|
||||||
|
}
|
||||||
|
|
||||||
// find account:
|
// find account:
|
||||||
$type = $this->findAccountType('Expense account');
|
$type = $this->findAccountType('Expense account');
|
||||||
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
|
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
|
||||||
|
|
||||||
// create if not found:
|
// create if not found:
|
||||||
if(strlen($name) > 0) {
|
if (strlen($name) > 0) {
|
||||||
$set = [
|
$set = [
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'user_id' => $this->_user->id,
|
'user_id' => $this->_user->id,
|
||||||
@ -129,40 +147,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
|
|
||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
*
|
|
||||||
* @return |Account|null
|
|
||||||
*/
|
|
||||||
public function findRevenueAccountByName($name)
|
|
||||||
{
|
|
||||||
// find account:
|
|
||||||
$type = $this->findAccountType('Revenue account');
|
|
||||||
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
|
|
||||||
|
|
||||||
// find cash account as fall back:
|
|
||||||
if (is_null($account)) {
|
|
||||||
$cashType = $this->findAccountType('Cash account');
|
|
||||||
$account = $this->_user->accounts()->where('account_type_id', $cashType->id)->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
// create cash account as ultimate fall back:
|
|
||||||
if (is_null($account)) {
|
|
||||||
$set = [
|
|
||||||
'name' => 'Cash account',
|
|
||||||
'user_id' => $this->_user->id,
|
|
||||||
'active' => 1,
|
|
||||||
'account_type_id' => $cashType->id
|
|
||||||
];
|
|
||||||
$account = $this->firstOrCreate($set);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($account->active == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $account;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param $type
|
||||||
@ -179,6 +163,55 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
return \Account::firstOrCreate($data);
|
return \Account::firstOrCreate($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
*
|
||||||
|
* @return |Account|null
|
||||||
|
*/
|
||||||
|
public function findRevenueAccountByName($name)
|
||||||
|
{
|
||||||
|
// catch Import account:
|
||||||
|
if ($name == 'Import account') {
|
||||||
|
$importType = $this->findAccountType('Import account');
|
||||||
|
$import = $this->firstOrCreate(
|
||||||
|
[
|
||||||
|
'name' => 'Import account',
|
||||||
|
'user_id' => $this->_user->id,
|
||||||
|
'account_type_id' => $importType->id,
|
||||||
|
'active' => 1
|
||||||
|
]
|
||||||
|
);
|
||||||
|
return $import;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find account:
|
||||||
|
$type = $this->findAccountType('Revenue account');
|
||||||
|
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
|
||||||
|
|
||||||
|
// find cash account as fall back:
|
||||||
|
if (is_null($account)) {
|
||||||
|
$cashType = $this->findAccountType('Cash account');
|
||||||
|
$account = $this->_user->accounts()->where('account_type_id', $cashType->id)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
// create cash account as ultimate fall back:
|
||||||
|
if (is_null($account)) {
|
||||||
|
$set = [
|
||||||
|
'name' => 'Cash account',
|
||||||
|
'user_id' => $this->_user->id,
|
||||||
|
'active' => 1,
|
||||||
|
'account_type_id' => $cashType->id
|
||||||
|
];
|
||||||
|
$account = $this->firstOrCreate($set);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($account->active == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a transaction/account component and updates the transaction journal to match.
|
* Takes a transaction/account component and updates the transaction journal to match.
|
||||||
*
|
*
|
||||||
@ -265,18 +298,37 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
$importType = $this->findAccountType('Import account');
|
$importType = $this->findAccountType('Import account');
|
||||||
/** @var \Transaction $transaction */
|
/** @var \Transaction $transaction */
|
||||||
foreach ($journal->transactions as $transaction) {
|
$updated = false;
|
||||||
|
\Log::debug(
|
||||||
|
'Connect "' . $account->name . '" (#' . $account->id . ') to "' . $journal->description . '" (#'
|
||||||
|
. $journal->id . ')'
|
||||||
|
);
|
||||||
|
foreach ($journal->transactions as $index => $transaction) {
|
||||||
/*
|
/*
|
||||||
* If it's of the right type, update it!
|
* If it's of the right type, update it!
|
||||||
*/
|
*/
|
||||||
|
\Log::debug(
|
||||||
|
'Transaction ' . $index . ' (#' . $transaction->id . '): [' . $transaction->account->account_type_id
|
||||||
|
. ' vs. ' . $importType->id . ']'
|
||||||
|
);
|
||||||
if ($transaction->account->account_type_id == $importType->id) {
|
if ($transaction->account->account_type_id == $importType->id) {
|
||||||
$transaction->account()->associate($account);
|
$transaction->account()->associate($account);
|
||||||
$transaction->save();
|
$transaction->save();
|
||||||
|
$updated = true;
|
||||||
|
\Log::debug(
|
||||||
|
'Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ($updated === false) {
|
||||||
|
\Log::error(
|
||||||
|
'Did not connect transactions of journal #' . $journal->id . ' to expense account ' . $account->id
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
$journal->save();
|
$journal->save();
|
||||||
\Log::debug('Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"');
|
|
||||||
|
|
||||||
$importMap->jobsdone++;
|
$importMap->jobsdone++;
|
||||||
$importMap->save();
|
$importMap->save();
|
||||||
@ -536,7 +588,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
|||||||
* create new transaction journal (and transactions):
|
* create new transaction journal (and transactions):
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$set =[
|
$set = [
|
||||||
'account_from_id' => $initial->id,
|
'account_from_id' => $initial->id,
|
||||||
'account_to_id' => $account->id,
|
'account_to_id' => $account->id,
|
||||||
'description' => 'Initial Balance for ' . $account->name,
|
'description' => 'Initial Balance for ' . $account->name,
|
||||||
|
@ -199,6 +199,18 @@ use LaravelBook\Ardent\Builder;
|
|||||||
* 'Budget[] $budgets
|
* 'Budget[] $budgets
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
* 'Category[] $categories
|
* 'Category[] $categories
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Budget[] $budgets
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Category[] $categories
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Budget[] $budgets
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Category[] $categories
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Budget[] $budgets
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||||
|
* 'Category[] $categories
|
||||||
*/
|
*/
|
||||||
class TransactionJournal extends Ardent
|
class TransactionJournal extends Ardent
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user