Move some stuff around.

This commit is contained in:
James Cole
2016-05-20 09:45:24 +02:00
parent 65b8882ed4
commit 724f423692
14 changed files with 58 additions and 80 deletions

View File

@@ -64,23 +64,18 @@ class AccountCrud implements AccountCrudInterface
} }
/** /**
* @param Account $account * @param $accountId
* *
* @return TransactionJournal|null * @return Account
*/ */
public function openingBalanceTransaction(Account $account): TransactionJournal public function find(int $accountId): Account
{ {
$journal = TransactionJournal $account = $this->user->accounts()->find($accountId);
::sortCorrectly() if (is_null($account)) {
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') $account = new Account;
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (is_null($journal)) {
return new TransactionJournal;
} }
return $journal; return $account;
} }
/** /**
@@ -339,4 +334,24 @@ class AccountCrud implements AccountCrudInterface
} }
} }
/**
* @param Account $account
*
* @return TransactionJournal|null
*/
private function openingBalanceTransaction(Account $account): TransactionJournal
{
$journal = TransactionJournal
::sortCorrectly()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
if (is_null($journal)) {
return new TransactionJournal;
}
return $journal;
}
} }

View File

@@ -13,7 +13,6 @@ namespace FireflyIII\Crud\Account;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\TransactionJournal;
/** /**
* Interface AccountCrudInterface * Interface AccountCrudInterface
@@ -31,12 +30,11 @@ interface AccountCrudInterface
public function destroy(Account $account, Account $moveTo): bool; public function destroy(Account $account, Account $moveTo): bool;
/** /**
* @param int $accountId
* *
* @param Account $account * @return Account
*
* @return TransactionJournal
*/ */
public function openingBalanceTransaction(Account $account) : TransactionJournal; public function find(int $accountId): Account;
/** /**
* @param array $data * @param array $data

View File

@@ -3,7 +3,6 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class AccountId * Class AccountId
@@ -18,10 +17,10 @@ class AccountId extends BasicConverter implements ConverterInterface
*/ */
public function convert(): Account public function convert(): Account
{ {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; $var = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$account = $repository->find($var); $account = $crud->find($var);
return $account; return $account;
} }

View File

@@ -23,17 +23,18 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find(intval($this->mapped[$this->index][$this->value])); $account = $crud->find(intval($this->mapped[$this->index][$this->value]));
return $account; return $account;
} }
if (strlen($this->value) > 0) { if (strlen($this->value) > 0) {
$account = $this->searchOrCreate($repository); $account = $this->searchOrCreate($repository, $crud);
return $account; return $account;
} }

View File

@@ -4,7 +4,6 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@@ -23,12 +22,10 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
/** @var AccountCrudInterface $crud */
$crud = app(AccountCrudInterface::class);
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find(intval($this->mapped[$this->index][$this->value])); $account = $crud->find(intval($this->mapped[$this->index][$this->value]));
return $account; return $account;
} }

View File

@@ -13,7 +13,6 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@@ -32,13 +31,11 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
/** @var AccountCrudInterface $crud */
$crud = app(AccountCrudInterface::class);
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find(intval($this->mapped[$this->index][$this->value])); $account = $crud->find(intval($this->mapped[$this->index][$this->value]));
return $account; return $account;
} }

View File

@@ -22,9 +22,10 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find($this->mapped[$this->index][$this->value]); $account = $crud->find($this->mapped[$this->index][$this->value]);
return $account; return $account;
} }

View File

@@ -3,7 +3,6 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class OpposingAccountId * Class OpposingAccountId
@@ -19,10 +18,9 @@ class OpposingAccountId extends BasicConverter implements ConverterInterface
*/ */
public function convert(): Account public function convert(): Account
{ {
/** @var AccountRepositoryInterface $repository */ $crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$repository = app(AccountRepositoryInterface::class); $value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value;
$value = isset($this->mapped[$this->index][$this->value]) ? $this->mapped[$this->index][$this->value] : $this->value; $account = $crud->find($value);
$account = $repository->find($value);
return $account; return $account;
} }

View File

@@ -3,7 +3,6 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class OpposingAccountName * Class OpposingAccountName
@@ -20,11 +19,10 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var AccountRepositoryInterface $repository */ $crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$repository = app(AccountRepositoryInterface::class);
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = $repository->find($this->mapped[$this->index][$this->value]); $account = $crud->find($this->mapped[$this->index][$this->value]);
return $account; return $account;
} }

View File

@@ -7,7 +7,6 @@ use Carbon\Carbon;
use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Validator; use Validator;
/** /**
@@ -246,8 +245,6 @@ class AssetAccount implements PostProcessorInterface
} }
} }
// create new if not exists and return that one: // create new if not exists and return that one:
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accountData = [ $accountData = [
'name' => $accountNumber, 'name' => $accountNumber,
'accountType' => 'asset', 'accountType' => 'asset',

View File

@@ -84,18 +84,17 @@ class AccountController extends Controller
} }
/** /**
* @param ARI $repository
* @param AccountCrudInterface $crud * @param AccountCrudInterface $crud
* @param Account $account * @param Account $account
* *
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/ */
public function destroy(ARI $repository, AccountCrudInterface $crud, Account $account) public function destroy(AccountCrudInterface $crud, Account $account)
{ {
$type = $account->accountType->type; $type = $account->accountType->type;
$typeName = config('firefly.shortNamesByFullName.' . $type); $typeName = config('firefly.shortNamesByFullName.' . $type);
$name = $account->name; $name = $account->name;
$moveTo = $repository->find(intval(Input::get('move_account_before_delete'))); $moveTo = $crud->find(intval(Input::get('move_account_before_delete')));
$crud->destroy($account, $moveTo); $crud->destroy($account, $moveTo);

View File

@@ -88,10 +88,8 @@ class ReportController extends Controller
/** @var BudgetRepositoryInterface $budgetRepository */ /** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class); $budgetRepository = app(BudgetRepositoryInterface::class);
$budget = $budgetRepository->find(intval($attributes['budgetId'])); $budget = $budgetRepository->find(intval($attributes['budgetId']));
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
/** @var AccountRepositoryInterface $accountRepository */ $account = $crud->find(intval($attributes['accountId']));
$accountRepository = app(AccountRepositoryInterface::class);
$account = $accountRepository->find(intval($attributes['accountId']));
switch (true) { switch (true) {
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)): case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)):
@@ -183,7 +181,8 @@ class ReportController extends Controller
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$account = $repository->find(intval($attributes['accountId'])); $crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$account = $crud->find(intval($attributes['accountId']));
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
$journals = $repository->journalsInPeriod($attributes['accounts'], $types, $attributes['startDate'], $attributes['endDate']); $journals = $repository->journalsInPeriod($attributes['accounts'], $types, $attributes['startDate'], $attributes['endDate']);
@@ -213,7 +212,8 @@ class ReportController extends Controller
{ {
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$account = $repository->find(intval($attributes['accountId'])); $crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$account = $crud->find(intval($attributes['accountId']));
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
$journals = $repository->journalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']); $journals = $repository->journalsInPeriod(new Collection([$account]), $types, $attributes['startDate'], $attributes['endDate']);
$destinations = $attributes['accounts']->pluck('id')->toArray(); $destinations = $attributes['accounts']->pluck('id')->toArray();
@@ -221,8 +221,8 @@ class ReportController extends Controller
$journals = $journals->filter( $journals = $journals->filter(
function (TransactionJournal $journal) use ($account, $destinations) { function (TransactionJournal $journal) use ($account, $destinations) {
if ( if (
$journal->source_account_id === $account->id && $journal->source_account_id === $account->id
in_array($journal->destination_account_id, $destinations) && in_array($journal->destination_account_id, $destinations)
) { ) {
return $journal; return $journal;
} }

View File

@@ -170,21 +170,6 @@ class AccountRepository implements AccountRepositoryInterface
return $journals; return $journals;
} }
/**
* @param $accountId
*
* @return Account
*/
public function find(int $accountId): Account
{
$account = $this->user->accounts()->find($accountId);
if (is_null($account)) {
$account = new Account;
}
return $account;
}
/** /**
* @param Account $account * @param Account $account
* *

View File

@@ -63,13 +63,6 @@ interface AccountRepositoryInterface
*/ */
public function expensesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection; public function expensesInPeriod(Collection $accounts, Carbon $start, Carbon $end): Collection;
/**
* @param int $accountId
*
* @return Account
*/
public function find(int $accountId): Account;
/** /**
* @param Account $account * @param Account $account
* *