Create deposits, transfers and withdrawals. Also tests!

This commit is contained in:
James Cole
2014-07-15 20:58:35 +02:00
parent 1f4436cb75
commit 9687b5fb33
8 changed files with 701 additions and 5 deletions

View File

@@ -35,16 +35,46 @@ class TransactionController extends BaseController
$budgets[0] = '(no budget)';
return View::make('transactions.withdrawal')->with('accounts', $accounts)->with('budgets', $budgets);
}
public function createDeposit()
{
// get accounts with names and id's.
$accounts = $this->accounts->getActiveDefaultAsSelectList();
$budgets = $this->budgets->getAsSelectList();
$budgets[0] = '(no budget)';
return View::make('transactions.deposit')->with('accounts', $accounts)->with('budgets', $budgets);
}
public function createTransfer()
{
// get accounts with names and id's.
$accounts = $this->accounts->getActiveDefaultAsSelectList();
$budgets = $this->budgets->getAsSelectList();
$budgets[0] = '(no budget)';
return View::make('transactions.transfer')->with('accounts', $accounts)->with('budgets', $budgets);
}
public function postCreateWithdrawal()
{
// create or find beneficiary:
$beneficiary = $this->accounts->createOrFindBeneficiary(Input::get('beneficiary'));
// fall back to cash account if empty:
if (is_null($beneficiary)) {
$beneficiary = $this->accounts->getCashAccount();
}
// create or find category:
$category = $this->categories->createOrFind(Input::get('category'));
@@ -61,7 +91,11 @@ class TransactionController extends BaseController
// create journal
/** @var \TransactionJournal $journal */
$journal = $this->tj->createSimpleJournal($account, $beneficiary, $description, $amount, $date);
try {
$journal = $this->tj->createSimpleJournal($account, $beneficiary, $description, $amount, $date);
} catch (\Firefly\Exception\FireflyException $e) {
return Redirect::route('transactions.withdrawal')->withInput();
}
// attach bud/cat (?)
if (!is_null($budget)) {
@@ -75,4 +109,72 @@ class TransactionController extends BaseController
return Redirect::route('index');
}
public function postCreateDeposit()
{
// create or find beneficiary:
$beneficiary = $this->accounts->createOrFindBeneficiary(Input::get('beneficiary'));
// fall back to cash account if empty:
if (is_null($beneficiary)) {
$beneficiary = $this->accounts->getCashAccount();
}
// create or find category:
$category = $this->categories->createOrFind(Input::get('category'));
// find account:
$account = $this->accounts->find(intval(Input::get('account_id')));
// find amount & description:
$description = trim(Input::get('description'));
$amount = floatval(Input::get('amount'));
$date = new \Carbon\Carbon(Input::get('date'));
// create journal
/** @var \TransactionJournal $journal */
try {
$journal = $this->tj->createSimpleJournal($beneficiary, $account, $description, $amount, $date);
} catch (\Firefly\Exception\FireflyException $e) {
return Redirect::route('transactions.deposit')->withInput();
}
if (!is_null($category)) {
$journal->categories()->save($category);
}
Session::flash('success', 'Transaction saved');
return Redirect::route('index');
}
public function postCreateTransfer()
{
// create or find category:
$category = $this->categories->createOrFind(Input::get('category'));
// find account to:
$to = $this->accounts->find(intval(Input::get('account_to_id')));
// find account from
$from = $this->accounts->find(intval(Input::get('account_from_id')));
// find amount & description:
$description = trim(Input::get('description'));
$amount = floatval(Input::get('amount'));
$date = new \Carbon\Carbon(Input::get('date'));
// create journal
/** @var \TransactionJournal $journal */
try {
$journal = $this->tj->createSimpleJournal($from, $to, $description, $amount, $date);
} catch (\Firefly\Exception\FireflyException $e) {
return Redirect::route('transactions.transfer')->withInput();
}
if (!is_null($category)) {
$journal->categories()->save($category);
}
Session::flash('success', 'Transaction saved');
return Redirect::route('index');
}
}