Files
firefly-iii/app/controllers/TransactionController.php

466 lines
15 KiB
PHP
Raw Normal View History

2014-07-15 06:58:08 +02:00
<?php
use FireflyIII\Database\TransactionJournal\TransactionJournal as Repository;
use FireflyIII\Exception\FireflyException;
use FireflyIII\Helper\TransactionJournal\HelperInterface as Helper;
use Illuminate\Support\Collection;
2014-07-15 06:58:08 +02:00
2014-07-15 22:16:29 +02:00
/**
* Class TransactionController
*
2014-07-15 22:16:29 +02:00
*/
class TransactionController extends BaseController
{
2014-07-15 06:58:08 +02:00
/** @var Helper */
protected $_helper;
/** @var Repository */
protected $_repository;
2014-07-15 22:16:29 +02:00
/**
* Construct a new transaction controller with two of the most often used helpers.
*
* @param Repository $repository
* @param Helper $helper
2014-07-15 22:16:29 +02:00
*/
public function __construct(Repository $repository, Helper $helper)
{
$this->_repository = $repository;
$this->_helper = $helper;
View::share('title', 'Transactions');
View::share('mainTitleIcon', 'fa-repeat');
2014-07-15 06:58:08 +02:00
}
2014-12-06 17:53:25 +01:00
/**
*
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return array|\Illuminate\Http\JsonResponse
*/
2014-12-04 20:38:45 +01:00
public function alreadyRelated(TransactionJournal $journal)
{
$ids = [];
/** @var TransactionGroup $group */
foreach ($journal->transactiongroups()->get() as $group) {
/** @var TransactionJournal $loopJournal */
foreach ($group->transactionjournals()->get() as $loopJournal) {
if ($loopJournal->id != $journal->id) {
$ids[] = $loopJournal->id;
2014-12-04 20:38:45 +01:00
}
}
}
$unique = array_unique($ids);
2014-12-13 21:59:02 +01:00
if (count($unique) > 0) {
2014-12-04 20:38:45 +01:00
$set = $this->_repository->getByIds($unique);
2014-12-04 20:38:45 +01:00
$set->each(
function (TransactionJournal $journal) {
$journal->amount = mf($journal->getAmount());
}
);
return Response::json($set->toArray());
} else {
return (new Collection)->toArray();
}
}
/**
* Shows the view helping the user to create a new transaction journal.
*
* @param string $what
*
* @return \Illuminate\View\View
*/
public function create($what = 'deposit')
{
$accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts());
$budgets = FFForm::makeSelectList($this->_helper->getBudgets());
$budgets[0] = '(no budget)';
$piggyBanks = $this->_helper->getPiggyBanks();
$repeatedExpenses = $this->_helper->getRepeatedExpenses();
$list = $piggyBanks->merge($repeatedExpenses);
$piggies = FFForm::makeSelectList($list);
$piggies[0] = '(no piggy bank)';
$preFilled = Session::has('preFilled') ? Session::get('preFilled') : [];
$respondTo = ['account_id', 'account_from_id'];
$subTitle = 'Add a new ' . $what;
2014-10-08 21:04:31 +02:00
foreach ($respondTo as $r) {
if (!is_null(Input::get($r))) {
2014-12-07 15:37:53 +01:00
$preFilled[$r] = Input::get($r);
2014-10-08 21:04:31 +02:00
}
}
2014-12-07 15:37:53 +01:00
Session::put('preFilled', $preFilled);
2014-10-08 21:04:31 +02:00
asort($piggies);
return View::make('transactions.create', compact('accounts', 'budgets', 'what', 'piggies', 'subTitle'));
}
2014-08-10 15:01:46 +02:00
/**
* Shows the form that allows a user to delete a transaction journal.
*
* @param TransactionJournal $journal
2014-08-10 15:01:46 +02:00
*
* @return $this
*/
public function delete(TransactionJournal $journal)
{
$type = strtolower($journal->transactionType->type);
$subTitle = 'Delete ' . $type . ' "' . $journal->description . '"';
return View::make('transactions.delete', compact('journal', 'subTitle'));
}
2014-08-10 15:01:46 +02:00
/**
* @param TransactionJournal $transactionJournal
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(TransactionJournal $transactionJournal)
{
$type = $transactionJournal->transactionType->type;
2014-12-06 17:34:39 +01:00
$return = 'withdrawal';
$this->_repository->destroy($transactionJournal);
2014-12-06 17:34:39 +01:00
switch ($type) {
case 'Deposit':
2014-12-06 17:34:39 +01:00
$return = 'deposit';
break;
case 'Transfer':
2014-12-06 17:34:39 +01:00
$return = 'transfers';
break;
}
2014-12-06 17:34:39 +01:00
return Redirect::route('transactions.index', $return);
}
2014-12-06 17:53:25 +01:00
/**
* TODO this needs cleaning up and thinking over.
*
* @return \Illuminate\Http\JsonResponse
*/
2014-12-04 20:38:45 +01:00
public function doRelate()
{
$id = intval(Input::get('id'));
$sister = intval(Input::get('relateTo'));
$journal = $this->_repository->find($id);
$sis = $this->_repository->find($sister);
2014-12-04 20:38:45 +01:00
if ($journal && $sis) {
$group = new TransactionGroup;
$group->relation = 'balance';
$group->user_id = $this->_repository->getUser()->id;
2014-12-04 20:38:45 +01:00
$group->save();
$group->transactionjournals()->save($journal);
$group->transactionjournals()->save($sis);
return Response::json(true);
}
return Response::json(false);
}
/**
* Shows the view to edit a transaction.
*
2014-08-10 15:01:46 +02:00
* @param TransactionJournal $journal
*
2014-08-10 15:01:46 +02:00
* @return $this
*/
public function edit(TransactionJournal $journal)
{
$what = strtolower($journal->transactiontype->type);
$subTitle = 'Edit ' . $what . ' "' . $journal->description . '"';
$budgets = FFForm::makeSelectList($this->_helper->getBudgets(), true);
$accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts());
$piggies = FFForm::makeSelectList($this->_helper->getPiggyBanks(), true);
$transactions = $journal->transactions()->orderBy('amount', 'DESC')->get();
$preFilled = [
'date' => $journal->date->format('Y-m-d'),
'category' => '',
'budget_id' => 0,
'piggy_bank_id' => 0
];
$category = $journal->categories()->first();
if (!is_null($category)) {
2014-12-07 15:37:53 +01:00
$preFilled['category'] = $category->name;
}
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
$preFilled['budget_id'] = $budget->id;
}
if ($journal->piggyBankEvents()->count() > 0) {
$preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->first()->piggy_bank_id;
}
$preFilled['amount'] = $journal->getAmount();
$preFilled['account_id'] = $this->_helper->getAssetAccount($what, $transactions);
$preFilled['expense_account'] = $transactions[0]->account->name;
$preFilled['revenue_account'] = $transactions[1]->account->name;
$preFilled['account_from_id'] = $transactions[1]->account->id;
$preFilled['account_to_id'] = $transactions[0]->account->id;
2014-11-12 22:36:02 +01:00
return View::make('transactions.edit', compact('journal', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled);
}
2014-11-12 22:36:02 +01:00
/**
* @param $what
*
* @return $this
*/
public function index($what)
{
switch ($what) {
case 'expenses':
case 'withdrawal':
$subTitleIcon = 'fa-long-arrow-left';
$subTitle = 'Expenses';
$journals = $this->_repository->getWithdrawalsPaginated(50);
2014-11-12 22:36:02 +01:00
break;
case 'revenue':
case 'deposit':
$subTitleIcon = 'fa-long-arrow-right';
$subTitle = 'Revenue, income and deposits';
$journals = $this->_repository->getDepositsPaginated(50);
2014-11-12 22:36:02 +01:00
break;
case 'transfer':
case 'transfers':
$subTitleIcon = 'fa-arrows-h';
$subTitle = 'Transfers';
$journals = $this->_repository->getTransfersPaginated(50);
2014-11-12 22:36:02 +01:00
break;
}
return View::make('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals'));
2014-11-12 22:36:02 +01:00
}
2014-12-06 17:53:25 +01:00
/**
* TODO refactor relate stuff into another controller.
*
2014-12-06 17:53:25 +01:00
* @param TransactionJournal $journal
*
* @return \Illuminate\View\View
*/
public function relate(TransactionJournal $journal)
{
$groups = $journal->transactiongroups()->get();
$members = new Collection;
/** @var TransactionGroup $group */
foreach ($groups as $group) {
/** @var TransactionJournal $loopJournal */
foreach ($group->transactionjournals()->get() as $loopJournal) {
if ($loopJournal->id != $journal->id) {
$members->push($loopJournal);
}
}
}
return View::make('transactions.relate', compact('journal', 'members'));
}
2014-12-06 17:53:25 +01:00
/**
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\JsonResponse
*/
2014-12-04 20:38:45 +01:00
public function relatedSearch(TransactionJournal $journal)
{
$search = e(trim(Input::get('searchValue')));
$result = $this->_repository->searchRelated($search, $journal);
2014-12-04 20:38:45 +01:00
$result->each(
function (TransactionJournal $j) {
$j->amount = mf($j->getAmount());
}
);
return Response::json($result->toArray());
}
/**
2014-08-10 15:01:46 +02:00
* @param TransactionJournal $journal
*
2014-08-10 15:01:46 +02:00
* @return $this
*/
2014-11-14 10:28:18 +01:00
public function show(TransactionJournal $journal)
{
2014-11-22 07:30:46 +01:00
$journal->transactions->each(
function (\Transaction $t) use ($journal) {
2014-11-22 07:30:46 +01:00
$t->before = floatval(
$t->account->transactions()->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))->where(
'transaction_journals.created_at', '<=', $journal->created_at->format('Y-m-d H:i:s')
)->where('transaction_journals.id', '!=', $journal->id)->sum('transactions.amount')
);
$t->after = $t->before + $t->amount;
2014-11-22 07:30:46 +01:00
}
);
$members = new Collection;
/** @var TransactionGroup $group */
2014-12-04 20:38:45 +01:00
foreach ($journal->transactiongroups()->get() as $group) {
/** @var TransactionJournal $loopJournal */
foreach ($group->transactionjournals()->get() as $loopJournal) {
if ($loopJournal->id != $journal->id) {
$members->push($loopJournal);
}
}
}
2014-11-22 07:30:46 +01:00
return View::make('transactions.show', compact('journal', 'members'))->with(
2014-09-21 15:40:41 +02:00
'subTitle', $journal->transactionType->type . ' "' . $journal->description . '"'
);
}
/**
* @param $what
*
* @return $this|\Illuminate\Http\RedirectResponse
* @throws FireflyException
*/
2014-11-12 15:34:32 +01:00
public function store($what)
{
2014-11-12 22:36:02 +01:00
$data = Input::except('_token');
$data['what'] = $what;
$data['currency'] = 'EUR'; // TODO allow custom currency
2014-11-12 15:34:32 +01:00
// always validate:
$messages = $this->_repository->validate($data);
2014-11-12 15:34:32 +01:00
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not store transaction: ' . $messages['errors']->first());
}
// return to create screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('transactions.create', $data['what'])->withInput();
}
2014-11-12 15:34:32 +01:00
// store:
$journal = $this->_repository->store($data);
Event::fire('transactionJournal.store', [$journal, Input::get('piggy_bank_id')]); // new and used.
/*
* Also trigger on both transactions.
*/
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
Event::fire('transaction.store', [$transaction]);
}
Session::flash('success', 'Transaction "' . e($data['description']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('transactions.index', $data['what']);
2014-11-12 15:34:32 +01:00
}
return Redirect::route('transactions.create', $data['what'])->withInput();
}
2014-12-06 17:53:25 +01:00
/**
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\JsonResponse
* @throws Exception
*/
2014-12-05 21:39:16 +01:00
public function unrelate(TransactionJournal $journal)
{
$groups = $journal->transactiongroups()->get();
$relatedTo = intval(Input::get('relation'));
/** @var TransactionGroup $group */
foreach ($groups as $group) {
foreach ($group->transactionjournals()->get() as $loopJournal) {
if ($loopJournal->id == $relatedTo) {
2014-12-05 21:39:16 +01:00
// remove from group:
$group->transactionjournals()->detach($relatedTo);
}
}
if ($group->transactionjournals()->count() == 1) {
$group->delete();
}
}
2014-12-06 17:34:39 +01:00
2014-12-05 21:39:16 +01:00
return Response::json(true);
}
2014-09-28 09:20:25 +02:00
2014-08-10 15:01:46 +02:00
/**
* @param TransactionJournal $journal
*
2014-12-13 21:59:02 +01:00
* @return $this
2014-09-21 16:22:18 +02:00
* @throws FireflyException
2014-08-10 15:01:46 +02:00
*/
2014-11-12 15:34:32 +01:00
public function update(TransactionJournal $journal)
{
2014-11-13 17:01:09 +01:00
$data = Input::except('_token');
$data['currency'] = 'EUR';
$data['what'] = strtolower($journal->transactionType->type);
// always validate:
$messages = $this->_repository->validate($data);
2014-11-13 17:01:09 +01:00
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update transaction: ' . $messages['errors']->first());
}
2014-11-13 17:01:09 +01:00
// return to update screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('transactions.edit', $journal->id)->withInput();
}
2014-11-13 17:01:09 +01:00
// update
$this->_repository->update($journal, $data);
Session::flash('success', 'Transaction "' . e($data['description']) . '" updated.');
Event::fire('transactionJournal.update', [$journal]); // new and used.
/*
* Also trigger on both transactions.
*/
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
Event::fire('transaction.update', [$transaction]);
2014-11-13 17:01:09 +01:00
}
// go back to list
if ($data['post_submit_action'] == 'update') {
return Redirect::route('transactions.index', $data['what']);
}
// go back to update screen.
return Redirect::route('transactions.edit', $journal->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
}