Fixed some bugs

This commit is contained in:
James Cole 2014-11-14 19:33:50 +01:00
parent de20563275
commit 7b7743c03e
5 changed files with 67 additions and 7 deletions

View File

@ -464,6 +464,16 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
$accountType = $accountTypeRepos->findByWhat('expense');
// if name is "", find cash account:
if (strlen($name) == 0) {
$cashAccountType = $accountTypeRepos->findByWhat('cash');
// find or create cash account:
return \Account::firstOrCreate(
['name' => 'Cash account', 'account_type_id' => $cashAccountType->id, 'active' => 1, 'user_id' => $this->getUser()->id,]
);
}
$data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1];
return \Account::firstOrCreate($data);
@ -488,9 +498,11 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
$start = \Session::get('start');
$end = \Session::get('end');
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0;
$set = $this->getUser()->transactionJournals()->withRelevantData()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)->take($limit)->offset($offset)->before($end)->after($start)->orderBy('date', 'DESC')
->get(['transaction_journals.*']);
$set = $this->getUser()->transactionJournals()->withRelevantData()->leftJoin(
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->where('transactions.account_id', $account->id)->take($limit)->offset($offset)->before($end)->after($start)->orderBy('date', 'DESC')->get(
['transaction_journals.*']
);
$count = $this->getUser()->transactionJournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->before($end)->after($start)->orderBy('date', 'DESC')->where('transactions.account_id', $account->id)->count();
$items = [];

View File

@ -113,6 +113,9 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
case 'revenue':
return \AccountType::whereType('Revenue account')->first();
break;
case 'cash':
return \AccountType::whereType('Cash account')->first();
break;
case 'initial':
return \AccountType::whereType('Initial balance account')->first();
break;

View File

@ -142,8 +142,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
*/
public function find($id)
{
// TODO: Implement find() method.
throw new NotImplementedException;
return $this->getUser()->budgets()->find($id);
}
/**

View File

@ -170,6 +170,11 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
throw new NotImplementedException;
}
public function firstOrCreate($name)
{
return \Category::firstOrCreate(['user_id' => $this->getUser()->id, 'name' => $name]);
}
public function getTransactionJournals(\Category $category, $limit = 50)
{
$offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0;

View File

@ -48,7 +48,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
*/
public function store(array $data)
{
/** @var \FireflyIII\Database\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType');
@ -127,12 +126,33 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/*
* TODO store budget and category.
* Store the budget.
*/
if(isset($data['budget_id']) && intval($data['budget_id']) > 0) {
/** @var \FireflyIII\Database\Budget $budgetRepository */
$budgetRepository = \App::make('FireflyIII\Database\Budget');
$budget = $budgetRepository->find(intval($data['budget_id']));
if($budget) {
$journal->budgets()->save($budget);
}
}
if(strlen($data['category']) > 0) {
/** @var \FireflyIII\Database\Category $categoryRepository */
$categoryRepository = \App::make('FireflyIII\Database\Category');
$category = $categoryRepository->firstOrCreate($data['category']);
if($category) {
$journal->categories()->save($category);
}
}
$journal->completed = 1;
$journal->save();
/*
* Trigger a search for a relevant recurring transaction.
*/
return $journal;
}
@ -199,6 +219,27 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
break;
}
/*
* Store the budget.
*/
if(isset($data['budget_id']) && intval($data['budget_id']) > 0) {
/** @var \FireflyIII\Database\Budget $budgetRepository */
$budgetRepository = \App::make('FireflyIII\Database\Budget');
$budget = $budgetRepository->find(intval($data['budget_id']));
if($budget) {
$model->budgets()->sync([$budget->id]);
}
}
if(strlen($data['category']) > 0) {
/** @var \FireflyIII\Database\Category $categoryRepository */
$categoryRepository = \App::make('FireflyIII\Database\Category');
$category = $categoryRepository->firstOrCreate($data['category']);
if($category) {
$model->categories()->sync([$category->id]);
}
}
/*
* Now we can update the transactions related to this journal.
*/