2014-07-14 23:58:08 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
|
2014-07-15 04:04:53 -05:00
|
|
|
use Firefly\Storage\Budget\BudgetRepositoryInterface as Bud;
|
|
|
|
use Firefly\Storage\Category\CategoryRepositoryInterface as Cat;
|
|
|
|
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
|
2014-07-14 23:58:08 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
/**
|
|
|
|
* Class TransactionController
|
|
|
|
*/
|
2014-07-15 04:04:53 -05:00
|
|
|
class TransactionController extends BaseController
|
|
|
|
{
|
2014-07-14 23:58:08 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
protected $_accounts;
|
|
|
|
protected $_budgets;
|
|
|
|
protected $_categories;
|
|
|
|
protected $_journal;
|
|
|
|
|
|
|
|
/**
|
2014-07-28 14:33:32 -05:00
|
|
|
* @param ARI $accounts
|
|
|
|
* @param Bud $budgets
|
|
|
|
* @param Cat $categories
|
2014-07-15 15:16:29 -05:00
|
|
|
* @param TJRI $journal
|
|
|
|
*/
|
|
|
|
public function __construct(ARI $accounts, Bud $budgets, Cat $categories, TJRI $journal)
|
2014-07-15 04:04:53 -05:00
|
|
|
{
|
2014-07-15 15:16:29 -05:00
|
|
|
$this->_accounts = $accounts;
|
|
|
|
$this->_budgets = $budgets;
|
|
|
|
$this->_categories = $categories;
|
|
|
|
$this->_journal = $journal;
|
2014-07-14 23:58:08 -05:00
|
|
|
|
|
|
|
|
2014-07-15 04:04:53 -05:00
|
|
|
View::share('menu', 'home');
|
2014-07-14 23:58:08 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param $what
|
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-16 14:11:43 -05:00
|
|
|
public function create($what)
|
2014-07-15 13:58:35 -05:00
|
|
|
{
|
|
|
|
// get accounts with names and id's.
|
2014-07-15 15:16:29 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
2014-07-15 13:58:35 -05:00
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
$budgets = $this->_budgets->getAsSelectList();
|
2014-07-15 13:58:35 -05:00
|
|
|
|
|
|
|
$budgets[0] = '(no budget)';
|
|
|
|
|
2014-07-25 05:12:08 -05:00
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
return View::make('transactions.create')->with('accounts', $accounts)->with('budgets', $budgets)->with(
|
|
|
|
'what', $what
|
|
|
|
);
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param $what
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-07-16 14:11:43 -05:00
|
|
|
public function store($what)
|
2014-07-15 13:58:35 -05:00
|
|
|
{
|
2014-07-16 14:11:43 -05:00
|
|
|
// $fromAccount and $toAccount are found
|
|
|
|
// depending on the $what
|
|
|
|
|
|
|
|
$fromAccount = null;
|
|
|
|
$toAccount = null;
|
|
|
|
|
|
|
|
switch ($what) {
|
|
|
|
case 'withdrawal':
|
|
|
|
$fromAccount = $this->_accounts->find(intval(Input::get('account_id')));
|
|
|
|
$toAccount = $this->_accounts->createOrFindBeneficiary(Input::get('beneficiary'));
|
|
|
|
break;
|
|
|
|
case 'deposit':
|
|
|
|
$fromAccount = $this->_accounts->createOrFindBeneficiary(Input::get('beneficiary'));
|
|
|
|
$toAccount = $this->_accounts->find(intval(Input::get('account_id')));
|
|
|
|
break;
|
|
|
|
case 'transfer':
|
|
|
|
$fromAccount = $this->_accounts->find(intval(Input::get('account_from_id')));
|
|
|
|
$toAccount = $this->_accounts->find(intval(Input::get('account_to_id')));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall back to cash if necessary:
|
2014-07-25 06:02:01 -05:00
|
|
|
$fromAccount = is_null($fromAccount) ? $fromAccount = $this->_accounts->getCashAccount() : $fromAccount;
|
|
|
|
$toAccount = is_null($toAccount) ? $toAccount = $this->_accounts->getCashAccount() : $toAccount;
|
2014-07-15 13:58:35 -05:00
|
|
|
|
2014-07-15 04:04:53 -05:00
|
|
|
// create or find category:
|
2014-07-15 15:16:29 -05:00
|
|
|
$category = $this->_categories->createOrFind(Input::get('category'));
|
2014-07-15 04:04:53 -05:00
|
|
|
|
|
|
|
// find budget:
|
2014-07-15 15:16:29 -05:00
|
|
|
$budget = $this->_budgets->find(intval(Input::get('budget_id')));
|
2014-07-15 04:04:53 -05:00
|
|
|
|
|
|
|
// 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 */
|
2014-07-15 13:58:35 -05:00
|
|
|
try {
|
2014-07-16 14:11:43 -05:00
|
|
|
$journal = $this->_journal->createSimpleJournal($fromAccount, $toAccount, $description, $amount, $date);
|
2014-07-15 13:58:35 -05:00
|
|
|
} catch (\Firefly\Exception\FireflyException $e) {
|
2014-07-16 14:11:43 -05:00
|
|
|
return Redirect::route('transactions.create', $what)->withInput();
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
2014-07-15 04:04:53 -05:00
|
|
|
|
|
|
|
// attach bud/cat (?)
|
2014-07-15 10:09:59 -05:00
|
|
|
if (!is_null($budget)) {
|
|
|
|
$journal->budgets()->save($budget);
|
|
|
|
}
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$journal->categories()->save($category);
|
|
|
|
}
|
|
|
|
|
2014-07-26 01:05:02 -05:00
|
|
|
Session::flash('success', 'Transaction "' . $description . '" saved');
|
2014-07-25 05:12:08 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
if (Input::get('create') == '1') {
|
|
|
|
return Redirect::route('transactions.create', $what)->withInput();
|
2014-07-25 05:12:08 -05:00
|
|
|
} else {
|
|
|
|
return Redirect::route('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
}
|
2014-07-15 13:58:35 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-25 05:12:08 -05:00
|
|
|
public function index()
|
|
|
|
{
|
2014-07-25 00:17:56 -05:00
|
|
|
$transactions = $this->_journal->paginate(25);
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-25 05:12:08 -05:00
|
|
|
return View::make('transactions.index')->with('transactions', $transactions);
|
2014-07-25 00:17:56 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param $journalId
|
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-16 14:11:43 -05:00
|
|
|
public function show($journalId)
|
|
|
|
{
|
|
|
|
$journal = $this->_journal->find($journalId);
|
|
|
|
if ($journal) {
|
|
|
|
return View::make('transactions.show')->with('journal', $journal);
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
return View::make('error')->with('message', 'Invalid journal');
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param $journalId
|
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-16 14:11:43 -05:00
|
|
|
public function edit($journalId)
|
2014-07-15 13:58:35 -05:00
|
|
|
{
|
2014-07-26 01:05:02 -05:00
|
|
|
// get journal:
|
2014-07-16 14:11:43 -05:00
|
|
|
$journal = $this->_journal->find($journalId);
|
2014-07-26 01:05:02 -05:00
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
if ($journal) {
|
2014-07-26 01:05:02 -05:00
|
|
|
// type is useful for display:
|
|
|
|
$what = strtolower($journal->transactiontype->type);
|
|
|
|
|
|
|
|
// some lists prefilled:
|
|
|
|
$budgets = $this->_budgets->getAsSelectList();
|
|
|
|
$budgets[0] = '(no budget)';
|
2014-07-16 14:11:43 -05:00
|
|
|
$accounts = $this->_accounts->getActiveDefaultAsSelectList();
|
2014-07-26 01:05:02 -05:00
|
|
|
|
|
|
|
// data to properly display form:
|
|
|
|
$data = [
|
2014-07-28 14:33:32 -05:00
|
|
|
'date' => $journal->date->format('Y-m-d'),
|
|
|
|
'category' => '',
|
2014-07-26 01:05:02 -05:00
|
|
|
'budget_id' => 0
|
|
|
|
];
|
|
|
|
$category = $journal->categories()->first();
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$data['category'] = $category->name;
|
|
|
|
}
|
|
|
|
switch ($journal->transactiontype->type) {
|
|
|
|
case 'Withdrawal':
|
|
|
|
$data['account_id'] = $journal->transactions[0]->account->id;
|
|
|
|
$data['beneficiary'] = $journal->transactions[1]->account->name;
|
|
|
|
$data['amount'] = floatval($journal->transactions[1]->amount);
|
|
|
|
$budget = $journal->budgets()->first();
|
|
|
|
if (!is_null($budget)) {
|
|
|
|
$data['budget_id'] = $budget->id;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Deposit':
|
|
|
|
$data['account_id'] = $journal->transactions[1]->account->id;
|
|
|
|
$data['beneficiary'] = $journal->transactions[0]->account->name;
|
|
|
|
$data['amount'] = floatval($journal->transactions[1]->amount);
|
|
|
|
break;
|
|
|
|
case 'Transfer':
|
|
|
|
$data['account_from_id'] = $journal->transactions[1]->account->id;
|
|
|
|
$data['account_to_id'] = $journal->transactions[0]->account->id;
|
|
|
|
$data['amount'] = floatval($journal->transactions[1]->amount);
|
|
|
|
break;
|
|
|
|
}
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-26 01:05:02 -05:00
|
|
|
return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with(
|
|
|
|
'what', $what
|
|
|
|
)->with('budgets', $budgets)->with('data', $data);
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-16 14:11:43 -05:00
|
|
|
return View::make('error')->with('message', 'Invalid journal');
|
2014-07-15 13:58:35 -05:00
|
|
|
}
|
|
|
|
|
2014-07-26 01:05:02 -05:00
|
|
|
public function update($journalId)
|
|
|
|
{
|
|
|
|
|
|
|
|
// get journal:
|
|
|
|
$journal = $this->_journal->find($journalId);
|
|
|
|
|
|
|
|
if ($journal) {
|
|
|
|
// update basics first:
|
|
|
|
$journal->description = Input::get('description');
|
|
|
|
$journal->date = Input::get('date');
|
|
|
|
$amount = floatval(Input::get('amount'));
|
|
|
|
|
|
|
|
// remove previous category, if any:
|
|
|
|
if (!is_null($journal->categories()->first())) {
|
|
|
|
$journal->categories()->detach($journal->categories()->first()->id);
|
|
|
|
}
|
|
|
|
// remove previous budget, if any:
|
|
|
|
if (!is_null($journal->budgets()->first())) {
|
|
|
|
$journal->budgets()->detach($journal->budgets()->first()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the category:
|
|
|
|
$category = $this->_categories->findByName(Input::get('category'));
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$journal->categories()->attach($category);
|
|
|
|
}
|
|
|
|
// update the amounts:
|
|
|
|
$journal->transactions[0]->amount = $amount * -1;
|
|
|
|
$journal->transactions[1]->amount = $amount;
|
|
|
|
|
|
|
|
// switch on type to properly change things:
|
|
|
|
switch ($journal->transactiontype->type) {
|
|
|
|
case 'Withdrawal':
|
|
|
|
// means transaction[0] is the users account.
|
|
|
|
$account = $this->_accounts->find(Input::get('account_id'));
|
|
|
|
$beneficiary = $this->_accounts->findByName(Input::get('beneficiary'));
|
|
|
|
$journal->transactions[0]->account()->associate($account);
|
|
|
|
$journal->transactions[1]->account()->associate($beneficiary);
|
|
|
|
|
|
|
|
|
|
|
|
// do budget:
|
|
|
|
$budget = $this->_budgets->find(Input::get('budget_id'));
|
|
|
|
$journal->budgets()->attach($budget);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'Deposit':
|
|
|
|
// means transaction[0] is the beneficiary.
|
|
|
|
$account = $this->_accounts->find(Input::get('account_id'));
|
|
|
|
$beneficiary = $this->_accounts->findByName(Input::get('beneficiary'));
|
|
|
|
$journal->transactions[0]->account()->associate($beneficiary);
|
|
|
|
$journal->transactions[1]->account()->associate($account);
|
|
|
|
break;
|
|
|
|
case 'Transfer':
|
|
|
|
// means transaction[0] is account that sent the money (from).
|
|
|
|
$fromAccount = $this->_accounts->find(Input::get('account_from_id'));
|
|
|
|
$toAccount = $this->_accounts->find(Input::get('account_to_id'));
|
|
|
|
$journal->transactions[0]->account()->associate($fromAccount);
|
|
|
|
$journal->transactions[1]->account()->associate($toAccount);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Firefly\Exception\FireflyException('Cannot edit this!');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$journal->transactions[0]->save();
|
|
|
|
$journal->transactions[1]->save();
|
|
|
|
$journal->save();
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-26 01:05:02 -05:00
|
|
|
return Redirect::route('transactions.edit', $journal->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:58:08 -05:00
|
|
|
}
|