Final updates for account controller. Looks like this one is finished. [skip ci]

This commit is contained in:
James Cole
2014-08-02 07:49:48 +02:00
parent d756324432
commit 99500d6201
6 changed files with 76 additions and 62 deletions

View File

@@ -32,11 +32,11 @@ interface AccountRepositoryInterface
public function createOrFindBeneficiary($name);
/**
* @param $accountId
* @param \Account $account
*
* @return bool
* @return mixed
*/
public function destroy($accountId);
public function destroy(\Account $account);
/**
* @param $accountId
@@ -97,10 +97,11 @@ interface AccountRepositoryInterface
public function store($data);
/**
* @param $data
* @param \Account $account
* @param $data
*
* @return \Account
* @return mixed
*/
public function update($data);
public function update(\Account $account, $data);
}

View File

@@ -64,16 +64,17 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $this->createOrFind($name, $type);
}
public function destroy($accountId)
public function destroy(\Account $account)
{
$account = $this->find($accountId);
if ($account) {
$account->delete();
$account->delete();
return true;
}
/**
*
* TODO
* Also delete: initial balance, initial balance account, and transactions
*/
return false;
return true;
}
/**
@@ -227,24 +228,31 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*
* @return \Account|void
*/
public function update($data)
public function update(\Account $account, $data)
{
$account = $this->find($data['id']);
if ($account) {
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
if (floatval($data['openingbalance']) != 0) {
/** @var \Firefly\Helper\Controllers\AccountInterface $interface */
$interface = \App::make('Firefly\Helper\Controllers\AccountInterface');
if ($account->accounttype->description == 'Default account') {
$journal = $this->findOpeningBalanceTransaction($account);
$journal->date = new Carbon($data['openingbalancedate']);
$journal->transactions[0]->amount = floatval($data['openingbalance']) * -1;
$journal->transactions[1]->amount = floatval($data['openingbalance']);
$journal->transactions[0]->save();
$journal->transactions[1]->save();
$journal->save();
$journal = $interface->openingBalanceTransaction($account);
if ($journal) {
$journal->date = new Carbon($data['openingbalancedate']);
$journal->transactions[0]->amount = floatval($data['openingbalance']) * -1;
$journal->transactions[1]->amount = floatval($data['openingbalance']);
$journal->transactions[0]->save();
$journal->transactions[1]->save();
$journal->save();
}
}
}