Can now actually store transactions without an expense / revenue account.

This commit is contained in:
James Cole 2015-04-01 09:17:07 +02:00
parent 9b4f87d44a
commit 58faa189ac
2 changed files with 30 additions and 4 deletions

View File

@ -113,6 +113,30 @@ class Account extends Model
// @codeCoverageIgnoreEnd
}
/**
* @param array $fields
* @return Account|null
*/
public static function firstOrCreateEncrypted(array $fields) {
// everything but the name:
$query = Account::orderBy('id');
foreach($fields as $name => $value) {
if($name != 'name') {
$query->where($name,$value);
}
}
$set = $query->get(['accounts.*']);
/** @var Account $account */
foreach($set as $account) {
if($account->name == $fields['name']) {
return $account;
}
}
// create it!
return Account::create($fields);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

View File

@ -149,7 +149,7 @@ class JournalRepository implements JournalRepositoryInterface
'amount' => $data['amount'] * -1
]
);
Transaction::create( // second transaction.
$transaction = Transaction::create( // second transaction.
[
'account_id' => $to->id,
'transaction_journal_id' => $journal->id,
@ -225,6 +225,8 @@ class JournalRepository implements JournalRepositoryInterface
*/
protected function storeAccounts(TransactionType $type, array $data)
{
$from = null;
$to = null;
switch ($type->type) {
case 'Withdrawal':
@ -237,7 +239,7 @@ class JournalRepository implements JournalRepositoryInterface
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
$to = Account::firstOrCreate(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
$to = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
}
break;
@ -246,12 +248,12 @@ class JournalRepository implements JournalRepositoryInterface
if (strlen($data['revenue_account']) > 0) {
$fromType = AccountType::where('type', 'Revenue account')->first();
$from = Account::firstOrCreate(
$from = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => $data['revenue_account'], 'active' => 1]
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
$from = Account::firstOrCreate(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
$from = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
}
break;