Create reconciliation transaction.

This commit is contained in:
James Cole
2017-11-22 17:49:06 +01:00
parent 69bb76b6fe
commit ba6a147032
8 changed files with 143 additions and 27 deletions

View File

@@ -109,6 +109,15 @@ interface AccountRepositoryInterface
*/
public function getCashAccount(): Account;
/**
* Find or create the opposing reconciliation account.
*
* @param Account $account
*
* @return Account|null
*/
public function getReconciliation(Account $account): ?Account;
/**
* Returns the date of the very last transaction in this account.
*

View File

@@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\User;
@@ -222,4 +223,37 @@ trait FindAccountsTrait
return $account;
}
/**
* @param Account $account
*
* @return Account|null
* @throws FireflyException
*/
public function getReconciliation(Account $account): ?Account
{
if ($account->accountType->type !== AccountType::ASSET) {
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
}
$name = $account->name . ' reconciliation';
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get();
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->name === $name) {
return $account;
}
}
// assume nothing was found. create it!
$data = [
'accountType' => 'reconcile',
'name' => $name,
'iban' => null,
'virtualBalance' => null,
'active' => true,
];
$account = $this->storeAccount($data);
return $account;
}
}

View File

@@ -67,6 +67,11 @@ trait SupportJournalsTrait
$accounts['source'] = Account::where('user_id', $user->id)->where('id', $data['source_account_id'])->first();
$accounts['destination'] = Account::where('user_id', $user->id)->where('id', $data['destination_account_id'])->first();
break;
case TransactionType::RECONCILIATION:
$accounts['source'] = $data['source'];
$accounts['destination'] = $data['destination'];
unset($data['source'], $data['destination']);
break;
default:
throw new FireflyException(sprintf('Did not recognise transaction type "%s".', $type->type));
}
@@ -229,6 +234,9 @@ trait SupportJournalsTrait
$check = 'destination';
}
switch ($transactionType->type) {
case TransactionType::RECONCILIATION:
// do nothing.
break;
case TransactionType::DEPOSIT:
case TransactionType::WITHDRAWAL:
// continue: